Codeforces Round #645 (Div. 2) E. Are You Fired?

Levian works as an accountant in a large company. Levian knows how much the company has earned in each of the n consecutive months — in the i-th month the company had income equal to ai (positive income means profit, negative income means loss, zero income means no change). Because of the general self-isolation, the first ⌈n/2⌉ months income might have been completely unstable, but then everything stabilized and for the last ⌊n/2⌋ months the income was the same.

Levian decided to tell the directors n−k+1 numbers — the total income of the company for each k consecutive months. In other words, for each i between 1 and n−k+1 he will say the value ai+ai+1+…+ai+k−1. For example, if a=[−1,0,1,2,2] and k=3 he will say the numbers 0,3,5.

Unfortunately, if at least one total income reported by Levian is not a profit (income ≤0), the directors will get angry and fire the failed accountant.

Save Levian’s career: find any such k, that for each k months in a row the company had made a profit, or report that it is impossible.

Input
The first line contains a single integer n (2≤n≤5⋅105) — the number of months for which Levian must account.

The second line contains ⌈n2⌉ integers a1,a2,…,a⌈n2⌉, where ai (−109≤ai≤109) — the income of the company in the i-th month.

Third line contains a single integer x (−109≤x≤109) — income in every month from ⌈n/2⌉+1 to n.

Output
In a single line, print the appropriate integer k or −1, if it does not exist.

If there are multiple possible answers, you can print any.

Examples
input
3
2 -1
2
output
2
input
5
2 2 -8
2
output
-1
input
6
-2 -2 6
-1
output
4
代码如下`。

这个题,读了好久,才理解题意,英文水平实在是有限,
因为他的最后n/2个是相同的,所以,这就让这个题只需要考虑单方向的就可以了
从后面开始考虑,保留最大值的情况下,判断他的变化会不会小于0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=5e5+5;
ll d[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    int a1=n/2+n%2;
    for(int i=1;i<=a1;i++)
    {
        scanf("%lld",&d[i]);
    }
    ll x;
    scanf("%lld",&x);
    ll now=(ll)(n-a1)*x;
    ll aa=0;
    int u;
    for(int i=a1;i>=1;i--)
    {
        now+=d[i];
        if(aa>0)
        {
            aa=aa-x+d[i];
        }
        if(aa<=0)
        {
            aa=0;
        }
        if(now>aa)
        {
            aa=now;
            u=i;
        }

    }
    if(aa) printf("%d\n",n-u+1);
    else puts("-1");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值