A1025 PAT Ranking (25point(s))
题意
输入n个考场的学生信息,要求排序输出全局排名,考场排名等信息。
思路
考场排名可以使用数组解决。
Sample Input:
2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85
Sample Output:
9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4
#include"bits/stdc++.h"
using namespace std;
struct Node{
string id;
int g;
int locate;
int rank;
int locateRank;
};
int main(){
freopen("input.txt","r",stdin);
int n; cin >> n;
vector<Node*> nodes;
for(int i=0;i<n;i++){
int k; scanf("%d",&k);
for(int j=0;j<k;j++){
string s; int g;
cin >> s >> g;
Node *p = new Node{s,g,i+1};
nodes.push_back(p);
}
}
sort(nodes.begin(),nodes.end(),[](Node *t1,Node *t2){
if(t1->g == t2->g)
return t1->id < t2->id;
return t1->g > t2->g;
});
vector<Node*> locates[n+1];
for(int i=0;i<nodes.size();i++){
Node *p = nodes[i];
if(i){
p->rank = p->g == nodes[i-1]->g ? nodes[i-1]->rank : i+1;
}else
p->rank = i+1;
vector<Node*>&temp =locates[p->locate];
p->locateRank = temp.size() + 1;
if(!temp.empty() && p->g ==temp.back()->g )
p->locateRank = temp.back()->locateRank;
temp.push_back(p);
cout << p->id;
printf(" %d %d %d\n",p->rank,p->locate,p->locateRank);
}
}