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++);
    }
}
时间: 2024-10-02 04:52:52

Codeforces Gym 100463D Evil DFS的相关文章

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. Wh

CF Gym 100463D Evil 前缀和+离散

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

Codeforces Gym - 101147J Whistle&#39;s New Car

Discription Statements Whistle has bought a new car, which has an infinite fuel tank capacity. He discovered an irregular country since it has n cities and there are exactly n?-?1roads between them, of course, all cities are connected. He is so much

Codeforces Gym 101174 A Within Arm&#39;s Reach 贪心 手臂

#include<iostream> #include<stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <math.h> using namespace std; #define LL long long const int maxn=25; double a[maxn],l[maxn],r[maxn]; double ex,ey

Codeforces gym Hello 2015 Div1 B and Div2 D

Codeforces gym 100571 problem D Problem 给一个有向图G<V,E>和源点S,边的属性有长度L和颜色C,即E=<L,C>.进行Q次询问,每次给定一个点X,输出S到X的最短路的长度(不存在则输出 -1).但要求S到X的路径中相邻两条边颜色不一样. Limits Time Limit(ms): 1000 Memory Limit(MB): 256 |V|, |E|: [1, 10^5] X, S: [1, |V| ] L: [1, 10^9] |C|

Codeforces gym Hello 2015 Div1 E

Codeforces gym 100570 problem E (一种处理动态最长回文子串问题的方法) Problem 给一个长度为N的字符串S,字符集是'a'-'z'.进行Q次操作,操作分三种.一,修改位置X的字符为C:二,查询以P位置为中心的最长回文子串的长度,并输出:三,查询以P与P+1的中间位置为中心的最长回文子串的长度,并输出. More 第二种操作子串长度为奇数,一定存在:第三种操作子串长度为偶数,若不存在,输出 -1. Limits Time Limit(ms): 4000(1s足

Codeforces gym Hello 2015 Div1 C and Div2 E

Codeforces gym 100570 problem C Codeforces gym 100571 problem E Problem 给一个N行M列的矩阵Ma,进行Q次(Q<=10)查询,每次给定一个K,问有多少子矩阵,满足最大值max - 最小值min <=K. Limits Time Limit(ms): 8000 Memory Limit(MB): 512 N, M: [1, 400] Q: [1, 10] Ma(i, j), K: [1, 10^9] Solution (Th

Codeforces 384E 线段树+dfs序

题目链接:点击打开链接 题意: 给定n个点,m个询问的无向树(1为根) 下面n个数表示每个点的权值 下面n-1行给出树 操作1:x点权值+v, x的第 i & 1 的儿子-v, 第 !(i&1) 的儿子+v 操作2:询问x点权值 dfs把树转成序列 根据深度把点分成2组 分别用线段树维护.. 然后Y一下 #include<stdio.h> #include<string.h> #include<iostream> #include<algorith

【模拟】ECNA 2015 I What&#39;s on the Grille? (Codeforces GYM 100825)

题目链接: http://codeforces.com/gym/100825 题目大意: 栅栏密码.给定N(N<=10),密钥为一个N*N的矩阵,'.'代表空格可以看到,'X'代表被遮挡,还有密文字符串S,长度为N*N 每次将这个矩阵顺时针旋转90°,把矩阵中空格对应的位置按照从上到下从左到右的顺序依次填充上密文字符,求最终这个密文字符能否填满N*N的矩阵,能按顺序输出得到的答案,不能输出"invalid grille" 题目思路: [模拟] 直接模拟即可.旋转的坐标公式很好推.