Input Specification:
Each input file contains one test case. For each case, the first line contains 3 positive integers, N (<=104), the total number of users, K (<=5), the total number of problems, and M (<=105), the total number of submittions. It is then assumed that the user id's are 5-digit numbers from 00001 to N, and the problem id's are from 1 to K. The next line contains K positive integers p[i] (i=1, ..., K), where p[i] corresponds to the full mark of the i-th problem. Then M lines follow, each gives the information of a submittion in the following format:
user_id problem_id partial_score_obtained
where partial_score_obtained is either -1 if the submittion cannot even pass the compiler, or is an integer in the range [0, p[problem_id]]. All the numbers in a line are separated by a space.
Output Specification:
For each test case, you are supposed to output the ranklist in the following format:
rank user_id total_score s[1] ... s[K]
where rank is calculated according to the total_score, and all the users with the same total_score obtain the same rank; and s[i] is the partial score obtained for the i-th problem. If a user has never submitted a solution for a problem, then "-" must be printed at the corresponding position. If a user has submitted several solutions to solve one problem, then the highest score will be counted.
The ranklist must be printed in non-decreasing order of the ranks. For those who have the same rank, users must be sorted in nonincreasing order according to the number of perfectly solved problems. And if there is still a tie, then they must be printed in increasing order of their id's. For those who has never submitted any solution that can pass the compiler, or has never submitted any solution, they must NOT be shown on the ranklist. It is guaranteed that at least one user can be shown on the ranklist.
IDEA
1.map<int,User> record;记录每个用户的分数情况
2.遍历record,计算每个用户的总分,如果发现某个用户没有提交或者没有通过编译,则删除该用户,record.erase(it++);
此处注意erase()在循环体中的应用(http://blog.csdn.net/baidu_32157201/article/details/51711152)
if(flag){
record.erase(it++);
}else{
it++;
}
3.对map进行排序,map没有直接排序方法,转成vector,再按照要求进行排序(http://blog.csdn.net/baidu_32157201/article/details/51711200)
4.排序完成,分配排名,注意总分相同则排名名次相同
5.注意:虽然编译未通过表示-1,但是不是分数为-1分,不参与分数就算,就没有这一项的成绩(同没有提交);
输出的的时候编译未通过的分数为0分,为提交的表示为-
CODE
#include<iostream>
#include<cstdio>
#include<vector>
#include<map>
#include<algorithm>
#include<fstream>
using namespace std;
#define MaxK 5
struct User{
int score[MaxK];
int sum;
int full_cnt;
int rank;
};
int cmp(const pair<int,User> &p1,const pair<int,User> &p2){
if(p1.second.sum==p2.second.sum){
if(p1.second.full_cnt==p2.second.full_cnt){
return p1.first<p2.first;
}else{
return p1.second.full_cnt>p2.second.full_cnt;
}
}else{
return p1.second.sum>p2.second.sum;
}
}
void sortMap(map<int,User> &m,vector<pair<int,User> > &vec){
map<int,User>::iterator it;
for(it=m.begin();it!=m.end();it++){
vec.push_back(make_pair(it->first,it->second));
}
sort(vec.begin(),vec.end(),cmp);
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif
int n,k,m;
cin>>n>>k>>m;
int p[MaxK];
for(int i=0;i<k;i++){
cin>>p[i];
}
map<int,User> record;
for(int i=0;i<m;i++){
int u_id,p_id,score;
cin>>u_id>>p_id>>score;
if(record.find(u_id)!=record.end()){
if(score>record[u_id].score[p_id-1]){
record[u_id].score[p_id-1]=score;
}
}else{
User user;
user.sum=0;
user.full_cnt=0;
for(int j=0;j<k;j++){
user.score[j]=-2;
}
record[u_id]=user;
record[u_id].score[p_id-1]=score;
}
}
map<int,User>::iterator it;
it=record.begin();
while(it!=record.end()){
int flag=-1;
for(int i=0;i<k;i++){
if(it->second.score[i]!=-2&&it->second.score[i]!=-1){
flag=0;
it->second.sum+=it->second.score[i];
}
if(it->second.score[i]==p[i]){
it->second.full_cnt++;
}
}
if(flag){
record.erase(it++);
}else{
it++;
}
}
vector< pair<int,User> > result;
sortMap(record,result);
result[0].second.rank=1;
for(int i=1;i<result.size();i++){
if(result[i].second.sum==result[i-1].second.sum){
result[i].second.rank=result[i-1].second.rank;
}else{
result[i].second.rank=i+1;
}
}
for(int i=0;i<result.size();i++){
printf("%d %05d %d ",result[i].second.rank,result[i].first,result[i].second.sum);
for(int j=0;j<k;j++){
if(j==0){
if(result[i].second.score[j]==-2){
printf("-");
}else if(result[i].second.score[j]==-1){
printf("0");
}else{
printf("%d",result[i].second.score[j]);
}
}else{
if(result[i].second.score[j]==-2){
printf(" -");
}else if(result[i].second.score[j]==-1){
printf(" 0");
}else{
printf(" %d",result[i].second.score[j]);
}
}
}
cout<<endl;
}
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}