A competition was just over. It had 3 problems and n players. Each player had an ID number from 1 to n. The final rank was decided by the total score of the 3 problems. The higher the total score was, the higher a player ranked (the smaller the rank number). If two players got the same total score, the one with the smaller ID number got a higher rank. We‘ve known for each problem, how much score each player might get if he din‘t solve totally wrong (if solved totally wrong, the player got zero in the problem). However, we don‘t know whether a player did get score in a problem. For a predicted final rank, you need to judge if the rank is possible.
Input contains several cases. For each case, the first line is an integer n, ( n 16384) to indicate the number of players, followed by n lines, the ith of which contains three real numbers a, b, c ( 0 a, b, c< 1000. a, b and c have 2 decimal places at most.) to respectively indicate the score of each problem Playeri might get if he didn‘t solve totally wrong. Another line containing n integers follows to indicate the player ID number in the order from rank 1st to rank nth.
The last case is followed by a line containing only a zero.
3 100 200 300 100 200 300 100 200 300 1 2 3 3 100 200 300 100 200 300 100 200 300 3 2 1 0
Case 1: 600.00 Case 2: 400.00
Sample Explanation:
Case 1:
Rank | Player ID Number | Problem 1‘s Score | Problem 2‘s Score | Problem 3‘s Score |
1 | 1 | 100 | 200 | 300 |
2 | 2 | 100 | 200 | 300 |
3 | 3 | 100 | 200 | 300 |
Case 2:
Rank | Player ID Number | Problem 1‘s Score | Problem 2‘s Score | Problem 3‘s Score |
1 | 3 | 100 | 200 | 300 |
2 | 2 | 0 (wrong) | 200 | 300 |
3 | 1 | 100 | 0 (wrong) | 300 |
刚开始没理解题意,后来找了别人的题解才懂。
因为一共只有三道题,所以每个人的得分最多有8种可能性。把这8种可能性都算出来,存在数组里,排好序备用
排名就是一个天然的链表,给出了扫描的顺序
扫描时,维护两个变量:前一个player的最大得分 recd 和他的ID recdID
扫描到每个player时,从大到小遍历他的8种得分,如果有等于recd的得分,且这个player的ID大于recdID,则只需更新recdID。否则遇到第一个小于recd的得分,就更新recd和recdID。
如果在遍历完8种得分后,还没有满足上面两种情况的,则说明无解
最后只需打印recd的值即可。要注意float型变量相等的判断方法。
在UVa Board中,Brian Fry给了一个很好的提示:尝试不用float。一看得分的范围:10^3,两位小数,则可以果断的乘100得到一个范围为10^5的int。这样出错的机会又少了很多。最后除以100.0输出即可
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include <cstring> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <algorithm> #include <stack> #include <queue> #include <bitset> #include <cassert> #include <cmath> #include <functional> using namespace std; const int maxn = 20000; int n; int rankOfPlayer[maxn]; void calcScores(vector<int>& v, int* a, int d, int maxd, int sum) { // 得到所有8种可能 if (d == maxd) v.push_back(sum); else { calcScores(v, a, d + 1, maxd, sum + a[d]); calcScores(v, a, d + 1, maxd, sum); } } struct Player{ vector<int> scores; int ID; Player(){} Player(int* a, int id) :ID(id){ calcScores(scores, a, 0, 3, 0); sort(scores.begin(), scores.end()); } }; int main() { int kase = 1; while (scanf("%d", &n) && n) { memset(rankOfPlayer, -1, sizeof(rankOfPlayer)); Player q[maxn]; float data_fl[3]; int data_int[3]; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { scanf("%f", &data_fl[j]); data_int[j] = (int)(round((data_fl[j] * 100.0))); } Player& p = q[i]; p = Player(data_int, i); } for (int i = 0; i < n; i++) { scanf("%d", &rankOfPlayer[i]); } int recd = 1000000; int recdID = -1; for (int i = 0; i < n; i++) { Player& u = q[rankOfPlayer[i] - 1]; bool ok = false; for (int j = 7; j >= 0; j--) { // 从大到小 if (u.scores[j] == recd && u.ID > recdID) { // 小分数ID比较大 recdID = u.ID; ok = true; break; } else if (u.scores[j] < recd) { // 最小的分数 recd = u.scores[j]; recdID = u.ID; ok = true; break; } } if (!ok) { recd = -1; break; } } if (recd == -1) { printf("Case %d: No solution\n", kase++); } else { printf("Case %d: %.2f\n", kase++, recd / 100.0); } } return 0; }
原文地址:http://blog.csdn.net/zyq522376829/article/details/46623171