CF 1611F - ATM and Students

F. ATM and Students

Polycarp started working at a bank. He was assigned to monitor the ATM. The ATM initially contains s rubles.
A queue of n students lined up to him. Each student wants to either withdraw a certain amount of money or deposit it into an account. If a i a_i ai is positive, then the student credits that amount of money via ATM. Otherwise, the student withdraws ∣ a i ∣ |a_i| ai rubles.
In the beginning, the ATM is turned off and an arbitrary number of students are not served. At some point, Polycarp turns on the ATM, which has an initial amount of s rubles. Then, the remaining students start queueing at the ATM. If at some point in time there is less money in the ATM than the student wants to withdraw, then the student is not served and Polycarp turns off the ATM and does not turn it on anymore.
More formally, the students that are served are forming a contiguous subsequence.
Polycarp wants the ATM to serve the maximum number of students. Help him in this matter. Print the numbers of the first and last student, or determine that he will not be able to serve anyone.
In other words, find such a longest continuous segment of students that, starting with the sum of s s s at the ATM, all these students will be served. ATM serves students consistently (i.e. one after another in the order of the queue).
 
Input
The first line of the input contains one integer t ( 1 ≤ t ≤ 1 0 4 ) t (1≤t≤10^4) t(1t104) — the number of test cases.
Each test case consists of two lines. The first one contains integers n n n and s s s ( 1 ≤ n ≤ 2 ⋅ 1 0 5 ; 0 ≤ s ≤ 1 0 9 ) (1≤n≤2⋅10^5; 0≤s≤10^9) (1n2105;0s109) — the length of the a array and the initial amount of rubles in the ATM. The second contains n integers a 1 a_1 a1, a 2 a_2 a2,…, a n a_n an ( − 1 0 9 ≤ a i ≤ 1 0 9 ) (−10^9≤a_i≤10^9) (109ai109) — elements of the a array. Note that a i a_i ai can be zero.
It is guaranteed that the sum of the values n n n over all test cases does not exceed 2 ⋅ 1 0 5 2⋅10^5 2105.
Output
Print t lines, each line must contain the answer to the corresponding set of input data: if the answer exists, print the numbers of the first and last served student. If there is no solution, then print -1 on the line.
If there are several possible answers, print any.
 
Example:
input:

3
4 10
-16 2 -6 8
3 1000
-100000 -100000 -100000
6 0
2 6 -164 1 -1 -6543

output:

2 4
-1
1 2

Note:
In the first test case, the only correct answer is 2 4, since when serving students, the number of rubles at the ATM does not become negative, and this is the maximum number of students that can be served.
In the second test case, the answer is -1, as there is not enough money for any student at the ATM.
In the third test case, the answer can be either 1 2 or 4 5.

解题思路: 利用双指针跑,左右指针同一出发点,如果当前连续子列和加上右指针指向的数能够 ≥ − s ≥-s s 的,右指针就往后移,直到不满足条件。就这样找到当前符合要求的最右的指针,判断当前右指针和左指针的差是否大于记录的左右指针差(我一开始都为0),如果大于就更新记录的左右指针。然后将连续子列和减去左指针指向的数,并往后移。直到左或右指针跑完就算结束。
(如果右指针未移动,左指针也会后移,再次进入循环后,右指针就能移动了。因为右指针不符合的条件,左指针后移后再进入循环,就能够抵消)

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main()
{
    int t; cin>>t;
    while(t--)
    {
        ll n, s; cin>>n>>s;
		vector<ll> a(n+1);
		for(int i=1; i<=n; i++) cin>>a[i];
        int l=0, r=0, i=1, j=1;
        ll sum = 0;
        while(i<=n && j<=n)
        {
        	while(sum+a[j]+s>=0 && j<=n) sum += a[j++];
        	if(j-i > r-l)
        	{
        		l = i;
        		r = j;
			}
			sum -= a[i++];
		}
		if(l && r)
			cout<<l<<' '<<r-1<<'\n';
		else cout<<"-1\n";
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花生ono

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值