Uva - 1612 - Guess

来源:互联网 发布:如何打开端口8081 编辑:程序博客网 时间:2024/04/30 13:23

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 

Input contains several cases. For each case, the first line is an integer n, ( n $ \leq$ 16384) to indicate the number of players, followed by n lines, the ith of which contains three real numbers abc ( $ \leq$ abc< 1000ab 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.

Output 

For each case, if the rank is possible, output the highest possible total score for the player with the lowest rank (calculate to 2 decimal places), otherwise output ``No solution" (quotes for clarity).

Sample Input 

3100 200 300100 200 300100 200 3001 2 33100 200 300100 200 300100 200 3003 2 10

Sample Output 

Case 1: 600.00Case 2: 400.00


Sample Explanation:


Case 1:


RankPlayer ID NumberProblem 1's ScoreProblem 2's ScoreProblem 3's Score111002003002210020030033100200300


Case 2:


RankPlayer ID NumberProblem 1's ScoreProblem 2's ScoreProblem 3's Score13100200300220 (wrong)200300311000 (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;}


0 0