首页 > 代码库 > uva10154

uva10154

C - Weights and Measures
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit Status Practice UVA 10154
Appoint description: 

Description

Download as PDF
 

Problem F: Weights and Measures

I know, up on top you are seeing great sights, 
But down at the bottom, we, too, should have rights. 
We turtles can‘t stand it. Our shells will all crack! 
Besides, we need food. We are starving!" groaned Mack.

The Problem

Mack, in an effort to avoid being cracked, has enlisted your advice as to the order in which turtles should be dispatched to form Yertle‘s throne. Each of the five thousand, six hundred and seven turtles ordered by Yertle has a different weight and strength. Your task is to build the largest stack of turtles possible.

Standard input consists of several lines, each containing a pair of integers separated by one or more space characters, specifying the weight and strength of a turtle. The weight of the turtle is in grams. The strength, also in grams, is the turtle‘s overall carrying capacity, including its own weight. That is, a turtle weighing 300g with a strength of 1000g could carry 700g of turtles on its back. There are at most 5,607 turtles.

Your output is a single integer indicating the maximum number of turtles that can be stacked without exceeding the strength of any one.

Sample Input

300 10001000 1200200 600100 101

Sample Outpu3


按能量排序,这样能保证最优,

状态转移方程:

dp[i][j]=dp[i-1][j];
dp[i][j]=min(dp[i][j],dp[i-1][j-1]+w[i]);

前i个,能组成j层的最小重量。
#include <iostream>#include <cstdlib>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int MAXN = 5670;const int INF = 0x3f3f3f3f;struct node{	int w, v;	bool operator < (const node a) const	{		return v < a.v;	}}A[MAXN];int n;int d[MAXN][MAXN];void init(){	memset(d, INF, sizeof(d));	for(int i = 0; i <= n; i++) d[i][0] = 0;}int dp(){	for(int i = 1; i <= n; i++)	{		for(int j = 1; j <= i; j++)		{			d[i][j] = d[i-1][j];			if(d[i-1][j-1] != INF && A[i].v >= d[i-1][j-1] + A[i].w)			{				d[i][j] = min(d[i][j], d[i-1][j-1]+A[i].w);			}		}	}	for(int i = n; i >= 1; i--) if(d[n][i] != INF) return i;}void read_case(){	n = 1;	while(~scanf("%d%d", &A[n].w, &A[n].v) && A[n].w) n++;	--n;	sort(A+1, A+1+n);}void solve(){	read_case();	init();	int ans = dp();	printf("%d\n", ans);}int main(){	solve();	system("pause");	return 0;}

  


uva10154