52862023-04-25 12:19:19sztomiTornyokcpp11Runtime error 95/100273ms63628 KiB
#include<bits/stdc++.h>

using namespace std;

const int MAXN = 1e6;
const int MAXK = 1e5;
const int INF = 1e9+7;

int n, k;
int magas[MAXN+1];
int indexek[MAXN];

bool aktiv[MAXN]{false};
int max_hossz[MAXN];

int megoldas[MAXN];
int elso[MAXN];
int utolso[MAXN];

int unio(int bal, int kozep, int jobb){
    bool a = (bal >= 0 && aktiv[bal]);
    bool b = (jobb < n && aktiv[jobb]);
    int ret = 1;
    if(a && b){ // XOX
        bal = elso[bal];
        jobb = elso[jobb];
        ret = max(max_hossz[bal], max_hossz[jobb] + 1);

        max_hossz[bal] = ret;
        elso[utolso[jobb]] = bal;
        utolso[bal] = utolso[jobb];
    }
    else if(a){ // XO_
        bal = elso[bal];
        ret = max_hossz[bal];

        max_hossz[bal] = ret;
        elso[kozep] = bal;
        utolso[bal] = kozep;
    }
    else if(b){ // _OX
        jobb = elso[jobb];
        ret = max_hossz[jobb] + 1;

        max_hossz[kozep] = ret;
        elso[utolso[jobb]] = kozep;
        utolso[kozep] = utolso[jobb];
    }
    return ret;
}

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

    cin >> n >> k;
    for(int i = 0; i < n; i++){
        cin >> magas[i];
        indexek[i] = i;
        max_hossz[i] = 1;
        elso[i] = utolso[i] = i;
    }
    sort(indexek, indexek+n, [&magas](const int &a, const int & b){
            return magas[a] < magas[b];
         });

    if(n == 1){
        int kerdes;
        for(int i = 0; i < k; i++){
            cin >> kerdes;
            cout << (magas[0] < kerdes) << " ";
        }
        return 0;
    }

    vector<int> rel_magas(n);
    for(int i = 0; i < n; i++){
        rel_magas[indexek[i]] = i;
    }

    megoldas[0] = 0;
    megoldas[1] = 1;
    int ind;
    aktiv[indexek[0]] = true;
    int mo = 1;
    for(int i = 2; i <= n; i++){
        int prev_ind = indexek[i-1]; // hova lett lerakva az elotte levo

        aktiv[prev_ind] = true;
        mo = max(mo, unio(prev_ind-1, prev_ind, prev_ind+1));

        /*
        cout << "---------------------------\n";
        cout << "i: " << i << " mo: " << mo << "\n";
        for(int j = 0; j < n; j++){
            cout << (aktiv[j] ? "X" : " ") << "\t";
        }
        cout << "\n";
        for(int j = 0; j < n; j++){
            cout << rel_magas[j] << "\t";
        }
        cout << "\n";
        for(int j = 0; j < n; j++){
            cout << elso[j] << "\t";
        }
        cout << "\n";
        for(int j = 0; j < n; j++){
            cout << utolso[j] << "\t";
        }
        cout << "\n";
        for(int j = 0; j < n; j++){
            cout << max_hossz[elso[j]] << "\t";
        }
        cout << "\n";
        cout << "----------------------------\n";
        */

        megoldas[i] = mo;
    }

/*
    for(int i = 0; i <= n; i++){
        cout << i << ": " << megoldas[i] << "\n";
    }
*/


    vector<int> kerdesek(k);
    vector<int> kerdes_ind(k);
    for(int i = 0; i < k; i++){
        cin >> kerdesek[i];
        kerdes_ind[i] = i;
    }
    sort(kerdes_ind.begin(), kerdes_ind.end(), [&kerdesek](const int &a, const int &b){
            return kerdesek[a] < kerdesek[b];
         });

    vector<int> valasz(k);

    magas[n] = INF;
    int temp_ind = 0;
    for(int i = 0; i <= n; i++){
        while(temp_ind < k && kerdesek[kerdes_ind[temp_ind]] <= magas[indexek[i]]){
            valasz[kerdes_ind[temp_ind]] = megoldas[i];
            temp_ind++;
        }
    }

    for(int i = 0; i < k; i++){
        cout << valasz[i] << " ";
    }
    cout << "\n";
}
SubtaskSumTestVerdictTimeMemory
base95/100
1Accepted0/03ms1984 KiB
2Accepted0/0228ms51180 KiB
3Accepted2/23ms2440 KiB
4Accepted2/23ms2680 KiB
5Accepted6/63ms2888 KiB
6Accepted6/63ms3056 KiB
7Accepted4/414ms6116 KiB
8Accepted4/419ms8864 KiB
9Accepted8/870ms26196 KiB
10Accepted8/8114ms37412 KiB
11Accepted5/5207ms50920 KiB
12Accepted5/5252ms60052 KiB
13Accepted5/550ms14088 KiB
14Accepted5/597ms26920 KiB
15Accepted5/5123ms38544 KiB
16Accepted5/5187ms44056 KiB
17Accepted5/5230ms52912 KiB
18Accepted5/5266ms62248 KiB
19Accepted5/5273ms62600 KiB
20Accepted5/5207ms62884 KiB
21Accepted5/5204ms62992 KiB
22Runtime error0/5165ms63628 KiB