- 采药
#include <algorithm>
#include <iostream>
using namespace std;
const int M = 110;
const int T = 1010;
int r[T];
int t,m;
int main(){
cin>>t>>m;
int a,b;
for(int i=0;i<m;i++){
cin>>a>>b;
for(int j=t;j>=a;j--){
r[j] = max(r[j],r[j-a]+b);
}
}
cout<<r[t];
return 0;
}
- 装箱问题
#include <algorithm>
#include <iostream>
using namespace std;
const int V = 20010;
int r[V];
int v;
int n;
int main(){
scanf("%d",&v);
scanf("%d",&n);
int t;
for(int i=0;i<n;i++){
scanf("%d",&t);
for(int j=v;j>=t;j--){
r[j] = max(r[j],r[j-t]+t);
}
}
printf("%d",v-r[v]);
return 0;
}
- 宠物小精灵之收服
宠物小精灵是一部讲述小智和他的搭档皮卡丘一起冒险的故事。
一天,小智和皮卡丘来到了小精灵狩猎场,里面有很多珍贵的野生宠物小精灵。
小智也想收服其中的一些小精灵。
然而,野生的小精灵并不那么容易被收服。
对于每一个野生小精灵而言,小智可能需要使用很多个精灵球才能收服它,而在收服过程中,野生小精灵也会对皮卡丘造成一定的伤害(从而减少皮卡丘的体力)。
当皮卡丘的体力小于等于0时,小智就必须结束狩猎(因为他需要给皮卡丘疗伤),而使得皮卡丘体力小于等于0的野生小精灵也不会被小智收服。
当小智的精灵球用完时,狩猎也宣告结束。
我们假设小智遇到野生小精灵时有两个选择:收服它,或者离开它。
如果小智选择了收服,那么一定会扔出能够收服该小精灵的精灵球,而皮卡丘也一定会受到相应的伤害;如果选择离开它,那么小智不会损失精灵球,皮卡丘也不会损失体力。
小智的目标有两个:主要目标是收服尽可能多的野生小精灵;如果可以收服的小精灵数量一样,小智希望皮卡丘受到的伤害越小(剩余体力越大),因为他们还要继续冒险。
现在已知小智的精灵球数量和皮卡丘的初始体力,已知每一个小精灵需要的用于收服的精灵球数目和它在被收服过程中会对皮卡丘造成的伤害数目。
请问,小智该如何选择收服哪些小精灵以达到他的目标呢?
输入格式
输入数据的第一行包含三个整数:N,M,K,分别代表小智的精灵球数量、皮卡丘初始的体力值、野生小精灵的数量。
之后的K行,每一行代表一个野生小精灵,包括两个整数:收服该小精灵需要的精灵球的数量,以及收服过程中对皮卡丘造成的伤害。
输出格式
输出为一行,包含两个整数:C,R,分别表示最多收服C个小精灵,以及收服C个小精灵时皮卡丘的剩余体力值最多为R。
数据范围
0<N≤1000
,
0<M≤500,
0<K≤100
输入样例1:
10 100 5
7 10
2 40
2 50
1 20
4 20
输出样例1:
3 30
#include <algorithm>
#include <iostream>
using namespace std;
const int N=1010,M=510;
int map[N][M];
int q,hp,n;
int main(){
scanf("%d %d %d",&q,&hp,&n);
int qc,hpc;
for(int i=0;i<n;i++){
scanf("%d %d",&qc,&hpc);
for(int j=q;j>=qc;j--){
for(int x=hp;x>hpc;x--){
map[j][x] = max(map[j][x],map[j-qc][x-hpc]+1);
}
}
}
int maxn = map[q][hp];
int tempi = hp;
while(tempi--){
if(map[q][tempi]<map[q][hp])break;
}
tempi++;
if(maxn==0)printf("%d %d",0,hp);
else printf("%d %d",map[q][tempi],hp-tempi+1);
return 0;
}
- 数字组合
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 110,M=10010;
int f[M];
int n,m;
int main(){
cin>>n>>m;
f[0] = 1;
for(int i=0;i<n;i++){
int t;
cin>>t;
for(int j=m;j>=t;j--){
f[j] = f[j]+f[j-t];
}
}
cout<<f[m];
return 0;
}
- 买书
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 1010;
int f[N];
int n;
int main(){
cin>>n;
if(n%10){
cout<<"0";
return 0;
}
n = n/10;
int m[4] = {1,2,5,10};
f[0] = 1;
for(int i=0;i<4;i++){
for(int j=m[i];j<=n;j++){
f[j]+=f[j-m[i]];
}
}
cout<<f[n];
return 0;
}