zoukankan      html  css  js  c++  java
  • Codeforces 940 E.Cashback (单调队列,dp)

    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{ a[k](i-c<k<=i) } )
    转移的时候,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;
    }
    
  • 相关阅读:
    linux centos7 防止暴力破解
    win10 配置git 环境变量
    Tengine的说明
    php composer 报错 requires php ^7.1.8 || ^8.0 -> your php version
    uni-app v-for循环遍历 动态切换class、动态切换style
    Tomcat安装与配置
    我的云服务之WWW
    RHEL7 单独安装图形 X11
    python3-sql解析库——sqlparse
    Python3实战——爬虫入门
  • 原文地址:https://www.cnblogs.com/orangee/p/9732941.html
Copyright © 2011-2022 走看看