2020 CCPC Henan Provincial Collegiate Programming Contest(B,广告投放)

明显的dp问题,dp[i][j]表示,前i个广告,有j个人时得到的最大收益

dp[i][j / d[i]] = max(dp[i - 1][j] + w[i] * j,dp[i][j / d[i]]);第i个广告播放。

dp[i][j] = max(dp[i][j],dp[i - 1][j]);第i个广告不播放。

如果全开的话会mle,考虑滚动数组,可以发现上一维的j会转移到下一维的[ j / d[i] ],和[ j ],

所以优化后的方程为j的从小到大枚举,dp[j / d[i]] = max(dp[j / d[i]],dp[j] + w[i] * j);

然后发现时间复杂度也不允许,可以发现每次的人数都会变为c/d[i]下取整,由于初始人数的m小于1e5,将m除以从1-m的所有数,得到的数字即为可能的有用人数,会很大的减少复杂度

#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cstdio>
#include<algorithm>
#include<string>
#include<string.h>
#include<queue>
#include<math.h>
#include<set>
#include<functional>
using namespace std;
#define int long long 
using ull = unsigned long long;

inline int inc(int x,int v,int mod){x+=v;return x>=mod?x-mod:x;}//代替取模+
#define endl '\n'
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N = 2e5 + 10,M = log2(N)+1;;
const int mod = 998244353;
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;
}

//第i个广告,有j个人的时候 
//f[i + 1][j / d[i]] = 

void solve()
{
    int n,m;
    cin >> n >> m;
    vector<int>p(n + 10);   
    vector<int>d(n + 10);   
    vector<int>f(m + 10);   
    for(int i = 1; i <= n; i ++) cin >> p[i];
    for(int i = 1; i <= n; i ++) cin >> d[i];
    int idx = 0;
    vector<int>a(10000);
    a[++idx] = 0;
    map<int,int>heap;
    for(int i = m; i >= 1; i --)
    {
        if(heap[m / i] == 0)
        {
            heap[m / i] = 1;
            a[++ idx] = m / i;
        } 
    }
    for(int i = 1; i <= n; i ++)//使用滚动数组优化第一维
    {
        for(int j = 1; j <= idx; j ++)//优化第二维有用的时间
        {
            f[a[j] / d[i]] = max(f[a[j] / d[i]],f[a[j]] + a[j] * p[i]);
        }
    }
    int ans = 0;
    for(int i = 0; i <= m; i ++)
    {
        ans = max(ans,f[i]);
    }
    cout << ans << endl;
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int tt = 1;
    //cin >> tt;
    while(tt --)
    {
        solve();
    }   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值