2013 Asia Hangzhou Regional Contest I
Gems Fight!
Time Limit: 20000/10000 MS (Java/Others)
Memory Limit: 327680/327680 K (Java/Others) Total Submission(s): 555 Accepted Submission(s): 233
Problem Description
Alice and Bob are playing "Gems Fight!": There are Gems of G different colors , packed in B bags. Each bag has several Gems. G different colors are numbered from color 1 to color G. Alice and Bob take turns to pick one bag and collect all the Gems inside. A bag cannot be picked twice. The Gems collected are stored in a shared cooker. After a player ,we name it as X, put Gems into the cooker, if there are S Gems which are the same color in the cooker, they will be melted into one Magic Stone. This reaction will go on and more than one Magic Stone may be produced, until no S Gems of the same color remained in that cooker. Then X owns those new Magic Stones. When X gets one or more new Magic Stones, he/she will also get a bonus turn. If X gets Magic Stone in a bonus turn, he will get another bonus turn. In short,a player may get multiple bonus turns continuously. There will be B turns in total. The goal of "Gems Fight!" is to get as more Magic Stones than the opponent as possible. Now Alice gets the first turn, and she wants to know, if both of them act the optimal way, what will be the difference between the number of her Magic Stones and the number of Bob's Magic Stones at the end of the game.
Input
There are several cases(<=20). In each case, there are three integers at the first line: G, B, and S. Their meanings are mentioned above. Then B lines follow. Each line describes a bag in the following format: n c1 c2 ... cn It means that there are n Gems in the bag and their colors are color c1,color c2...and color cn respectively. 0<=B<=21, 0<=G<=8, 0<n<=10, S < 20. There may be extra blank lines between cases. You can get more information from the sample input. The input ends with G = 0, B = 0 and S = 0.
Output
One line for each case: the amount of Alice's Magic stones minus the amount of Bob's Magic Stones.
Sample Input
3 4 3
2 2 3
2 1 3
2 1 2
3 2 3 1
3 2 2
3 2 3 1
3 1 2 3
0 0 0
Sample Output
3
-3
Hint
For the first case, in turn 2, bob has to choose at least one bag, so that Alice will make a Magic Stone at the end of turn 3, thus get turn 4 and get all the three Magic Stones.Source
Recommend
1 #include <cstdio> 2 #include <iostream> 3 #include <vector> 4 #include <set> 5 #include <cstring> 6 #include <string> 7 #include <map> 8 #include <cmath> 9 #include <ctime> 10 #include <algorithm> 11 #include <queue> 12 13 using namespace std; 14 #define INF 0x7fffffff 15 #define maxm 1001 16 #define mp make_pair 17 #define pb push_back 18 #define rep(i,n) for(int i = 0; i < (n); i++) 19 #define re return 20 #define fi first 21 #define se second 22 #define sz(x) ((int) (x).size()) 23 #define all(x) (x).begin(), (x).end() 24 #define sqr(x) ((x) * (x)) 25 #define sqrt(x) sqrt(abs(x)) 26 #define y0 y3487465 27 #define y1 y8687969 28 #define fill(x,y) memset(x,y,sizeof(x)) 29 30 typedef vector<int> vi; 31 typedef long long ll; 32 typedef long double ld; 33 typedef double D; 34 typedef pair<int, int> ii; 35 typedef vector<ii> vii; 36 typedef vector<string> vs; 37 typedef vector<vi> vvi; 38 39 template<class T> T abs(T x) { re x > 0 ? x : -x; } 40 41 const int maxn = (1<<21); 42 43 int n, m, t, s, k, x; 44 //string s; 45 int dp[maxn]; 46 int res[maxn][9]; 47 int a[22][11]; 48 int sum[maxn]; 49 int v[maxn]; 50 int main(){ 51 v[0] = -1; 52 for (int i = 1; i < (1 << 21); i++)v[i] = v[i >> 1] + 1; 53 while (scanf("%d%d%d", &k, &n, &s) && (k + n + s)){ 54 memset(a, 0, sizeof a); 55 memset(res, 0, sizeof res); 56 for (int i = 0; i < n; i++){ 57 scanf("%d", &m); 58 for (int j = 0; j < m; j++){ 59 scanf("%d", &x); 60 a[i][x]++; 61 } 62 } 63 sum[0] = 0; 64 for (int i = 1; i < (1 << n); i++){ 65 int pre = i ^ (i&-i); 66 int cur = v[i&-i]; 67 sum[i] = sum[pre]; 68 for (int j = 1; j <= k; j++) 69 res[i][j] = res[pre][j] + a[cur][j]; 70 } 71 for (int i = 1; i < (1 << n); i++){ 72 sum[i] = 0; 73 for (int j = 1; j <= k; j++) 74 sum[i] = sum[i] + res[i][j] / s; 75 } 76 for (int i = 0; i < (1 << n); i++)dp[i] = -INF; 77 dp[(1 << n) - 1] = 0; 78 for (int i = (1 << n) - 1; i >= 0; i--){ 79 for (int j = i; j > 0; j -= j&-j){ 80 int pre = i ^ (j&-j); 81 int tp = sum[i] - sum[pre]; 82 if (tp>0)dp[pre] = max(dp[pre], dp[i] + tp); 83 else dp[pre] = max(dp[pre], -dp[i]); 84 } 85 } 86 printf("%d\n", dp[0]); 87 } 88 return 0; 89 }
浙公网安备 33010602011771号