52782023-04-25 09:44:50sztomiTornyokcpp11Time limit exceeded 42/100500ms32408 KiB
#include <bits/stdc++.h>

using namespace std;

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

int magas[MAXN+1];
int indexek[MAXN];

int megoldas[MAXN+1];

int max_level_tav[MAXN]{0};
int szulo[MAXN];

int fenwick[MAXN+1];

void update(int ind, int val){
    while(ind < MAXN){
        fenwick[ind] += val;
        ind += ind & -ind;
    }
}

int query(int r){
    int ret = 0;
    while(r){
        ret += fenwick[r];
        r -= r & -r;
    }
    return ret;
}

int query(int l, int r){
    return query(r) - query(l-1);
}

int keres_szulo(int l, int r){
    if(l == r){
        return l;
    }
    int mid = (l+r) / 2;
    int ret = -1;
    if(query(mid+1, r) > 0){
        ret = keres_szulo(mid+1, r);
    }
    else if(query(l, mid) > 0){
        ret = keres_szulo(l, mid);
    }

    return ret;
}

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

    int n, k;
    cin >> n >> k;
    for(int i = 0; i < n; i++){
        cin >> magas[i];
        indexek[i] = i;
    }

    sort(indexek, indexek + n, [&magas](const int& a, const int &b){
                return magas[a] < magas[b];
         });

    // nala nagyobbak indexei
    for(int i = n-1; i >= 0; --i){
        // elso nala kisebb index kell
        // 1 indexeles a fenwick fa miatt
        int sz = keres_szulo(1, indexek[i]) - 1;
        if(sz != -1){
            szulo[i] = sz;
        }
        else{
            szulo[i] = -1;
        }
        update(indexek[i]+1, 1);
    }

    megoldas[0] = 0;
    for(int i = 1; i <= n; i++){
        if(max_level_tav[indexek[i-1]] == 0){
            max_level_tav[indexek[i-1]] = 1;
        }
        if(szulo[i-1] != -1){
            max_level_tav[szulo[i-1]] = max(max_level_tav[szulo[i-1]], max_level_tav[indexek[i-1]] + 1);
        }
        megoldas[i] = max(megoldas[i-1], max_level_tav[indexek[i-1]]);
    }

    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
base42/100
1Accepted0/03ms2300 KiB
2Time limit exceeded0/0500ms12296 KiB
3Accepted2/23ms2552 KiB
4Accepted2/23ms2784 KiB
5Accepted6/63ms2836 KiB
6Accepted6/63ms2836 KiB
7Accepted4/437ms5596 KiB
8Accepted4/467ms7964 KiB
9Accepted8/8231ms21868 KiB
10Time limit exceeded0/8432ms31248 KiB
11Time limit exceeded0/5467ms12736 KiB
12Time limit exceeded0/5479ms13676 KiB
13Accepted5/5134ms12192 KiB
14Accepted5/5298ms22728 KiB
15Time limit exceeded0/5444ms32408 KiB
16Time limit exceeded0/5462ms12352 KiB
17Time limit exceeded0/5442ms13096 KiB
18Time limit exceeded0/5460ms14280 KiB
19Time limit exceeded0/5460ms14640 KiB
20Time limit exceeded0/5444ms15536 KiB
21Time limit exceeded0/5442ms15552 KiB
22Time limit exceeded0/5462ms15912 KiB