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 turtlesordered 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; }
相关文章推荐
- UVA11300——Spreading the Wealth (贪心+建模)
- android新建工程R报错或者R cannot be resolved to a variable
- Linux计划任务
- servlet单实例多线程问题
- Android_Json_GSON
- mysql 做增量备份
- Dragon Balls
- Describe the difference between repeater, bridge and router.
- 九九乘法表新打表(倒三角式)
- a标签,href和onclick比较
- reverb linux 下环境配置
- 《Cisco2801路由器》 用户配置手册
- cloudfoundry 中hbase_master启动不成功 vsphere搭建的ubuntu1004
- 撰写POPUSH需求文档
- sql plus登陆
- C#中close和dispose区别
- 关于 input 文本垂直对齐
- apache安装过程中的常见问题
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (四) —— ContentProvider
- apache安装过程中的常见问题