Submission #787591

# Submission time Handle Problem Language Result Execution time Memory
787591 2023-07-19T10:00:09 Z RecursiveCo Global Warming (CEOI18_glo) C++14
100 / 100
1578 ms 75860 KB
// CF template, version 3.0

#include <bits/stdc++.h>

using namespace std;

#define improvePerformance ios_base::sync_with_stdio(false); cin.tie(0)
#define getTest int t; cin >> t
#define eachTest for (int _var=0;_var<t;_var++)
#define get(name) int (name); cin >> (name)
#define out(o) cout << (o)
#define getList(cnt, name) vector<int> (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }
#define sortl(name) sort((name).begin(), (name).end())
#define rev(name) reverse((name).begin(), (name).end())
#define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
#define decision(b) if (b){out("YES");}else{out("NO");}

#define int long long

int m;
void upd(vector<int>& tree, int i, int x, int l=0, int r=m-1, int v=1) {
    if (l == r) tree[v] = max(tree[v], x);
    else {
        int middle = (l + r) / 2;
        if (middle >= i) upd(tree, i, x, l, middle, 2 * v);
        else upd(tree, i, x, middle + 1, r, 2 * v + 1);
        tree[v] = max(tree[2 * v], tree[2 * v + 1]);
    }
}

int query(vector<int>& tree, int ql, int qr, int l=0, int r=m-1, int v=1) {
    if (ql <= l && r <= qr) return tree[v];
    if (qr < l || r < ql) return 0;
    int middle = (l + r) / 2;
    return max(query(tree, ql, qr, l, middle, 2 * v), query(tree, ql, qr, middle + 1, r, 2 * v + 1));
}

signed main() {
    //improvePerformance;
    //getTest;

    //eachTest {
        get(n);
        get(x);
        getList(n, nums);
        vector<int> sorted;
        for (int num: nums) sorted.push_back(num);
        sortl(sorted);
        map<int, int> coord;
        m = 0;
        forto(n, i) {
            if (coord.find(sorted[i]) == coord.end()) {
                coord[sorted[i]] = m++;
            }
        }
        coord[1e18] = m;
        // assuming x is small enough...
        vector<vector<vector<int>>> dp(n, vector<vector<int>>(3, vector<int>(2, -1)));
        vector<int> segtree0(4 * m, 0);
        vector<int> segtree1(4 * m, 0);
        vector<int> segtree2(4 * m, 0);
        // dp[i][s][d] = LIS in the first i elements if index i is before, in or outside the interval (s=0, 1, or 2 resp.) and we increase by d. LIS must end at index i.
        forto(2, d) {
            int dd;
            if (d == 0) dd = 0;
            else dd = 2 * x;
            forto(4 * m, i) {
                segtree0[i] = 0;
                segtree1[i] = 0;
                segtree2[i] = 0;
            }
            // calculate the DP values when delta = d.
            int D = dd - x;
            forto(n, i) {
                forto(3, s) {
                    if (s == 0) {
                        int co = coord[nums[i]];
                        if (co == 0) dp[i][s][d] = 1;
                        else {
                            dp[i][s][d] = query(segtree0, 0, co - 1) + 1;
                        }
                    } else if (s == 1) {
                        auto it = lower_bound(sorted.begin(), sorted.end(), nums[i] + D);
                        int el = it == sorted.end()? 1e18: *it;
                        int co = coord[el];
                        int cur;
                        if (co == 0) cur = 1;
                        else {
                            cur = query(segtree0, 0, co - 1) + 1;
                        }
                        int curco = coord[nums[i]];
                        int second;
                        if (curco == 0) second = 1;
                        else {
                            second = query(segtree1, 0, curco - 1) + 1;
                        }
                        dp[i][s][d] = max(cur, second);
                    } else {
                        auto it = lower_bound(sorted.begin(), sorted.end(), nums[i] - D);
                        int el = it == sorted.end()? 1e18: *it;
                        int co = coord[el];
                        int cur;
                        if (co == 0) cur = 1;
                        else {
                            cur = query(segtree1, 0, co - 1) + 1;
                        }
                        int curco = coord[nums[i]];
                        int second;
                        if (curco == 0) second = 1;
                        else {
                            second = query(segtree2, 0, curco - 1) + 1;
                        }
                        dp[i][s][d] = max(cur, second);
                    }
                }
                upd(segtree0, coord[nums[i]], dp[i][0][d]);
                upd(segtree1, coord[nums[i]], dp[i][1][d]);
                upd(segtree2, coord[nums[i]], dp[i][2][d]);
            }
        }
        int ans = 0;
        forto(n, i) forto(3, s) forto(2, d) ans = max(ans, dp[i][s][d]);
        out(ans);
    //}
}

Compilation message

glo.cpp: In function 'int main()':
glo.cpp:10:23: warning: unnecessary parentheses in declaration of 'n' [-Wparentheses]
   10 | #define get(name) int (name); cin >> (name)
      |                       ^
glo.cpp:43:9: note: in expansion of macro 'get'
   43 |         get(n);
      |         ^~~
glo.cpp:10:23: warning: unnecessary parentheses in declaration of 'x' [-Wparentheses]
   10 | #define get(name) int (name); cin >> (name)
      |                       ^
glo.cpp:44:9: note: in expansion of macro 'get'
   44 |         get(x);
      |         ^~~
glo.cpp:12:40: warning: unnecessary parentheses in declaration of 'nums' [-Wparentheses]
   12 | #define getList(cnt, name) vector<int> (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }
      |                                        ^
