貌似去年暑假就听过这道题。。。那时候还YY了个什么平面三条轴,夹角Π/3之类的。。。
正解嘛。。。当然是DP
令f[i][j][k]表示到了第i种面值,第一个人还有j元钱,第二个人还有k元钱的最少交换张数。
于是就是个背包问题的说,然后因为dp方程太复杂了,请参考程序吧。。。
(还有个非常厉害的剪枝我的程序没加,因为太懒了≥v≤。。。。。。誒!不要打我啊~~~都过了嘛好不好QAQ)
1 /************************************************************** 2 Problem: 1021 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:516 ms 7 Memory:8696 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 #include <cstring> 12 #include <algorithm> 13 14 using namespace std; 15 const int n = 6; 16 const int M = 1005; 17 const int val[n] = {1, 5, 10, 20, 50, 100}; 18 int x1, x2, x3; 19 int now, tot; 20 int i, j, k, a, b, suma, sumb, dis; 21 int sum[3], Cnt[n]; 22 int d[2][M][M], cnt[3][n]; 23 24 inline void update(int &x, int y){ 25 if (x == -1) x = y; 26 else x = min(x, y); 27 } 28 29 inline int calc(){ 30 return (abs(a - cnt[0][i]) + abs(b - cnt[1][i]) + abs(Cnt[i] - a - b - cnt[2][i])) / 2; 31 } 32 33 int main(){ 34 scanf("%d%d%d", &x1, &x2, &x3); 35 for (i = 0; i < 3; ++i){ 36 sum[i] = 0; 37 for (j = n - 1; j >= 0; --j){ 38 scanf("%d", cnt[i] + j); 39 Cnt[j] += cnt[i][j]; 40 sum[i] += cnt[i][j] * val[j]; 41 } 42 tot += sum[i]; 43 } 44 45 memset(d[0], -1, sizeof(d[0])); 46 d[0][sum[0]][sum[1]] = 0; 47 for (i = 0; i < n; ++i){ 48 now = i & 1; 49 memset(d[now ^ 1], -1, sizeof(d[now ^ 1])); 50 for (j = 0; j <= tot; ++j){ 51 for (k = 0; j + k <= tot; ++k){ 52 if (d[now][j][k] >= 0){ 53 update(d[now ^ 1][j][k], d[now][j][k]); 54 for (a = 0; a <= Cnt[i]; ++a){ 55 for (b = 0; a + b <= Cnt[i]; ++b){ 56 suma = j + val[i] * (a - cnt[0][i]); 57 sumb = k + val[i] * (b - cnt[1][i]); 58 if (suma >= 0 && sumb >= 0 && suma + sumb <= tot){ 59 dis = calc(); 60 update(d[now ^ 1][suma][sumb], d[now][j][k] + dis); 61 } 62 } 63 } 64 } 65 } 66 } 67 } 68 int ea = sum[0], eb = sum[1], ec = sum[2]; 69 ea += x3 - x1, eb += x1 - x2, ec += x2 - x3; 70 if (ea < 0 || eb < 0 || ec < 0 || ea + eb + ec != tot || d[n & 1][ea][eb] < 0) 71 printf("impossible\n"); 72 else printf("%d\n", d[n & 1][ea][eb]); 73 return 0; 74 }
时间: 2024-10-13 09:56:10