Gym 100463D Evil DFS

Evil

Time Limit: 5 Sec

Memory Limit: 256 MB

题目连接

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

代码

  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 }

时间: 2024-10-12 13:08:53

Gym 100463D Evil DFS的相关文章

Codeforces Gym 100463D Evil DFS

Evil Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 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. W

CF Gym 100463D Evil 前缀和+离散

题意:给一些带颜色的点,求一个最小的矩形,恰好包括一半的红色点,且不包括蓝色点. 题解:暴力,求个二维前缀和,用容斥原理更新一下.N很小所以我采用了离散优化,跑了个0ms. 之前没写过二维前缀和,加上离散写得也不是很熟练,所以搞了2个小时,好在是一发就过了.貌似有个坑,线不要特判,不然wa. #include<cstdio> #include<cmath> #include<vector> #include<map> #include<set>

Gym 100553J Jokewithpermutation(dfs)

题意:有n个数(n<=50)写在一行,将两数之间的空格去掉得到一个数字串.根据这个数字串回复原数: 思路:先求数的个数,当串长度小于10时,均为个位数:当串长度大于9时,存在两位数,剪去9个个位数,同样得到数的个数: 数的个数也是串中的最大数: 采用枚举的方法将每个数与串中的位置匹配,属于同一个数的数字对应同一个数: 输出时,两位数的数字间不加空格: #include<cstdio> #include<cstring> #include<algorithm> us

很好的脑洞题:dfs+暴力 Gym - 101128A Promotions

http://codeforces.com/gym/101128 题目大意:给你一个a,b,e,p.有e个点,p条有向边,每条边为(x,y),表示x->y,每次我们都取出一个入度为0的,并且一次性取出来的个数为a(或b).当然,取出来的种类可能有很多种(即一个集合),问,这个集合中有多少个数字是相同的. 第一个输出集合长度为a的,第二个输出集合长度为b的,第三个输出无论如何都无法被取出的个数. 思路:建立正向图和反向图. 定义pair<int, int> interval[i] 表示第i

Gym 101246D Fire in the Country(dfs求SG函数)

http://codeforces.com/gym/101246/problem/D 题意: 给定一个无向有环图,大火从1点开始,每个时间点与它相邻的点也将会着火,现在有两个人轮流操作机器人,机器人从1点出发,每个人每次选择一个点走,谁最后被火烧了谁就输了. 思路: 博弈题. 我们先预处理求出每个点着火的时间点,然后根据时间点重建新图,也就是重新建一个有向无环图,原来图中相连的并且时间点相差1的相连,由时间低的连向时间高的. 接下来我们在新图上求每个点的SG值,SG值为0的点就是叶子结点,这样父

ACM: Gym 100935G Board Game - DFS暴力搜索

Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935G Description standard input/outputStatements Feras bought to his nephew Saleem a new game to help him learning calculating. The game consists of a boar

codeforces Gym 100187J J. Deck Shuffling dfs

J. Deck Shuffling Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/J Description The world famous scientist Innokentiy continues his innovative experiments with decks of cards. Now he has a deck of n cards and k s

Gym 100338I TV show (dfs枚举)

Gym 100338I 题意: 一个人去参加电视有奖问答的节目,初始奖金为100元,每答对一道问题奖金翻倍,答错奖金清零.此外有一次保险机会:花费C的奖金,下一题可以答对奖金翻倍,答错奖金不清零. 现在给你答对每道题的概率,求最优答题策略的奖金期望. 思路: 先不考虑有保险机会.回答对第j题后离开的奖金期望就是: 100?2j?∏ji=1pi 那么我们枚举回答对第j题后离开的奖金期望,维护其最大值即可(注意也可以一题不回答直接走人,期望为100). 那么我们现在来考虑有保险机会的情况,我们枚举回

CodeForces Gym 100500A A. Poetry Challenge DFS

Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/attachments Description Let’s check another challenge of the IBM ICPC Chill Zone, a poetry challenge. One says a poetry string that starts with a