谁能帮我看看玄学代码

P1417 烹调方案

yibin @ 2022-06-25 15:52:23

#include<bits/stdc++.h>
#include <math.h>
#define pr(x) cerr<<#x<<"="<<(x)<<endl
#define pri(x,lo) {cerr<<#x<<"={";for (int ol=0;ol<=lo;ol++)cerr<<x[ol]<<",";cerr<<"}"<<endl;}
#define ll long long
using namespace std;
const int Inf=1e5+1;
ll a[Inf],f[Inf],V,n;
struct E{
    ll a,b,c;
}e[Inf];
bool cmp(E x,E y){
    return y.b*x.c>x.b*y.c;
}
int main(){
    scanf("%lld%lld",&V,&n);
    for(int i=1;i<=n;i++){
        scanf("%lld",&e[i].a);
    }
    for(int i=1;i<=n;i++){
        scanf("%lld",&e[i].b);
    }
    for(int i=1;i<=n;i++){
        scanf("%lld",&e[i].c);
    }
    sort(e+1,e+n+1,cmp);
    for(int i=1;i<=n;i++){
        for(ll j=V;j>=e[i].c;j--){
            f[j]=max(f[j],f[j-e[i].c]+e[i].a-(V-(j-e[i].c))*e[i].b);
        }
    }
    printf("%lld",f[V]);
    return 0;
}
/*
1:bycx
2:bxcy
*/

by yibin @ 2022-06-25 15:54:37

玄学过了


by Wf_yjqd @ 2022-07-14 22:53:44

tlqtj??


by Wf_yjqd @ 2022-07-14 23:52:15

其实不算玄学,j已包含c[i],则-t-c[i]求成了V-t-c[i]+c[i]

排序便与c[i]无关,正解排序为

a[x]-t*b[x]+a[y]-j*b[y]
a[y]-t*b[y]+a[x]-j*b[x]
-c[x]*b[y]<-c[y]*b[x]  

现改成

a[x]-t*b[x]+a[y]-(V-j+c[x])*b[y]
a[y]-t*b[y]+a[x]-(V-j+c[y])*b[x]
b[y]>b[x]

排序的比较值为正,从而变成从大到小排序,正好成立


by Wf_yjqd @ 2022-07-14 23:52:44

@yibin


by yibin @ 2022-07-15 09:00:46

@Wf_yjqd 谢啦


|