Luo Gu P1510 Jingwei

Hit the jump meaning of problem-solving

Solution: a simple 01 backpack, put a little thinking about change, imagine when seeking to spend physical time is v, the volume of rock band the most is how much.

If dp [vmax] has not reached the remaining volume of Tokai, no solution, otherwise, loop to find the minimum physical consumption.

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define maxn 10005
 6 
 7 using namespace std;
 8 
 9 int dp[maxn],w[maxn],c[maxn];
10 int vsea,vmax,n;
11 
12 int main()
13 {
14     scanf("%d%d%d",&vsea,&n,&vmax);
15     for(int i=1;i<=n;i++)
16         scanf("%d%d",&c[i],&w[i]);
17     for(int i=1;i<=n;i++)
18         for(int v=vmax;v>=w[i];v--)
19             dp[v]=max(dp[v],dp[v-w[i]]+c[i]);
20     if(dp[vmax]<vsea) 
21     {
22         printf("Impossible");
23         return 0;
24     } 
25     for(int i=1;i<=vmax;i++)
26         if(dp[i]>=vsea) 
27         {
28             printf("%d",vmax-i);
29             return 0;
30         }
31     return 0;
32 }

 

Guess you like

Origin www.cnblogs.com/Hoyoak/p/11373507.html