165982025-05-06 20:26:41algoproTornyokcpp17Runtime error 77/100418ms160000 KiB
// UUID: 104a9792-505c-4808-b318-4d928aec6ea9
#pragma GCC optimize("O3,no-stack-protector")
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1000005;
const int MAXK = 100005;

int n, k;

// Épületmagasságok és tornyok magassága
int h[MAXN];          // 0-tĂłl n-1-ig, h[n] lesz INT_MAX
int towerH[MAXK];     // K darab toronymagasság
int numLess[MAXN];    // DP tömb
long long sortedArr[MAXN + MAXK]; // magasság és index együtt

// Szomszédsági lista: minden épülethez max N szomszédot tárolunk külön tömbben
int* nbrs[MAXN];      // minden elem egy külön dinamikus tömb
int nbrSize[MAXN];    // hány elem van egy adott lista esetén

// max méret minden listához – legrosszabb esetben n darab szomszéd is lehet
const int MAX_NEIGHBORS = 20;

int calcLess(int Indx) {
    if (numLess[Indx] >= 0) return numLess[Indx];
    int ans = 0;
    for (int i = 0; i < nbrSize[Indx]; i++) {
        ans = max(ans, calcLess(nbrs[Indx][i]) + 1);
    }
    return numLess[Indx] = ans;
}

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

    cin >> n >> k;

    for (int i = 0; i < n; i++) {
        cin >> h[i];
        sortedArr[i] = (((long long)h[i]) << 32) | i;
    }

    for (int i = 0; i < k; i++) {
        cin >> towerH[i];
        sortedArr[n + i] = (((long long)towerH[i]) << 32) | (n + i);
    }

    h[n] = INT_MAX; // sentinel az utolsĂł helyen

    sort(sortedArr, sortedArr + n + k);

    // Inicializáljuk a szomszédlistákat
    for (int i = 0; i <= n; i++) {
        nbrs[i] = new int[MAX_NEIGHBORS];
        nbrSize[i] = 0;
    }

    stack<int> stc;
    stc.push(n);

    for (int i = 0; i < n; i++) {
        while (h[stc.top()] < h[i]) stc.pop();
        int top = stc.top();
        nbrs[top][nbrSize[top]++] = i;
        stc.push(i);
    }

    for (int i = 0; i <= n; i++) numLess[i] = -1;

    calcLess(n);

    int bestSoFar = 0;
    int* ans = towerH;

    for (int i = 0; i < n + k; i++) {
        int idx = sortedArr[i] & 0xffffffff;
        if (idx >= n) {
            ans[idx - n] = bestSoFar + 1;
        } else {
            bestSoFar = max(bestSoFar, numLess[idx]);
        }
    }

    for (int i = 0; i < k; i++) {
        cout << ans[i] << ' ';
    }
    cout << '\n';


    return 0;
}
SubtaskSumTestVerdictTimeMemory
base77/100
1Accepted0/01ms316 KiB
2Accepted0/0323ms103476 KiB
3Accepted2/21ms316 KiB
4Accepted2/21ms568 KiB
5Accepted6/61ms564 KiB
6Accepted6/61ms564 KiB
7Accepted4/417ms7240 KiB
8Accepted4/432ms12800 KiB
9Runtime error0/8105ms47412 KiB
10Accepted8/8204ms88460 KiB
11Accepted5/5361ms102452 KiB
12Accepted5/5381ms121908 KiB
13Accepted5/564ms22400 KiB
14Accepted5/5162ms56884 KiB
15Accepted5/5197ms89140 KiB
16Accepted5/5287ms85044 KiB
17Accepted5/5347ms103572 KiB
18Accepted5/5384ms123188 KiB
19Accepted5/5391ms123188 KiB
20Time limit exceeded0/5418ms157748 KiB
21Time limit exceeded0/5404ms158004 KiB
22Time limit exceeded0/5414ms160000 KiB