Educational Codeforces Round 79 (Rated for Div. 2) B. Verse For Santa

链接:

https://codeforces.com/contest/1279/problem/B

题意:

New Year is coming! Vasya has prepared a New Year's verse and wants to recite it in front of Santa Claus.

Vasya's verse contains n parts. It takes ai seconds to recite the i-th part. Vasya can't change the order of parts in the verse: firstly he recites the part which takes a1 seconds, secondly — the part which takes a2 seconds, and so on. After reciting the verse, Vasya will get the number of presents equal to the number of parts he fully recited.

Vasya can skip at most one part of the verse while reciting it (if he skips more than one part, then Santa will definitely notice it).

Santa will listen to Vasya's verse for no more than s seconds. For example, if s=10, a=[100,9,1,1], and Vasya skips the first part of verse, then he gets two presents.

Note that it is possible to recite the whole verse (if there is enough time).

Determine which part Vasya needs to skip to obtain the maximum possible number of gifts. If Vasya shouldn't skip anything, print 0. If there are multiple answers, print any of them.

You have to process t test cases.

思路:

贪心取一个最大值,检测能不能往后读,
一个都读不了也是-1

代码:

#include
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;

LL a[MAXN];
LL s;
int n;

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        cin >> n >> s;
        LL sum = 0;
        for (int i = 1;i <= n;i++)
            cin >> a[i], sum += a[i];
        if (sum <= s)
        {
            puts("0");
            continue;
        }
        int p = 0, ans = 0;
        sum = 0;
        for (int i = 1;i <= n && sum-a[p] <= s;i++)
        {
            if (a[i] > a[p])
                p = i;
            sum += a[i];
            if (sum-a[p] <= s)
                ans = p;
        }
        if (ans == 0)
            puts("-1");
        else
            cout << ans << endl;
    }

    return 0;
}

你可能感兴趣的:(Educational Codeforces Round 79 (Rated for Div. 2) B. Verse For Santa)