답안 #655344

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
655344 2022-11-03T23:00:41 Z kingfran1907 Peru (RMI20_peru) C++14
100 / 100
171 ms 56272 KB
#include "peru.h"
#include <bits/stdc++.h>
#define X first
#define Y second

typedef long long llint;
using namespace std;

const int mod = 1e9+7;
const int maxn = 25e5+10;

struct node {
    int ind;
    int first;
    llint second;

    node(int ind, int first, llint second) {
        this->ind = ind;
        this->first = first;
        this->second = second;
    }
    pair<int, llint> p() {return {first, second};}
};

llint dp[maxn];
deque< node > s;
stack< llint > l, r;

int mul(int a, int b) {
    llint out = (llint)a * b;
    return out % mod;
}

void rebuild() {
    while (!l.empty()) l.pop();
    while (!r.empty()) r.pop();
    deque< node > c = s;
    int kol = c.size() / 2;

    stack< llint > t;
    for (int i = 0; i < kol; i++) {
        t.push(c.front().X + c.front().Y);
        c.pop_front();
    }
    while (!t.empty()) {
        if (l.empty() || l.top() >= t.top()) l.push(t.top());
        t.pop();
    }

    while (!c.empty()) {
        t.push(c.back().X + c.back().Y);
        c.pop_back();
    }
    while (!t.empty()) {
        if (r.empty() || r.top() >= t.top()) r.push(t.top());
        t.pop();
    }
}

void pop_right() {
    //printf("poppin\n");
    llint x = s.back().X + s.back().Y;
    s.pop_back();
    if (r.empty()) rebuild();
    else if (r.top() == x) r.pop();
}

void pop_left(int y) {
    node tr = s.front();
    int ind = tr.ind;
    llint x = tr.X + tr.Y;

    s.pop_front();
    if (l.empty()) rebuild();
    else if (l.top() == x) l.pop();

    if (y < ind) {
        tr.Y = dp[y + 1];
        if (l.empty() || l.top() >= tr.X + tr.Y) l.push(tr.X + tr.Y);
        s.push_front(tr);
    }
}

bool cmp(pair<int, llint> a, pair<int, llint> b) {
    if (a.X == b.X) return a.Y < b.Y;
    else return a.X > b.X;
}

void push_right(int h, llint val, int ind) {
    while (!s.empty() && cmp({h, val}, s.back().p())) {
        val = min(val, s.back().Y);
        pop_right();
    }
    s.push_back(node(ind, h, val));
    if (r.empty() || r.top() >= h + val) r.push(h + val);
}

void debug() {
    printf("deque: ");
    for (auto iter : s) printf("(%d %lld, %d) ", iter.X, iter.Y, iter.ind);
    printf("\n");
}

llint get_min() {
    llint out = 1LL << 60;
    if (!l.empty()) out = min(out, l.top());
    if (!r.empty()) out = min(out, r.top());
    return out;
}

int solve(int n, int k, int* v){
    push_right(v[0], 0, 0);
    for (int i = 1; i <= n; i++) {
        dp[i] = get_min();
        //debug();
        //printf("dp %d: %lld\n", i, dp[i]);
        if (i < n) push_right(v[i], dp[i], i);
        if (i >= k) pop_left(i - k);
    }

    int sol = 0;
    int pot = 1;
    for (int i = n; i >= 0; i--) {
        sol += mul(dp[i] % mod, pot), sol %= mod;
        pot = mul(pot, 23);
    }
    return sol;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 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 1 ms 336 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 348 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 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 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 1 ms 336 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 348 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 1 ms 340 KB Output is correct
15 Correct 25 ms 9212 KB Output is correct
16 Correct 25 ms 9244 KB Output is correct
17 Correct 30 ms 9244 KB Output is correct
18 Correct 28 ms 9140 KB Output is correct
19 Correct 29 ms 9196 KB Output is correct
20 Correct 32 ms 9128 KB Output is correct
21 Correct 26 ms 9176 KB Output is correct
22 Correct 27 ms 9292 KB Output is correct
23 Correct 28 ms 9300 KB Output is correct
24 Correct 28 ms 9172 KB Output is correct
25 Correct 27 ms 9136 KB Output is correct
26 Correct 28 ms 9152 KB Output is correct
27 Correct 29 ms 9200 KB Output is correct
28 Correct 29 ms 9232 KB Output is correct
29 Correct 24 ms 9296 KB Output is correct
30 Correct 27 ms 9236 KB Output is correct
31 Correct 30 ms 9204 KB Output is correct
32 Correct 26 ms 9200 KB Output is correct
33 Correct 30 ms 9180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 9212 KB Output is correct
2 Correct 25 ms 9244 KB Output is correct
3 Correct 30 ms 9244 KB Output is correct
4 Correct 28 ms 9140 KB Output is correct
5 Correct 29 ms 9196 KB Output is correct
6 Correct 32 ms 9128 KB Output is correct
7 Correct 26 ms 9176 KB Output is correct
8 Correct 27 ms 9292 KB Output is correct
9 Correct 28 ms 9300 KB Output is correct
10 Correct 28 ms 9172 KB Output is correct
11 Correct 27 ms 9136 KB Output is correct
12 Correct 28 ms 9152 KB Output is correct
13 Correct 29 ms 9200 KB Output is correct
14 Correct 29 ms 9232 KB Output is correct
15 Correct 24 ms 9296 KB Output is correct
16 Correct 27 ms 9236 KB Output is correct
17 Correct 30 ms 9204 KB Output is correct
18 Correct 26 ms 9200 KB Output is correct
19 Correct 30 ms 9180 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 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 1 ms 336 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 348 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 1 ms 340 KB Output is correct
34 Correct 149 ms 53328 KB Output is correct
35 Correct 148 ms 53704 KB Output is correct
36 Correct 136 ms 53580 KB Output is correct
37 Correct 149 ms 53428 KB Output is correct
38 Correct 141 ms 53596 KB Output is correct
39 Correct 141 ms 53632 KB Output is correct
40 Correct 151 ms 53704 KB Output is correct
41 Correct 160 ms 53528 KB Output is correct
42 Correct 150 ms 53628 KB Output is correct
43 Correct 55 ms 21732 KB Output is correct
44 Correct 111 ms 33272 KB Output is correct
45 Correct 98 ms 33308 KB Output is correct
46 Correct 102 ms 33356 KB Output is correct
47 Correct 147 ms 55744 KB Output is correct
48 Correct 147 ms 55616 KB Output is correct
49 Correct 158 ms 55716 KB Output is correct
50 Correct 141 ms 55932 KB Output is correct
51 Correct 149 ms 55944 KB Output is correct
52 Correct 147 ms 56272 KB Output is correct
53 Correct 136 ms 56132 KB Output is correct
54 Correct 166 ms 53436 KB Output is correct
55 Correct 131 ms 53484 KB Output is correct
56 Correct 145 ms 53360 KB Output is correct
57 Correct 134 ms 53504 KB Output is correct
58 Correct 140 ms 53528 KB Output is correct
59 Correct 136 ms 53444 KB Output is correct
60 Correct 137 ms 53532 KB Output is correct
61 Correct 155 ms 55724 KB Output is correct
62 Correct 153 ms 55848 KB Output is correct
63 Correct 171 ms 55636 KB Output is correct