# 【BZOJ 1021】[SHOI2008]Debt 循环的债务

【题目链接】:http://www.lydsy.com/JudgeOnline/problem.php?id=1021

【题意】

【题解】

(这里我们可以一开始通过a,b,c处理出最后第一个人该有多少钱、第二个人该有多少钱…)
DP的依据就是;

(同一种钱币)

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const int val[7] = { 0,1,5,10,20,50,100 };
const double pi = acos(-1.0);
const int N = 1e3+100;
const int INF = 0x3f3f3f3f;

int total[4], cnt[4][7],tot[7],sum;
int f[2][N][N],a,b,c,target[3],pre,now;

#define UPD(x,y) (x = min(x,y))

int main()
{
//freopen("F:\\rush.txt", "r", stdin);
rei(a), rei(b), rei(c);
rep1(i, 1, 3)
rep2(j, 6, 1)
{
rei(cnt[i][j]);
total[i] += val[j] * cnt[i][j];
tot[j] += cnt[i][j];
sum += val[j] * cnt[i][j];
}
target[1] = total[1] - a + c;
target[2] = total[2] - b + a;
if (target[1] < 0 || target[2] < 0 || sum - target[1] - target[2] < 0)
{
puts("impossible");
return 0;
}

pre = now = 0;
memset(f[pre], INF, sizeof f[pre]);
f[pre][total[1]][total[2]] = 0;
rep1(i, 1, 6)
{
pre = now;
now = now ^ 1;
memset(f[now], INF, sizeof f[now]);
rep1(j, 0, sum)
{
int t = sum - j;
rep1(k, 0, t)
{
if (f[pre][j][k] == INF) continue;
UPD(f[now][j][k], f[pre][j][k]);
//assert f[pre][j][k]!=INF
rep1(q, 0, tot[i])
{
int r = tot[i] - q;
rep1(w, 0, r)
{
int da = q - cnt[1][i], db = w - cnt[2][i];
int cnta = j + da*val[i], cntb = k + db*val[i];
if (cnta < 0 || cntb < 0 || sum - cnta - cntb < 0) continue;
UPD(f[now][cnta][cntb], f[pre][j][k] + (abs(da) + abs(db) + abs(da + db)) / 2);
}
}
}
}
}
if (f[now][target[1]][target[2]] == INF) return puts("impossible"), 0;
printf("%d\n", f[now][target[1]][target[2]]);
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}

posted @ 2017-10-04 18:45  AWCXV  阅读(107)  评论(0编辑  收藏  举报