您的位置:首页 > 其它

2010 浙大机试 奥运排序问题

2017-08-04 00:51 369 查看
[align=center]题目1007:奥运排序问题 [/align]

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:9450

解决:2044

题目描述:
按要求,给国家进行排名。

输入:
有多组数据。

第一行给出国家数N,要求排名的国家数M,国家号从0到N-1。

第二行开始的N行给定国家或地区的奥运金牌数,奖牌数,人口数(百万)。

接下来一行给出M个国家号。
输出:
排序有4种方式: 金牌总数 奖牌总数 金牌人口比例 奖牌人口比例

对每个国家给出最佳排名排名方式 和 最终排名

格式为: 排名:排名方式

如果有相同的最终排名,则输出排名方式最小的那种排名,对于排名方式,金牌总数 < 奖牌总数 < 金牌人口比例 < 奖牌人口比例

如果有并列排名的情况,即如果出现金牌总数为 100,90,90,80.则排名为1,2,2,4.

每组数据后加一个空行。
样例输入:
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

来源: 2010年浙江大学计算机及软件工程研究生机试真题
答疑: 解题遇到问题?分享解题心得?讨论本题请访问:http://t.jobdu.com/thread-7731-1-1.html代码 :[注]使用了函数指针和函数指针数组
#include <cstdio>
#include <algorithm>

using namespace std;

const int INF(0x7fffffff);

struct best_rank{
int rank, way;
best_rank(){
this->rank = INF;
this->way = INF;
}
best_rank(int r, int w){
this->rank = r;
this->way = w;
}
};
inline bool operator < (const best_rank &one, const best_rank &another){
if(one.rank != another.rank){
return (one.rank < another.rank);
}
return one.way < another.way;
}
struct nation{
int id;
int gold_medals, medals, population;
best_rank nation_rank;
};
int n, m;
const int maxn(1e5 + 10);
nation all_nations[maxn];
nation nations[maxn];

bool cmp1(nation one, nation another)
{
return (one.gold_medals > another.gold_medals);
}
bool cmp2(nation one, nation another)
{
return (one.medals > another.medals);
}
bool cmp3(nation one, nation another)
{
return (one.gold_medals * another.population > another.gold_medals * one.population);
}
bool cmp4(nation one, nation another)
{
return (one.medals * another.population > another.medals * one.population);
}
bool cmp5(nation one, nation another)
{
return (one.id < another.id);
}
void update(int way, bool (*cmp)(nation one, nation another))
{
int last = 1;
if(1 <= m){
nations[0].nation_rank = min(nations[0].nation_rank,best_rank(1,way));
}
for(int i = 1; i < m; i++){
if(!cmp(nations[i],nations[i - 1]) && !cmp(nations[i - 1],nations[i])){
nations[i].nation_rank = min(nations[i].nation_rank, best_rank(last,way));
}
else{
last = i + 1;
nations[i].nation_rank = min(nations[i].nation_rank, best_rank(last,way));
}
}
}
int main()
{
while(scanf("%d %d", &n, &m) != EOF)
{
for(int i = 0; i < n; i++){
all_nations[i].id = i;
scanf("%d %d %d",&all_nations[i].gold_medals, &all_nations[i].medals, &all_nations[i].population);
}
int index;
for(int i = 0; i < m; i++){
scanf("%d",&index);
nations[i] = all_nations[index];
}
bool (*cmp[4])(nation one, nation another) = {cmp1,cmp2,cmp3,cmp4};
for(int i = 0; i < 4; i++){
sort(nations, nations + m, cmp[i]);
update(i + 1, cmp[i]);
}
sort(nations, nations + m, cmp5);

for(int i = 0; i < m; i++){
printf("%d:%d\n", nations[i].nation_rank.rank, nations[i].nation_rank.way);
}
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  考研机试题集