NC24870. [USACO 2009 Dec G]Video Game Troubles
描述
Consider one dataset with N=3 consoles and a V=$800 budget. The first console costs $300 and has 2 games with cost $30 and $25 and production values as shown: Game # Cost Production Value 1 $30 50 2 $25 80 The second console costs $600 and has only 1 game: Game # Cost Production Value 1 $50 130 The third console costs $400 and has 3 games: Game # Cost Production Value 1 $40 70 2 $30 40 3 $35 60 Farmer John should buy consoles 1 and 3, game 2 for console 1, and games 1 and 3 for console 3 to maximize his expected production at 210: Production Value Budget: $800 Console 1 -$300 Game 2 -$25 80 Console 3 -$400 Game 1 -$40 70 Game 3 -$35 60 ------------------------------------------- Total: 0 (>= 0) 210
输入描述
* Line 1: Two space-separated integers: N and V
* Lines 2..N+1: Line i+1 describes the price of and the games ?available for console i; it contains: Pi, Gi, and Gi pairs of space-separated integers GPj, PVj
输出描述
* Line 1: The maximum production value that Farmer John can get with his budget.
示例1
输入:
3 800 300 2 30 50 25 80 600 1 50 130 400 3 40 70 30 40 35 60
输出:
210
C++14(g++5.4) 解法, 执行用时: 87ms, 内存消耗: 1252K, 提交时间: 2019-08-20 00:36:03
#include<stdio.h> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) int n,m,f[110000],g[110000],V,ww,vv,t,te; int main(){ scanf("%d%d",&n,&m); fo(i,1,n){ scanf("%d",&V); fo(j,V,m) g[j]=f[j-V]; scanf("%d",&t); while (t--){ scanf("%d%d",&vv,&ww); fd(j,m,V+vv){ te=g[j-vv]+ww; if (te>g[j]) g[j]=te; } } fo(j,V,m) if (g[j]>f[j]) f[j]=g[j]; } printf("%d\n",f[m]); return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 78ms, 内存消耗: 1368K, 提交时间: 2019-08-18 14:11:34
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int N=1e5+5; int n,m,f[N],g[N]; int main(){ scanf("%d%d",&n,&m); for(int i=1,p,k,a,b;i<=n;++i){ scanf("%d%d",&p,&k); memcpy(g,f,sizeof(g)); while(k--){ scanf("%d%d",&a,&b); for(int j=m;j>=a;--j) g[j]=max(g[j],g[j-a]+b); } for(int j=p;j<=m;++j)f[j]=max(f[j],g[j-p]); } printf("%d\n",f[m]);return 0; }