Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贡献一个3000ms(g++)的代码

Posted by meJustPlay at 2012-04-27 18:46:02 on Problem 1742
/**
 * POJ: 1742 Coins
 *
 * 多重背包特例: 物品价值 = 物品体积(Wi = Vi)
 * 本题: 求dp[1...m] 中 dp[v] = v的个数
 */

/** ***/
#include <stdio.h>
#include <string.h>
#include <limits.h>

/** ***/
#define N	200 + 2
#define SIZE	100000 + 2

#define SUBMIT	1

/** ***/
#define max(a, b) ( (a) > (b) ? (a) : (b) )

/** ***/
int ans = 0;
int cnt = 0;
int ks[SIZE];
int dp[SIZE];	// dp

/** ***/
int main(void) 
{
	int cash = 0;
	int kind = 0;
	int in[N];	// input

	#ifndef SUBMIT
	freopen("input/1742",  "r", stdin);
	#endif

	while ((2 == scanf("%d%d", &kind, &cash)) && (kind | cash)) {
		/* init */
		ans = 0;
		cnt = 0;
		memset(dp, 0, sizeof(dp));

		/* input */
		for (int i = 0; i < (kind << 1); ++i) {
			scanf("%d", in + i);
		}

		/* convert */
		for (int i = kind, j = 0; i < (kind << 1); ++i, ++j) {
			for (int k = 1; k < in[i]; k <<= 1) {
				ks[++cnt] = k * in[j];
				in[i] -= k;
			}
			ks[++cnt] = in[i] * in[j];
		}
		
		/* dp */
		for (int i = 1; i <= cnt; ++i) {
			for (int v = cash; v >= ks[i]; --v) {
				if (dp[v] != v) { 
					int val = dp[v - ks[i]] + ks[i];
					if (val == v) {
						++ans;
						dp[v] = val;
					}

				}
			}
		}

		/* output */
		printf("%d\n", ans);
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator