Submission #872595

# Submission time Handle Problem Language Result Execution time Memory
872595 2023-11-13T12:25:20 Z sleepntsheep Peru (RMI20_peru) C++17
49 / 100
600 ms 41292 KB
#include "peru.h"
#include <cassert>
#include <tuple>
#include <utility>
#include <iostream>
#include <algorithm>
#include <stack>

#if 0
template <typename T, const T& (*F)(const T&, const T&)>
struct stack
{
    std::stack<T> a, b;

    void push(const T &v)
    {
        b.push(size() ? F(b.top(), v) : v);
        a.push(v);
    }

    bool empty()
    {
        return a.empty();
    }

    size_t size()
    {
        return a.size();
    }

    T top()
    {
        return a.top();
    }

    T op()
    {
        return b.top();
    }

    void pop()
    {
        a.pop(); b.pop();
    }

    void swap(stack &x) {a.swap(x.a);b.swap(x.b);}
};

template <typename T, const T& (*F)(const T&, const T&)>
struct deque
{
    stack<T, F> f, b, t;

    deque() { }

    void rebalance() {
        bool a = false;
        if (b.empty()) {a = true; f.swap(b);}
        int sz = b.size() / 2;
        while (sz--) {t.push(b.top()); b.pop();}
        while (!b.empty()) {f.push(b.top()); b.pop();}
        while (!t.empty()) {b.push(t.top()); t.pop();}
        if (a) f.swap(b);
    }

    void push_back(const T &v)
    {
        b.push(v);
    }

    void pop_front()
    {
        if (f.empty()) rebalance();
        f.pop();
    }

    void pop_back()
    {
        if (b.empty()) rebalance();
        b.pop();
    }

    T front()
    {
        if (f.empty()) rebalance();
        return f.top();
    }

    T back()
    {
        if (b.empty()) rebalance();
        return b.top();
    }

    T op()
    {
        if (f.empty()) return b.op();
        if (b.empty()) return f.op();
        return F(f.op(), b.op());
    }

    size_t size()
    {
        return f.size() + b.size();
    }
};
#else

template <typename T, const T& (*F)(const T&, const T&)>
struct deque :std::deque<T>
{
    T op()
    {
        T x = this->front();
        for (auto y : *this) x = F(x, y);
        return x;
    }
};
#endif

using i64 = long long;
using u64 = unsigned long long;

int solve(int n, int k, int* v)
{
    i64 *dp = new i64[n];

    std::deque<int> dq;
    deque<i64, std::min> qry;

    for (int i = 0; i < k; ++i)
    {
        while (dq.size() && v[dq.back()] < v[i])
        {
            dq.pop_back();
            if (dq.size()) qry.pop_back();
        }
        if (dq.size()) qry.push_back(dp[dq.back()] + v[i]);
        dq.push_back(i);
        dp[i] = v[dq.front()];
    }

    for (int i = k; i < n; ++i)
    {
        while (dq.size() && v[dq.back()] <= v[i])
        {
            dq.pop_back();
            if (dq.size()) qry.pop_back();
        }

        if (dq.size()) qry.push_back(dp[dq.back()] + v[i]);
        dq.push_back(i);

        if (dq.size() && dq.front() + k == i)
        {
            dq.pop_front();
            if (dq.size()) qry.pop_front();
        }

        dp[i] = dp[i-k] + v[dq.front()];
        if (qry.size()) dp[i] = std::min(dp[i], qry.op());
    }

    //for (int i = 0; i < n; ++i) std::cout << dp[i] << '\n'; std::cout<<std::endl;

    constexpr const i64 M = 1000000007;
    i64 x = 0;
    for (size_t i = 0; i < n; ++i) x = (x * 23ll % M + dp[i]) % M;
    
    delete []dp;

    return int(x);
}

Compilation message

peru.cpp: In function 'int solve(int, int, int*)':
peru.cpp:168:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  168 |     for (size_t i = 0; i < n; ++i) x = (x * 23ll % M + dp[i]) % M;
      |                        ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 356 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 356 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 95 ms 9944 KB Output is correct
16 Correct 95 ms 9856 KB Output is correct
17 Correct 104 ms 9944 KB Output is correct
18 Correct 21 ms 9832 KB Output is correct
19 Correct 22 ms 9976 KB Output is correct
20 Correct 22 ms 9816 KB Output is correct
21 Correct 134 ms 9812 KB Output is correct
22 Correct 345 ms 9832 KB Output is correct
23 Correct 394 ms 10168 KB Output is correct
24 Correct 239 ms 9816 KB Output is correct
25 Correct 236 ms 10216 KB Output is correct
26 Correct 82 ms 9808 KB Output is correct
27 Correct 100 ms 9900 KB Output is correct
28 Correct 122 ms 9972 KB Output is correct
29 Correct 128 ms 9836 KB Output is correct
30 Correct 137 ms 9952 KB Output is correct
31 Correct 96 ms 9820 KB Output is correct
32 Correct 179 ms 9880 KB Output is correct
33 Correct 84 ms 9832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 9944 KB Output is correct
2 Correct 95 ms 9856 KB Output is correct
3 Correct 104 ms 9944 KB Output is correct
4 Correct 21 ms 9832 KB Output is correct
5 Correct 22 ms 9976 KB Output is correct
6 Correct 22 ms 9816 KB Output is correct
7 Correct 134 ms 9812 KB Output is correct
8 Correct 345 ms 9832 KB Output is correct
9 Correct 394 ms 10168 KB Output is correct
10 Correct 239 ms 9816 KB Output is correct
11 Correct 236 ms 10216 KB Output is correct
12 Correct 82 ms 9808 KB Output is correct
13 Correct 100 ms 9900 KB Output is correct
14 Correct 122 ms 9972 KB Output is correct
15 Correct 128 ms 9836 KB Output is correct
16 Correct 137 ms 9952 KB Output is correct
17 Correct 96 ms 9820 KB Output is correct
18 Correct 179 ms 9880 KB Output is correct
19 Correct 84 ms 9832 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 344 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 356 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Execution timed out 1010 ms 41292 KB Time limit exceeded
35 Halted 0 ms 0 KB -