Submission #857693

# Submission time Handle Problem Language Result Execution time Memory
857693 2023-10-06T15:36:56 Z boris_mihov Peru (RMI20_peru) C++17
100 / 100
105 ms 50280 KB
#include "peru.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
#include <stack>
#include <set>

typedef long long llong;
const int MAXN = 2500000 + 10;
const int MOD = 1e9 + 7;
const llong INF = 1e18;
const int INTINF = 1e9;

int n, k;
int a[MAXN];
llong dp[MAXN];
std::deque <int> dq;
int fromWhere[MAXN];

struct WindowMIN
{
    std::stack <llong> right;
    std::deque <std::pair <llong,int>> left;

    void pushLeft(llong value, int idx)
    {
        while (left.size() && left.back().first > value)
        {       
            left.pop_back();
        }

        left.push_back({value, idx});
    }

    void popLeft(int idx)
    {
        if (left[0].second == idx)
        {
            left.pop_front();
        }
    }

    void pushRight(llong value)
    {
        if (right.size()) right.push(std::min(right.top(), value));
        else right.push(value);
    }

    void popRight()
    {
        while (right.empty());
        right.pop();
    }

    llong findMIN()
    {
        llong res = INF;
        if (left.size()) res = std::min(res, left[0].first);
        if (right.size()) res = std::min(res, right.top());
        return res;
    }
};

WindowMIN wMin;
int solve(int N, int K, int* v)
{
    n = N; k = K;
    for (int i = 1 ; i <= n ; ++i)
    {
        a[i] = v[i - 1];
    }

    a[0] = INTINF;
    dp[0] = 0;

    for (int i = 1 ; i <= n ; ++i)
    {
        if (dq.size() && dq[0] == i - k)
        {
            if (dq.size() >= 2)
            {
                fromWhere[dq[1]] = 1;
            }

            dq.pop_front();
        }

        while (dq.size() && a[i] > a[dq.back()])
        {
            if (dq.size() >= 2)
            {
                fromWhere[dq.back()] = 2;
            }

            dq.pop_back();
        }

        dq.push_back(i);
    }
    
    dq.clear();
    llong nonPopped = INF;
    for (int i = 1 ; i <= n ; ++i)
    {
        if (dq.size() && dq[0] == i - k)
        {
            if (dq.size() >= 2)
            {
                wMin.popLeft(dq[1]);
            }
            
            dq.pop_front();
        }

        while (dq.size() && a[i] > a[dq.back()])
        {
            if (dq.size() >= 2)
            {
                wMin.popRight();
            }

            dq.pop_back();
        }

        if (dq.size())
        {
            if (fromWhere[i] == 1) wMin.pushLeft(dp[dq.back()] + a[i], i);
            else if (fromWhere[i] == 2) wMin.pushRight(dp[dq.back()] + a[i]);
            else nonPopped = std::min(nonPopped, dp[dq.back()] + a[i]);
        }

        dq.push_back(i);
        dp[i] = wMin.findMIN();
        dp[i] = std::min(dp[i], nonPopped);
        dp[i] = std::min(dp[i], dp[std::max(0, i - k)] + a[dq[0]]);
    }

    int ans = 0;
    for (int i = 1 ; i <= n ; ++i)
    {
        ans = (1LL * ans * 23 + dp[i]) % MOD;
    }

    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6488 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6592 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6488 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6592 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 17 ms 14940 KB Output is correct
16 Correct 17 ms 14940 KB Output is correct
17 Correct 18 ms 15020 KB Output is correct
18 Correct 25 ms 14940 KB Output is correct
19 Correct 26 ms 14968 KB Output is correct
20 Correct 24 ms 14844 KB Output is correct
21 Correct 17 ms 14936 KB Output is correct
22 Correct 18 ms 15136 KB Output is correct
23 Correct 17 ms 14940 KB Output is correct
24 Correct 18 ms 14924 KB Output is correct
25 Correct 19 ms 15032 KB Output is correct
26 Correct 17 ms 14936 KB Output is correct
27 Correct 17 ms 14940 KB Output is correct
28 Correct 18 ms 14912 KB Output is correct
29 Correct 17 ms 14940 KB Output is correct
30 Correct 18 ms 14940 KB Output is correct
31 Correct 18 ms 14940 KB Output is correct
32 Correct 17 ms 14936 KB Output is correct
33 Correct 17 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 14940 KB Output is correct
2 Correct 17 ms 14940 KB Output is correct
3 Correct 18 ms 15020 KB Output is correct
4 Correct 25 ms 14940 KB Output is correct
5 Correct 26 ms 14968 KB Output is correct
6 Correct 24 ms 14844 KB Output is correct
7 Correct 17 ms 14936 KB Output is correct
8 Correct 18 ms 15136 KB Output is correct
9 Correct 17 ms 14940 KB Output is correct
10 Correct 18 ms 14924 KB Output is correct
11 Correct 19 ms 15032 KB Output is correct
12 Correct 17 ms 14936 KB Output is correct
13 Correct 17 ms 14940 KB Output is correct
14 Correct 18 ms 14912 KB Output is correct
15 Correct 17 ms 14940 KB Output is correct
16 Correct 18 ms 14940 KB Output is correct
17 Correct 18 ms 14940 KB Output is correct
18 Correct 17 ms 14936 KB Output is correct
19 Correct 17 ms 14940 KB Output is correct
20 Correct 1 ms 6488 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6748 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6488 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6592 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 1 ms 6492 KB Output is correct
32 Correct 1 ms 6492 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 101 ms 49436 KB Output is correct
35 Correct 97 ms 49476 KB Output is correct
36 Correct 100 ms 49488 KB Output is correct
37 Correct 105 ms 49512 KB Output is correct
38 Correct 98 ms 49488 KB Output is correct
39 Correct 98 ms 49748 KB Output is correct
40 Correct 100 ms 49488 KB Output is correct
41 Correct 103 ms 50008 KB Output is correct
42 Correct 97 ms 49468 KB Output is correct
43 Correct 41 ms 27468 KB Output is correct
44 Correct 88 ms 37640 KB Output is correct
45 Correct 88 ms 37464 KB Output is correct
46 Correct 87 ms 37456 KB Output is correct
47 Correct 100 ms 49472 KB Output is correct
48 Correct 99 ms 49616 KB Output is correct
49 Correct 100 ms 49700 KB Output is correct
50 Correct 99 ms 49976 KB Output is correct
51 Correct 99 ms 49744 KB Output is correct
52 Correct 100 ms 50280 KB Output is correct
53 Correct 99 ms 50004 KB Output is correct
54 Correct 100 ms 49500 KB Output is correct
55 Correct 99 ms 49636 KB Output is correct
56 Correct 103 ms 49440 KB Output is correct
57 Correct 97 ms 49492 KB Output is correct
58 Correct 99 ms 49532 KB Output is correct
59 Correct 100 ms 49752 KB Output is correct
60 Correct 97 ms 49524 KB Output is correct
61 Correct 102 ms 49608 KB Output is correct
62 Correct 100 ms 49496 KB Output is correct
63 Correct 101 ms 49488 KB Output is correct