Brownie Points II - POJ 2464 线段树
2014-12-12 23:00
459 查看
Brownie Points II
Description
Stan and Ollie play the game of Odd Brownie Points. Some brownie points are located in the plane, at integer coordinates. Stan plays first and places a vertical line in the plane. The line must go through a brownie point and may cross many (with the same x-coordinate).
Then Ollie places a horizontal line that must cross a brownie point already crossed by the vertical line.
Those lines divide the plane into four quadrants. The quadrant containing points with arbitrarily large positive coordinates is the top-right quadrant.
The players score according to the number of brownie points in the quadrants. If a brownie point is crossed by a line, it doesn't count. Stan gets a point for each (uncrossed) brownie point in the top-right and bottom-left quadrants. Ollie gets a point for
each (uncrossed) brownie point in the top-left and bottom-right quadrants.
Stan and Ollie each try to maximize his own score. When Stan plays, he considers the responses, and chooses a line which maximizes his smallest-possible score.
Input
Input contains a number of test cases. The data of each test case appear on a sequence of input lines. The first line of each test case contains a positive odd integer 1 < n < 200000 which is the number of brownie points. Each of the following n lines contains
two integers, the horizontal (x) and vertical (y) coordinates of a brownie point. No two brownie points occupy the same place. The input ends with a line containing 0 (instead of the n of a test).
Output
For each input test, print a line of output in the format shown below. The first number is the largest score which Stan can assure for himself. The remaining numbers are the possible (high) scores of Ollie, in increasing order.
Sample Input
Sample Output
题意:第一个人先选一个列,要求这个列包含点,第二个人在这个列上再选一个行,要求包含这个列上的某个点,最后一三象限的点是第一个人的得分,二四象限的点是第二个人的得分。第一个人会使自己可能的最小值最大,第二个人使自己的得分最大。问第一个人的最小的分的最大值,和在此情况下第二个人可能的得分。
思路:对于每个点,只要找出以它为中心对于两个人的得分即可,这个用线段树,先考虑第三象限的,再重建树做第一象限的,有了第一个人的得分,就可以推出第二个人的得分了,最后进行统计计算。
AC代码如下:
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 1987 | Accepted: 704 |
Stan and Ollie play the game of Odd Brownie Points. Some brownie points are located in the plane, at integer coordinates. Stan plays first and places a vertical line in the plane. The line must go through a brownie point and may cross many (with the same x-coordinate).
Then Ollie places a horizontal line that must cross a brownie point already crossed by the vertical line.
Those lines divide the plane into four quadrants. The quadrant containing points with arbitrarily large positive coordinates is the top-right quadrant.
The players score according to the number of brownie points in the quadrants. If a brownie point is crossed by a line, it doesn't count. Stan gets a point for each (uncrossed) brownie point in the top-right and bottom-left quadrants. Ollie gets a point for
each (uncrossed) brownie point in the top-left and bottom-right quadrants.
Stan and Ollie each try to maximize his own score. When Stan plays, he considers the responses, and chooses a line which maximizes his smallest-possible score.
Input
Input contains a number of test cases. The data of each test case appear on a sequence of input lines. The first line of each test case contains a positive odd integer 1 < n < 200000 which is the number of brownie points. Each of the following n lines contains
two integers, the horizontal (x) and vertical (y) coordinates of a brownie point. No two brownie points occupy the same place. The input ends with a line containing 0 (instead of the n of a test).
Output
For each input test, print a line of output in the format shown below. The first number is the largest score which Stan can assure for himself. The remaining numbers are the possible (high) scores of Ollie, in increasing order.
Sample Input
11 3 2 3 3 3 4 3 6 2 -2 1 -3 0 0 -3 -3 -3 -2 -3 -4 3 -7 0
Sample Output
Stan: 7; Ollie: 2 3;
题意:第一个人先选一个列,要求这个列包含点,第二个人在这个列上再选一个行,要求包含这个列上的某个点,最后一三象限的点是第一个人的得分,二四象限的点是第二个人的得分。第一个人会使自己可能的最小值最大,第二个人使自己的得分最大。问第一个人的最小的分的最大值,和在此情况下第二个人可能的得分。
思路:对于每个点,只要找出以它为中心对于两个人的得分即可,这个用线段树,先考虑第三象限的,再重建树做第一象限的,有了第一个人的得分,就可以推出第二个人的得分了,最后进行统计计算。
AC代码如下:
#include<cstdio> #include<cstring> #include<algorithm> #include<map> using namespace std; struct node1 { int x,y,ny,nx; }point[200010]; struct node2 { int l,r,num; }tree[800010]; int num[200010],num2[200010],ans[200010],p[200010],nx[200010],ny[200010]; map<int,int> match3; bool cmp1(node1 a,node1 b) { if(a.x==b.x) return a.y>b.y; return a.x<b.x; } bool cmp2(node1 a,node1 b) { return a.y>b.y; } void build(int o,int l,int r) { tree[o].l=l; tree[o].r=r; tree[o].num=0; if(l==r) return; int mi=(l+r)/2; build(o<<1,l,mi); build(o<<1|1,mi+1,r); } void update(int o,int l) { if(tree[o].l==tree[o].r) { tree[o].num++; return; } int mi=(tree[o].l+tree[o].r)/2; if(l<=mi) update(o<<1,l); else update(o<<1|1,l); tree[o].num=tree[o<<1].num+tree[o<<1|1].num; } int query(int o,int l,int r) { if(tree[o].l==l && tree[o].r==r) return tree[o].num; int mi=(tree[o].l+tree[o].r)/2; if(r<=mi) return query(o<<1,l,r); else if(l>mi) return query(o<<1|1,l,r); else return query(o<<1,l,mi)+query(o<<1|1,mi+1,r); } int main() { int n,m,i,j,k,pos,a,b,maxa,pre,q; while(~scanf("%d",&n) && n>0) { match3.clear(); for(i=1;i<=n;i++) num[i]=0; for(i=1;i<=n;i++) scanf("%d%d",&point[i].x,&point[i].y); sort(point+1,point+1+n,cmp2); match3[point[1].y]=1; m=1; for(i=2;i<=n;i++) if(point[i].y!=point[i-1].y) match3[point[i].y]=++m; pre=0;q=1;point[n+1].y=point .y+10; for(i=1;i<=n;i++) { point[i].ny=q; if(point[i].y!=point[i+1].y) { ny[q]=i-pre; pre=i; q++; } } sort(point+1,point+1+n,cmp1); pre=0;q=1;point[n+1].x=point .x+10; for(i=1;i<=n;i++) { point[i].nx=q; if(point[i].x!=point[i+1].x) { nx[q]=i-pre; pre=i; q++; } } build(1,1,m); for(i=1;i<=n;i++) { pos=p[i]=match3[point[i].y]; if(pos<m) num[i]+=query(1,pos+1,m); update(1,pos); } build(1,1,m); for(i=n;i>=1;i--) { pos=p[i]; if(pos>1) num[i]+=query(1,1,pos-1); update(1,pos); } for(i=1;i<=n;i++) num2[i]=n-nx[point[i].nx]-ny[point[i].ny]+1-num[i]; point[n+1].x=point .x+1; a=-1;b=-1;maxa=-1; for(i=1;i<=n;i++) { if(num2[i]>b) { b=num2[i]; a=num[i]; } else if(num2[i]==b) a=min(a,num[i]); if(point[i].x!=point[i+1].x) { if(a>maxa) { maxa=a; ans[0]=1; ans[1]=b; } else if(a==maxa) ans[++ans[0]]=b; a=b=-1; } } sort(ans+1,ans+1+ans[0]); printf("Stan: %d; Ollie: %d",maxa,ans[1]); for(i=2;i<=ans[0];i++) if(ans[i]!=ans[i-1]) printf(" %d",ans[i]); printf(";\n"); } }
相关文章推荐
- POJ 2464 Brownie Points II(树状数组||线段树)
- poj 2464 Brownie Points II(两棵线段树——线段树区间和)
- POJ 2464 Brownie Points II(线段树:扫描线)
- 线段树专题:poj 2464 Brownie Points II
- POJ 2464 Brownie Points II(扫描线 + 两棵线段树 区间求和)
- poj 2464 Brownie Points II(扫描线)
- poj 2464 Brownie Points II (扫描线)
- Poj 2464 Brownie Points II(平面两条垂直的先划分成四个象限,一三象限和二四象限各有多少个)
- POJ 2464 Brownie Points II(树状数组)
- POJ 2464 Brownie Points II --树状数组
- poj 2464 Brownie Points II 树状数组
- POJ 2464 Brownie Points II 树状数组+扫描线
- POJ 2464 Brownie Points II --树状数组
- POJ 2464 Brownie Points II
- poj 2464 Brownie Points II
- POJ 2464 Brownie Points II(树状数组)
- hdu 1156 && poj 2464 Brownie Points II (BIT)
- POJ 2464 Brownie Points II(树状数组+扫描线)
- Poj 3416 Crossing + Poj 2464 Brownie Points II (树状数组)
- UVA10869 - Brownie Points II(线段树)