Cow Rectangles

Cow Rectangles

题目描述

The locations of Farmer John‘s N cows (1 <= N <= 500) are described by distinct points in the 2D plane.  The cows belong to two different breeds: Holsteins and Guernseys.  Farmer John wants to build a rectangular fence with sides parallel to the coordinate axes enclosing only Holsteins, with no Guernseys (a cow counts as enclosed even if it is on the boundary of the fence).  Among all such fences, Farmer John wants to build a fence enclosing the maximum number of Holsteins.  And among all these fences, Farmer John wants to build a fence of minimum possible area.  Please determine this area.  A fence of zero width or height is allowable.

输入

The first line of input contains N.  Each of the next N lines describes a cow, and contains two integers and a character. The integers indicate a point (x,y) (0 <= x, y <= 1000) at which the cow
is located. The character is H or G, indicating the cow‘s breed.  No two cows are located at the same point, and there is always at least one Holstein.

输出

Print two integers. The first line should contain the maximum number of Holsteins that can be enclosed by a fence containing no Guernseys, and second line should contain the minimum area enclosed by such a fence.

样例输入

5
1 1 H
2 2 H
3 3 G
4 4 H
6 6 H

样例输出

2
1分析:答案的矩形四个边界必然有H型牛;   所以可以枚举上下边界,对于左右边界双指针更新答案,复杂度O(N³);   注意要排除边界上G型牛;代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e3+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,k,t,y[maxn],ans[2];
struct node
{
    int x,y,z;
    bool operator<(const node&p)const
    {
        if(x==p.x)return z<p.z;
        else return x<p.x;
    }
}a[maxn];
char b[10];
int main()
{
    int i,j;
    scanf("%d",&n);
    rep(i,0,n-1){
        scanf("%d%d%s",&a[i].x,&a[i].y,b);
        if(b[0]==‘H‘)a[i].z=1,y[m++]=a[i].y;;
    }
    sort(a,a+n);
    sort(y,y+m);
    int num=unique(y,y+m)-y;
    rep(i,0,num-1)rep(j,i,num-1)
    {
        int l=0,r,now=0,flag=-1;
        rep(k,0,n-1)
        {
            if(a[k].y>=y[i]&&a[k].y<=y[j])
            {
                if(!a[k].z)
                {
                    flag=a[k].z;
                    if(now>ans[0]||(now==ans[0]&&(r-l)*(y[j]-y[i])<ans[1]))
                    {
                        ans[0]=now;
                        ans[1]=(r-l)*(y[j]-y[i]);
                    }
                    now=0,l=0;
                }
                else
                {
                    if(a[k].x==flag)continue;
                    now++;
                    if(l)r=a[k].x;
                    else l=a[k].x,r=a[k].x;
                }
            }
        }
        if(now>ans[0]||(now==ans[0]&&(r-l)*(y[j]-y[i])<ans[1]))
        {
            ans[0]=now;
            ans[1]=(r-l)*(y[j]-y[i]);
        }
    }
    printf("%d\n%d\n",ans[0],ans[1]);
    //system("Pause");
    return 0;
}
时间: 2024-10-09 21:20:32

Cow Rectangles的相关文章

【BZOJ3885】【Usaco2015 Jan】Cow Rectangles 某奇怪的最大子矩形

广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/44095063"); } 题意: 坐标系上给出n个点,分"H"和"G",一个整点坐标上至多一个点. 现在求一个不包含"G"的包含尽量多"H"的子矩形,然后

poj3278Catch That Cow

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88361   Accepted: 27679 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,00

The Cow Lineup_找规律

Description Farmer John's N cows (1 <= N <= 100,000) are lined up in a row.Each cow is labeled with a number in the range 1...K (1 <= K <=10,000) identifying her breed. For example, a line of 14 cows might have these breeds: 1 5 3 2 5 1 3 4 4

poj 1985 Cow Marathon 【树的直径】

题目:poj 1985 Cow Marathon 题意:给出一个树,让你求树的直径. 分析: 树的直径:树上两点之间的最大距离. 我们从任意一点出发,BFS一个最远距离,然后从这个点出发,在BFS一个最远距离,就是树的直径. AC代码: /* POJ:1985 Cow Marathon 2014/10/12/21:18 Yougth*/ #include <cstdio> #include <iostream> #include <algorithm> #include

POJ 2018 Best Cow Fences

斜率优化DP...<浅谈数形结合思想在信息学竞赛中的应用 安徽省芜湖一中 周源>例题... Best Cow Fences Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9311   Accepted: 2986 Description Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field c

[USACO11FEB] Cow Line

https://www.luogu.org/problem/show?pid=3014 题目描述 The N (1 <= N <= 20) cows conveniently numbered 1...N are playing yet another one of their crazy games with Farmer John. The cows will arrange themselves in a line and ask Farmer John what their line

luogu P2863 [USACO06JAN]牛的舞会The Cow Prom

https://www.luogu.org/problem/show?pid=2863#sub 题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their finest gowns, complete with corsages and new shoes. They know that tonight they will each try to perform th

Catch That Cow(广搜)

个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时: 将每个动作扔入队列,但要注意如何更简便,更节省时间,空间 Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and t

TOJ 2703: Cow Digit Game

2703: Cow Digit Game Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte Total Submit: 1            Accepted:1 Description Bessie is playing a number game against Farmer John, and she wants you to help her achieve victory. Game i start