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

按A[i]排序,然后dp第i块搭到j最多剩下数量,79ms

Posted by will7101 at 2015-12-18 21:16:29 on Problem 2392
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

struct blk{
	int h, a, c;
	bool operator<(blk bb) const{
		return a < bb.a;
	}
}A[405];

int N, ans;
int dp[40005];
//dp[i][j]:用前i种block搭到j高度时第i种剩下的最多块数,不存在为-1;
//dp[i][j] = { c[i];                ( dp[i-1][j] >= 0 )
//           { dp[i][j - h[i]] - 1; ( h[i] <= j <= a[i] && dp[i][j - h[i]] > 0 ) 
//           { -1;                  ( 其他 ) 
//(重复使用) 

void solve(){
	memset(dp, -1, sizeof(dp));
	dp[0] = 0;
	
	for(int i = 0; i < N; i++){
		for(int j = 0; j <= A[i].a; j++){
			if(dp[j] >= 0){
				dp[j] = A[i].c;
			}
			else if(A[i].h <= j && dp[j - A[i].h] > 0){
				dp[j] = dp[j - A[i].h] - 1;
			}
			else{
				dp[j] = -1;
			}
		}
	}
	
	for(int j = A[N-1].a; j >= 0; j--){
		if(dp[j] >= 0){
			ans = j;
			return;
		}
	}
}

int main(){
	scanf("%d", &N);
	for(int i = 0; i < N; i++){
		scanf("%d%d%d", &A[i].h, &A[i].a, &A[i].c);
	}
	sort(A, A + N);
	
	solve();
	printf("%d\n", ans);
	
	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