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;
    }
    
  • 相关阅读:
    Oracle 启动关闭
    GridView 自动序号
    提高数据库处理查询速度
    Linux目录结构
    GridView 全选
    Linux 窗口启动和命令行启动设置
    Oracle 备份
    二进制部署K8S集群(三)前置准备工作之准备签发证书环境
    nginx的location详解
    二进制部署K8S集群(七)Master节点之kubeapiserver集群部署
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12113500.html
Copyright © 2011-2022 走看看