Evil
Time Limit: 5 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/gym/100463/attachments
Description
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
代码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 <queue> 10 #include <map> 11 #include <stack> 12 #define inf 1000000007 13 #define maxn 32001 14 using namespace std; 15 typedef __int64 ll; 16 inline ll read() 17 { 18 ll x=0,f=1; 19 char ch=getchar(); 20 while(ch<'0'||ch>'9') 21 { 22 if(ch=='-')f=-1; 23 ch=getchar(); 24 } 25 while(ch>='0'&&ch<='9') 26 { 27 x=x*10+ch-'0'; 28 ch=getchar(); 29 } 30 return x*f; 31 } 32 //******************************************************************* 33 34 struct ss 35 { 36 int x,y; 37 } a[30],b[30]; 38 bool cmp(ss s1,ss s2) 39 { 40 if(s1.x!=s2.x) 41 return s1.x<s2.x; 42 else return s1.y<s2.y; 43 } 44 int N,M; 45 bool jug(int up,int down,int l,int r) 46 { 47 for(int i=1; i<=M; i++) 48 { 49 if((r>=b[i].x&&b[i].x>=l)&&(up>=b[i].y&&b[i].y>=down)) 50 return false; 51 } 52 return true; 53 } 54 int n; 55 int ans; 56 void dfs(int x,int k,int up,int down,int l,int r) 57 { 58 if(k==N/2+N%2) 59 { 60 ans=min(abs(r-l)*abs(up-down),ans); 61 return ; 62 } 63 for(int i=x+1; i<=N; i++) 64 { 65 int ups=max(a[i].y,up); 66 int downs=min(a[i].y,down); 67 int ls=min(l,a[i].x); 68 int rs=max(r,a[i].x); 69 if(jug(ups,downs,ls,rs)) 70 dfs(i,k+1,ups,downs,ls,rs); 71 } 72 } 73 int main() 74 { 75 int oo=1; 76 77 while(scanf("%d",&n)!=EOF) 78 { 79 ans=inf; 80 if(n==0) break; 81 N=0; 82 M=0; 83 int x,y,ch; 84 for(int i=1; i<=n; i++) 85 { 86 x=read(); 87 y=read(); 88 ch=read(); 89 if(ch==0) 90 { 91 a[++N].x=x; 92 a[N].y=y; 93 } 94 else 95 { 96 b[++M].x=x; 97 b[M].y=y; 98 } 99 } 100 sort(a+1,a+N+1,cmp); 101 sort(b+1,b+M+1,cmp); 102 103 dfs(0,0,-inf,inf,inf,-inf); 104 printf("Case %d: ",oo++); 105 if(ans==inf) 106 { 107 printf("-1\n"); 108 } 109 else printf("%d\n",ans); 110 } 111 return 0; 112 }