답안 #892435

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
892435 2023-12-25T10:42:03 Z MinaRagy06 Rabbit Carrot (LMIO19_triusis) C++17
100 / 100
206 ms 8788 KB
#include <bits/stdc++.h>
#pragma GCC optimize("trapv")
using namespace std;

const int N = 200'005;
struct bit {
    int tree[N], n;
    void init(int _n) {
        n = _n;
        assert(n < N);
        for (int i = 0; i < n; i++) {
            tree[i] = 2e9;
        }
    }
    void upd(int x, int v) {
        assert(x < n);
        for (int i = x; i < n; i = (i | (i + 1))) {
            tree[i] = min(tree[i], v);
        }
    }
    int query(int r) {
        assert(r < n);
        int ans = 2e9;
        if (r < 0) {
            return ans;
        }
        for (int i = r; i >= 0; i = (i & (i + 1)) - 1) {
            ans = min(ans, tree[i]);
        }
        return ans;
    }
} bit1, bit2;
int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    int n, m;
    cin >> n >> m;
    int a[n + 1];
    a[0] = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    n++;
    vector<int> vals1, vals2, v1, v2;
    for (int i = 0; i < n - 1; i++) {
        vals1.push_back(a[i + 1]);
        vals2.push_back(a[i + 1] - i * m);
    }
    sort(vals1.begin(), vals1.end());
    sort(vals2.begin(), vals2.end());
    for (int i = 0; i < vals1.size(); i++) {
        if (i == 0 || vals1[i] != vals1[i - 1]) {
            v1.push_back(vals1[i]);
        }
    }
    for (int i = 0; i < vals2.size(); i++) {
        if (i == 0 || vals2[i] != vals2[i - 1]) {
            v2.push_back(vals2[i]);
        }
    }
    int dp[n + 1];
    dp[n - 1] = dp[n] = 0;
    bit1.init(v1.size());
    bit2.init(v2.size());
    for (int i = n - 2; i >= 0; i--) {
        dp[i] = dp[n] + n - 1;
        
        int idx = upper_bound(v1.begin(), v1.end(), a[i] + 2 * m) - v1.begin() - 1;
        dp[i] = min(dp[i], bit1.query(idx));
        idx = lower_bound(v1.begin(), v1.end(), a[i + 1]) - v1.begin();
        bit1.upd(idx, dp[i + 1] + i);
        
        idx = lower_bound(v2.begin(), v2.end(), a[i + 1] - i * m) - v2.begin();
        bit2.upd(idx, dp[i + 1] + i);
        idx = upper_bound(v2.begin(), v2.end(), a[i] - i * m + m) - v2.begin() - 1;
        dp[i] = min(dp[i], bit2.query(idx));
        // for (int j = i + 1; j < n - 1; j++) {
        //     if (a[j + 1] <= a[i] + 2 * m) {
        //         dp[i] = min(dp[i], dp[j + 1] + j);
        //     }
        // }
        // for (int j = i; j < n - 1; j++) {
        //     if (a[j + 1] - j * m <= a[i] - i * m + m) {
        //         dp[i] = min(dp[i], dp[j + 1] + j);
        //     }
        // }
        dp[i] -= i;
        if (a[i] >= a[i + 1] || a[i + 1] - a[i] <= m) {
            dp[i] = min(dp[i], dp[i + 1]);
        }
    }
    cout << dp[0] << '\n';
    return 0;
}

Compilation message

triusis.cpp: In function 'int main()':
triusis.cpp:50:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |     for (int i = 0; i < vals1.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
triusis.cpp:55:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |     for (int i = 0; i < vals2.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 2 ms 604 KB Output is correct
22 Correct 4 ms 604 KB Output is correct
23 Correct 3 ms 600 KB Output is correct
24 Correct 3 ms 604 KB Output is correct
25 Correct 3 ms 604 KB Output is correct
26 Correct 3 ms 604 KB Output is correct
27 Correct 2 ms 604 KB Output is correct
28 Correct 2 ms 604 KB Output is correct
29 Correct 2 ms 604 KB Output is correct
30 Correct 3 ms 604 KB Output is correct
31 Correct 2 ms 756 KB Output is correct
32 Correct 3 ms 604 KB Output is correct
33 Correct 3 ms 672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 1 ms 348 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 4 ms 604 KB Output is correct
7 Correct 3 ms 600 KB Output is correct
8 Correct 3 ms 604 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 3 ms 604 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 3 ms 604 KB Output is correct
15 Correct 2 ms 756 KB Output is correct
16 Correct 3 ms 604 KB Output is correct
17 Correct 3 ms 672 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 4 ms 604 KB Output is correct
35 Correct 4 ms 604 KB Output is correct
36 Correct 4 ms 604 KB Output is correct
37 Correct 3 ms 604 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 604 KB Output is correct
42 Correct 3 ms 604 KB Output is correct
43 Correct 3 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 604 KB Output is correct
2 Correct 4 ms 604 KB Output is correct
3 Correct 4 ms 604 KB Output is correct
4 Correct 3 ms 604 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 3 ms 604 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 4 ms 604 KB Output is correct
17 Correct 3 ms 600 KB Output is correct
18 Correct 3 ms 604 KB Output is correct
19 Correct 3 ms 604 KB Output is correct
20 Correct 3 ms 604 KB Output is correct
21 Correct 2 ms 604 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 3 ms 604 KB Output is correct
25 Correct 2 ms 756 KB Output is correct
26 Correct 3 ms 604 KB Output is correct
27 Correct 3 ms 672 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 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 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 344 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 68 ms 5292 KB Output is correct
45 Correct 206 ms 8780 KB Output is correct
46 Correct 190 ms 8788 KB Output is correct
47 Correct 185 ms 8784 KB Output is correct
48 Correct 103 ms 6496 KB Output is correct
49 Correct 122 ms 7856 KB Output is correct
50 Correct 76 ms 7156 KB Output is correct
51 Correct 71 ms 7240 KB Output is correct
52 Correct 95 ms 6336 KB Output is correct
53 Correct 72 ms 6640 KB Output is correct
54 Correct 95 ms 6500 KB Output is correct
55 Correct 90 ms 6324 KB Output is correct