UVA10154 Weights and Measures0-1背包

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10154 Weights and Measures0-1背包相关的知识,希望对你有一定的参考价值。

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.
    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.
Input
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.
Output
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

问题链接UVA10154 Weights and Measures
问题简述:给定若干乌龟的体重和负重,问最多有多少只乌龟可以堆叠起来?
问题分析:0-1背包问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10154 Weights and Measures */

#include <bits/stdc++.h>

using namespace std;

const int INF = 0x3F3F3F3F;
const int N = 5607 + 1;
struct Turtles {
    int wt, st;
} t[N];
int dp[N];

bool cmp(Turtles a, Turtles b)
{
    return a.st > b.st;
}

int main()
{
    int n = 1;
    while (~scanf("%d%d", &t[n].wt, &t[n].st)) n++;
    n--;

    sort(t + 1, t + 1 + n, cmp);

    memset(dp, -1, sizeof dp);
    dp[0] = INF;
    for (int i = 1; i <= n; i++) {
        for (int j = n; j >= 1; j--) {
            if (dp[j - 1] != -1 && dp[j - 1] >= t[i].wt) {
                int tmp = min(dp[j - 1] - t[i].wt, t[i].st - t[i].wt);
                dp[j] = max(dp[j], tmp);
            }
        }
    }

    int ans = 0;
    for (int i = 1; i <= n; i++)
        if(dp[i] != -1) ans = i;

    printf("%d\\n", ans);

    return 0;
}

以上是关于UVA10154 Weights and Measures0-1背包的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 339CXenia and Weights

atcoder NIKKEI Programming Contest 2019 E - Weights on Vertices and Edges

Loj10154 选课

论文翻译:BinaryNet: Training Deep Neural Networks with Weights and Activations Constrained to +1 or ?1

wandb(w&b)(weights and biases): 深度学习轻量级可视化工具入门教程

UVAlive 10154:Dire Wolf 区间DP