# 杭电ACM 3789 奥运排序问题

#include "stdio.h"#include "stdlib.h"
#include "string.h"

struct Medal
{
int number;
int gold_medal;
int all_medal;
int population;
float gold_pop;
float all_pop;
}value[1000],valueall[1000];

int cmp1(const void *value1, const void *value2)
{
struct Medal *p = (struct Medal *)value2;
struct Medal *q = (struct Medal *)value1;
return p ->gold_medal - q->gold_medal;
}
int cmp2(const void *value1, const void *value2)
{
struct Medal *p = (struct Medal *)value2;
struct Medal *q = (struct Medal *)value1;
return p ->all_medal - q->all_medal;
}
int cmp3(const void *value1, const void *value2)
{
struct Medal *p = (struct Medal *)value2;
struct Medal *q = (struct Medal *)value1;
return p->gold_pop > q->gold_pop ? 1 : -1;
}
int cmp4(const void *value1, const void *value2)
{
struct Medal *p = (struct Medal *)value2;
struct Medal *q = (struct Medal *)value1;
return p->all_pop > q->all_pop ? 1 : -1;
}

void swap(struct Medal *s,int i,int j)
{
int temp;
float temp1;
temp = value[i].number;
value[i].number = value[j].number;
value[j].number = temp;

temp = value[i].gold_medal;
value[i].gold_medal = value[j].gold_medal;
value[j].gold_medal = temp;

temp = value[i].all_medal;
value[i].all_medal = value[j].all_medal;
value[j].all_medal = temp;

temp = value[i].population;
value[i].population = value[j].population;
value[j].population = temp;

temp1 = value[i].all_pop;
value[i].all_pop = value[j].all_pop;
value[j].all_pop = temp1;

temp1 = value[i].gold_pop;
value[i].gold_pop = value[j].gold_pop;
value[j].gold_pop = temp1;
}

int main()
{
int country,sort_country,i,j;
int rank[1000][2],rank1[1000];//rank 要求排名的各国家总的排名情况，包括排名与排名方式。rank1 要求排名的各国家以当前排名方式所得的名次
int country_number[1000];
while(scanf("%d%d",&country,&sort_country) == 2)
{
for(i = 0; i < country; i ++)
{
valueall[i].number = i;
scanf("%d%d%d",&valueall[i].gold_medal,&valueall[i].all_medal,&valueall[i].population);
valueall[i].gold_pop = (float)valueall[i].gold_medal / (float)valueall[i].population;
valueall[i].all_pop = (float)valueall[i].all_medal / (float)valueall[i].population;
}
for(i = 0; i < sort_country; i ++) //把要求排名的国家放到一起
{
scanf("%d",&country_number[i]);
value[i] = valueall[country_number[i]];
}
qsort(value,sort_country,sizeof(struct Medal),cmp1);
rank1[value[0].number] = 1;
for(i = 1; i < sort_country; i ++) //确定以金牌数为准的国家排名，rank1的下标是国家的序号
{
if(value[i].gold_medal == value[i - 1].gold_medal)
{
rank1[value[i].number] = rank1[value[i - 1].number];
}
else
{
rank1[value[i].number] = i + 1;
}
}
for(j = 0; j < sort_country; j ++)  //这是第一次，直接赋值。
{
for(i = 0; i < sort_country; i ++)
{
if(value[i].number == country_number[j])
{
rank[j][0] = rank1[value[i].number];
rank[j][1] = 1;
}
}
}
qsort(value,sort_country,sizeof(struct Medal),cmp2);
rank1[value[0].number] = 1;
for(i = 1; i < sort_country; i ++)
{
if(value[i].all_medal == value[i - 1].all_medal)
{
rank1[value[i].number] = rank1[value[i - 1].number];
}
else
{
rank1[value[i].number] = i + 1;
}
}
for(j = 0; j < sort_country; j ++) //这是第二次，要与上一次的排名相比较，谁更优。
{
for(i = 0; i < sort_country; i ++)
{
if(value[i].number == country_number[j] && rank[j][0] > rank1[value[i].number] )
{
rank[j][0] = rank1[value[i].number];
rank[j][1] = 2;
}
}
}
qsort(value,sort_country,sizeof(struct Medal),cmp3);
rank1[value[0].number] = 1;
for(i = 1; i < sort_country; i ++)
{
if(value[i].gold_medal * value[i - 1].population == value[i - 1].gold_medal * value[i].population)
{
rank1[value[i].number] = rank1[value[i - 1].number];
}
else
{
rank1[value[i].number] = i + 1;
}
}
for(j = 0; j < sort_country; j ++)
{
for(i = 0; i < sort_country; i ++)
{
if(value[i].number == country_number[j] && rank[j][0] > rank1[value[i].number] )
{
rank[j][0] = rank1[value[i].number];
rank[j][1] = 3;
}
}
}
qsort(value,sort_country,sizeof(struct Medal),cmp4);
rank1[value[0].number] = 1;
for(i = 1; i < sort_country; i ++)
{
if(value[i].all_medal * value[i - 1].population == value[i - 1].all_medal * value[i].population)
{
rank1[value[i].number] = rank1[value[i - 1].number];
}
else
{
rank1[value[i].number] = i + 1;
}
}
for(j = 0; j < sort_country; j ++)
{
for(i = 0; i < sort_country; i ++)
{
if(value[i].number == country_number[j] && rank[j][0] > rank1[value[i].number] )
{
rank[j][0] = rank1[value[i].number];
rank[j][1] = 4;
}
}
}
for(i = 0; i < sort_country; i ++)
{
printf("%d:%d/n",rank[i][0],rank[i][1]);
}
printf("/n");
}
return 0;
}