Cow Rectangles

时间:2021-06-08 13:26:03

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+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,y[maxn],ans[];
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[];
int main()
{
int i,j;
scanf("%d",&n);
rep(i,,n-){
scanf("%d%d%s",&a[i].x,&a[i].y,b);
if(b[]=='H')a[i].z=,y[m++]=a[i].y;;
}
sort(a,a+n);
sort(y,y+m);
int num=unique(y,y+m)-y;
rep(i,,num-)rep(j,i,num-)
{
int l=-,r,now=,flag=-;
rep(k,,n-)
{
if(a[k].y>=y[i]&&a[k].y<=y[j])
{
if(!a[k].z)
{
flag=a[k].x;
if(now>ans[]||(now==ans[]&&(r-l)*(y[j]-y[i])<ans[]))
{
ans[]=now;
ans[]=(r-l)*(y[j]-y[i]);
}
now=,l=-;
}
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[]||(now==ans[]&&(r-l)*(y[j]-y[i])<ans[]))
{
ans[]=now;
ans[]=(r-l)*(y[j]-y[i]);
}
}
printf("%d\n%d\n",ans[],ans[]);
//system("Pause");
return ;
}