您的位置:首页 > 其它

uva 10154 Weights and Measures(dp)

2013-10-21 18:52 393 查看

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 Output

3

题意:给定一些乌龟,每个乌龟都有重量和可承受的重要,要求出最多能叠几层乌龟,

思路:,dp,从上往下放,承重越大放越下面,所以先按承重排序,然后进行dp,i,j表示前i个乌龟取j个。如果能放,dp[i][j] = min{dp[i - 1][j], dp[i - 1][j - 1] + w[i]};如果为INTMAX则表示不能叠出。其实说白了,就是看第i个乌龟要不要放,

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAXN = 5666;
struct G {
int w, p;
} t[MAXN];

int dp[MAXN][MAXN];

int cmp(G a, G b) {
return a.p < b.p;
}
int main() {
int n = 1;
while (~scanf("%d%d", &t
.w, &t
.p)) {
dp
[0] = 0;
n ++;
}
n --;
for (int i = 0; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
dp[i][j] = 999999999;
}
}
sort(t + 1, t + n + 1, cmp);
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= i; j ++) {
dp[i][j] = dp[i - 1][j];
if (dp[i - 1][j - 1] + t[i].w <= t[i].p && dp[i - 1][j - 1] != 999999999)
dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + t[i].w);
}
int ans = n;
for (;ans >= 1; ans --)
if (dp
[ans] != 999999999)
break;
printf("%d\n", ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: