F. 养猪

F. 养猪

解题思路:本以为是贪心的。原来是排序+dp。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lf;
typedef unsigned long long ull;
typedef pair<double,double>P;
const int inf = 0x7f7f7f7f;
const ll INF = 1e16;
const int N = 1e3+10;
const ll mod =  998244353;
const double PI = acos(-1.0);
const double eps = 1e-4;

inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
inline string readstring(){string str;char s=getchar();while(s==' '||s=='\n'||s=='\r'){s=getchar();}while(s!=' '&&s!='\n'&&s!='\r'){str+=s;s=getchar();}return str;}
int random(int n){return (int)(rand()*rand())%n;}
void writestring(string s){int n = s.size();for(int i = 0;i < n;i++){printf("%c",s[i]);}}
ll fast_power(ll a,ll p){
    ll ans = 1;
    while(p){
        if(p&1) ans = (ans*a)%mod;
        p >>= 1;
        a = (a*a)%mod;
    }
    return ans;
}
priority_queue<int,vector<int>,greater<int> >que;



struct str{
    int a,p;
}a[N];
int n,k;
int dp[N][N];



bool cmp(str a,str b){
    return (a.p>b.p)||(a.p==b.p&&a.a>b.a);
}
int main(){
    srand((unsigned)time(NULL));
    n = read(),k = read();
    for(int i = 1;i <= n;i++){
        a[i].a = read();
    }
    for(int i = 1;i <= n;i++){
        a[i].p = read();
    }
    sort(a+1,a+1+n,cmp);
    int ans = 0;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= min(i,k);j++){
            dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+max(a[i].a-(j-1)*a[i].p,0));
        }
    }
    for(int i = 1;i <= k;i++){
        ans = max(ans,dp[n][i]);
    }
    cout<<ans<<endl;
    return 0;
}


猜你喜欢

转载自blog.csdn.net/weixin_42868863/article/details/114102733
今日推荐