답안 #854251

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
854251 2023-09-26T13:29:49 Z anhphant Global Warming (CEOI18_glo) C++14
100 / 100
103 ms 9896 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll N, X, A[500007];

void initialize() {
    ios_base :: sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    if (fopen("FILE.INP", "r")) {
        freopen("FILE.INP", "r", stdin);
        freopen("FILE.OUT", "w", stdout);
    }

    cin >> N >> X;
    for(int i = 1; i <= N; ++i) cin >> A[i];
}

namespace subtask1 {
    ll LIS(vector<ll> v) {
        //for(int id = 1; id <= N; ++id) cerr << v[id] << " "; cerr << endl;
        ll ans = 0;
        for(ll mask = 1; mask < (1 << N); ++mask) {
            //cerr << "mask = " << mask << endl;
            ll last_value = -1e12;
            bool flag = 1;
            for(ll i = 0; i < N; ++i) {
                if (!(mask & (1 << i))) continue;
                if (last_value >= v[i + 1]) {
                    flag = 0;
                    break;
                }
                last_value = v[i + 1];
            }
            if (flag) ans = max(ans, ll(__builtin_popcountll(mask)));
        }
        return ans;
    }

    void solve() {
        vector<ll> arr(N + 7, 0LL);
        for(int i = 1; i <= N; ++i) arr[i] = A[i];
        ll res = 0;

        for(ll x = -X; x <= X; ++x) {
            //cerr << "x = " << x << endl;
            for(int i = 1; i <= N; ++i) {
                vector<ll> v = arr;
                for(int j = i; j <= N; ++j) {
                    v[j] += x;
                    res = max(res, LIS(v));
                    //cerr << i << " " << j << " " << LIS(v) << endl;
                }
            }
        }

        cout << res << endl;
    }

    void process() {
        solve();
    }
}

namespace subtask2 {
    ll LIS(vector<ll> v) {
        vector<ll> bslen(N + 1, 0LL);
        for(int i = 1; i <= N; ++i) bslen[i] = 1e18;
        bslen[0] = -1e18;
        ll ans = 0;

        for(ll i = 1; i <= N; ++i) {
            ll k = lower_bound(bslen.begin(), bslen.end(), v[i]) - bslen.begin() - 1;
            ans = max(ans, k + 1);
            bslen[k + 1] = min(bslen[k + 1], v[i]);
        }

        return ans;
    }

    void solve() {
        vector<ll> arr(N + 7, 0LL);
        for(int i = 1; i <= N; ++i) arr[i] = A[i];
        ll res = 0;

        for(ll x = -X; x <= X; ++x) {
            //cerr << "x = " << x << endl;
            for(int i = 1; i <= N; ++i) {
                vector<ll> v = arr;
                for(int j = i; j <= N; ++j) {
                    v[j] += x;
                    res = max(res, LIS(v));
                    //cerr << i << " " << j << " " << LIS(v) << endl;
                }
            }
        }

        cout << res << endl;
    }

    void process() {
        solve();
    }
}

namespace subtask3 {
    ll LIS(vector<ll> v) {
        vector<ll> bslen(N + 1, 0LL);
        for(int i = 1; i <= N; ++i) bslen[i] = 1e18;
        bslen[0] = -1e18;
        ll ans = 0;

        for(ll i = 1; i <= N; ++i) {
            ll k = lower_bound(bslen.begin(), bslen.end(), v[i]) - bslen.begin() - 1;
            ans = max(ans, k + 1);
            bslen[k + 1] = min(bslen[k + 1], v[i]);
        }

        return ans;
    }

    void solve() {
        vector<ll> arr(N + 7, 0LL);
        for(int i = 1; i <= N; ++i) arr[i] = A[i];
        ll res = LIS(arr);

        for(int i = 1; i <= N; ++i) {
            arr[i] -= X;
            res = max(res, LIS(arr));

            //for(int i = 1; i <= N; ++i) cerr << arr[i] << " "; cerr << endl;
            //cerr << "i = " << i << " : " << LIS(arr) << endl;
        }

        cout << res << endl;
    }

    void process() {
        solve();
    }
}

