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. 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个点,要求找到一个矩形,使得能够框住一半的红点,不框进任何一个蓝点,求最小矩形面积

题解:

暴力枚举就好了,注意,矩形面积可以为0

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
const int maxn=2501;
#define mod 1000000009
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//**************************************************************************************

int n;
int num1,num2;
struct node
{
    int x,y;
};
node a[40];
node b[40];
int ans;
bool ok(int x,int xx,int y,int yy)
{
    for(int i=0;i<num2;i++)
        if(b[i].x<=x&&b[i].x>=xx&&b[i].y<=y&&b[i].y>=yy)
            return 0;
    return 1;
}

void dfs(int t,int pre,int xmax,int xmin,int ymax,int ymin)
{
    if(t>=num1/2)
    {
        if(ok(xmax,xmin,ymax,ymin))
            ans=min(ans,(xmax-xmin)*(ymax-ymin));
        return;
    }
    for(int i=pre+1;i<num1;i++)
        dfs(t+1,i,max(a[i].x,xmax),min(xmin,a[i].x),max(ymax,a[i].y),min(ymin,a[i].y));
}
int main()
{
    int t=1;
    while(cin>>n)
    {
        if(n==0)
            break;
        ans=inf;
        num1=num2=0;
        for(int i=0;i<n;i++)
        {
            int x=read(),y=read(),z=read();
            if(z==0)
                a[num1].x=x,a[num1++].y=y;
            else
                b[num2].x=x,b[num2++].y=y;
        }
        dfs(0,-1,-inf,inf,-inf,inf);
        if(ans!=inf)
            printf("Case %d: %d\n",t++,ans);
        else
            printf("Case %d: -1\n",t++);
    }
}

 

posted @ 2015-07-21 18:32  qscqesze  阅读(216)  评论(0编辑  收藏  举报