标签:des style blog http io ar os sp for
Description
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.
300 1000 1000 1200 200 600 100 101
按能量排序,这样能保证最优,
状态转移方程:
dp[i][j]=dp[i-1][j];
dp[i][j]=min(dp[i][j],dp[i-1][j-1]+w[i]);
前i个,能组成j层的最小重量。
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int MAXN = 5670; const int INF = 0x3f3f3f3f; struct node { int w, v; bool operator < (const node a) const { return v < a.v; } }A[MAXN]; int n; int d[MAXN][MAXN]; void init() { memset(d, INF, sizeof(d)); for(int i = 0; i <= n; i++) d[i][0] = 0; } int dp() { for(int i = 1; i <= n; i++) { for(int j = 1; j <= i; j++) { d[i][j] = d[i-1][j]; if(d[i-1][j-1] != INF && A[i].v >= d[i-1][j-1] + A[i].w) { d[i][j] = min(d[i][j], d[i-1][j-1]+A[i].w); } } } for(int i = n; i >= 1; i--) if(d[n][i] != INF) return i; } void read_case() { n = 1; while(~scanf("%d%d", &A[n].w, &A[n].v) && A[n].w) n++; --n; sort(A+1, A+1+n); } void solve() { read_case(); init(); int ans = dp(); printf("%d\n", ans); } int main() { solve(); system("pause"); return 0; }
标签:des style blog http io ar os sp for
原文地址:http://www.cnblogs.com/a972290869/p/4107069.html