165962025-05-06 20:25:07algoproTornyokcpp17Runtime error 57/100467ms160000 KiB
// UUID: 479e9775-5b1a-4242-82d7-9dbb3d0d687b
#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';

    // Felszabadítás
    for (int i = 0; i <= n; i++) {
        delete[] nbrs[i];
    }

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base57/100
1Accepted0/01ms512 KiB
2Accepted0/0393ms103476 KiB
3Accepted2/21ms316 KiB
4Accepted2/21ms756 KiB
5Accepted6/61ms564 KiB
6Accepted6/61ms564 KiB
7Accepted4/419ms7408 KiB
8Accepted4/437ms12708 KiB
9Runtime error0/8101ms47368 KiB
10Accepted8/8231ms88500 KiB
11Time limit exceeded0/5402ms102520 KiB
12Time limit exceeded0/5412ms121908 KiB
13Accepted5/568ms22400 KiB
14Accepted5/5165ms57008 KiB
15Accepted5/5250ms89216 KiB
16Accepted5/5324ms85096 KiB
17Accepted5/5361ms103476 KiB
18Time limit exceeded0/5446ms123192 KiB
19Time limit exceeded0/5423ms123188 KiB
20Time limit exceeded0/5467ms157944 KiB
21Time limit exceeded0/5411ms158008 KiB
22Time limit exceeded0/5455ms160000 KiB