Submission #1339737

#TimeUsernameProblemLanguageResultExecution timeMemory
1339737ghassanhasanGlobal Warming (CEOI18_glo)C++20
100 / 100
147 ms10312 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define ld long double
#define all(v) v.begin(), v.end()


vector<int> LIS(const vector<int>& nums) {
    map<int, int> m;
    vector<int> ret;
    for (int x : nums) {
        auto it = m.lower_bound(x);
        int len = 1;
        if (it != m.begin()) {
            len = prev(it)->second + 1;
        }
        m[x] = len;
        ret.push_back(len);
        auto next_it = next(m.find(x));
        while (next_it != m.end() && next_it->second <= len) {
            next_it = m.erase(next_it);
        }
    }
    return ret;
}

void doing() {
    int n, x; cin >> n >> x;
    vector<int> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    auto lft = LIS(a);
    // for(auto it: lft) cout << it << ' '; cout << '\n';
    map<int, int, greater<>> m;
    int ans = 0;
    for(int i = n - 1; i >= 0; i--){
        auto it = m.lower_bound(a[i]);
        int r = 0;
        if(it != m.begin()) r = prev(it)->second;
        ans = max(ans, lft[i] + r);
        // cout << i << " : " << lft[i] << ' ' << r << '\n';
        it = m.lower_bound(a[i] + x);
        int len = 1;
        if(it != m.begin()) len = prev(it)->second + 1;
        m[a[i] + x] = len;
        // cout << i << " : " << len << endl;
        auto nxt = next(m.find(a[i] + x));
        while(nxt != m.end() && nxt->second <= len) nxt = m.erase(nxt);
    }
    cout << ans << endl;
}

signed main()
{
    ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#ifdef GH
    freopen("IN.txt", "r", stdin);
    cout << string(60, '*') << endl;
#else
    if (const string file = ""; !file.empty()) {
        freopen((file + ".in").c_str(), "r", stdin);
        freopen((file + ".out").c_str(), "w", stdout);
    }
#endif
    int tc = 1;
    // cin >> tc;
    while (tc--)
        doing();
    return 0;
}

Compilation message (stderr)

glo.cpp: In function 'int main()':
glo.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         freopen((file + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
glo.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |         freopen((file + ".out").c_str(), "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...