Submission #1096974

# Submission time Handle Problem Language Result Execution time Memory
1096974 2024-10-05T15:30:07 Z ducdev Global Warming (CEOI18_glo) C++14
38 / 100
2000 ms 7764 KB
// Author: 4uckd3v - Nguyen Cao Duc
#include <bits/stdc++.h>
using namespace std;
 
#define all(x) (x).begin(), (x).end()
 
typedef long long ll;
 
const int MAX_N = 1e6;
const int MOD = 1e9 + 7;
 
int n;
ll a[MAX_N + 5], x;
 
struct FenwickTree {
    vector<int> bit;
    int n;
 
    void update(int pos, int val) {
        for (; pos <= n; pos += pos & (-pos))
            bit[pos] = max(bit[pos], val);
    };
 
    int get(int pos) {
        int ret = 0;
        for (; pos > 0; pos -= pos & (-pos))
            ret = max(ret, bit[pos]);
        return ret;
    };
 
    FenwickTree(int n) : n(n) {
        bit.assign(n + 5, 0);
    };
};
 
namespace SUBTASK_1 {
    const int N = MAX_N;
 
    int dp[N + 5][4];
 
    void Solve() {
        int res = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < 4; j++) dp[i][j] = 1;
            for (int j = 1; j < i; j++) {
                if (a[i] + x > a[j]) {
                    dp[i][1] = max(dp[i][1], dp[j][0] + 1);
                };
                if (a[i] - x > a[j]) {
                    dp[i][2] = max(dp[i][2], dp[j][0] + 1);
                };
                if (a[i] > a[j]) {
                    dp[i][0] = max(dp[i][0], dp[j][0] + 1);
                    dp[i][1] = max(dp[i][1], dp[j][1] + 1);
                    dp[i][2] = max(dp[i][2], dp[j][2] + 1);
                };
                if (a[i] > a[j] + x) {
                    dp[i][3] = max(dp[i][3], dp[j][1] + 1);
                };
                if (a[i] > a[j] - x) {
                    dp[i][4] = max(dp[i][4], dp[j][2] + 1);
                };
            }
            res = max({res, dp[i][0], dp[i][1], dp[i][2], dp[i][3], dp[i][4]});
        }
 
        cout << res << '\n';
    };
};  // namespace SUBTASK_1
 
namespace SUBTASK_2 {
    const int N = MAX_N;
 
    ll c[N + 5];
 
    void Solve() {
        vector<ll> v;
        for (int i = 1; i <= n; i++) c[i] = a[i];
        for (int i = 1; i <= n; i++) v.push_back(c[i]);
 
        sort(all(v));
        v.resize(unique(all(v)) - v.begin());
 
        for (int i = 1; i <= n; i++) {
            c[i] = lower_bound(all(v), c[i]) - v.begin() + 1;
        };
 
        FenwickTree bit(n);
        int res = 0;
        for (int i = 1; i <= n; i++) {
            int dp = bit.get(c[i] - 1);
            bit.update(c[i], dp + 1);
            res = max(res, dp + 1);
        }
 
        cout << res << '\n';
    };
};  // namespace SUBTASK_2
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if (fopen("MAIN.INP", "r")) {
        freopen("MAIN.INP", "r", stdin);
        freopen("MAIN.OUT", "w", stdout);
    };
 
    cin >> n >> x;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    };
 
    if (x == 0)
        return SUBTASK_2::Solve(), 0;
 
return SUBTASK_1::Solve(), 0;
};

Compilation message

glo.cpp: In function 'int main()':
glo.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |         freopen("MAIN.INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
glo.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |         freopen("MAIN.OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
glo.cpp: In function 'void SUBTASK_1::Solve()':
glo.cpp:61:43: warning: array subscript 4 is above array bounds of 'int [4]' [-Warray-bounds]
   61 |                     dp[i][4] = max(dp[i][4], dp[j][2] + 1);
      |                                    ~~~~~~~^
glo.cpp:61:28: warning: array subscript 4 is above array bounds of 'int [4]' [-Warray-bounds]
   61 |                     dp[i][4] = max(dp[i][4], dp[j][2] + 1);
      |                     ~~~~~~~^
glo.cpp:64:76: warning: array subscript 4 is above array bounds of 'int [4]' [-Warray-bounds]
   64 |             res = max({res, dp[i][0], dp[i][1], dp[i][2], dp[i][3], dp[i][4]});
      |                                                                     ~~~~~~~^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 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
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 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 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 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 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 3 ms 344 KB Output is correct
20 Correct 2 ms 348 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 2 ms 344 KB Output is correct
24 Correct 4 ms 344 KB Output is correct
25 Correct 2 ms 344 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 7764 KB Output is correct
2 Correct 68 ms 7636 KB Output is correct
3 Correct 61 ms 7752 KB Output is correct
4 Correct 65 ms 7732 KB Output is correct
5 Correct 33 ms 7124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2062 ms 1872 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2035 ms 2536 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 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 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 3 ms 344 KB Output is correct
20 Correct 2 ms 348 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 2 ms 344 KB Output is correct
24 Correct 4 ms 344 KB Output is correct
25 Correct 2 ms 344 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 63 ms 7764 KB Output is correct
28 Correct 68 ms 7636 KB Output is correct
29 Correct 61 ms 7752 KB Output is correct
30 Correct 65 ms 7732 KB Output is correct
31 Correct 33 ms 7124 KB Output is correct
32 Execution timed out 2062 ms 1872 KB Time limit exceeded
33 Halted 0 ms 0 KB -