Submission #872597

# Submission time Handle Problem Language Result Execution time Memory
872597 2023-11-13T12:28:36 Z sleepntsheep Peru (RMI20_peru) C++17
100 / 100
154 ms 56728 KB
#include "peru.h"
#include <utility>
#include <cassert>
#include <tuple>
#include <utility>
#include <iostream>
#include <algorithm>
#include <stack>

#if 0
struct minstack {
	stack<pair<int, int>> st;
	int getmin() {return st.top().second;}
	bool empty() {return st.empty();}
	int size() {return st.size();}
	void push(int x) {
		int mn = x;
		if (!empty()) mn = min(mn, getmin());
		st.push({x, mn});
	}
	void pop() {st.pop();}
	int top() {return st.top().first;}
	void swap(minstack &x) {st.swap(x.st);}
};

struct mindeque {
	minstack l, r, t;
	void rebalance() {
		bool f = false;
		if (r.empty()) {f = true; l.swap(r);}
		int sz = r.size() / 2;
		while (sz--) {t.push(r.top()); r.pop();}
		while (!r.empty()) {l.push(r.top()); r.pop();}
		while (!t.empty()) {r.push(t.top()); t.pop();}
		if (f) l.swap(r);
	}
	int getmin() {
		if (l.empty()) return r.getmin();
		if (r.empty()) return l.getmin();
		return min(l.getmin(), r.getmin());
	}
	bool empty() {return l.empty() && r.empty();}
	int size() {return l.size() + r.size();}
	void push_front(int x) {l.push(x);}
	void push_back(int x) {r.push(x);}
	void pop_front() {if (l.empty()) rebalance(); l.pop();}
	void pop_back() {if (r.empty()) rebalance(); r.pop();}
	int front() {if (l.empty()) rebalance(); return l.top();}
	int back() {if (r.empty()) rebalance(); return r.top();}
	void swap(mindeque &x) {l.swap(x.l); r.swap(x.r);}
};
#endif

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

    void push(const T &v)
    {
        if (a.empty()) a.push({v, v});
        else a.push({v, F(a.top().second, v)});
    }

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

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

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

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

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

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

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:213:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  213 |     for (size_t i = 0; i < n; ++i) x = (x * 23ll % M + dp[i]) % M;
      |                        ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 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 348 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 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 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 348 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 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 19 ms 5720 KB Output is correct
16 Correct 20 ms 5724 KB Output is correct
17 Correct 23 ms 5752 KB Output is correct
18 Correct 21 ms 5720 KB Output is correct
19 Correct 20 ms 5724 KB Output is correct
20 Correct 20 ms 5868 KB Output is correct
21 Correct 23 ms 5864 KB Output is correct
22 Correct 22 ms 6232 KB Output is correct
23 Correct 26 ms 5976 KB Output is correct
24 Correct 23 ms 5720 KB Output is correct
25 Correct 23 ms 5724 KB Output is correct
26 Correct 21 ms 5728 KB Output is correct
27 Correct 19 ms 5724 KB Output is correct
28 Correct 20 ms 5984 KB Output is correct
29 Correct 17 ms 5976 KB Output is correct
30 Correct 19 ms 5720 KB Output is correct
31 Correct 23 ms 5724 KB Output is correct
32 Correct 19 ms 5900 KB Output is correct
33 Correct 23 ms 5724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 5720 KB Output is correct
2 Correct 20 ms 5724 KB Output is correct
3 Correct 23 ms 5752 KB Output is correct
4 Correct 21 ms 5720 KB Output is correct
5 Correct 20 ms 5724 KB Output is correct
6 Correct 20 ms 5868 KB Output is correct
7 Correct 23 ms 5864 KB Output is correct
8 Correct 22 ms 6232 KB Output is correct
9 Correct 26 ms 5976 KB Output is correct
10 Correct 23 ms 5720 KB Output is correct
11 Correct 23 ms 5724 KB Output is correct
12 Correct 21 ms 5728 KB Output is correct
13 Correct 19 ms 5724 KB Output is correct
14 Correct 20 ms 5984 KB Output is correct
15 Correct 17 ms 5976 KB Output is correct
16 Correct 19 ms 5720 KB Output is correct
17 Correct 23 ms 5724 KB Output is correct
18 Correct 19 ms 5900 KB Output is correct
19 Correct 23 ms 5724 KB Output is correct
20 Correct 1 ms 344 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 348 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 0 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 122 ms 29860 KB Output is correct
35 Correct 115 ms 53600 KB Output is correct
36 Correct 115 ms 53356 KB Output is correct
37 Correct 127 ms 53356 KB Output is correct
38 Correct 123 ms 53472 KB Output is correct
39 Correct 121 ms 53604 KB Output is correct
40 Correct 133 ms 53500 KB Output is correct
41 Correct 145 ms 53376 KB Output is correct
42 Correct 133 ms 53532 KB Output is correct
43 Correct 48 ms 21980 KB Output is correct
44 Correct 81 ms 34072 KB Output is correct
45 Correct 83 ms 33828 KB Output is correct
46 Correct 81 ms 33760 KB Output is correct
47 Correct 127 ms 55528 KB Output is correct
48 Correct 129 ms 55524 KB Output is correct
49 Correct 122 ms 55828 KB Output is correct
50 Correct 114 ms 56276 KB Output is correct
51 Correct 120 ms 56048 KB Output is correct
52 Correct 114 ms 56728 KB Output is correct
53 Correct 119 ms 56700 KB Output is correct
54 Correct 142 ms 53332 KB Output is correct
55 Correct 114 ms 53464 KB Output is correct
56 Correct 111 ms 53476 KB Output is correct
57 Correct 118 ms 53516 KB Output is correct
58 Correct 124 ms 53508 KB Output is correct
59 Correct 117 ms 53256 KB Output is correct
60 Correct 125 ms 53332 KB Output is correct
61 Correct 154 ms 55684 KB Output is correct
62 Correct 137 ms 55632 KB Output is correct
63 Correct 144 ms 55632 KB Output is correct