记录编号 151792 评测结果 AAAAAAAAAA
题目名称 [SHOI 2008] 循环的债务 最终得分 100
用户昵称 GravatarAsm.Def 是否通过 通过
代码语言 C++ 运行时间 0.326 s
提交时间 2015-03-10 22:01:43 内存使用 7.95 MiB
显示代码纯文本
/***********************************************************************/
/**********************By Asm.Def-Wu Jiaxin*****************************/
/***********************************************************************/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <algorithm>
using namespace std;
template<class T>inline void getd(T &x){
	char ch = getchar();bool neg = false;
	while(!isdigit(ch) && ch != '-')ch = getchar();
	if(ch == '-')ch = getchar(), neg = true;
	x = ch - '0';
	while(isdigit(ch = getchar()))x = x * 10 - '0' + ch;
	if(neg)x = -x;
}
/***********************************************************************/
const int maxn = 1002, INF = 0x3f3f3f3f, val[6] = {1, 5, 10, 20, 50, 100};

int cnt[3][6], tot[6], Cur[3], Tar[2], Sum, f[2][maxn][maxn];

inline void init(){
	int a, b, c;
	getd(a), getd(b), getd(c);
	for(int i = 0;i < 3;++i)for(int j = 5;j >= 0;--j){
		getd(cnt[i][j]);
		Cur[i] += cnt[i][j] * val[j];
		tot[j] += cnt[i][j];
	}
	Sum = Cur[0] + Cur[1] + Cur[2];
	Tar[0] = Cur[0] - a + c;
	Tar[1] = Cur[1] - b + a;

	if(Tar[0] < 0 || Tar[1] < 0 || Sum - Tar[0] - Tar[1] < 0){
		printf("impossible\n");
		exit(0);
	}

	//printf("Sum = %d\n", Sum);
}

#define UPD(a, b) (a = min(a, b) )
#include <cmath>

inline void work(){
	const int rang = Sum + 1;
	int i, j, k, a, b, t, tmp, da, db, cnta, cntb;
	bool cur, las;
	for(i = 0;i <= Sum;++i)memset(f[1][i], 0x3f, sizeof(int) * rang);
	f[1][Cur[0]][Cur[1]] = 0;
	for(i = 0;i < 6;++i){
		cur = i & 1, las = cur ^ 1;
		for(j = 0;j <= Sum;++j)memset(f[cur][j], 0x3f, sizeof(int) * rang);
		for(j = 0;j <= Sum;++j){
			t = Sum - j;
			for(k = 0;k <= t;++k){
				if(f[las][j][k] == INF)continue;
				UPD(f[cur][j][k], f[las][j][k]);
				for(a = 0;a <= tot[i];++a){
					tmp = tot[i] - a;
					for(b = 0;b <= tmp;++b){
						da = a - cnt[0][i], db = b - cnt[1][i];
						cnta = j + da * val[i], cntb = k + db * val[i];
						if(cnta < 0 || cntb < 0 || cnta + cntb > Sum)continue;
						UPD(f[cur][cnta][cntb], f[las][j][k] + (abs(da) + abs(db) + abs(da + db)) / 2);
					}
				}
			}
		}
	}
	if(f[cur][Tar[0]][Tar[1]] == INF)printf("impossible\n");
	else printf("%d\n", f[cur][Tar[0]][Tar[1]]);
}

int main(){

#ifdef DEBUG
	freopen("test.txt", "r", stdin);
#elif not defined ONLINE_JUDGE
	freopen("bzoj_1021.in", "r", stdin);
	freopen("bzoj_1021.out", "w", stdout);
#endif
	init();
	work();

#ifdef DEBUG
	printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}