NC24740. Making Money
描述
Consider the situation when FJ has just 3 kinds of curios and starts with M=17. Below are the cost and revenue numbers for each curio: Curio Cost Revenue # C_i R_i 1 2 4 2 5 6 3 3 7 In this case, FJ should purchase 5 curios of type 3 for 15 money and 1 more curio of type 1 for 2 money, a total of 17 money. His profit will be 5 * (7-3) + 1 * (4-2) = 5*4 + 1*2 = 22 money. He can do no better than this given the cost and revenue structure.NOTE: The second test case is challenging -- but our answer is correct.
输入描述
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Line i+1 contains two space-separated integers: and
输出描述
* Line 1: The maximum profit FJ can generate given the costs and revenues
示例1
输入:
3 17 2 4 5 6 3 7
输出:
22
C++11(clang++ 3.9) 解法, 执行用时: 17ms, 内存消耗: 724K, 提交时间: 2020-02-26 21:17:18
#include<algorithm> #include<cstdio> using namespace std; int n,m,ans,f[100003]; int main() { scanf("%d%d",&n,&m); for(int a,b,i=1;i<=n;i++) { scanf("%d%d",&a,&b); for(int j=a;j<=m;j++) ans=max(ans,(f[j]=max(f[j],f[j-a]+b-a))+m-j); } printf("%d",ans); return 0; }
C++14(g++5.4) 解法, 执行用时: 49ms, 内存消耗: 772K, 提交时间: 2019-10-27 19:51:54
#include<bits/stdc++.h> using namespace std; int n,m,z,w,c,f[100007]; int main() { cin>>n>>m; for(int i=1;i<=n;i++) { cin>>c>>w; for(int j=1;j<=m;j++) if(j-c>=0)f[j]=max(max(f[j-1]+1,f[j]),f[j-c]+w-c); } cout<<f[m]<<endl; return 0; }