• 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
    ");
    108         }
    109         else printf("%d
    ",ans);
    110     }
    111     return 0;
    112 }
    View Code
  • 相关阅读:
    最小生成树示例程序_Prim算法
    邻接表示例程序
    拓扑排序示例程序
    hdu1754 I Hate It && hdu1166 敌兵布阵 ——线段树复习
    2013年4月26日 晴
    Snakes & Ladders ——BFS入门题
    zoj1203 Swordfish ——最小生成树入门题_Kruscal算法
    poj3087 Shuffle'm Up ——水题
    poj1002 4873279 ——水题
    2013年4月21日 阴
  • 原文地址:https://www.cnblogs.com/zxhl/p/4665724.html
Copyright © 2020-2023  润新知