Result: Accepted
Time: 28ms
Memory: 1448kB
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
struct score{
char id[5];
int a;
int b;
int c;
int d;
};
int comp(const void *p,const void *q){
if(((struct score *)q)->d!=((struct score *)p)->d){
return ((struct score *)q)->d-((struct score *)p)->d;
} else if(((struct score *)q)->a!=((struct score *)p)->a){
return ((struct score *)q)->a-((struct score *)p)->a;
} else if(((struct score *)q)->b!=((struct score *)p)->b){
return ((struct score *)q)->b-((struct score *)p)->b;
} else if(((struct score *)q)->c!=((struct score *)p)->c){
return ((struct score *)q)->c-((struct score *)p)->c;
} else {
return strcmp(((struct score *)p)->id,((struct score *)q)->id);
}
}
int main()
{
struct score Array[10000];
int N,Q,i,j,k,l;
int sum[3];
char ID[5];
scanf("%d",&N);
for(i=0;i<N;i++){
scanf("%s %d %d %d",Array[i].id,&Array[i].a,&Array[i].b,&Array[i].c);
Array[i].d=Array[i].a+Array[i].b+Array[i].c;
}
for(i=0;i<N;i++){
sum[0]+=Array[i].a;
sum[1]+=Array[i].b;
sum[2]+=Array[i].c;
}
scanf("%d",&Q);
for(i=1;i<=Q;i++){
scanf("%d %d",&k,&l);
if(k==1){
for(j=4;j>=0;j--){
ID[j]='0'+l%10;
l=l/10;
}
printf("Query #%d: %d ",i,k);
puts(ID);
for(j=0;j<N;j++){
if(strcmp(Array[j].id,ID)==0){
break;
}
}
printf("%d %d %d %d\n",Array[j].d,Array[j].a,Array[j].b,Array[j].c);
} else if(k==2){
printf("Query #%d: %d %d\n",i,k,l);
printf("%.2lf\n",1.0*sum[l-1]/N);
}
}
qsort(Array,N,sizeof(struct score),comp);
for(i=0;i<N;i++){
printf("%s %d %d %d %d\n",Array[i].id,Array[i].d,Array[i].a,Array[i].b,Array[i].c);
}
return 0;
}