namespace subtask4 {
    ll LIS(vector<ll> v) {
        vector<ll> bslen(N + 1, 0LL);
        for(int i = 1; i <= N; ++i) bslen[i] = 1e18;
        bslen[0] = -1e18;
        ll ans = 0;

        for(ll i = 1; i <= N; ++i) {
            ll k = lower_bound(bslen.begin(), bslen.end(), v[i]) - bslen.begin() - 1;
            ans = max(ans, k + 1);
            bslen[k + 1] = min(bslen[k + 1], v[i]);
        }

        return ans;
    }

    void solve() {
        vector<ll> arr(N + 7, 0LL);
        for(int i = 1; i <= N; ++i) arr[i] = A[i];
        cout << LIS(arr);
    }

    void process() {
        solve();
    }
}

namespace subtask5 {
    ll LIS(vector<ll> v, ll x) {
        vector<ll> type1(N + 1, 0LL), type2(N + 1, 0LL), type3(N + 1, 0LL);
        for(int i = 1; i <= N; ++i) {
            type1[i] = type2[i] = type3[i] = 1e18;
        }
        type1[0] = type2[0] = type3[0] = -1e18;

        int ans = 0;

        for(ll i = 1; i <= N; ++i) {
            int DP1 = lower_bound(type1.begin(), type1.end(), A[i]) - type1.begin() - 1;

            int DP2 = lower_bound(type2.begin(), type2.end(), A[i]) - type2.begin() - 1;
            DP2 = max(DP2, int(lower_bound(type1.begin(), type1.end(), A[i] + x) - type1.begin() - 1));

            int DP3 = lower_bound(type3.begin(), type3.end(), A[i]) - type3.begin() - 1;
            DP3 = max(DP2, int(lower_bound(type2.begin(), type2.end(), A[i] - x) - type2.begin() - 1));

            ans = max({ans, DP1 + 1, DP2 + 1, DP3 + 1});

            DP1++;
            DP2++;
            DP3++;
            type1[DP1] = min(type1[DP1], A[i]);
            type2[DP2] = min(type2[DP2], A[i]);
            type3[DP3] = min(type3[DP3], A[i]);
        }

        return ans;
    }

    void solve() {
        vector<ll> arr(N + 7, 0LL);
        for(int i = 1; i <= N; ++i) arr[i] = A[i];
        ll res = 0;

        for(ll x = -X; x <= X; ++x) {
            res = max(res, LIS(arr, x));
        }

        cout << res << endl;
    }

    void process() {
        solve();
    }
}

namespace subtask6 {
    ll preDP[200000], sufDP[200000];

    void preDP_calc() {
        vector<ll> minval(N + 7, 0LL);
        for(int i = 1; i < minval.size(); ++i) minval[i] = 1e18;
        minval[0] = -1e18;

        for(int i = 1; i <= N; ++i) {
            ll k = lower_bound(minval.begin(), minval.end(), A[i]) - minval.begin() - 1;
            preDP[i] = k + 1;
            minval[k + 1] = min(minval[k + 1], A[i]);
        }
    }

    void sufDP_calc() {
        vector<ll> maxval(N + 7, 0LL);
        for(int i = 1; i < maxval.size(); ++i) maxval[i] = -1e18;
        for(int i = N + 1; i < maxval.size(); ++i) maxval[i] = 1e18;

        for(int i = N; i > 0; i--) {
            ll k = upper_bound(maxval.begin(), maxval.end(), A[i]) - maxval.begin() - 1;
            sufDP[i] = N - k + 1;
            maxval[k] = max(maxval[k], A[i]);
        }
    }

    void solve() {
        preDP_calc();
        sufDP_calc();

        //for(int i = 0; i <= N + 1; ++i) cerr << preDP[i] << " "; cerr << endl;
        //for(int i = 0; i <= N + 1; ++i) cerr << sufDP[i] << " "; cerr << endl;

        for(int i = 1; i <= N; ++i) {
            preDP[i] = max(preDP[i - 1], preDP[i]);
        }
        for(int i = N; i >= 1; --i) {
            sufDP[i] = max(sufDP[i + 1], sufDP[i]);
        }

        ll ans = 0;
        for(int i = 0; i <= N; ++i) {
            //cerr << "i = " << i << " " << preDP[i] << " " << sufDP[i + 1] << " " << preDP[i] + sufDP[i + 1] << endl;
            ans = max(ans, preDP[i] + sufDP[i + 1]);
            //cerr << "ans = " << ans << endl;
        }

        cout << ans;
    }

