请各位苣佬帮本蒟蒻看一看代码(感激不尽)

P1478 陶陶摘苹果(升级版)

Naruto_steven @ 2019-01-19 17:09:19

/*
8 15
20 130
120 3
150 2
110 7
180 1
50 8
200 0
140 3
120 2
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,s,a,b,du[5001],li[5001],ha[5001],shu[5001],zhi[1005];
    memset(zhi,0,sizeof(zhi));
    scanf("%d%d%d%d",&n,&s,&a,&b);
    for(register int i=1;i<=n;++i){
        scanf("%d%d",&du[i],&li[i]);
        if(li[i]!=0){
            zhi[i]=du[i]/li[i];
        }else{
            zhi[i]=0x7fff;
        }
    }
    memcpy(ha,zhi,sizeof(zhi));
    sort(zhi,zhi+n+1);
    for(register int i=n+1,p=0;i>=1;--i,++p){
        for(register int j=1;j<=n;++j){
            if(zhi[i]==ha[j]){
                shu[p]=j;
            }
        }
    }
    int x=0,ans=0;
    while(s>=0){
        ++x;
        s-=li[shu[x]];
        ++ans;
    }
    cout<<ans; 
    return 0;
}

by Koakuma @ 2019-01-19 17:19:57

@Naruto_steven 以前写的,可以参考一下QwQ 个人认为比较好理解

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int apl[5001],pwr[5001],f[5001];
int max_l,n,s,a,b,i,j,k;
int main()
{
    cin>>n>>s;
    cin>>a>>b;
    max_l=a+b;
    for(i=1;i<=n;++i)
    cin>>apl[i]>>pwr[i];
    for(i=1;i<=n;++i)
     for(j=s;j>=pwr[i];--j)
     if(apl[i]<=max_l)
     f[j]=max(f[j],f[j-pwr[i]]+1);
    cout<<f[s]<<endl;
    return 0;
}

by t162 @ 2019-01-19 17:26:20

@Koakuma 这题不就是简单的贪心嘛用不着动态规划吧QwQ(用了也可以)


by Koakuma @ 2019-01-19 17:30:27

@Bambusoideae qaq


by AC_Automation @ 2019-01-19 17:31:30

先将够不到的删去,然后按消耗的体力值从小到大排序,之后从头开始扫就好了


by Naruto_steven @ 2019-01-19 17:44:59

@Koakuma 谢谢大佬


by Naruto_steven @ 2019-01-19 17:46:06

@sumingyang_bj 但是应该需要把那个性价比拿来比较吧


by AC_Automation @ 2019-01-19 17:55:05

不需要,性价比都是1(摘下来的苹果数量)


by AC_Automation @ 2019-01-19 17:55:58

每次消耗一些体力来摘1个苹果


by Naruto_steven @ 2019-01-19 18:39:47

@sumingyang_bj 哦,明白了。谢谢巨佬


|