P1048 [NOIP2005 普及组] 采药
dp,从dp【n】往回递归,确保只采一次
#include<bits/stdc++.h>
#define f(i,x,y) for(int i=(x);i<=(y);++i)
using namespace std;
int n=0,ans[100001],t;
class herb{
public:
int time,value;
}h[10001];
void input() {
cin>>t>>n;
f(i,1,n)
cin>>h[i].time>>h[i].value;
}
int get_ans()
{
f(i,1,n)
{
for(int tt=t;tt>=h[i].time;--tt)
ans[tt]=max(ans[tt-h[i].time]+h[i].value,ans[tt]);
}
return ans[t];
}
int main()
{
input();
cout<<get_ans();
return 0;
}
P1616 疯狂的采药
从dp【time】开始递推,实现可以采药无数次
#include<bits/stdc++.h>
#define f(i,x,y) for(long long i=(x);i<=(y);++i)
const int mmax=10000001;
using namespace std;
long long n,ans[mmax],t;
class herb{
public:
long long time,value;
}h[10010];
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>t>>n;
f(i,1,n)
cin>>h[i].time>>h[i].value;
f(i,1,n)
f(tt,h[i].time,t)
ans[tt]=max(ans[tt-h[i].time]+h[i].value , ans[tt]);
cout<<ans[t];
return 0;
}
P1049 [NOIP2001 普及组] 装箱问题
和问题一一样啊。。。
#include<bits/stdc++.h>
#define f(i,x,y) for(int i=(x);i<=(y);++i)
const int mmax=0;
using namespace std;
int spa[20001]={0};
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int v,n;
cin>>v>>n;
int a[n+1],mmin=v;
f(i,1,n)
cin>>a[i];
f(i,1,n)
for(int j=v;j>=a[i];--j)
spa[j]=max(spa[j],spa[j-a[i]]+a[i]);
cout<<v-spa[v];
return 0;
}
P1833 樱花
分类一下,0的时候是问题二,非0次数时再运行x次问题1
#include<bits/stdc++.h>
#define f(i,x,y) for(int i=(x);i<=(y);++i)
const int mmax=0;
using namespace std;
class clockk{
public:
int hour,min;
clockk(string y)
{
hour=0,min=0;
int flag=0;
f(i,0,y.size())
{
if(y[i]>='0'&&y[i]<='9')
{
if(flag==0)
hour=hour*10+y[i]-'0';
if(flag==1)
min=min*10+y[i]-'0';
}
else
flag=1;
}
}
};
class cherry{
public:
int time_admire,times,beauty;
};
int n;
int dp[10001];
int solve(clockk x,clockk y)
{
int ans=(y.hour-x.hour)*60+y.min-x.min;
return ans;
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
string a,b;
cin>>a>>b>>n;
clockk beg(a),end(b);
int t=solve(beg,end);
cherry ch[n+1];
f(i,1,n)
cin>>ch[i].time_admire>>ch[i].beauty>>ch[i].times;
f(i,1,n)
{
if(ch[i].times!=0)
f(k,1,ch[i].times)
for(int j=t;j>=ch[i].time_admire;--j)
dp[j]=max(dp[j],dp[j-ch[i].time_admire]+ch[i].beauty);
else
f(j,ch[i].time_admire,t)
dp[j]=max(dp[j],dp[j-ch[i].time_admire]+ch[i].beauty);
}
cout<<dp[t];
return 0;
}