Submission #636881

# Submission time Handle Problem Language Result Execution time Memory
636881 2022-08-30T11:07:16 Z stoyan_malinin Peru (RMI20_peru) C++14
100 / 100
337 ms 192512 KB
#include "peru.h"
#ifdef _LOCAL_
    #include "grader.cpp"
#endif // _LOCAL_

#include <cassert>
#include <vector>
#include <deque>
#include <stack>
#include <queue>
#include <list>
#include <set>

using namespace std;

int rmq(int l, int r, const int *a)
{
    int maxVal = a[l];
    for(int i = l+1;i<=r;i++) maxVal = max(maxVal, a[i]);

    return maxVal;
}

int convertToAns(const vector<long long> &dp)
{
    long long p23 = 1, ans = 0;
    const long long mod = 1e9 + 7;

    for(int i = dp.size()-1;i>=0;i--)
    {
        ans = (ans + (dp[i]%mod)*p23)%mod;
        p23 = (p23*23)%mod;
    }

    return ans;
}

int solve(int n, int k, int* a)
{
    vector <long long> dp(n);

    deque <int> dq;
    vector <list<bool>> popTypes(n);

    for(int i = 0;i<n;i++)
    {
        while(dq.empty()==false && i-dq.front()>=k)
        {
            popTypes[dq.front()].push_back(false);
            dq.pop_front();
        }
        while(dq.empty()==false && a[dq.back()]<=a[i])
        {
            dq.pop_back();
            if(dq.empty()==false) popTypes[dq.back()].push_back(true);
        }

        dq.push_back(i);
    }

    dq = {0};
    dp[0] = a[0];
    deque <pair <int, int>> valuesLeft;
    stack <pair <int, int>> valuesRight;

    long long prefMaxVal = a[0];
    for(int i = 1;i<n;i++)
    {
        dp[i] = 1e18;
        prefMaxVal = max(prefMaxVal, (long long)a[i]);
        if(i<k) dp[i] = min(dp[i], prefMaxVal);

        while(dq.empty()==false && i-dq.front()>=k)
        {
            int j = dq.front();
            dq.pop_front();

            if(dq.empty()==false)
            {
                if(valuesLeft.empty()==false && valuesLeft.front()==make_pair(j, dq.front()))
                    valuesLeft.pop_front();
            }
        }

        while(dq.empty()==false && a[dq.back()]<=a[i])
        {
            if(dq.size()>=2)
            {
                if(valuesRight.empty()==false && valuesRight.top()==make_pair(*prev(prev(dq.end())), dq.back()))
                    valuesRight.pop();

            }
            dq.pop_back();
        }

        if(dq.empty()==false)
        {
            long long currVal = dp[dq.back()] + a[i];
            if(popTypes[dq.back()].empty()==false && popTypes[dq.back()].front()==false)
            {
                while(valuesLeft.empty()==false && dp[valuesLeft.back().first]+a[valuesLeft.back().second]>=currVal)
                    valuesLeft.pop_back();
                valuesLeft.emplace_back(dq.back(), i);

                popTypes[dq.back()].pop_front();
            }
            else
            {
                if(valuesRight.empty()==true || dp[valuesRight.top().first]+a[valuesRight.top().second]>=currVal)
                    valuesRight.emplace(dq.back(), i);

                if(popTypes[dq.back()].empty()==false) popTypes[dq.back()].pop_front();
            }
        }
        dq.push_back(i);

        int last = max(i-k, 0);
        if(dq.front()>last) dp[i] = min(dp[i], dp[last] + a[dq.front()]);

        if(valuesLeft.empty()==false) dp[i] = min(dp[i], dp[valuesLeft.front().first] + a[valuesLeft.front().second]);
        if(valuesRight.empty()==false) dp[i] = min(dp[i], dp[valuesRight.top().first] + a[valuesRight.top().second]);
    }

    return convertToAns(dp);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 352 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 352 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 58 ms 31120 KB Output is correct
16 Correct 51 ms 30816 KB Output is correct
17 Correct 51 ms 30880 KB Output is correct
18 Correct 68 ms 30852 KB Output is correct
19 Correct 63 ms 30912 KB Output is correct
20 Correct 63 ms 30896 KB Output is correct
21 Correct 52 ms 31044 KB Output is correct
22 Correct 62 ms 30960 KB Output is correct
23 Correct 51 ms 31052 KB Output is correct
24 Correct 51 ms 31096 KB Output is correct
25 Correct 50 ms 31072 KB Output is correct
26 Correct 54 ms 31056 KB Output is correct
27 Correct 54 ms 31028 KB Output is correct
28 Correct 53 ms 31108 KB Output is correct
29 Correct 52 ms 31280 KB Output is correct
30 Correct 51 ms 31104 KB Output is correct
31 Correct 52 ms 31096 KB Output is correct
32 Correct 55 ms 31060 KB Output is correct
33 Correct 53 ms 30968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 31120 KB Output is correct
2 Correct 51 ms 30816 KB Output is correct
3 Correct 51 ms 30880 KB Output is correct
4 Correct 68 ms 30852 KB Output is correct
5 Correct 63 ms 30912 KB Output is correct
6 Correct 63 ms 30896 KB Output is correct
7 Correct 52 ms 31044 KB Output is correct
8 Correct 62 ms 30960 KB Output is correct
9 Correct 51 ms 31052 KB Output is correct
10 Correct 51 ms 31096 KB Output is correct
11 Correct 50 ms 31072 KB Output is correct
12 Correct 54 ms 31056 KB Output is correct
13 Correct 54 ms 31028 KB Output is correct
14 Correct 53 ms 31108 KB Output is correct
15 Correct 52 ms 31280 KB Output is correct
16 Correct 51 ms 31104 KB Output is correct
17 Correct 52 ms 31096 KB Output is correct
18 Correct 55 ms 31060 KB Output is correct
19 Correct 53 ms 30968 KB Output is correct
20 Correct 1 ms 352 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 324 ms 190276 KB Output is correct
35 Correct 313 ms 190396 KB Output is correct
36 Correct 312 ms 190368 KB Output is correct
37 Correct 318 ms 190364 KB Output is correct
38 Correct 309 ms 190368 KB Output is correct
39 Correct 311 ms 190560 KB Output is correct
40 Correct 310 ms 190464 KB Output is correct
41 Correct 321 ms 190396 KB Output is correct
42 Correct 313 ms 190364 KB Output is correct
43 Correct 122 ms 76344 KB Output is correct
44 Correct 255 ms 115484 KB Output is correct
45 Correct 234 ms 115532 KB Output is correct
46 Correct 236 ms 115412 KB Output is correct
47 Correct 324 ms 192420 KB Output is correct
48 Correct 302 ms 192408 KB Output is correct
49 Correct 312 ms 192456 KB Output is correct
50 Correct 306 ms 192332 KB Output is correct
51 Correct 319 ms 192352 KB Output is correct
52 Correct 320 ms 192512 KB Output is correct
53 Correct 306 ms 192460 KB Output is correct
54 Correct 321 ms 190184 KB Output is correct
55 Correct 301 ms 190308 KB Output is correct
56 Correct 306 ms 190288 KB Output is correct
57 Correct 314 ms 190244 KB Output is correct
58 Correct 306 ms 190328 KB Output is correct
59 Correct 313 ms 190492 KB Output is correct
60 Correct 314 ms 190284 KB Output is correct
61 Correct 337 ms 192448 KB Output is correct
62 Correct 316 ms 192156 KB Output is correct
63 Correct 309 ms 192184 KB Output is correct