码迷,mamicode.com
首页 > 其他好文 > 详细

uva10154

时间:2014-11-19 07:14:55      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   os   sp   for   

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

Description

bubuko.com,布布扣
 

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 1000
1000 1200
200 600
100 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

标签:des   style   blog   http   io   ar   os   sp   for   

原文地址:http://www.cnblogs.com/a972290869/p/4107069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!