http://ac.jobdu.com/problem.php?pid=1077
题目难,按着题目的信息,条理清晰,切记浮躁。没想好方法,不要乱写。
// 浙大10排序
// 九度:1007
// puts():输出一个字符串,并输出回车,
// PE:puts("\n");空两行,应使用puts("");
//
//
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
#define SIZE 500 //世界上的国家数不会超过500
struct Node
{
int id;
int gold, medal, people;
double goldrate, totalrate;
};
Node a[SIZE];
Node w[SIZE];//待排国家信息
int b[SIZE];//待排名国家id
int p1[SIZE];//记录第一种排名下,i的名次
int p2[SIZE];
int p3[SIZE];
int p4[SIZE];
int n, m;
void Init()
{
memset(a, 0, sizeof(a));
memset(w, 0, sizeof(w));
}
void Input()
{
for(int i=0; i<n; i++)
{
scanf("%d%d%d", &a[i].gold, &a[i].medal, &a[i].people);
a[i].id=i;
a[i].goldrate = (double)a[i].gold / a[i].people;
a[i].totalrate = (double)a[i].medal / a[i].people;
}
for(int j=0; j<m; j++)
{
scanf("%d", &b[j]);
}
}
void Select()
{
for(int i=0; i<m; i++)
{
int id = b[i];
w[i].id = id;
w[i].gold = a[id].gold;
w[i].goldrate = a[id].goldrate;
w[i].medal = a[id].medal;
w[i].people = a[id].people;
w[i].totalrate = a[id].totalrate;
}
}
bool cmp1(Node x, Node y)
{
return x.gold > y.gold;
}
bool cmp2(Node x, Node y)
{
return x.medal > y.medal;
}
bool cmp3(Node x, Node y)
{
return x.goldrate > y.goldrate;
}
bool cmp4(Node x, Node y)
{
return x.totalrate > y.totalrate;
}
void Pai()
{
int i;
sort(w, w+m, cmp1);
for(i=0; i<m; i++)
{
if(i>0)
{
int id = w[i].id;
int preid = w[i-1].id;
if(w[i].gold == w[i-1].gold)
{
p1[id] = p1[preid];//与前面的排名相同
}
else
{
p1[id] = i+1;
}
}
else
{
int id = w[i].id;
p1[id] = i+1;
}//第一个
}
sort(w, w+m, cmp2);
for(i=0; i<m; i++)
{
if(i>0)
{
int id = w[i].id;
int preid = w[i-1].id;
if(w[i].medal == w[i-1].medal)
{
p2[id] = p2[preid];//与前面的排名相同
}
else
{
p2[id] = i+1;
}
}
else
{
int id = w[i].id;
p2[id] = i+1;
}//第一个
}
sort(w, w+m, cmp3);
for(i=0; i<m; i++)
{
if(i>0)
{
int id = w[i].id;
int preid = w[i-1].id;
if(w[i].goldrate == w[i-1].goldrate)
{
p3[id] = p3[preid];//与前面的排名相同
}
else
{
p3[id] = i+1;
}
}
else
{
int id = w[i].id;
p3[id] = i+1;
}//第一个
}
sort(w, w+m, cmp4);
for(i=0; i<m; i++)
{
if(i>0)
{
int id = w[i].id;
int preid = w[i-1].id;
if(w[i].totalrate == w[i-1].totalrate)
{
p4[id] = p4[preid];//与前面的排名相同
}
else
{
p4[id] = i+1;
}
}
else
{
int id = w[i].id;
p4[id] = i+1;
}//第一个
}
}
void Output()
{
int i;
for(i=0; i<m; i++)
{
int id = b[i];
int best = 1;
int final = p1[id];
if(p2[id] < final)
{
final = p2[id];
best = 2;
}
if(p3[id] < final)
{
final = p3[id];
best = 3;
}
if(p4[id] < final)
{
final = p4[id];
best = 4;
}
printf("%d:%d\n", final, best);
}
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("E:\\in.txt", "r", stdin);
#endif
while(scanf("%d%d", &n, &m) != EOF)
{
Init();
Input();
Select();
Pai();
Output();
puts("");// 每一个测试例之间有一个空行
// puts("\n"), 就变成2个空行了
}
return 0;
}