Submission #1223037

#TimeUsernameProblemLanguageResultExecution timeMemory
1223037khoile08Global Warming (CEOI18_glo)C++20
100 / 100
201 ms19300 KiB
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i = a; i <= b; i++)
#define FOD(i,a,b) for(int i = a; i >= b; i--)
#define int long long
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ull unsigned long long
#define db double
#define lcm(a,b) a / __gcd(a, b) * b
#define ii pair<int,int>
#define iii pair<int,pair<int,int>>
#define iv pair<pair<int,int>,pair<int,int>>
#define sq(a) (a) * (a)
#define MASK(i) (1LL << i)
#define task "task"

const int inf = 1e9;
const ll INF = 1e18;
const int mod = 1e9 + 7;
const int N = 2e5 + 5;

int n, x;
int a[N];
vector<int> val;

int get_id(int v) {
    return lower_bound(val.begin(), val.end(), v) - val.begin() + 1;
}

struct Bit {
    int bit[3 * N];

    void up(int x, int val) {
        for(; x <= 3 * n; x += x & -x) bit[x] = max(bit[x], val);
    }

    int get(int x) {
        if(x == 0) return 0;
        int ret = 0;
        for(; x; x -= x & -x) ret = max(ret, bit[x]);
        return ret;
    }
};
Bit bit[2];
int dp[2][N];

void Solve() {
    FOR(i, 1, n) {
        val.pb(a[i] - 1);
        val.pb(a[i] + x - 1);
        val.pb(a[i]);
    }
    sort(val.begin(), val.end());
    val.erase(unique(val.begin(), val.end()), val.end());
    FOR(i, 1, n) {
        dp[0][i] = bit[0].get(get_id(a[i] - 1)) + 1;
        dp[1][i] = max(bit[0].get(get_id(a[i] + x - 1)), bit[1].get(get_id(a[i] - 1))) + 1;
        bit[0].up(get_id(a[i]), dp[0][i]);
        bit[1].up(get_id(a[i]), dp[1][i]);
    }
    int ans = 0;
    FOR(i, 1, n) ans = max({ans, dp[0][i], dp[1][i]});
    cout << ans << '\n';
}

signed main() {
    if(fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int test = 1;
//    cin >> test;
    while(test--) {
        cin >> n >> x;
        FOR(i, 1, n) cin >> a[i];
        Solve();
    }
}

Compilation message (stderr)

glo.cpp: In function 'int main()':
glo.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
glo.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...