您的位置:首页 > 其它

BZOJ1021 [SHOI2008]Debt 循环的债务

2014-10-24 16:52 246 查看
貌似去年暑假就听过这道题。。。那时候还YY了个什么平面三条轴,夹角Π/3之类的。。。

正解嘛。。。当然是DP

令f[i][j][k]表示到了第i种面值,第一个人还有j元钱,第二个人还有k元钱的最少交换张数。

于是就是个背包问题的说,然后因为dp方程太复杂了,请参考程序吧。。。

(还有个非常厉害的剪枝我的程序没加,因为太懒了≥v≤。。。。。。誒!不要打我啊~~~都过了嘛好不好QAQ)

/**************************************************************
Problem: 1021
User: rausen
Language: C++
Result: Accepted
Time:516 ms
Memory:8696 kb
****************************************************************/

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int n = 6;
const int M = 1005;
const int val
= {1, 5, 10, 20, 50, 100};
int x1, x2, x3;
int now, tot;
int i, j, k, a, b, suma, sumb, dis;
int sum[3], Cnt
;
int d[2][M][M], cnt[3]
;

inline void update(int &x, int y){
if (x == -1) x = y;
else x = min(x, y);
}

inline int calc(){
return (abs(a - cnt[0][i]) + abs(b - cnt[1][i]) + abs(Cnt[i] - a - b - cnt[2][i])) / 2;
}

int main(){
scanf("%d%d%d", &x1, &x2, &x3);
for (i = 0; i < 3; ++i){
sum[i] = 0;
for (j = n - 1; j >= 0; --j){
scanf("%d", cnt[i] + j);
Cnt[j] += cnt[i][j];
sum[i] += cnt[i][j] * val[j];
}
tot += sum[i];
}

memset(d[0], -1, sizeof(d[0]));
d[0][sum[0]][sum[1]] = 0;
for (i = 0; i < n; ++i){
now = i & 1;
memset(d[now ^ 1], -1, sizeof(d[now ^ 1]));
for (j = 0; j <= tot; ++j){
for (k = 0; j + k <= tot; ++k){
if (d[now][j][k] >= 0){
update(d[now ^ 1][j][k], d[now][j][k]);
for (a = 0; a <= Cnt[i]; ++a){
for (b = 0; a + b <= Cnt[i]; ++b){
suma = j + val[i] * (a - cnt[0][i]);
sumb = k + val[i] * (b - cnt[1][i]);
if (suma >= 0 && sumb >= 0 && suma + sumb <= tot){
dis = calc();
update(d[now ^ 1][suma][sumb], d[now][j][k] + dis);
}
}
}
}
}
}
}
int ea = sum[0], eb = sum[1], ec = sum[2];
ea += x3 - x1, eb += x1 - x2, ec += x2 - x3;
if (ea < 0 || eb < 0 || ec < 0 || ea + eb + ec != tot || d[n & 1][ea][eb] < 0)
printf("impossible\n");
else printf("%d\n", d[n & 1][ea][eb]);
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: