F. ATM and Students

Codeforces Round #756 (Div. 3)
date:2021/11/28

题意:

求最大连续区间长度,使其和 > = − s >=-s >=s;

思路

1.双指针
重要的是双指针如何运动;

code

// Problem: F. ATM and Students
// Contest: Codeforces - Codeforces Round #756 (Div. 3)
// URL: https://codeforces.com/contest/1611/problem/F
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define _orz ios::sync_with_stdio(false),cin.tie(0)
#define mem(str,num) memset(str,num,sizeof(str))
#define forr(i,a,b) for(int i = a;i <= b;i++)
#define forn(i,n) for(int i = 0; i < n; i++)
#define all(a) (a.begin(),a.end())
#define dbg() cout << "0k!" << endl;
//#define _DEBUG
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1e6+10;
const ll MOD = 1e9+7;

void so1ve(){
    
    
	int n,m;
	cin >> n >>m;
	vector<int> a(n+1);
	forr(i,1,n) cin >> a[i];
	int l = 1, r = 1;
	ll lx = 0,rx = 0,now = 0,res = 0;
	while(l <= n && r <= n){
    
    
		while(m+now+a[r] >= 0 && r <= n) now += a[r++];
		if(r-l > res){
    
    
			res = r - l;
			lx = l;
			rx = r - 1;
		}
		now -= a[l++];
	}
	
	if(rx == 0 && lx == 0) puts("-1");
	else cout << lx <<" " << rx << endl;
	
}
int main()
{
    
    
#ifdef _DEBUG
    //freopen("input.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
    int t; cin >> t;
    while(t--) so1ve();
    return 0;
}

2.后缀和+二分

思维较高
思路后补…

// Problem: F. ATM and Students
// Contest: Codeforces - Codeforces Round #756 (Div. 3)
// URL: https://codeforces.com/contest/1611/problem/F
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define _orz ios::sync_with_stdio(false),cin.tie(0)
#define mem(str,num) memset(str,num,sizeof(str))
#define forr(i,a,b) for(int i = a;i <= b;i++)
#define forn(i,n) for(int i = 0; i < n; i++)
#define all(a) (a.begin(),a.end())
#define dbg() cout << "0k!" << endl;
//#define _DEBUG
using ll = long long;
const int inf = 0x3f3f3f3f;
const int N = 2e5+10;
const ll MOD = 1e9+7;
struct node{
    
    
	ll val,id;
	bool operator < (const node &t)const{
    
    
		return val > t.val;
	}
}t[N];
ll a[N];
set<int> s;
void so1ve(){
    
    
	int n,m;cin >> n >> m;
	forr(i,1,n) cin >> a[i];
	a[++n] = -(1LL << 60LL);
	a[++n] = 0;
	for(int i = n; i;i--){
    
    
		t[i].val = a[i];
		if(i != n) t[i].val += t[i+1].val;
		t[i].id = i;
	}
	sort(t + 1,t + 1 + n);
	s.clear();
	int l = 1,lx = 0,rx = -1;
	forr(i,1,n){
    
    
		for(;l <= n && t[l].val > t[i].val+m;l++)
			s.insert(t[l].id);
		auto it = s.lower_bound(t[i].id);
		int x = t[i].id,y = *it - 2;
		cout << x <<"  " << y << endl;
		if(y-x > rx - lx){
    
    
			lx = x;
			rx = y;
		}
	}
	if(lx > rx) cout << -1 << endl;
	else	cout << lx <<" " << rx << endl;
}
int main()
{
    
    
#ifdef _DEBUG
    //freopen("input.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
    int t; cin >> t;
    while(t--) so1ve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_51687628/article/details/121597602
atm
今日推荐