码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Gym 100463D Evil DFS

时间:2015-08-21 11:09:11      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

题目连接

http://codeforces.com/gym/100463/attachments

Description

Richard is evil. He wants to give another geometry problem to the participants of this contest and I’m afraid I have no choice but to comply. When asked why exactly he only could respond Richard Peng: for lulz So here’s what he wants you to do Richard Peng: find a circle that divides red into half Richard Peng: without taking any of the blue :D Fortunately our hero, Mark, has managed to change that circle to an axis parallel rectangle. Given a set of points in the plane each colored red or blue, find the area of the smallest rectangle that contains exactly half of the red points and none of the blue. The rectangle’s sides should be parallel to the x and y axis. There will always be a positive even number of red points. No two points will be at the same position. For the purposes of this problem you can assume that a rectangle contains all points on its border and interior.

Input

There are several test cases in each input file. The first line of each test case contains N (2 ≤ N ≤ 20), the number of points. The following N lines contain xi , yi , and ci (−1000 ≤ xi , yi , ≤ 1000, 0 ≤ ci ≤ 1) giving the x and y coordinates of the ith point. The ith point is red if ci = 0 and blue if ci = 1. The last line of input contains a zero.

Output

For each test case output the case number followed by the area of the smallest rectangle that satisfies the conditions above. If it is impossible output -1 instead. Follow the format in the sample output.

Sample Input

7 -10 0 0 -1 0 0 1 0 0 10 0 0 -1 -1 0 1 1 0 0 0 1 7 -4 0 0 -2 0 0 2 0 0 4 0 0 -3 0 1 0 0 1 3 0 1 0

Sample Output

Case 1: 9 Case 2: -1

HINT

 

题意

一个坐标系上面有n个点,要求找到一个矩形,使得能够框住一半的红点,不框进任何一个蓝点,求最小矩形面积

题解:

用dfs生成子集,并且判定矩形是否框住了蓝点。注意AC的代码中,是在所有至少有一半红点且无蓝点的矩形中寻找最小,所以最终如果有的话得到的一定是正好有一半红点的最小矩形。

//debug :仍然不知道为什么第二种代码第一例就WA了

 

代码:

AC:

技术分享
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <ctime>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <set>
 8 #include <vector>
 9 #include <sstream>
10 #include <queue>
11 #include <typeinfo>
12 #include <fstream>
13 #include <map>
14 #include <stack>
15 typedef long long ll;
16 using namespace std;
17 //freopen("D.in","r",stdin);
18 //freopen("D.out","w",stdout);
19 #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
20 #define test freopen("test.txt","r",stdin)
21 const int maxn=2501;
22 #define mod 1000000009
23 #define eps 1e-9
24 const int inf=0x3f3f3f3f;
25 const ll infll = 0x3f3f3f3f3f3f3f3fLL;
26 inline ll read()
27 {
28     ll x=0,f=1;char ch=getchar();
29     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
30     while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
31     return x*f;
32 }
33 //**************************************************************************************
34 
35 int n;
36 int num1,num2;
37 struct node
38 {
39     int x,y;
40 };
41 node a[40];
42 node b[40];
43 int ans;
44 bool ok(int x,int xx,int y,int yy)
45 {
46     for(int i=0;i<num2;i++)
47         if(b[i].x<=x&&b[i].x>=xx&&b[i].y<=y&&b[i].y>=yy)
48             return 0;
49     return 1;
50 }
51 
52 void dfs(int t,int pre,int xmax,int xmin,int ymax,int ymin)
53 {
54     if(t>=num1/2)
55     {
56         if(ok(xmax,xmin,ymax,ymin))
57             ans=min(ans,(xmax-xmin)*(ymax-ymin));
58         return;
59     }
60     for(int i=pre+1;i<num1;i++)
61         dfs(t+1,i,max(a[i].x,xmax),min(xmin,a[i].x),max(ymax,a[i].y),min(ymin,a[i].y));
62 }
63 int main()
64 {
65     int t=1;
66     while(cin>>n)
67     {
68         if(n==0)
69             break;
70         ans=inf;
71         num1=num2=0;
72         for(int i=0;i<n;i++)
73         {
74             int x=read(),y=read(),z=read();
75             if(z==0)
76                 a[num1].x=x,a[num1++].y=y;
77             else
78                 b[num2].x=x,b[num2++].y=y;
79         }
80         dfs(0,-1,-inf,inf,-inf,inf);
81         if(ans!=inf)
82             printf("Case %d: %d\n",t++,ans);
83         else
84             printf("Case %d: -1\n",t++);
85     }
86 }
View Code

