标签:style blog class code ext color
A cricket team consists of 11 players and some are good at batting, others are good at bowling and some of them are good at both batting and bowling. The batting coach wants to select exactly K players having maximum possible sum of scores. Given the batting score of each of the 11 players, find the number of ways in which we can select exactly K players such that the sum of their scores is the maximum possible. Two ways are different if there is a player who is selected in one of them is not in the other. See explanation of sample cases for more clarity.
First line contains T, number of test cases ( 1 ≤ T ≤ 100 ). T cases follow, each having 2 lines. First line of each case contains scores of 11 players ( 1 ≤ score ≤ 100 ) and the second line contains K (1 ≤ K ≤ 11)
For each test case, output the answer in a new line.
Input: 2 1 2 3 4 5 6 7 8 9 10 11 3 2 5 1 2 4 1 6 5 2 2 1 6 Output: 1 6
这里主要熟悉priority_queue的用法:
1 它没有clear用法,需要手动清空,别忘记循环中清零了,我老是犯这个错误
2 使用这些容器要注意判空,否则范围溢出,也是老毛病了
当然,其实本题有更加简洁的写法。
#pragma once #include <stdio.h> #include <queue> #include <vector> using std::priority_queue; using std::vector; class TopBatsmen { const static int TEAMS = 11; public: TopBatsmen() { struct cmpTopBatsmen { bool operator()(const int a, const int b) const { return a > b; } }; priority_queue<int,vector<int>, cmpTopBatsmen> pqi; int A[11] = {0}; int T = 0; scanf("%d", &T); while (T--) { while (pqi.size()) pqi.pop();//又是忘记清空 for (int i = 0; i < TEAMS; i++) { scanf("%d", &A[i]); } int k = 0; scanf("%d", &k); for (int i = 0; i < TEAMS; i++) { if (i < k) pqi.push(A[i]); else if (pqi.size() && A[i] > pqi.top()) { pqi.pop(); pqi.push(A[i]); } } int totalMin = 0, minIn = 1; for (int i = 0; i < TEAMS; i++) { if (A[i] == pqi.top()) totalMin++; } int t = -1; if ( pqi.size() ) { t = pqi.top(); pqi.pop(); } while (pqi.size() && pqi.top() == t)//逻辑没写好错误 { pqi.pop(); minIn++; } int sma = 1, lar = 1; for (int i = 1, j = totalMin; i <= minIn; i++, j--) { sma *= i, lar *= j; } int ans = lar / sma; printf("%d\n", ans); } } };
codechef Top Batsmen题解,布布扣,bubuko.com
标签:style blog class code ext color
原文地址:http://blog.csdn.net/kenden23/article/details/25117671