hdu3789/九度OJ1007 奧運排序問題

題目描述:

按要求,給國家進行排名。

輸入:
有多組數據。
第一行給出國家數N,要求排名的國家數M,國家號從0到N-1。
第二行開始的N行給定國家或地區的奧運金牌數,獎牌數,人口數(百萬)。
接下來一行給出M個國家號。
輸出:
排序有4種方式: 金牌總數 獎牌總數 金牌人口比例 獎牌人口比例 
對每個國家給出最佳排名排名方式 和 最終排名
格式爲: 排名:排名方式
如果有相同的最終排名,則輸出排名方式最小的那種排名,對於排名方式,金牌總數 < 獎牌總數 < 金牌人口比例 < 獎牌人口比例 
如果有並列排名的情況,即如果出現金牌總數爲 100,90,90,80.則排名爲1,2,2,4.
每組數據後加一個空行。

中文題,題意理解起來沒什麼問題。但是!!這個題特別坑!我在WA了六發之後,堅信代碼沒有bug了,就重新回去讀題,終於發現trick:只需要對M個國家進行排名,而不是全部國家進行排名。

感覺題目出成這樣完全沒有意義嘛,在給的樣例中根本看不出這一點,題目就像是文字遊戲。。

要/求排名的/國家數M要求/排名的/國家數M

按照AC的代碼來看就是後者的斷句。。我純屬WA的太多想吐槽一下

#include <iostream>
#include <algorithm>

using namespace std;

const int size = 1005;

class Country{
public:
	double g, j, p;
	int id;
}c[size];

bool cmpg(Country x, Country y){
	return x.g > y.g;
}
bool cmpj(Country x, Country y){
	return x.j > y.j;
}
bool cmpgp(Country x, Country y){
	return x.g / x.p > y.g / y.p;
}
bool cmpjp(Country x, Country y){
	return x.j / x.p > y.j / y.p;
}

int main(){
	int n, m;
	int need[size];
	int rank[size][5];
	while (cin >> n >> m){
		for (int i = 0; i < n; i++){
			cin >> c[i].g >> c[i].j >> c[i].p;
			c[i].id = i;
		}
		for (int i = 0; i < m; i++){
			cin >> need[i];
		}

		sort(c, c + n, cmpg);
		rank[c[0].id][1] = 1;
		for (int i = 1; i < n; i++){
			if (c[i].g == c[i - 1].g)
				rank[c[i].id][1] = rank[c[i - 1].id][1];
			else
				rank[c[i].id][1] = i + 1;
		}

		sort(c, c + n, cmpj);
		rank[c[0].id][2] = 1;
		for (int i = 1; i < n; i++){
			if (c[i].j == c[i - 1].j)
				rank[c[i].id][2] = rank[c[i - 1].id][2];
			else
				rank[c[i].id][2] = i + 1;
		}

		sort(c, c + n, cmpgp);
		rank[c[0].id][3] = 1;
		for (int i = 1; i < n; i++){
			if (c[i].g / c[i].p == c[i - 1].g / c[i - 1].p)
				rank[c[i].id][3] = rank[c[i - 1].id][3];
			else
				rank[c[i].id][3] = i + 1;
		}

		sort(c, c + n, cmpjp);
		rank[c[0].id][4] = 1;
		for (int i = 1; i < n; i++){
			if (c[i].j / c[i].p == c[i - 1].j / c[i - 1].p)
				rank[c[i].id][4] = rank[c[i - 1].id][4];
			else
				rank[c[i].id][4] = i + 1;
		}

		for (int i = 0; i < m; i++){
			int min = n + 1, type = 5;
			for (int t = 1; t <= 4; t++){
				int tmp = 1;
				for (int j = 0; j < m; j++){
					if (i != j){
						if (rank[need[i]][t] > rank[need[j]][t]){
							tmp++;
						}
					}
				}
				if (min > tmp){
					min = tmp;
					type = t;
				}
			}
			cout << min << ":" << type << endl;
		}
		cout << endl;
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章