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

和武大上次网络赛一道题很像。假设已经知道要选哪几个乌龟,那么根据贪心可以推出按照 c从小到大排序,是最有可能使这个方案合法的。于是先按c排序,然后考虑dp。如果dp[i][j]表示前i个龟,总重量为j时,最长是多长,由于重量可能很大,所以dp会超时。但是,换一种思路,用dp[i][j]表示从前i个龟里选j个的最小总重量,就可以搞定了!(类似的经典模型像最长上升子序列也可以有两种dp方式)
这种选或者不选的模型,通常都有一维是表示从前i个中选,然后转移时考虑第i个选或者不选。然后如果另外只有一维元素,那么这一维通常就是要求的答案,那么一维dp就可以解决了。如果另外有2两维,就像这道题,就可以有两种考虑,选择其中的任意一维表示状态,可以根据那一维的数据范围大小去决定哪个更合适。

#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <iostream>
#include <stack>
#include <set>
#include <cstring>
#include <stdlib.h>
#include <cmath>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 1000000000;
const int maxn = 10000 + 5;

struct Node{
    int first, second;
    Node(int f=0, int s=0){
        this -> first = f;
        this -> second = s;
    }
}t[maxn];

bool cmp(Node a, Node b){
    return a.first+a.second < b.first+b.second;
}

int dp[maxn][maxn];

int main(){
    int w, c;
    int n = 0;
    while(scanf("%d%d", &w, &c) != EOF){
        t[n++] = Node(w, c-w);
    }
    sort(t, t+n, cmp);
    for(int i = 0;i < n;i++){
        fill(dp[i], dp[i]+maxn, INF);
        dp[i][0] = 0;
    }
    dp[0][1] = t[0].first;
    for(int i = 1;i < n;i++){
        for(int j = 1;j <= n;j++){
            dp[i][j] = dp[i-1][j];
            if(t[i].second >= dp[i-1][j-1]){
                dp[i][j] = min(dp[i][j], dp[i-1][j-1]+t[i].first);
            }
        }
    }
    int ans = 0;
    for(int i = n;i >= 0;i--){
        if(dp[n-1][i] != INF){
            ans = i;
            break;
        }
    }
    printf("%d\n", ans);
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值