7972. Good Coalition
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB , Special Judge
Description
The Dutch political system is in turmoil. There have been six coalition governments in the past fourteen years, all of which have fallen before completing their term in office. Recently there have been elections (again), the outcome of which has been described as “impossible” by several political commentators. The only bright spot in this bleak situation is that they have appointed you as the “informateur”. As the informateur it is your task to find a suitable coalition.
Being the rational person you are, you have decided the first negotiation attempt should be started between the parties forming the most stable coalition. A coalition is formed by a set of parties having won a strict majority of seats in the election (i.e. at least 76 seats out of a total of 150). The most stable coalition is one that has the highest chance of completing its term in office. A coalition falls (and new elections must be held) if a single party leaves the coalition. The probability of a coalition completing their term is estimated by the product of the probabilities of each party in the coalition completing their term. This probability is in turn based on historical data.
Find the best coalition and save the Netherlands from becoming a banana republic!
Input
On the first line one positive number: the number of test cases, at most 100. After that per test case:
- one line with an integer n (1 <= n <= 150), the number of political parties that have won at least one seat in the election.
- n lines, each with two space-separated integers si and pi (1 <= si <= 150 and 1 <= pi <=100): the number of seats won by the i-th party and the probability (expressed as a percentage) that the i-th party will complete its term in office, respectively.
Note that there are exactly 150 seats divided among all parties (s1+s2+...+sn = 150).
Output
Per test case:
- one line with a floating point number: the probability (expressed as a percentage) of the most stable coalition sitting out their term in office.
This number should be accurate up to 10-6 relative or absolute precision.
Sample Input
1 4 35 80 25 70 60 60 30 90
Sample Output
54.0
Problem Source
2013年每周一赛第五场暨校赛模拟赛III/BAPC 2012
// Problem#: 7972
// Submission#: 3593656
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
// Solution to Good Coalition
// Author: Thomas Beuman
// Time complexity: O(150*n)
// Memory: O(150)
// @EXPECTED_RESULTS@: CORRECT
// Solution method: Dynamic Programming
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int S = 150; // # seats
int Seats[S], Prob[S];
double MaxProb[S+1];
int main()
{ int cases, casenr, n, p, s;
double m;
scanf("%d", &cases);
for (casenr = 1; casenr <= cases; casenr++)
{ // Read input
scanf("%d", &n);
for (p = 0; p < n; p++)
scanf("%d %d", &Seats[p], &Prob[p]);
memset(MaxProb, 0, sizeof(MaxProb));
MaxProb[0] = 1;
// DP over parties and seats
for (p = 0; p < n; p++)
for (s = S; s >= Seats[p]; s--)
// ^ Process in reverse to avoid using party p more than once
// See if using party p is an improvement
MaxProb[s] = max(MaxProb[s], Prob[p] / 100.0 * MaxProb[s-Seats[p]]);
// Look for best majority
m = 0;
for (s = S; s > S/2; s--)
m = max(m, MaxProb[s]);
printf("%.6lf\n", m * 100.0);
}
return 0;
}