답안 #636609

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
636609 2022-08-29T16:53:27 Z stoyan_malinin Peru (RMI20_peru) C++14
100 / 100
471 ms 36176 KB
#include "peru.h"
#ifdef _LOCAL_
    #include "grader.cpp"
#endif // _LOCAL_

#include <cassert>
#include <vector>
#include <deque>
#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);

    dp[0] = a[0];
    deque <int> dq = {0};
    multiset <long long> ms;

    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) ms.erase(ms.find(dp[j]+a[dq.front()]));
        }

        while(dq.empty()==false && a[dq.back()]<=a[i])
        {
            if(dq.size()>=2) ms.erase(ms.find(dp[*prev(prev(dq.end()))] + a[dq.back()]));
            dq.pop_back();
        }

        if(dq.empty()==false) ms.insert(dp[dq.back()]+a[i]);
        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(ms.empty()==false) dp[i] = min(dp[i], (long long)*ms.begin());
    }

    return convertToAns(dp);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 316 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 316 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 316 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 316 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 320 KB Output is correct
15 Correct 61 ms 5364 KB Output is correct
16 Correct 74 ms 5232 KB Output is correct
17 Correct 76 ms 5180 KB Output is correct
18 Correct 47 ms 5204 KB Output is correct
19 Correct 49 ms 5216 KB Output is correct
20 Correct 50 ms 5232 KB Output is correct
21 Correct 62 ms 5208 KB Output is correct
22 Correct 73 ms 5332 KB Output is correct
23 Correct 76 ms 9308 KB Output is correct
24 Correct 64 ms 9164 KB Output is correct
25 Correct 65 ms 9132 KB Output is correct
26 Correct 68 ms 9204 KB Output is correct
27 Correct 74 ms 9248 KB Output is correct
28 Correct 66 ms 9204 KB Output is correct
29 Correct 65 ms 9444 KB Output is correct
30 Correct 63 ms 9232 KB Output is correct
31 Correct 63 ms 9196 KB Output is correct
32 Correct 72 ms 9236 KB Output is correct
33 Correct 72 ms 9164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 5364 KB Output is correct
2 Correct 74 ms 5232 KB Output is correct
3 Correct 76 ms 5180 KB Output is correct
4 Correct 47 ms 5204 KB Output is correct
5 Correct 49 ms 5216 KB Output is correct
6 Correct 50 ms 5232 KB Output is correct
7 Correct 62 ms 5208 KB Output is correct
8 Correct 73 ms 5332 KB Output is correct
9 Correct 76 ms 9308 KB Output is correct
10 Correct 64 ms 9164 KB Output is correct
11 Correct 65 ms 9132 KB Output is correct
12 Correct 68 ms 9204 KB Output is correct
13 Correct 74 ms 9248 KB Output is correct
14 Correct 66 ms 9204 KB Output is correct
15 Correct 65 ms 9444 KB Output is correct
16 Correct 63 ms 9232 KB Output is correct
17 Correct 63 ms 9196 KB Output is correct
18 Correct 72 ms 9236 KB Output is correct
19 Correct 72 ms 9164 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 316 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 316 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 320 KB Output is correct
31 Correct 1 ms 316 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 320 KB Output is correct
34 Correct 404 ms 32588 KB Output is correct
35 Correct 445 ms 32844 KB Output is correct
36 Correct 412 ms 32800 KB Output is correct
37 Correct 433 ms 32748 KB Output is correct
38 Correct 440 ms 32748 KB Output is correct
39 Correct 424 ms 32844 KB Output is correct
40 Correct 444 ms 32720 KB Output is correct
41 Correct 412 ms 32652 KB Output is correct
42 Correct 452 ms 32896 KB Output is correct
43 Correct 166 ms 21748 KB Output is correct
44 Correct 165 ms 33224 KB Output is correct
45 Correct 170 ms 33232 KB Output is correct
46 Correct 162 ms 33228 KB Output is correct
47 Correct 423 ms 32996 KB Output is correct
48 Correct 459 ms 33116 KB Output is correct
49 Correct 467 ms 33452 KB Output is correct
50 Correct 405 ms 34516 KB Output is correct
51 Correct 453 ms 33964 KB Output is correct
52 Correct 446 ms 36176 KB Output is correct
53 Correct 423 ms 34888 KB Output is correct
54 Correct 417 ms 32720 KB Output is correct
55 Correct 402 ms 32588 KB Output is correct
56 Correct 376 ms 32712 KB Output is correct
57 Correct 411 ms 32736 KB Output is correct
58 Correct 424 ms 32712 KB Output is correct
59 Correct 398 ms 32636 KB Output is correct
60 Correct 470 ms 32696 KB Output is correct
61 Correct 471 ms 32972 KB Output is correct
62 Correct 457 ms 32972 KB Output is correct
63 Correct 460 ms 32640 KB Output is correct