Max Kingdom

Problem Description

Max is the king of the universe, he rules billions of planets. In each planet, there may be several residents. Residents of Max's kingdom will never die, so some of them may be as old as the universe (It is a magic world, isn't it?). One day, Max wants to know how intellegent his people are so that he collects the intellegence information of all residents throughout universe. The intellegence can be presented by integer under a standard decided by Max. (That means, if the intellegence of resident Bob is presented as 10000, Bob is much more intellgent than the stadard. And if Max's intellegence number is 0, Max is as intellegent as the standard.) Actually, Max just cares how intellegent each planet is. To decide the intellegent degree of one planet, Max will take the middle one from all intellegent numbers of that planet and make it as the criteria. Here is the method to pick up the middle one: Supposed there are n residents in planet i. The intellegence of them are a1, a2, …, an (a1 ≤ a2 ≤ a3 ≤ … ≤ an). If n is odd, then the middle one is a[(n+1)/2], otherwise, middle one equals (a[n/2] + a[n/2 + 1]) / 2 (Here character / means integer division, e.g. 3 / 2 = 1, 4 / 2 = 2). Since there are too many residents to process, now is your task to tell Max how smart his people are.

Input

Input contains multiple test data sets.
For each data set, there is one integer, n ( 1 <= n <= 1000000) the number of residents, in first line. Then comes n lines, each line i has two integers: p, a ( 1 ≤ p < 1.5 × 10^9, |a| ≤ 1.5 × 10^9) which means that resident i comes from planet p and his/her intellegent number is a. Input is terminated by EOF.

Output

For each test data set, one output data set should be generated as follow
For each planet p appears in the test data set, output one line presenting two integers: p which is the planet identifier from input and the middle one number described as above. Output data set should be sorted by planet identifier in ascending order.

Sample Input

3
2 10
1 5
2 15
2
1 2
3 4

Sample Output

1 5
2 12
1 2
3 4
 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <iostream>
 4 using namespace std;
 5 typedef long long ll;
 6 struct pa {
 7     int id;
 8     ll num;
 9 } t[1000005];
10 bool cmp(pa a,pa b) {
11     if(a.id!=b.id) return a.id<b.id;
12     else return a.num<b.num;
13 }
14 
15 int main() {
16     int n,i;
17     while(scanf("%d",&n)!=EOF) {
18         for(i=0; i<n; i++) scanf("%d %I64d",&t[i].id,&t[i].num);
19         sort(t,t+n,cmp);
20         int k=1,start=0;
21         t[n].id=-1,t[n].num=-1;
22         for(i=0; i<n; i++) {
23             if(t[i].id!=t[i+1].id){
24                 if(k%2==0) printf("%d %I64d\n",t[i].id,(t[(i+start)/2].num+t[(i+start)/2+1].num)/2);
25                 else printf("%d %I64d\n",t[i].id,t[(i+start)/2].num);
26                 k=1;
27                 start=i+1;
28             }
29             else k++;
30         }
31     }
32     return 0;
33 }
View Code

 

posted @ 2013-06-03 17:40  1002liu  阅读(190)  评论(0编辑  收藏  举报