首页 > 代码库 > PAT 1075. PAT Judge (25)

PAT 1075. PAT Judge (25)

题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1075

此题主要考察细节的处理,和对于题目要求的正确理解,另外就是相同的总分相同的排名的处理一定要熟练,还有就是编译没有通过为零分,没有提交显示为"-":

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int NUM=10001;
vector<int> p(6);
int N,K,M;
struct User
{
    User()
    {
        scores.resize(6);
        for(int i=1;i<=5;++i)
        {
            scores[i]=-2;
        }
        totalScores=0;
        id=NUM;
    }
    int id;
    int totalScores;
    vector<int> scores;
    bool operator<(const User& rhs) const
    {
        
        if(totalScores!=rhs.totalScores)
        {
            return totalScores>rhs.totalScores;
        }
        int psl=0,psr=0;
        for(int i=1;i<=K;++i)
        {
            if(p[i]==scores[i])
                ++psl;
            if(p[i]==rhs.scores[i])
                ++psr;
        }
        if(psl!=psr)
        {
            return psl>psr;
        }
        else
        {
            return id<rhs.id;
        }
    }
};


vector<User> users(NUM);
int _tmain(int argc, _TCHAR* argv[])
{
    freopen("1075.txt","r",stdin);
    scanf("%d %d %d",&N,&K,&M);
    int i;
    for(i=1;i<=K;++i)
    {
        scanf("%d",&p[i]);
    }
    int userID,pID,partialScore;
    int totalUsers=0;
    for(i=0;i<M;++i)
    {
        scanf("%d %d %d",&userID,&pID,&partialScore);
        if(partialScore==-1)
        {
            if(users[userID].scores[pID]<0)
                users[userID].scores[pID]=0;
            continue;
        }
        else if(partialScore>users[userID].scores[pID])
        {
            if(users[userID].scores[pID]>=0)
                users[userID].totalScores+=partialScore-users[userID].scores[pID];
            else
                users[userID].totalScores+=partialScore;
            users[userID].scores[pID]=partialScore;
        }
        if(users[userID].id==NUM)
        {
            users[userID].id=userID;
            ++totalUsers;
        }
    }
    sort(users.begin()+1,users.begin()+N+1);
    int curRank=1;
    int curTotalScore=users[1].totalScores;
    int j,sameRank=-1;
    for(i=1;i<=totalUsers;++i)
    {
        if(curTotalScore>users[i].totalScores)
        {
            ++curRank;
            curRank+=sameRank;
            sameRank=0;
            curTotalScore=users[i].totalScores;
        }
        else
            ++sameRank;
        printf("%d %.5d %d",curRank,users[i].id,users[i].totalScores);
        for(j=1;j<=K;++j)
        {
            if(users[i].scores[j]>=0)
                printf(" %d",users[i].scores[j]);
            else
                printf(" -");
        }
        printf("\n");
    }
    return 0;
}
View Code