8732022-01-21 22:06:19Valaki2Ládapakolás raktárban (50)cpp14Accepted 50/5028ms11292 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second

int n, k;
vector<int> v;

set<int> s;
int max_pos;
int sum;
int cnt;

void new_segment(int i) {
    sum = v[i];
    max_pos = i;
    s.clear();
    s.insert(v[i]);
    cnt++;
    // cout << i << " ";
}

void same_segment(int i) {
    s.insert(v[i]);
    if(v[i] > v[max_pos]) {
        max_pos = i;
    }
}

void solve() {
    cin >> n >> k;
    v.assign(1 + n, 0);
    for(int i = 1; i <= n; i++) {
        cin >> v[i];
    }
    sum = k;
    for(int i = 1; i <= n; i++) {
        sum += v[i];
        if(s.count(v[i])) {
            new_segment(i);
            continue;
        }
        if(sum > k) {
            new_segment(i);
            continue;
        }
        if(v[i] > v[max_pos] && max_pos == i - 1) {
            same_segment(i);
            continue;
        }
        if(v[i] < v[i - 1]) {
            same_segment(i);
            continue;
        }
        new_segment(i);
    }
    cout << n - cnt << "\n";
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}

/*
12 16
1 3 5 4 2 6 8 7 6 5 3 4
*/
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/02ms1816 KiB
2Accepted0/03ms2100 KiB
3Accepted2/21ms1968 KiB
4Accepted2/21ms1972 KiB
5Accepted2/21ms1972 KiB
6Accepted2/21ms1980 KiB
7Accepted2/21ms1980 KiB
8Accepted2/22ms1984 KiB
9Accepted3/31ms1992 KiB
10Accepted2/21ms1992 KiB
11Accepted2/21ms2004 KiB
12Accepted2/22ms2032 KiB
13Accepted3/32ms2064 KiB
14Accepted3/33ms2244 KiB
15Accepted3/33ms2292 KiB
16Accepted2/216ms3876 KiB
17Accepted3/317ms4552 KiB
18Accepted3/318ms5468 KiB
19Accepted3/316ms6044 KiB
20Accepted3/328ms9744 KiB
21Accepted3/324ms10156 KiB
22Accepted3/325ms11292 KiB