2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018) A. Altruistic Amphibians (DP)

题目链接:https://codeforc.es/gym/101933/problem/A

题意:有 n 只青蛙在一个坑里面,要求可以跳出坑的青蛙的最大数量。每个青蛙有 3 种属性:l 为青蛙一次可以跳的高度,w 为青蛙的重量,h 为青蛙作为垫背时的高度,垫背的前提是垫背的青蛙的重量比在他上面的青蛙的总重量要小。

题解:首先将青蛙按重量排序,dp[j]表示在已经垫背的青蛙上面还可以放一个重量  < j 的青蛙时的垫背最大高度,更新的时候,因为重量从大到小排序,dp[j] 由 dp[j + a[i].w] 转移而来,表示把当前的青蛙放到最上面时垫背的最大高度。详见代码~

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define ull unsigned long long
 5 #define mst(a,b) memset((a),(b),sizeof(a))
 6 #define mp(a,b) make_pair(a,b)
 7 #define pi acos(-1)
 8 #define pii pair<int,int>
 9 #define pb push_back
10 const int INF = 0x3f3f3f3f;
11 const double eps = 1e-6;
12 const int MAXN = 1e5 + 10;
13 const int MAXM = 1e8 + 10;
14 const ll mod = 1e9 + 7;
15 
16 struct node {
17     int l,w,h;
18 }a[MAXN];
19 
20 bool cmp(node x,node y) {
21     return x.w > y.w;
22 }
23 
24 int dp[MAXM];
25 
26 int main() {
27 #ifdef local
28     freopen("data.txt", "r", stdin);
29 //    freopen("data.txt", "w", stdout);
30 #endif
31     mst(dp, 0);
32     int n,d;
33     scanf("%d%d",&n,&d);
34     for(int i = 0; i < n; i++)
35         scanf("%d%d%d",&a[i].l,&a[i].w,&a[i].h);
36     sort(a,a + n,cmp);
37     int ans = 0;
38     for(int i = 0; i < n; i++) {
39         if(dp[a[i].w] + a[i].l > d) ans++;
40         int mx = min((int)1e8 - a[i].w, a[i].w);
41         for(int j = 1; j < mx; j++) {
42             dp[j] = max(dp[j],dp[j + a[i].w] + a[i].h);
43             if(dp[j] > MAXM) dp[j] = MAXM;
44         }
45     }
46     printf("%d
",ans);
47     return 0;
48 }
原文地址:https://www.cnblogs.com/scaulok/p/9900038.html