WA on first test:

技术分享
  1 #include <cstdio>
  2 #include <cmath>
  3 #include <cstring>
  4 #include <ctime>
  5 #include <iostream>
  6 #include <algorithm>
  7 #include <set>
  8 #include <vector>
  9 #include <sstream>
 10 #include <queue>
 11 #include <typeinfo>
 12 #include <fstream>
 13 #include <map>
 14 #include <stack>
 15 typedef long long ll;
 16 using namespace std;
 17 #define maxn 2000001
 18 #define mod 10007
 19 #define eps 1e-9
 20 const int inf=0x3f3f3f3f;
 21 const ll infll = 0x3f3f3f3f3f3f3f3fLL;
 22 inline int read()
 23 {
 24     ll x=0,f=1;char ch=getchar();
 25     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
 26     while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
 27     return x*f;
 28 }
 29 inline void out(int x) {
 30    if(x>9) out(x/10);
 31    putchar(x%10+0);
 32 }
 33 //**************************************************************************************
 34 int n;
 35 struct point
 36 {
 37     int x,y,is_blue;
 38 }point[100];
 39 
 40 int init()
 41 {
 42     scanf("%d",&n);
 43     for(int i=0;i<n;i++)
 44         scanf("%d %d %d",&point[i].x,&point[i].y,&point[i].is_blue);
 45     return n;
 46 }
 47 struct S
 48 {
 49     bool vis[100];
 50     int minS;
 51     int minx,miny;
 52     int maxx,maxy;
 53     bool check()
 54     {
 55         int cnt_red=0,cnt_blue=0;
 56         for(int i=0;i<n;i++)
 57         {
 58             int x,y,isblue;
 59             x=point[i].x;y=point[i].y;isblue=point[i].is_blue;
 60             if(x>=minx&&x<=maxx&&y>=miny&&y<=maxy)
 61             {
 62                 if(isblue==1) cnt_blue++;
 63                 else cnt_red++;
 64             }
 65         }
 66         if(cnt_blue==0&&cnt_red==n/2) return 1;
 67         else return 0;
 68     }
 69     void dfs(int k,int d)
 70     {
 71         if(d==n/2)
 72         {
 73             minx=0x3f3f3f3f,miny=0x3f3f3f3f;
 74             maxx=-1,maxy=-1;
 75             for(int i=0;i<n;i++)
 76             {
 77                 if(vis[i])
 78                 {
 79                     int x=point[i].x,y=point[i].y;
 80                     minx=min(minx,x);miny=min(miny,y);
 81                     maxx=max(maxx,x);maxy=max(maxy,y);
 82                 }
 83             }
 84             int flag=check();
 85             if(flag==0) return ;
 86             int dx=maxx-minx;int dy=maxy-miny;
 87             if(minS==-1) minS=dx*dy;
 88             else minS=min(minS,dx*dy);
 89             return ;
 90         }
 91         if(k==n) return;
 92         if(!point[k].is_blue)
 93         {
 94             vis[k]=1;
 95             dfs(k+1,d+1);
 96         }
 97         vis[k]=0;
 98         dfs(k+1,d);
 99     }
100     void solve()
101     {
102         minS=-1;
103         memset(vis,0,sizeof(vis));
104         dfs(0,0);
105     }
106 }S;
107 int main()
108 {
109     int Cases=1;
110     while(init())
111     {
112         S.solve();
113         if(S.minS!=-1)
114             printf("Case %d: %d\n",Cases,S.minS);
115         else
116             printf("Case %d: -1\n",Cases);
117         Cases++;
118     }
119     return 0;
120 }
View Code

 

Codeforces Gym 100463D Evil DFS

标签:

原文地址:http://www.cnblogs.com/diang/p/4747089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!