Coder RatingsProblem code: RATING |
Some of the more elite (and not-so-elite) coders around take part in a certain unnamed programming contest. In said contest, there are multiple types of competitions. Here, we consider the Open and High School competition types. For each type, each competitor receives a rating, an integer between 1 and 100000, inclusive. A coder's rating is based upon his or her level of performance in matches and is calculated using a complicated formula which, thankfully, you will not be asked to implement.
Although the Open and High School ratings for a coder who has participated in both competition types lately are usually close, this is not always the case. In particular, High School matches are more about speed, since many coders are able to solve all the problems, whereas Open matches require more thinking and there is a steeper curve in terms of problem difficulty.
Problem Statement
You are given N coders (1 ≤ N ≤ 300000), conveniently numbered from 1 to N. Each of these coders participates in both High School and Open matches. For each coder, you are also given an Open rating Ai and a High School rating Hi. Coder i is said to be better than coder j if and only if both of coder i's ratings are greater than or equal to coder j's corresponding ratings, with at least one being greater. For each coder i, determine how many coders coder i is better than.
Input Format
On the first line of input is a single integer N, as described above.
N lines then follow. Line i+1 contains two space-separated integers, Ai and Hi.
Output Format
Line i should contain the number of coders that coder i is better than.
Sample Input
8 1798 1832 862 700 1075 1089 1568 1557 2575 1984 1033 950 1656 1649 1014 1473
Sample Output
6 0 2 4 7 1 5 1
解决方案:做这题是,w了好几遍不知怎么处理当两个rating都一样的情况。可先对Hi排序,Hi相等再对Ai排序,数状数组求和后,在看看之前加入树状数组有多少个coder的rating和当前的一模一样,这些一样的个数必须减去。
code:#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define MMAX 100003 #define Max 300003 using namespace std; int C[MMAX]; int ans[Max]; int N; struct coder { int open,high; int s; }; coder Code[Max]; bool cmp(coder a,coder b) { if(a.high!=b.high) return a.high<b.high; else return a.open<b.open; } int sum(int x) { int s=0; while(x>0) { s+=C[x]; x-=(x&-x); } return s; } void update(int x,int v) { while(x<=MMAX) { C[x]+=v; x+=(x&-x); } } int main() { while(~scanf("%d",&N)) { memset(C,0,sizeof(C)); memset(ans,0,sizeof(ans)); for(int i=0; i<N; i++) { scanf("%d%d",&Code[i].open,&Code[i].high); Code[i].s=i; } sort(Code,Code+N,cmp); for(int i=0; i<N; i++) { int k=sum(Code[i].open); int temp=0; for(int j=i-1;j>=0&&Code[i].high==Code[j].high;j--){ if(Code[i].open==Code[j].open){ temp++; } } ans[Code[i].s]=k-temp; update(Code[i].open,1); } for(int i=0; i<N; i++) { printf("%d\n",ans[i]); } } return 0; }