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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【UVA - 10154 】Weights and Measures (贪心排序,dp,类似0-1背包,状态设定思维)

發布時間:2023/12/10 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【UVA - 10154 】Weights and Measures (贪心排序,dp,类似0-1背包,状态设定思维) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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.

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

?

題目大意:

給出n只烏龜(n<=6000)的體重和可以承受的重量。?求可以疊起來的最高的烏龜是多少。

解題報告:

? 類似一個0-1背包,只不過他把真正的權值放在了第二維上,反而把原本的第二維放在了權值上,這樣我們需要權值的時候就倒著判斷一遍就好了。或者直接記錄可以到達的最大值然后輸出就行了。

?

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5; const int INF = 0x3f3f3f3f; struct Node {int w,p;//w體重,p承重 } node[MAX]; bool cmp(Node a,Node b) {if(a.p!=b.p) return a.p<b.p;else return a.w < b.w; //貌似可以不加 } int tot; int dp[MAX]; int main() {int q,qq;while(~scanf("%d%d",&q,&qq)) {node[++tot].w=q;node[tot].p=qq;}sort(node+1,node+tot+1,cmp);memset(dp,INF,sizeof dp);dp[0]=0;int maxx = 0;for(int i = 1; i<=tot; i++) {for(int j = maxx+1;j>=0; j--) {if(node[i].p > dp[j-1]+node[i].w) {dp[j] = min(dp[j],dp[j-1]+node[i].w);maxx = max(maxx,j);}}}printf("%d\n",maxx);return 0 ;}

二維去寫:

一篇

另一篇

大概貼一個代碼過來、

#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; }

?

總結

以上是生活随笔為你收集整理的【UVA - 10154 】Weights and Measures (贪心排序,dp,类似0-1背包,状态设定思维)的全部內容,希望文章能夠幫你解決所遇到的問題。

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