176052025-08-09 14:33:24BucsMateLádapakolás raktárban (50)cpp17Accepted 50/5052ms3076 KiB
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main()
{
    int N, M;
    cin >> N >> M;
    vector<int> heights(N+1);

    for(int i = 1; i <= N; ++i){
        cin >> heights[i];
    }

    set<int> currTower;
    currTower.insert(heights[1]);
    int currHeight = heights[1];
    bool increasing = true;
    int nrTowers = 1;

    for(int i = 2; i <= N; i++){
        if(currTower.find(heights[i]) != currTower.end() || currHeight + heights[i] > M){
            nrTowers++;
            currTower.clear();
            currTower.insert(heights[i]);
            currHeight = heights[i];
            increasing = true;
        }
        else if((heights[i-1] < heights[i]) == increasing){
            currTower.insert(heights[i]);
            currHeight += heights[i];
        }
        else if(increasing){
            currTower.insert(heights[i]);
            currHeight += heights[i];
            increasing = false;
        }
        else{
            nrTowers++;
            currTower.clear();
            currTower.insert(heights[i]);
            currHeight = heights[i];
            increasing = true;
        }
    }
    cout << N - nrTowers;
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/04ms316 KiB
3Accepted2/21ms508 KiB
4Accepted2/21ms316 KiB
5Accepted2/21ms316 KiB
6Accepted2/21ms592 KiB
7Accepted2/21ms556 KiB
8Accepted2/21ms408 KiB
9Accepted3/31ms316 KiB
10Accepted2/21ms316 KiB
11Accepted2/21ms316 KiB
12Accepted2/22ms316 KiB
13Accepted3/33ms316 KiB
14Accepted3/34ms316 KiB
15Accepted3/34ms316 KiB
16Accepted2/230ms1176 KiB
17Accepted3/341ms1332 KiB
18Accepted3/350ms1556 KiB
19Accepted3/335ms1364 KiB
20Accepted3/352ms2936 KiB
21Accepted3/345ms2868 KiB
22Accepted3/348ms3076 KiB