glo.cpp:45:9: note: in expansion of macro 'getList'
   45 |         getList(n, nums);
      |         ^~~~~~~
glo.cpp:10:23: warning: unnecessary parentheses in declaration of 'a' [-Wparentheses]
   10 | #define get(name) int (name); cin >> (name)
      |                       ^
glo.cpp:12:76: note: in expansion of macro 'get'
   12 | #define getList(cnt, name) vector<int> (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }
      |                                                                            ^~~
glo.cpp:45:9: note: in expansion of macro 'getList'
   45 |         getList(n, nums);
      |         ^~~~~~~
glo.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
glo.cpp:51:9: note: in expansion of macro 'forto'
   51 |         forto(n, i) {
      |         ^~~~~
glo.cpp:15:35: warning: unnecessary parentheses in declaration of 'd' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
glo.cpp:63:9: note: in expansion of macro 'forto'
   63 |         forto(2, d) {
      |         ^~~~~
glo.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
glo.cpp:67:13: note: in expansion of macro 'forto'
   67 |             forto(4 * m, i) {
      |             ^~~~~
glo.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
glo.cpp:74:13: note: in expansion of macro 'forto'
   74 |             forto(n, i) {
      |             ^~~~~
glo.cpp:15:35: warning: unnecessary parentheses in declaration of 's' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
glo.cpp:75:17: note: in expansion of macro 'forto'
   75 |                 forto(3, s) {
      |                 ^~~~~
glo.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
glo.cpp:122:9: note: in expansion of macro 'forto'
  122 |         forto(n, i) forto(3, s) forto(2, d) ans = max(ans, dp[i][s][d]);
      |         ^~~~~
glo.cpp:15:35: warning: unnecessary parentheses in declaration of 's' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
glo.cpp:122:21: note: in expansion of macro 'forto'
  122 |         forto(n, i) forto(3, s) forto(2, d) ans = max(ans, dp[i][s][d]);
      |                     ^~~~~
glo.cpp:15:35: warning: unnecessary parentheses in declaration of 'd' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
glo.cpp:122:33: note: in expansion of macro 'forto'
  122 |         forto(n, i) forto(3, s) forto(2, d) ans = max(ans, dp[i][s][d]);
      |                                 ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 300 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 300 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 300 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 4 ms 596 KB Output is correct
20 Correct 3 ms 564 KB Output is correct
21 Correct 3 ms 596 KB Output is correct
22 Correct 4 ms 596 KB Output is correct
23 Correct 2 ms 500 KB Output is correct
24 Correct 2 ms 596 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 3 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1554 ms 74152 KB Output is correct
2 Correct 1497 ms 74084 KB Output is correct
3 Correct 1517 ms 74144 KB Output is correct
4 Correct 1520 ms 74148 KB Output is correct
5 Correct 622 ms 58616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 270 ms 18952 KB Output is correct
2 Correct 272 ms 18992 KB Output is correct
3 Correct 265 ms 18948 KB Output is correct
4 Correct 135 ms 14980 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 156 ms 15040 KB Output is correct
7 Correct 228 ms 18956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 529 ms 37980 KB Output is correct
2 Correct 510 ms 38148 KB Output is correct
3 Correct 1344 ms 75808 KB Output is correct
4 Correct 575 ms 59388 KB Output is correct
5 Correct 355 ms 37572 KB Output is correct
6 Correct 669 ms 71408 KB Output is correct
7 Correct 675 ms 72072 KB Output is correct
8 Correct 416 ms 38072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 300 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 4 ms 596 KB Output is correct
20 Correct 3 ms 564 KB Output is correct
21 Correct 3 ms 596 KB Output is correct
22 Correct 4 ms 596 KB Output is correct
23 Correct 2 ms 500 KB Output is correct
24 Correct 2 ms 596 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 3 ms 596 KB Output is correct
27 Correct 1554 ms 74152 KB Output is correct
28 Correct 1497 ms 74084 KB Output is correct
29 Correct 1517 ms 74144 KB Output is correct
30 Correct 1520 ms 74148 KB Output is correct
31 Correct 622 ms 58616 KB Output is correct
32 Correct 270 ms 18952 KB Output is correct
33 Correct 272 ms 18992 KB Output is correct
34 Correct 265 ms 18948 KB Output is correct
35 Correct 135 ms 14980 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 156 ms 15040 KB Output is correct
38 Correct 228 ms 18956 KB Output is correct
39 Correct 529 ms 37980 KB Output is correct
40 Correct 510 ms 38148 KB Output is correct
41 Correct 1344 ms 75808 KB Output is correct
42 Correct 575 ms 59388 KB Output is correct
43 Correct 355 ms 37572 KB Output is correct
44 Correct 669 ms 71408 KB Output is correct
45 Correct 675 ms 72072 KB Output is correct
46 Correct 416 ms 38072 KB Output is correct
47 Correct 633 ms 38060 KB Output is correct
48 Correct 615 ms 38068 KB Output is correct
49 Correct 1578 ms 75808 KB Output is correct
50 Correct 627 ms 59388 KB Output is correct
51 Correct 565 ms 48576 KB Output is correct
52 Correct 867 ms 75148 KB Output is correct
53 Correct 692 ms 75052 KB Output is correct
54 Correct 711 ms 75860 KB Output is correct
55 Correct 1059 ms 75728 KB Output is correct