• UVa 10154 Weights and Measures dp 降维


    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
    -------------------------------------

    将乌龟以承重量从小到大排序

    f[i][j]表示前i个乌龟组成高度为j的塔时所用的最小重量。

    f[i][j]=min( f[i-1][j], f[i-1][j-1]+a[i].weight ) ( a[i].eight+f[i-1][j-1]<a[i].strength )(将第i个乌龟放到最下面)

    降一维变成f[j]=min( f[j], f[j-1]+a[i].weight )

    --------------------------------------

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    const int OO=1e9;
    
    struct STR{
        int wt;
        int ms;
    }a[11111];
    
    bool cmp(STR a,STR b)
    {
        return a.ms<b.ms;
    }
    
    int f[11111];
    int maxi;
    
    
    int main()
    {
        int n=0;
        while (~scanf("%d%d",&a[n].wt,&a[n].ms))
        {
            if (a[n].wt>a[n].ms) continue;
            n++;
        }
        sort(a,a+n,cmp);
        for (int i=1;i<=n;i++) f[i]=OO;
        f[0]=0;
        maxi=0;
        for (int i=0;i<n;i++)
        {
            for (int j=maxi;j>=0;j--)
            {
                if (f[j]+a[i].wt<a[i].ms&&f[j]+a[i].wt<f[j+1])
                {
                    f[j+1]=f[j]+a[i].wt;
                    maxi=max(maxi,j+1);
                }
            }
        }
        printf("%d\n",maxi);
        return 0;
    }
    





  • 相关阅读:
    51Nod1136--欧拉函数
    ubuntu裸机镜像问题
    汉诺塔问题
    lwm2m协议
    WPF自定义控件与样式(4)-CheckBox/RadioButton自定义样式
    图解大顶堆的构建、排序过程
    WindowsService开发简单入门
    数据结构和算法参考网址
    c#创建windows服务入门教程实例
    C#比较两个对象是否为同一个对象。 Visual Studio调试器指南---多线程应用程序调试(一)
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226391.html
Copyright © 2020-2023  润新知