Submission #395839

#TimeUsernameProblemLanguageResultExecution timeMemory
395839Qw3rTyGlobal Warming (CEOI18_glo)C++11
100 / 100
60 ms5132 KiB
#include <bits/stdc++.h>
#define pb push_back
using namespace std;

const int maxN = 2e5+5;

int a[maxN];
int f[maxN]; //length of LIS that ends on a[i]
int g[maxN]; //Length of longest decreasing subsequence that ends at a[i]
int N,d;

bool cmp(const int &a, const int &b){return a > b;}

void testIO(){
    freopen("../test.in","r",stdin);
    return;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    //testIO();
    cin >> N >> d;
    for(int i = 1; i <= N; ++i)cin >> a[i];
    a[0] = -1;
    //Calculate f
    f[0] = 0;
    vector<int> dp;
    for(int i = 1; i <= N; ++i){
        int pos = lower_bound(dp.begin(),dp.end(),a[i]) - dp.begin();
        if(pos == dp.size())
            dp.pb(a[i]);
        else dp[pos] = a[i];
        f[i] = pos + 1;
    }
    //Calculate g
    g[N+1] = 0;
    dp.clear();
    for(int i = N; i >= 1; --i){
        int pos1 = lower_bound(dp.begin(),dp.end(),a[i] - d, cmp) - dp.begin();
        int pos2 = lower_bound(dp.begin(),dp.end(),a[i],cmp) - dp.begin();
        if(pos2 == dp.size())
            dp.pb(a[i]);
        else dp[pos2] = a[i];
        g[i] = pos1+1;
    }
//    for(int i = N; i >= 1; --i){
//        int pos = lower_bound(dp.begin(),dp.end(),a[i],cmp) - dp.begin();
//        if(pos == dp.size())
//            dp.pb(a[i]);
//        else dp[pos] = a[i];
//        g[i] = pos + 1;
//    }
    //Calculate answer
    int res = f[N]; //Decrease all elements by d is the same as not changing at all = f[N] (basic LIS)
    for(int i = 1; i <= N; ++i){
        res = max(res, f[i] + g[i] - 1);
    }
    cout << res << '\n';
    return 0;
}

Compilation message (stderr)

glo.cpp: In function 'int main()':
glo.cpp:31:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |         if(pos == dp.size())
      |            ~~~~^~~~~~~~~~~~
glo.cpp:42:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |         if(pos2 == dp.size())
      |            ~~~~~^~~~~~~~~~~~
glo.cpp: In function 'void testIO()':
glo.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   15 |     freopen("../test.in","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#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...