题目链接 通过插入数使任意子段不为零。 通过前缀和,如果两个前缀和相同,说明夹着的部分字段和为零,用map记录,但是每次要清空,因为前面的会影响。 相当于在这个数之前加一个特别的数使之往后字段不可能为0;并把这个数放入map;
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <map>
#define inf 0x3f3f3f3f
using namespace std
;
int main()
{
ios
::sync_with_stdio(false);
cin
.tie(0),cout
.tie(0);
int n
;
cin
>>n
;
map
<long long,int> g
;
long long sum
=0;
int ans
=0;
g
[0]=1;
for(int i
=0;i
<n
;i
++)
{
int t
;
cin
>>t
;
sum
+=t
;
if(g
[sum
]==1)
{
ans
++;
sum
=t
;
g
.clear();
g
[0]=1;
}
g
[sum
]=1;
}
cout
<<ans
<<endl
;
return 0;
}
转载请注明原文地址:https://blackberry.8miu.com/read-16838.html