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

对ai排序,再多重背包

Posted by meJustPlay at 2012-05-01 01:53:22 on Problem 2392
/** 
 * POJ: 2392 Space Elevator
 *
 * DP;
 */

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

/** ***/
#define CNT	400	// K  <= 400
#define HGT	40000	// Ai <= 40000

#define SUBMIT	1

/** ***/
typedef struct {
	int h;	// hi
	int a;	// ai
	int c;	// ci
} blk_t;
blk_t blk[CNT + 1];

int dp[HGT + 1];	// dp[i][j]: 考察前i种Block, 高度j是否能被达到

/** ***/
static inline int max(int a, int b)
{
	return (a > b ? a : b);
}

/** ***/
int compar(const void *p1, const void *p2)
{
	return ((blk_t *)p1)->a - ((blk_t *)p2)->a;
}

/** ***/
int main(int argc, char **argv)
{
	#ifndef SUBMIT
	freopen("input/2392", "r", stdin);
	#endif

	int cnt, hgt;
	int kid, hi, ai, ci;

	while (1 == scanf("%d", &kid)) {
		/* init */
		cnt = hgt = 0;
		memset(dp, 0, sizeof(dp));

		/* input */
		for (int i = 0; i < kid; ++i) {
			scanf("%d%d%d", &hi, &ai, &ci);
			if (hi > ai) {
				continue;
			}
			blk[++cnt].h = hi;
			blk[cnt].a   = ai;
			blk[cnt].c   = ci;
		}

		/* sort */
		qsort(blk + 1, cnt, sizeof(blk_t), compar);

		/* dp */
		dp[0] = 1;	// 高度0, 定能达到
		for (int i = 1; i <= cnt; ++i) {
			for (int j = hgt; j >= 0; --j) { // 逆序
				// 如若顺序, 某些hgt可能因为Bi而可达到
				// 从而导致dp[i - 1][j] 被dp[i][j] 覆盖
				if (!dp[j]) {
					continue;
				}
				for (int k = 1, tmp; k <= blk[i].c; ++k) {
					tmp = j + k * blk[i].h;
					if (tmp > blk[i].a) {
						break;
					}
					dp[tmp] = 1;
					hgt = max(hgt, tmp);
				} 
			}
		}

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

	return 0;
}

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