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;
    }
    
  • 相关阅读:
    Arduino系列之智能家居蓝牙语音遥控灯(四)
    Arduino系列之光照传感器(三)
    address2line 定位 Android c++奔溃位置
    android UI线程安全问题
    android 后台服务定时通知
    eclipse 完全智能提示
    IOS 7 Xcode 5 免IDP证书 真机调试(转载)
    DS5 调试 android c++
    javap -s 查看java方法签名
    ndk-stack 调试 android c++ 代码崩溃位置
  • 原文地址:https://www.cnblogs.com/orangee/p/9732941.html
Copyright © 2011-2022 走看看