zoukankan      html  css  js  c++  java
  • 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<bits/stdc++.h>
    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;
    }
    
  • 相关阅读:
    tomcat 堆内存设置
    Java日历类(GregorianCalendar和Calendar)的简单例子
    oracle中的greatest 函数和 least函数
    极光推送
    oracle的start with connect by prior如何使用
    Eclipse菜单栏中Tomcat 插件的配置
    svn取消关联
    nagios检测http
    python升级
    fabric
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12113500.html
Copyright © 2011-2022 走看看