    void process() {
        solve();
    }
}

namespace subtask7 {
    ll sufDP[200007];
    ll ans = 0;

    void sufDP_calc() {
        vector<ll> maxval(N + 7, 0LL);
        for(int i = 1; i < maxval.size(); ++i) maxval[i] = -1e18;
        for(int i = N + 1; i < maxval.size(); ++i) maxval[i] = 1e18;

        for(int i = N; i > 0; i--) {
            ll k = upper_bound(maxval.begin(), maxval.end(), A[i]) - maxval.begin() - 1;
            sufDP[i] = N - k + 1;
            maxval[k] = max(maxval[k], A[i]);
        }
    }

    void preDP_calc() {
        vector<ll> minval(N + 7, 0LL);
        for(int i = 1; i < minval.size(); ++i) minval[i] = 1e18;
        minval[0] = -1e18;

        for(int i = 1; i <= N; ++i) {
            ll k = lower_bound(minval.begin(), minval.end(), A[i]) - minval.begin() - 1;
            ll k2 = lower_bound(minval.begin(), minval.end(), A[i] + X) - minval.begin() - 1;
            minval[k + 1] = min(minval[k + 1], A[i]);
            ans = max(ans, k2 + sufDP[i]);
        }
    }


    void solve() {
        sufDP_calc();

        preDP_calc();

        cout << ans << endl;
    }

    void process() {
        solve();
    }
}

int main() {
    initialize();
    if (N <= 10 && X <= 10) subtask1 :: process();
    else if (N <= 50 && X <= 50) subtask2 :: process();
    else if (X == 0) subtask4 :: process();
    else if (N <= 1000) subtask3 :: process();
    else if (X == 1000000000) subtask6 :: process();
    else if (X <= 5 && N <= 50000) subtask5 :: process();
    else subtask7 :: process();
}

Compilation message

glo.cpp: In function 'void subtask6::preDP_calc()':
glo.cpp:224:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  224 |         for(int i = 1; i < minval.size(); ++i) minval[i] = 1e18;
      |                        ~~^~~~~~~~~~~~~~~
glo.cpp: In function 'void subtask6::sufDP_calc()':
glo.cpp:236:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  236 |         for(int i = 1; i < maxval.size(); ++i) maxval[i] = -1e18;
      |                        ~~^~~~~~~~~~~~~~~
glo.cpp:237:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  237 |         for(int i = N + 1; i < maxval.size(); ++i) maxval[i] = 1e18;
      |                            ~~^~~~~~~~~~~~~~~
glo.cpp: In function 'void subtask7::sufDP_calc()':
glo.cpp:281:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  281 |         for(int i = 1; i < maxval.size(); ++i) maxval[i] = -1e18;
      |                        ~~^~~~~~~~~~~~~~~
glo.cpp:282:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  282 |         for(int i = N + 1; i < maxval.size(); ++i) maxval[i] = 1e18;
      |                            ~~^~~~~~~~~~~~~~~
glo.cpp: In function 'void subtask7::preDP_calc()':
glo.cpp:293:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  293 |         for(int i = 1; i < minval.size(); ++i) minval[i] = 1e18;
      |                        ~~^~~~~~~~~~~~~~~
