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
#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;
}
本文介绍了一个经典的乌龟堆叠问题,旨在通过合理的排序和动态规划算法找到能够堆叠的最大乌龟数量。问题涉及五千六百零七只乌龟,每只乌龟有不同的重量和承重能力。
7万+

被折叠的 条评论
为什么被折叠?



