Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
按x-y 升序sort 然后二分查找求出下标,2个下标中较小的大小,是不是就是位于当前星星左下方的个数?#include <iostream> #include <cstring> #include <stdio.h> #include <stdlib.h> #include <algorithm> #define N 15005 using namespace std; typedef struct node { int x,y; }Node; Node a[N],b[N]; int ans[N]; int cmp1(const void *a,const void *b) { Node *q1 = (Node*)a; Node *q2 = (Node*)b; if(q1->x != q2->x) return q1->x - q2->x; return q1->y - q2->y; } int search(Node a[],int left,int right,Node val) { int mid; while(left <= right) { mid = (left+right)/2; if(a[mid].x < val.x) left = mid + 1; else if(a[mid].x > val.x) right = mid - 1; else break; } while(a[mid].y != val.y) { if(a[mid].y > val.y) mid--; else mid++; } return mid; } int main() { int i,n,x,y,k; while(scanf("%d",&n) != EOF) { memset(ans,0,sizeof(ans)); for(i = 0;i < n;i++) { scanf("%d%d",&a[i].x,&a[i].y); b[i] = a[i]; } qsort(b,n,sizeof(Node),cmp1); for(i = 0;i < n;i++) { k = search(b,0,n-1,a[i]); k = k > i ? i : k; ans[k]++; } for(i = 0;i < n;i++) { cout << ans[i] << endl; } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator