最后一个测试点一直通不过,琢磨了一上午,终于找出bug
主要原因:考生k的第一个成绩是-1的情况,应将这项成绩记下,至于k是否所有提交的成绩都是-1,通过设置成员变量AC来判断,AC为true,写入vector参加后续排序,AC为false,不写入vector不参加后续排序。
AC代码:
#include <algorithm> #include <vector> #include <map> #include <cstdio> using namespace std; class pat{ public: bool ac; int id; map<int,int> score; int sum; int rank; int perfect; pat(){sum = 0;ac = false;perfect = 0;}; }; bool operator<(const pat& a,const pat& b){ if(a.ac && !b.ac){ return true; } else if(!a.ac) return false; else{ if(a.sum != b.sum){ return a.sum > b.sum; } else{ if(a.perfect != b.perfect) return a.perfect > b.perfect; else return a.id < b.id; } } } int main(){ int n,k,m; scanf("%d %d %d",&n,&k,&m); vector<int> s(k+1); map<int,pat> list; for(int i = 0;i < k;i++){ int ps; scanf("%d",&ps); s[i+1] = ps; } for(int i = 0;i < m;i++){ int id,pid,ps; scanf("%d %d %d",&id,&pid,&ps); if(ps != -1){ if(list.find(id) == list.end() || list[id].score.find(pid) == list[id].score.end() || list[id].score[pid] < ps){ list[id].id = id; list[id].ac = true; /*if(list[id].score.find(pid) != list[id].score.end()){ list[id].sum -= list[id].score[pid]; list[id].sum += ps; } else{ list[id].sum += ps; }*/ list[id].score[pid] = ps; if(ps == s[pid]){ list[id].perfect++; } } } else{ if(list.find(id) == list.end() || list[id].score.find(pid) == list[id].score.end()){ list[id].id = id; list[id].score[pid] = 0; } } } vector<pat> ret; for(map<int,pat>::iterator ite = list.begin();ite != list.end();ite++){ if(!ite->second.ac) continue; ret.push_back(ite->second); for(int j = 1;j <= k;j++){ if(ret.back().score.find(j) != ret.back().score.end()) ret.back().sum += ret.back().score[j]; } } sort(ret.begin(),ret.end()); for(int i = 0;i < ret.size();i++){ if(!ret[i].ac) continue; if(i == 0){ printf("1 %05d %d",ret[i].id,ret[i].sum); ret[i].rank = 1; for(int j = 1;j <= k;j++){ if(ret[i].score.find(j) == ret[i].score.end()){ printf(" -"); } else{ printf(" %d",ret[i].score[j]); } } printf("\n"); } else{ if(ret[i].sum == ret[i-1].sum) ret[i].rank = ret[i-1].rank; else ret[i].rank = i+1; printf("%d %05d %d",ret[i].rank,ret[i].id,ret[i].sum); for(int j = 1;j <= k;j++){ if(ret[i].score.find(j) == ret[i].score.end()){ printf(" -"); } else{ printf(" %d",ret[i].score[j]); } } printf("\n"); } } return 0; }