zoukankan      html  css  js  c++  java
  • Codeforces 1077 F2

    F2 - Pictures with Kittens (hard version)

    思路:

    单调队列优化dp

    代码:

    #pragma GCC optimize(2)
    #pragma GCC optimize(3)
    #pragma GCC optimize(4)
    #include<bits/stdc++.h>
    using namespace std;
    #define fi first
    #define se second
    #define pi acos(-1.0)
    #define LL long long
    //#define mp make_pair
    #define pb push_back
    #define ls rt<<1, l, m
    #define rs rt<<1|1, m+1, r
    #define ULL unsigned LL
    #define pll pair<LL, LL>
    #define pli pair<LL, int>
    #define pii pair<int, int>
    #define piii pair<pii, int>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
    //head
    
    const int N = 5e3 + 10;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    int a[N];
    deque<pair<int, LL>> dp[N]; 
    LL tmp[N];
    int main() {
        int n, k, x;
        scanf("%d %d %d", &n, &k, &x);
        for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    
            dp[0].pb({0, 0});
            for (int i = 1; i <= n; i++) {
                // i-k 
                for (int l = 1; l <= x; l++) {
                    while(!dp[l-1].empty() && dp[l-1].front().fi < i-k) dp[l-1].pop_front();
                    if(!dp[l-1].empty()) tmp[l] = dp[l-1].front().se + a[i];
                }
                
                for (int l = 1; l <= x; l++) {
                    while(!dp[l].empty() && dp[l].back().se <= tmp[l]) dp[l].pop_back();
                    if(tmp[l])dp[l].push_back({i, tmp[l]});
                    tmp[l] = 0;
                }
            }
        LL ans = -1; 
        while(!dp[x].empty() && dp[x].front().fi <= n-k)  dp[x].pop_front();
        if(!dp[x].empty()) ans = dp[x].front().se; 
        printf("%lld
    ", ans);
        return 0;
    }
  • 相关阅读:
    Qt 去除控件边框线
    Qt 自定义可编辑 模型视图
    Qt double类型输出问题
    vue实例
    初识vue
    python中的数据类型
    python 列表解析式
    Goland常用快键键 mac pro
    文档对象模型DOM
    正则表达式
  • 原文地址:https://www.cnblogs.com/widsom/p/9972681.html
Copyright © 2011-2022 走看看