奥运排序问题
题目描述
按要求,给国家进行排名。
输入描述:
有多组数据。
第一行给出国家数N,要求排名的国家数M,国家号从0到N-1。
第二行开始的N行给定国家或地区的奥运金牌数,奖牌数,人口数(百万)。
接下来一行给出M个国家号。
输出描述:
排序有4种方式: 金牌总数 奖牌总数 金牌人口比例 奖牌人口比例
对每个国家给出最佳排名排名方式 和 最终排名
格式为: 排名:排名方式
如果有相同的最终排名,则输出排名方式最小的那种排名,对于排名方式,金牌总数 < 奖牌总数 < 金牌人口比例 < 奖牌人口比例
如果有并列排名的情况,即如果出现金牌总数为 100,90,90,80.则排名为1,2,2,4.
每组数据后加一个空行。
示例1
输入
4 4
4 8 1
6 6 2
4 8 2
2 12 4
0 1 2 3
4 2
8 10 1
8 11 2
8 12 3
8 13 4
0 3
输出
1:3
1:1
2:1
1:2
1:1
1:1
难点:要注意对于人口等于0 的处理,这是本题最坑的点。除的时候如果分母为零,要按照分子讨论,分子为零则整体为零,否则置为正无穷(用一个较大的值即可)。
代码:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef pair<int,int> PII1;
typedef pair<double,int> PII2;
int n,m;
int get_rank1(int k,vector<PII1> m){
int ans=0;
for(int i=0;i<m.size();i++){
if(m[i].second!=k){
if(i==0||m[i].first!=m[i-1].first)
ans=i+1;
}
else
{
if(i==0||m[i].first!=m[i-1].first)
ans=i+1;
break;
}
}
return ans;
}
int get_rank2(int k,vector<PII2> m){
int ans=0;
for(int i=0;i<m.size();i++){
if(m[i].second!=k){
if(i==0||m[i].first!=m[i-1].first)
ans=i+1;
}
else
{
if(i==0||m[i].first!=m[i-1].first)
ans=i+1;
break;
}
}
return ans;
}
int main(){
while(cin>> n >>m){
vector<PII1> gold,medal;
vector<PII2> gold_proportion,medal_proportion;
for(int i=0;i<n;i++){
int a,b,c;
cin >> a >> b >> c;
gold.push_back({a,i});
medal.push_back({b,i});
if(c!=0){
gold_proportion.push_back({(double)a/c,i});
medal_proportion.push_back({(double)b/c,i});
}
else{
if(a==0)
gold_proportion.push_back({0.0,i});
else
gold_proportion.push_back({0x3f,i});
if(b==0)
medal_proportion.push_back({0.0,i});
else
medal_proportion.push_back({0x3f,i});
}
}
sort(gold.begin(),gold.end());
reverse(gold.begin(),gold.end());
sort(medal.begin(),medal.end());
reverse(medal.begin(),medal.end());
sort(gold_proportion.begin(),gold_proportion.end());
reverse(gold_proportion.begin(),gold_proportion.end());
sort(medal_proportion.begin(),medal_proportion.end());
reverse(medal_proportion.begin(),medal_proportion.end());
while(m--){
int k;
cin >> k;
int t=get_rank1(k,gold);
int way=1;
if(t>get_rank1(k,medal)){
t=get_rank1(k,medal);
way=2;
}
if(t>get_rank2(k,gold_proportion)){
t=get_rank2(k,gold_proportion);
way=3;
}
if(t>get_rank2(k,medal_proportion)){
t=get_rank2(k,medal_proportion);
way=4;
}
printf("%d:%d\n",t,way);
}
printf("\n");
}
}