16442022-12-01 11:39:381478Leggyorsabb pénzkeresés (50)cpp11Accepted 50/5037ms5608 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<vector<ll>> vv64;
typedef vector<vector<p64>> vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i, e) for (ll i = 0; i < e; i++)
#define forsn(i, s, e) for (ll i = s; i < e; i++)
#define rforn(i, s) for (ll i = s; i >= 0; i--)
#define rforsn(i, s, e) for (ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())

const int nMax = 100000;
vector<int> prefixSum(nMax + 1);

int binarySearch(int i, int j, int val)
{
    /// prefixSum[i] should be <= val
    int m;
    while (i <= j)
    {
        m = i + (j - i) / 2;
        if (prefixSum[m] == val)
            return m;
        else if (prefixSum[m] > val)
        {
            j = m - 1;
        }
        else
        {
            i = m + 1;
        }
    }
    return j;
}

int main()
{
    //ifstream cin("be.txt");

    int n;
    cin >> n;

    int p;
    cin >> p;

    for (int i = 1; i <= n; i++)
    {
        int a;
        cin >> a;
        prefixSum[i] = prefixSum[i - 1] + a;

        //cout<<prefixSum[i]<<" ";
    }


    int ans = nMax;
    for (int i = n; i >= 1; i--)
    {

        int val = prefixSum[i] - p;
        if (val >= 0)
        {
            //cout<<i<<" ";
            int j = binarySearch(0, i - 1, val);
            //cout<<i<<" "<<j<<ln;
            ans = min(ans, i - j);
        }
    }
    //cout<<ln;
    cout << ans;

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms2388 KiB
2Accepted0/035ms2556 KiB
3Accepted2/22ms2632 KiB
4Accepted2/22ms2760 KiB
5Accepted2/23ms2964 KiB
6Accepted2/26ms3168 KiB
7Accepted2/26ms3244 KiB
8Accepted2/26ms3240 KiB
9Accepted2/26ms3516 KiB
10Accepted2/26ms3512 KiB
11Accepted2/234ms3740 KiB
12Accepted2/235ms3936 KiB
13Accepted2/235ms4140 KiB
14Accepted2/235ms4176 KiB
15Accepted2/235ms4400 KiB
16Accepted2/235ms4632 KiB
17Accepted2/235ms4468 KiB
18Accepted2/235ms4576 KiB
19Accepted2/235ms5068 KiB
20Accepted2/235ms5020 KiB
21Accepted2/235ms5124 KiB
22Accepted2/235ms5120 KiB
23Accepted2/235ms5096 KiB
24Accepted2/235ms5144 KiB
25Accepted2/237ms5368 KiB
26Accepted2/235ms5608 KiB
27Accepted2/235ms5372 KiB