找中位数。先进行多关键字排序,然后再从头扫面一遍,统计结果。 /* source code of submission 312635, Zhongshan University Online Judge System */ #include<cstdio> #include<algorithm> using namespace std; struct node { int p,a; bool operator<(const node &y) const { if ( p==y.p ) { return a<y.a; } else { return p<y.p; } } }; int main() { int n; node v[1000003]; while ( scanf("%d", &n)!=EOF ) { for (int i=0; i<n; ++i) { scanf("%d%d", &v[i].p, &v[i].a); } sort( v, v+n );//调用sort,加上自定义"<"操作符实现多关键字排序 int s,t; s=t=0;//找出每个星球的范围[s,t) while ( s<n ) { while ( v[++t].p == v[s].p && t<n ) ; if ( (t-s)%2==1 ) { printf("%d %d/n", v[s].p, v[(t+s)/2].a); } else { printf("%d %d/n", v[s].p, (v[(t+s)/2].a+v[(s+t)/2-1].a)/2 ); } s=t; } } return 0; }