답안 #643165

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
643165 2022-09-21T11:00:58 Z Tenis0206 Peru (RMI20_peru) C++17
0 / 100
1 ms 332 KB
#include <bits/stdc++.h>

//#define home

#ifndef home

#include "peru.h"

#endif // home

using namespace std;

const long long oo = LLONG_MAX;

const long long Mod = 1e9 + 7;

int n,k;
long long v[2500005];

long long dp[2500005];

int tip[2500005];

void precalc()
{
    deque<int> d;
    for(int i=1; i<=n; i++)
    {
        while(!d.empty() && v[i]>v[d.back()])
        {
            tip[d.back()] = 1;
            d.pop_back();
        }
        d.push_back(i);
        if(d.front() < i - k)
        {
            tip[d.front()] = 2;
            d.pop_front();
        }
    }
}

int solve(int N, int K, int s[])
{
    n = N;
    k = K;
    for(int i=1; i<=n; i++)
    {
        v[i] = s[i-1];
    }
    precalc();
    deque<int> d;
    deque<pair<long long,int>> d_left;
    stack<pair<long long,int>> st_right;
    long long poz_front = 0,val_front = 0;
    long long Max = 0;
    for(int i=1; i<=n; i++)
    {
        while(!d.empty() && v[i] > v[d.back()])
        {
            d.pop_back();
            st_right.pop();
        }
        if(tip[i]==1)
        {
            if(st_right.empty())
            {
                st_right.push({oo,i});
                if(!d.empty())
                {
                    val_front = dp[d.back()] + v[i];
                    poz_front = d.back();
                }
            }
            else
            {
                st_right.push({min(st_right.top().first,dp[d.back()] + v[i]),i});
            }
        }
        else
        {
            while(!d_left.empty() && (v[i] + dp[d.back()]) < d_left.back().first)
            {
                d_left.pop_back();
            }
            if(!d.empty())
            {
                d_left.push_back({v[i] + dp[d.back()],d.back()});
            }
            if(!d_left.empty() && d_left.front().second < i - k)
            {
                d_left.pop_front();
            }
        }
        d.push_back(i);
        if(d.front() < i - k)
        {
            d.pop_front();
        }
        Max = max(Max,v[i]);
        if(i<=k)
        {
            dp[i] = Max;
            continue;
        }
        dp[i] = v[d.front()] + dp[i-k];
        if(poz_front >= i - k)
        {
            dp[i] = min(dp[i],val_front);
        }
        if(!d_left.empty())
        {
            dp[i] = min(dp[i],d_left.front().first);
        }
        if(!st_right.empty())
        {
            dp[i] = min(dp[i],st_right.top().first);
        }
    }
    long long rez = 0, put = 1;
    for(int i=n;i>=1;i--)
    {
        dp[i] %= Mod;
        rez += 1LL * dp[i] * put % Mod;
        rez %= Mod;
        put = 1LL * put * 23 % Mod;
    }
    return rez;
}

#ifdef home

int nn;
int vv[100005];

signed main()
{
    freopen("peru.in","r",stdin);
    freopen("peru.out","w",stdout);
    cin>>n>>k;
    for(int i=0; i<n; i++)
    {
        cin>>vv[i];
    }
    cout<<solve(n,k,vv)<<'\n';
    return 0;
}

#endif // home
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -