http://pat.zju.edu.cn/contests/pat-a-practise/1025
灵活掌握容器的使用
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct stu
{
string no;
int score;
int location; //学生所在位置
int local; //本地排名(注意并列的处理)
int global; //全局排名
};
vector<stu> local_list; //保存本地学生数据
vector<stu> global_list; //保存全局学生数据
bool cmp(stu a,stu b)
{
if (a.score!=b.score)
{
return a.score>b.score;
}
else
{
return a.no<b.no;
}
}
int main()
{
//freopen("D:\\test.txt","r",stdin);
int n,k,i,j; //n个地区,每个地区k个学生数据
cin>>n;
for (i=1;i<=n;i++)
{
cin>>k;
while (k--) //输入
{
stu s;
cin>>s.no>>s.score; //输入编号,成绩
s.location=i; //位置就是i
local_list.push_back(s);
}
sort(local_list.begin(),local_list.end(),cmp); //成绩排序
for (j=0;j<local_list.size();j++) //本地排名
{ //表达式中有j-1,所以j!=0
if (j!=0 && local_list[j].score==local_list[j-1].score )
{
local_list[j].local=local_list[j-1].local;
}
else
{
local_list[j].local=j+1;
}
}
global_list.insert(global_list.end(),local_list.begin(),local_list.end()); //本地数据插进全局容器
local_list.clear(); //清空容器
}
cout<<global_list.size()<<endl;
sort(global_list.begin(),global_list.end(),cmp);
for (i=0;i<global_list.size();i++) //全局排名
{
if (i!=0 && global_list[i].score==global_list[i-1].score )
{
global_list[i].global=global_list[i-1].global;
}
else
{
global_list[i].global=i+1;
}
cout<<global_list[i].no<<" "<<global_list[i].global<<" "
<<global_list[i].location<<" "<<global_list[i].local<<endl;
}
return 0;
}