yuyuyuyu12345 @ 2020-03-11 17:10:33
#include<iostream>
using namespace std;
int n,s,a,b,ans;
int xi[5005],yi[5005];
bool visit[5005][1001];//存储是否访问过调用这两个参量的函数
int mem[5005][1001];//存储调用这两个参量的函数的返回值
int dfs(int num,int rest){
if(num>n) return 0;
if(visit[num][rest]) return mem[num][rest];//如果调用这两个参量的函数已经被访问过,那么直接返回之前存储的值即可
visit[num][rest]=true;
int maxn=dfs(num+1,rest);
if(xi[num]<=a+b&&rest>=yi[num]){
int t=dfs(num+1,rest-yi[num])+1;
maxn=t>maxn?t:maxn;
}
return mem[num][rest]=maxn;//返回值的同时存储这次运算的返回值
}
int main(){
cin>>n>>s>>a>>b;
for(int i=1;i<=n;i++){
cin>>xi[i]>>yi[i];
}
cout<<dfs(1,s);
return 0;
}
by yuyuyuyu12345 @ 2020-03-11 17:11:54
麻烦各位大佬帮忙解答一下,谢谢!
by yuyuyuyu12345 @ 2020-03-11 17:13:07
return mem[num][rest]=maxn;//返回值的同时存储这次运算的返回值
求解这步为什么要存储这次运算的返回值
by PrincessYR✨~ @ 2020-03-11 17:14:27
@yuyuyuyu12345
#include<iostream>
#include<algorithm>
using namespace std;
struct qp
{
int gao;
int tili;
}n[5002];
bool pq(qp n,qp y)
{
return n.tili<y.tili;
}
int main()
{
int a,b,c,d,e=0;
cin>>a>>b>>c>>d;
d=d+c;
for(int i=1;i<=a;i++)
{
cin>>n[i].gao>>n[i].tili;
}
sort(n+1,n+1+a,pq);
for(int i=1;i<=a;i++)
{
if(n[i].gao<=d&&b-n[i].tili>=0)
{
b-=n[i].tili;
e++;
}
}
cout<<e;
return 0;
}
by jijidawang @ 2020-03-11 17:16:07
@yuyuyuyu12345 记忆化,算过的就不再算一遍了
by vectorwyx @ 2020-03-11 17:36:06
@jijidawang 可是这题不需要记忆化啊(难道是我太弱了)
by jijidawang @ 2020-03-11 17:41:36
@vectorwyx 题解可能想炫技
by yuyuyuyu12345 @ 2020-03-11 22:47:10
@jijidawang 谢谢!
by yuyuyuyu12345 @ 2020-03-11 22:47:23
@PrincessYR✨~ 谢谢!
by Poiyzy @ 2020-03-13 16:32:19
题解前面的大佬比较多。。可能理解会有问题。。 献上本人的代码。。。这题有点类似排队接水那道题。。
```#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
struct apple{
int high,power;
}e[8000];
bool cmp(apple p1,apple p2){
if(p1.high==p2.high){
return p1.power<p2.power;
}
return p1.power<p2.power;
}
int n,s,a,b,ans;
int main(){
cin>>n>>s>>a>>b;
ans=0;
for(int i=0;i<n;i++){
cin>>e[i].high >>e[i].power;
}
sort(e,e+n,cmp);
for(int i=0;i<n;i++){
if(a+b>=e[i].high && s>=e[i].power){
s-=e[i].power;
ans++;
}
}
cout<<ans;
return 0;
}