16442022-12-01 11:39:381478Leggyorsabb pénzkeresés (50)cpp11Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/03ms2388 KiB
2Elfogadva0/035ms2556 KiB
3Elfogadva2/22ms2632 KiB
4Elfogadva2/22ms2760 KiB
5Elfogadva2/23ms2964 KiB
6Elfogadva2/26ms3168 KiB
7Elfogadva2/26ms3244 KiB
8Elfogadva2/26ms3240 KiB
9Elfogadva2/26ms3516 KiB
10Elfogadva2/26ms3512 KiB
11Elfogadva2/234ms3740 KiB
12Elfogadva2/235ms3936 KiB
13Elfogadva2/235ms4140 KiB
14Elfogadva2/235ms4176 KiB
15Elfogadva2/235ms4400 KiB
16Elfogadva2/235ms4632 KiB
17Elfogadva2/235ms4468 KiB
18Elfogadva2/235ms4576 KiB
19Elfogadva2/235ms5068 KiB
20Elfogadva2/235ms5020 KiB
21Elfogadva2/235ms5124 KiB
22Elfogadva2/235ms5120 KiB
23Elfogadva2/235ms5096 KiB
24Elfogadva2/235ms5144 KiB
25Elfogadva2/237ms5368 KiB
26Elfogadva2/235ms5608 KiB
27Elfogadva2/235ms5372 KiB