辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师。为此,他想拜附近最有威望的医师为师。医师为了判断他的资质,给他出了一个难题。医师把他带到一个到处都是草药的山洞里对他说:“孩子,这个山洞里有一些不同的草药,采每一株都需要一些时间,每一株也有它自身的价值。我会给你一段时间,在这段时间里,你可以采到一些草药。如果你是一个聪明的孩子,你应该可以让采到的草药的总价值最大。”
如果你是辰辰,你能完成这个任务吗?
输入输出格式
输入格式:
第一行有222个整数T(1≤T≤1000)T(1 le T le 1000)T(1≤T≤1000)和M(1≤M≤100)M(1 le M le 100)M(1≤M≤100),用一个空格隔开,TTT代表总共能够用来采药的时间,MMM代表山洞里的草药的数目。
接下来的MMM行每行包括两个在111到100100100之间(包括111和100100100)的整数,分别表示采摘某株草药的时间和这株草药的价值。输出格式:
111个整数,表示在规定的时间内可以采到的草药的最大总价值。
完全背包,滚动数组

1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1e6+5; 4 const int INF=1e9+7; 5 int t,m,ans,v[maxn],w[maxn],f[maxn]; 6 template <class t>void red(t &x) 7 { 8 x=0; 9 int w=1; 10 char ch=getchar(); 11 while(ch<'0'||ch>'9') 12 { 13 if(ch=='-') 14 w=-1; 15 ch=getchar(); 16 } 17 while(ch>='0'&&ch<='9') 18 { 19 x=(x<<3)+(x<<1)+ch-'0'; 20 ch=getchar(); 21 } 22 x*=w; 23 } 24 void input() 25 { 26 freopen("input.txt","r",stdin); 27 } 28 void read() 29 { 30 red(t); 31 red(m); 32 for(int i=1;i<=m;++i) 33 { 34 red(v[i]); 35 red(w[i]); 36 } 37 } 38 void work() 39 { 40 for(int i=1;i<=m;++i) 41 for(int j=v[i];j<=t;++j) 42 f[j]=max(f[j-v[i]]+w[i],f[j]); 43 for(int i=1;i<=t;++i) 44 ans=max(ans,f[i]); 45 printf("%d",ans); 46 } 47 int main() 48 { 49 //input(); 50 read(); 51 work(); 52 return 0; 53 }