제출 #1349805

#제출 시각아이디문제언어결과실행 시간메모리
1349805bentonewGlobal Warming (CEOI18_glo)C++20
0 / 100
60 ms12244 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
const ll INF = numeric_limits<ll>::max();
const int inf = numeric_limits<int>::max();
const char nl = '\n', sp = ' ';

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ll n, x;
    cin >> n >> x;
    vector<ll> arr(n,0);
    for(int i = 0; i < n; i++){
        cin >> arr[i];
    }

    vector<ll> LIS(n,0); //longest IS form [O,i]
    vector<ll> LDS(n,0);
    vector<ll> OPT(n,INF); // optimal of length n
    vector<ll> BACKOPT(n,-INF);

    for(int i = 0; i < n; i++){
        ll value = arr[i];
        auto it = lower_bound(OPT.begin(), OPT.end(), value);
        int index = it - OPT.begin() ;
        OPT[index] = value;
        LIS[i] = index+1;

        //longest increasing from 0 to i, that has the minimum ending element as established already
    }

    for(int i = n-1; i >= 0; i--){
        //find longest decreasing sequence
        ll value = arr[i];
        auto it = lower_bound(BACKOPT.begin(), BACKOPT.end(), value, greater<ll>());
        //find first element LEQ, we want to replace this
        int index = it - BACKOPT.begin() ;
        BACKOPT[index] = value;
       
        
        LDS[i] = index+1; //longest with lowest last element

        
    }
    ll maxi = -1;
    for(int i = 0; i < n; i++){
        maxi = max(maxi,LIS[i]);
    }

    //BACKOPT[i] contains the LDS with the highest most element
    for(int i = 0; i < n; i++){
        cout << OPT[i] << sp;
    }
    cout << nl;

    OPT.clear();
    OPT.resize(n,INF); 
    for(int i = 0; i < n; i++){
        ll val = arr[i];
        
        ll lo = val-x+1;
        ll hi = val+x-1;

        // return 0;
        auto loit = lower_bound(OPT.begin(),OPT.end(),lo);
        auto hiit = lower_bound(OPT.begin(),OPT.end(),hi);

        int L = loit-OPT.begin();
        int R = hiit-OPT.begin();

        if(L <= R){
            maxi = max(maxi, LDS[i] + R);   
        }

        ll value = arr[i];
        
        auto it = lower_bound(OPT.begin(), OPT.end(), value);
        int index = it - OPT.begin() ;
        OPT[index] = value;
        
        

        
        
    }
    /*
    lets start with LIS ending at 0
    notice an LIS that ends later but with a lower or equal value, will have a higher value ending

    */
    /*
    From 0 to n-1:
    LIS[i] + max(LDS[j]) for all j > i and llabs(arr[j] - arr[i]) < x
    */
    

        //binarysearch from arr[i] - x and arr[i] + x
        // RM

    cout << maxi << endl;



}



#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...