ATM and Students 双指针,前缀和(1800)

在这里插入图片描述
题意 :

  • 给定长度为n的序列和数字s,找出最长的区间 [ l , r ] [l,r] [l,r],使得 ∀ i ∈ [ l , r ] , p r e i − p r e l − 1 + s ≥ 0 \forall i \in [l,r], pre_i-pre_{l-1}+s \geq0 i[l,r],preiprel1+s0,其中 p r e i pre_i prei表示前缀和

思路 :

  • 考虑双指针,对某一段 [ l , r ] [l,r] [l,r]区间满足答案要求,则更新答案,且右指针移动
  • 若右指针移动后不满足条件,则不断移动左指针直到满足条件
  • 注意如何设置初始值,来应对无解的情况
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <unordered_set>
#include <math.h>
using namespace std;
 
typedef long long ll;
//typedef pair<int, int> PII;
 
#define endl '\n'
#define fi first
#define se second
#define push_back
#define rep(i, l, r) for (ll i = l; i <= r; i ++ )

#define int long long

const int N = 2e5 + 10;

int a[N];

void solve()
{
    int n, s; cin >> n >> s;
    for (int i = 1; i <= n && cin >> a[i]; i ++ ) a[i] += a[i - 1];
    
    int l = 1, r = 0;
    for (int j = 1, i = 1; j <= n; j ++ )
    {
        while (i <= j && a[j] - a[i - 1] + s < 0) i ++ ;
        
        if (r - l + 1 < j - i + 1)
            l = i, r = j;
    }
    
    if (r == 0) cout << -1 << endl;
    else cout << l << ' ' << r << endl;
}
 
signed main()
{
    cin.tie(nullptr) -> sync_with_stdio(false);
    
    int _;
    cin >> _;
    while (_ -- )
        solve();

//    return 0;
}

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <unordered_set>
#include <math.h>
using namespace std;
 
//typedef long long ll;
typedef pair<int, int> PII;
 
#define endl '\n'
#define fi first
#define se second
#define push_back
#define rep(i, l, r) for (ll i = l; i <= r; i ++ )

#define int long long

const int N = 2e5 + 10;

int n, s;
int a[N];
int l, r, ansl, ansr, sum;

void init()
{
    l = r = 1, sum = 0, ansl = ansr = -1;
}

void update()
{
    if (r - l > ansr - ansl) ansl = l, ansr = r;
}
 
void solve()
{
    cin >> n >> s;
    for (int i = 1; i <= n && cin >> a[i]; i ++ );
    
    init();
    
    while (l <= n && r <= n + 1)
    {
        if (sum + s >= 0) update(), sum += a[r ++ ];
        else sum -= a[l ++ ];
    }
    
    if (ansr == -1) cout << -1 << endl;
    else cout << ansl << ' ' << ansr - 1 << endl;
}
 
signed main()
{
    cin.tie(nullptr) -> sync_with_stdio(false);
    
    int _;
    cin >> _;
    while (_ -- )
        solve();
 
//    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值