题目:http://pat.zju.edu.cn/contests/pat-a-practise/1075
题解:
提交时未通过的在排名输出时显示为0。
需要在排名中显示的至少有一个通过的提交(即只有-1的不显示)。
代码:
#include<cstdio> #include<cstring> #include<cmath> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<algorithm> using namespace std; #define INF 0x6fffffff #define MAX 10005 struct student { int id; int score[7]; int solved; int summ; bool flag; } stu[MAX]; int score[7]; bool cmp(const struct student &a,const struct student &b) { if(a.summ>b.summ) return true; else if(a.summ==b.summ) { return a.solved==b.solved?(a.id<b.id):(a.solved>b.solved); } else return false; } int main() { int n,m,k,len; int name,id,s; scanf("%d%d%d",&n,&k,&m); for(int i=1; i<=k; ++i) scanf("%d",&score[i]); for(int i=1; i<=n; ++i) { stu[i].id=i; stu[i].solved=0; stu[i].summ=0; stu[i].flag=false; for(int j=1; j<=k; ++j) stu[i].score[j]=-2; } for(int i=0; i<m; ++i) { scanf("%d%d%d",&name,&id,&s); if(s>=0) { stu[name].flag=true; if(stu[name].score[id]<s) { stu[name].score[id]=s; if(s==score[id]) ++stu[name].solved; } } else if(s==-1) { if(stu[name].score[id]<s) { stu[name].score[id]=s; } } } for(int i=1; i<=n; ++i) { for(int j=1; j<=k; ++j) if(stu[i].score[j]>0) stu[i].summ+=stu[i].score[j]; } sort(stu+1,stu+1+n,cmp); printf("1 %05d %d",stu[1].id,stu[1].summ); for(int i=1; i<=k; ++i) { if(stu[1].score[i]>=0) printf(" %d",stu[1].score[i]); else if(stu[1].score[i]==-1) printf(" 0"); else printf(" -"); } printf("\n"); if(n>1&&stu[0].summ!=stu[1].summ) len=1; else len=0; for(int i=2,idx=1; i<=n; ++i) { if(stu[i].flag==false) continue; if(stu[i].summ==stu[i-1].summ) { printf("%d %05d %d",idx,stu[i].id,stu[i].summ); for(int j=1; j<=k; ++j) { if(stu[i].score[j]>=0) printf(" %d",stu[i].score[j]); else if(stu[i].score[j]==-1) printf(" 0"); else printf(" -"); } ++len; } else { idx+=len; len=0; printf("%d %05d %d",idx,stu[i].id,stu[i].summ); for(int j=1; j<=k; ++j) { if(stu[i].score[j]>=0) printf(" %d",stu[i].score[j]); else if(stu[i].score[j]==-1) printf(" 0"); else printf(" -"); } ++len; } printf("\n"); } return 0; }