Codeforces 940 E.Cashback (單調隊列,dp)

來源:互聯網
上載者:User

標籤: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_backusing 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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.