glo.cpp: In function 'void initialize()':
glo.cpp:11:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 |         freopen("FILE.INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
glo.cpp:12:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |         freopen("FILE.OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 2392 KB Output is correct
2 Correct 2 ms 2392 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 2 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 0 ms 2392 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 0 ms 2392 KB Output is correct
9 Correct 3 ms 2392 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 15 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 2392 KB Output is correct
2 Correct 2 ms 2392 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 2 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 0 ms 2392 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 0 ms 2392 KB Output is correct
9 Correct 3 ms 2392 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 15 ms 2396 KB Output is correct
12 Correct 22 ms 2392 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 67 ms 2392 KB Output is correct
15 Correct 28 ms 2392 KB Output is correct
16 Correct 27 ms 2396 KB Output is correct
17 Correct 8 ms 2392 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 2392 KB Output is correct
2 Correct 2 ms 2392 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 2 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 0 ms 2392 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 0 ms 2392 KB Output is correct
9 Correct 3 ms 2392 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 15 ms 2396 KB Output is correct
12 Correct 22 ms 2392 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 67 ms 2392 KB Output is correct
15 Correct 28 ms 2392 KB Output is correct
16 Correct 27 ms 2396 KB Output is correct
17 Correct 8 ms 2392 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 18 ms 2392 KB Output is correct
20 Correct 16 ms 2392 KB Output is correct
21 Correct 16 ms 2648 KB Output is correct
22 Correct 1 ms 2392 KB Output is correct
23 Correct 16 ms 2392 KB Output is correct
24 Correct 16 ms 2392 KB Output is correct
25 Correct 14 ms 2392 KB Output is correct
26 Correct 20 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 7000 KB Output is correct
2 Correct 27 ms 7000 KB Output is correct
3 Correct 27 ms 7236 KB Output is correct
4 Correct 27 ms 7000 KB Output is correct
5 Correct 20 ms 7000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 101 ms 4440 KB Output is correct
2 Correct 103 ms 4796 KB Output is correct
3 Correct 101 ms 4444 KB Output is correct
4 Correct 70 ms 4484 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 80 ms 4480 KB Output is correct
7 Correct 73 ms 4484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 7496 KB Output is correct
2 Correct 19 ms 7240 KB Output is correct
3 Correct 40 ms 7988 KB Output is correct
4 Correct 30 ms 7996 KB Output is correct
5 Correct 15 ms 7240 KB Output is correct
6 Correct 29 ms 8064 KB Output is correct
7 Correct 27 ms 8072 KB Output is correct
8 Correct 17 ms 7244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 2392 KB Output is correct
2 Correct 2 ms 2392 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 2 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 0 ms 2392 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 0 ms 2392 KB Output is correct
9 Correct 3 ms 2392 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 15 ms 2396 KB Output is correct
12 Correct 22 ms 2392 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 67 ms 2392 KB Output is correct
15 Correct 28 ms 2392 KB Output is correct
16 Correct 27 ms 2396 KB Output is correct
17 Correct 8 ms 2392 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 18 ms 2392 KB Output is correct
20 Correct 16 ms 2392 KB Output is correct
21 Correct 16 ms 2648 KB Output is correct
22 Correct 1 ms 2392 KB Output is correct
23 Correct 16 ms 2392 KB Output is correct
24 Correct 16 ms 2392 KB Output is correct
25 Correct 14 ms 2392 KB Output is correct
26 Correct 20 ms 2392 KB Output is correct
27 Correct 27 ms 7000 KB Output is correct
28 Correct 27 ms 7000 KB Output is correct
29 Correct 27 ms 7236 KB Output is correct
30 Correct 27 ms 7000 KB Output is correct
31 Correct 20 ms 7000 KB Output is correct
32 Correct 101 ms 4440 KB Output is correct
33 Correct 103 ms 4796 KB Output is correct
34 Correct 101 ms 4444 KB Output is correct
35 Correct 70 ms 4484 KB Output is correct
36 Correct 1 ms 2392 KB Output is correct
37 Correct 80 ms 4480 KB Output is correct
38 Correct 73 ms 4484 KB Output is correct
39 Correct 19 ms 7496 KB Output is correct
40 Correct 19 ms 7240 KB Output is correct
41 Correct 40 ms 7988 KB Output is correct
42 Correct 30 ms 7996 KB Output is correct
43 Correct 15 ms 7240 KB Output is correct
44 Correct 29 ms 8064 KB Output is correct
45 Correct 27 ms 8072 KB Output is correct
46 Correct 17 ms 7244 KB Output is correct
47 Correct 23 ms 5464 KB Output is correct
48 Correct 25 ms 6468 KB Output is correct
49 Correct 48 ms 8020 KB Output is correct
50 Correct 34 ms 7460 KB Output is correct
51 Correct 29 ms 6748 KB Output is correct
52 Correct 35 ms 7504 KB Output is correct
53 Correct 27 ms 9268 KB Output is correct
54 Correct 30 ms 9896 KB Output is correct
55 Correct 39 ms 8016 KB Output is correct