日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Weights and Measures(贪心+动态规划)

發布時間:2023/12/15 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.
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
題意:有幾只烏龜,每只烏龜有一定的重量與力量。每只烏龜可以背小于它力量的重量(包括它自身的重量)。問最多一共可以有多少只烏龜疊在一起。

可以證明,將力量大的烏龜放在下面可以得到一個更優的狀態。因此在dp之前應先將所有烏龜按力量大小排好序,力量小的在前面。
首先,我們不妨證明一下這個命題,如果一個力量小的烏龜可以馱著一個力量大的烏龜,那么這個力量大的烏龜也必然可以馱起這個力量小的烏龜,而且還能夠使兩個烏龜上方增加承重能力。

我們不妨設力量小的烏龜的重量和力量分別為w1、s1,而力量大的烏龜為w2、s2,由于烏龜1可以馱起烏龜2,那么有s1>=w1+w2,如果我們假設烏龜2馱不起烏龜1,那么就應該有s2<w1+w2,然而我們知道烏龜2的力量更大,所以應該有s2>s1>=w1+w2,這樣就產生矛盾了,原命題得證。接著,如果烏龜1在烏龜2的下面,兩龜上方的承重能力至多為s1-(w1+w2)。然而如果換成烏龜2在烏龜1的下面的話,對于烏龜1來講是無所謂的,因為之前馱得動,現在少了烏龜2肯定也馱得動,因此僅從烏龜1的承重限制來講,兩龜上方的承重能力增加了。當然僅憑烏龜1的承重限制的角度來看是不全面的,我們還要考慮烏龜2,對于烏龜2來講,兩龜承重能力是s2-(w1+w2),而前面也說到了,烏龜1在下的時候承重能力至多為s1-(w1+w2),而s2-(w1+w2)>s1-(w1+w2),因此從烏龜2的角度來講,盡管上面多了個烏龜1,但就烏龜1和烏龜2作為整體而言,他們上方的承重能力也一定增加了。因此,無論兩龜整體的承重能力取決于哪只龜,調換之后最終的整體承重能力一定增加了。于是這樣我們就可以先把烏龜按力量排序了,剩下的問題就是怎么求這個最長非降子序列了。

1.用dp[i][j]表示從前 i 只烏龜中選出 j 只可以得打的最小總重量。

轉移方程為:如果dp[i-1][j-1]+t[i].w <=t[i].s,dp[i][j] = min( dp[i-1][j-1]+t[i].w, dp[i-1][j] ); 如果不等,則dp[i][j] = dp[i-1][j];

代碼如下:

#include <iostream> #include <cstdio> #include <string.h> #include <cmath> #include <algorithm> using namespace std;int dp[5650][5650]; typedef struct {int w, s; }tur; tur t[5650]; int n; bool comp( tur a, tur b) {if( a.s <b.s||(a.s==b.s&& a.w< b.w))return true;return false; } int main() {n=0;while( scanf("%d%d",&t[n].w,&t[n].s )!=EOF )n++;sort( t, t+n, comp);int i,j;memset( dp, 0x7f, sizeof( dp));for( i=0; i<=n; i++)dp[i][0] =0;for( i=1; i<=n; i++)for( j=1;j<=i; j++){if(dp[i-1][j] <dp[i][j])dp[i][j] =dp[i-1][j];if( dp[i-1][j-1]+t[i-1].w <=t[i-1].s &&dp[i-1][j-1]+t[i-1].w<dp[i][j] )dp[i][j] =dp[i-1][j-1]+t[i-1].w;}for( i=n; i>=0; i--)if( dp[n][i]<(1<<30))break;printf("%d\n",i);return 0; }

努力加油a啊,(o)/~

總結

以上是生活随笔為你收集整理的Weights and Measures(贪心+动态规划)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。