码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 940 E.Cashback (单调队列,dp)

时间:2018-09-30 20:09:54      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:scanf   function   priority   second   algo   cas   tor   lld   min   

Codeforces 940 E.Cashback

题意:一组数,要分为若干个区间,每个区间长度为ki(1<=ki<=n),并且对于每个区间删去前ki/c(向下取整)个小的数(即对区间升序排序后的前ki/c个数),要求找出最佳的划分方案使所有最终数组的和最小
思路:通过观察和分析
①一个长度2*c的区间总是不会优于把它划分成两个长度为c的区间
②在一个长度为c的区间后面添不超过c个数,都不会使删掉的数之和变大,还可能更小。
因此得到dp状态与转移方程:
dp[i]:前i个数最大可删掉数之和
dp[i]=max( dp[i-1], dp[i-c]+min{ ak } )
转移的时候,RMQ问题可以用单调队列或者ST表解决。


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<string>
#include<vector>
#include<cmath>
#include<climits>
#include<functional>
#include<set>
#define dd(x) cout<<#x<<" = "<<x<<" "
#define de(x) cout<<#x<<" = "<<x<<endl
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> V;
typedef map<int,int> M;
typedef queue<int> Q;
typedef priority_queue<int> BQ;
typedef priority_queue<int,vector<int>,greater<int> > SQ;
const int maxn=1e5+10,INF=0x3f3f3f3f;
ll h,t,q[maxn],a[maxn],dp[maxn];
int main()
{
    int n,c;
    ll ans=0;
    scanf("%d%d",&n,&c);
    for (int i=1;i<=n;++i)
    {
        scanf("%lld",&a[i]);
        ans+=a[i];
    }
    for (int i=1;i<=n;++i)
    {
        while (t>h&&q[h]<=i-c)
            ++h;
        while (t>h&&a[q[t-1]]>=a[i])
            --t;
        q[t++]=i;
        dp[i]=dp[i-1];
        if (i-c>=0)
            dp[i]=max(dp[i],dp[i-c]+a[q[h]]);
    }
    printf("%lld",ans-dp[n]);
    return 0;
}

Codeforces 940 E.Cashback (单调队列,dp)

标签:scanf   function   priority   second   algo   cas   tor   lld   min   

原文地址:https://www.cnblogs.com/orangee/p/9732941.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!