判不合法起点的思路,为什么只有15分
查看原帖
判不合法起点的思路,为什么只有15分
1190741
Violet_Evergardon楼主2024/11/10 16:58

只有负数会导致不合法,故记录所有负数位置,从负数往前判有几个不合法起点。

ok[]避免重复计算

#include<bits/stdc++.h>
using namespace std;
const int N=2e6+5;
int n;
int a[N];
int b[N],tot;
int ans,js;
bool ok[N];
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[n+i]);
		a[i]=a[n+i];
		if(a[i]<0) b[++tot]=n+i;
	}
	ans=n;
	int js,j;
	for(int i=1;i<=tot;i++){
		j=b[i];
		if(!ok[j]) ans--,ok[j]=ok[j-n]=1;
		js=a[j];
		while(js<0){
			if(!ok[j])	ans--,ok[j]=ok[j-n]=1;
			j--;
			js+=a[j];
		}
	}
	printf("%d",ans);
	return 0;
}
2024/11/10 16:58
加载中...