Submission #636880

# Submission time Handle Problem Language Result Execution time Memory
636880 2022-08-30T11:05:06 Z stoyan_malinin Peru (RMI20_peru) C++14
18 / 100
114 ms 262144 KB
#include "peru.h"
#ifdef _LOCAL_
    #include "grader.cpp"
#endif // _LOCAL_

#include <cassert>
#include <vector>
#include <deque>
#include <stack>
#include <queue>
#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 <queue<bool>> popTypes(n);

    for(int i = 0;i<n;i++)
    {
        while(dq.empty()==false && i-dq.front()>=k)
        {
            popTypes[dq.front()].push(false);
            dq.pop_front();
        }
        while(dq.empty()==false && a[dq.back()]<=a[i])
        {
            dq.pop_back();
            if(dq.empty()==false) popTypes[dq.back()].push(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)
        {
            //while(values.empty()==false && values.back().first>=dp[dq.back()]+a[i])
            //    values.pop_back();

            //values.emplace_back(dp[dq.back()]+a[i], make_pair(dq.back(), i));

            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();
            }
            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();
            }
        }
        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)
        {
            //cout << "valuesRight: " << valuesRight.top().first << " " << valuesRight.top().second << " || " << dp[valuesRight.top().first] + a[valuesRight.top().second] << '\n';
            dp[i] = min(dp[i], dp[valuesRight.top().first] + a[valuesRight.top().second]);
        }

        /*
        //if(values.empty()==false) dp[i] = min(dp[i], values.front().first);
        for(auto item: values) dp[i] = min(dp[i], item.first);

        assert(values.size()<=dq.size()-1);
        if(values.empty()==false) assert(values.front().first>=dq.front());

        /*
        cout << "dq: ";for(int x: dq) cout << x << " ";
        cout << '\n';

        if(i==1285)
        {
            cout << "dq: ";for(int j: dq) cout << j << " ";cout << '\n';
            cout << "values: ";for(auto item: values) cout << "(" << item.first << ", (" << item.second.first << " " << item.second.second << "))" << " ";cout << '\n';
            cout << values.front().first << " || " << dp[last] + a[dq.front()] << '\n';
        }
        */
        //cout << "valuesLeft: ";for(auto item: valuesLeft) cout << "(" << item.first << ", " << item.second << " | " << dp[item.first] + a[item.second] << ") ";
        //cout << '\n';
        //cout << "valuesRight: ";for(auto item: valuesRight) cout << "(" << item.first << ", " << item.second << " | " << dp[item.first] + a[item.second] << ") ";
        //cout << '\n';
        //cout << "dp[" << i << "] = " << dp[i] << '\n';

    }

    return convertToAns(dp);
}

Compilation message

peru.cpp:138:9: warning: "/*" within comment [-Wcomment]
  138 |         /*
      |
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1620 KB Output is correct
2 Correct 1 ms 1720 KB Output is correct
3 Correct 1 ms 1620 KB Output is correct
4 Correct 1 ms 1620 KB Output is correct
5 Correct 1 ms 1600 KB Output is correct
6 Correct 1 ms 1620 KB Output is correct
7 Correct 1 ms 1620 KB Output is correct
8 Correct 1 ms 1724 KB Output is correct
9 Correct 1 ms 1724 KB Output is correct
10 Correct 2 ms 1604 KB Output is correct
11 Correct 1 ms 1620 KB Output is correct
12 Correct 1 ms 1620 KB Output is correct
13 Correct 1 ms 1620 KB Output is correct
14 Correct 1 ms 1620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1620 KB Output is correct
2 Correct 1 ms 1720 KB Output is correct
3 Correct 1 ms 1620 KB Output is correct
4 Correct 1 ms 1620 KB Output is correct
5 Correct 1 ms 1600 KB Output is correct
6 Correct 1 ms 1620 KB Output is correct
7 Correct 1 ms 1620 KB Output is correct
8 Correct 1 ms 1724 KB Output is correct
9 Correct 1 ms 1724 KB Output is correct
10 Correct 2 ms 1604 KB Output is correct
11 Correct 1 ms 1620 KB Output is correct
12 Correct 1 ms 1620 KB Output is correct
13 Correct 1 ms 1620 KB Output is correct
14 Correct 1 ms 1620 KB Output is correct
15 Runtime error 114 ms 262144 KB Execution killed with signal 9
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 114 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -