欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

牛客 - 排序(模拟)

程序员文章站 2022-05-27 15:25:45
...

题目链接:点击查看

题目大意:模拟ACM赛制下每个队伍的排名,中文题面,不多赘述,规则在原题中讲的很清楚了

题目分析:直接模拟即可。。一点坑都没有,我是因为sort忘记调用cmp函数然后WA了一晚上,哭了,细节决定成败呜呜呜

代码:

#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<unordered_map>
using namespace std;
   
typedef long long LL;
   
const int inf=0x3f3f3f3f;
   
const int N=110;
  
struct Problem
{
    bool AC;//是否AC
    int waste;//罚时
    int cnt_waste;//错误次数
    int time;//AC时间
    void cal()//AC后计算内部的数值
    {
        cnt_waste++;
        AC=true;
        waste+=time;
    }
};
  
struct Node
{
    string name;//姓名(其实赋个值当构造函数用了,不然怕map里不给存)
    Problem problem[20];//每个题目
    int time;//总罚时
    int cnt_AC;//AC数
    int rank;//排名
    bool operator<(const Node& a)const//重载小于号用于排序
    {
        if(cnt_AC!=a.cnt_AC)
            return cnt_AC>a.cnt_AC;
        return time<a.time;
    }
    bool operator==(const Node& a)const//重载等于号用于排序和rank更新
    {
        return cnt_AC==a.cnt_AC&&time==a.time;
    }
};
  
unordered_map<string,Node>mp;//维护整体的容器
  
bool cmp(pair<string,Node>a,pair<string,Node>b)//排序
{
    if(a.second==b.second)
        return a.first<b.first;
    return a.second<b.second;
}
  
int main()
{
//  freopen("input.txt","r",stdin);
//  ios::sync_with_stdio(false);
    int w;
    cin>>w;
    while(w--)
    {
        mp.clear();//记得初始化
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=m;i++)//读入姓名,当初始化了
        {
            string s;
            cin>>s;
            mp[s].name=s;
        }
        int t;
        cin>>t;
        while(t--)
        {
            string name;//姓名
            int time;//提交时间
            string pos;//题目
            string state;//状态
            cin>>name>>time>>pos>>state;
            if(state=="Compilation-Error"||mp[name].problem[pos[0]-'A'].AC)//编译错误或者已经AC
                continue;
            else if(state=="Accepted")//AC
            {
                mp[name].problem[pos[0]-'A'].time=time;
                mp[name].problem[pos[0]-'A'].cal();
                mp[name].time+=mp[name].problem[pos[0]-'A'].waste;
                mp[name].cnt_AC++;
            }
            else//WA
            {
                mp[name].problem[pos[0]-'A'].waste+=20;
                mp[name].problem[pos[0]-'A'].cnt_waste++;
            }
        }
        vector<pair<string,Node>>v;
        copy(mp.begin(),mp.end(),back_inserter(v));//把map扔进vector里方便排序
        sort(v.begin(),v.end(),cmp);//排序
        v[0].second.rank=1;
        for(int i=1;i<v.size();i++)//更新rank
        {
            if(v[i].second==v[i-1].second)
                v[i].second.rank=v[i-1].second.rank;
            else
                v[i].second.rank=i+1;
        }
        for(int i=0;i<v.size();i++)//输出答案
        {
            printf("%d %s %d %d",v[i].second.rank,v[i].first.c_str(),v[i].second.cnt_AC,v[i].second.time);
            for(int j=0;j<n;j++)
            {
                printf(" %c%d",v[i].second.problem[j].AC?'+':'-',v[i].second.problem[j].cnt_waste);
                if(v[i].second.problem[j].AC)
                    printf("(%d)",v[i].second.problem[j].time);
            }
            printf("\n");
        }
        if(w)
            printf("\n");
    }
      
      
      
      
      
      
      
       
       
       
       
        
       
    return 0;
}

 

相关标签: 模拟