Weights and Measures+uva+一般dp

来源:互联网 发布:linux不显示电池 编辑:程序博客网 时间:2024/04/29 19:05

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 Output

3
解决方案:想的时候竟然没想到当乌龟的重量会叠加,下面的乌龟就举不起来了。
还是看了别人的代码才把思路理清的,从只有一个乌龟开始,不断往上推,同时对叠加起来的乌龟的总重量进行更新,越轻越好。dp[j+1]=min{dp[j]+w[i]}&&(g[i]>dp[j]+w[i])边界条件:dp[1]=0;
code:
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;struct node{    int w,g;} N[10000];bool cmp(node a,node b){    return a.g<b.g;}int dp[10000];int main(){    int len=1;    while(~scanf("%d%d",&N[len].w,&N[len].g))    {        len++;    }    sort(N+1,N+len+1,cmp);    memset(dp,0x3f,sizeof(dp));    dp[1]=0;    int k=0;    for(int i=1; i<len; i++)    {        for(int j=i-1; j>=1; j--)        {            if(dp[j]+N[i].w<N[i].g&&dp[j]+N[i].w<dp[j+1]){                dp[j+1]=dp[j]+N[i].w;                if(j>k) k=j;            }        }    }    printf("%d\n",k);    return 0;}

0 0
原创粉丝点击