Codeforces Round #674 (Div. 3) D. Non-zero Segments

D. Non-zero Segments
Description

Kolya got an integer array a1,a2,…,an. The array can contain both positive and negative integers, but Kolya doesn’t like 0, so the array doesn’t contain any zeros.

Kolya doesn’t like that the sum of some subsegments of his array can be 0. The subsegment is some consecutive segment of elements of the array.

You have to help Kolya and change his array in such a way that it doesn’t contain any subsegments with the sum 0. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, 0, any by absolute value, even such a huge that they can’t be represented in most standard programming languages).

Your task is to find the minimum number of integers you have to insert into Kolya’s array in such a way that the resulting array doesn’t contain any subsegments with the sum 0.

Input

The first line of the input contains one integer n (2≤n≤200000) — the number of elements in Kolya’s array.

The second line of the input contains n integers a1,a2,…,an ( − 109 ≤ a i ≤ 109 , a i ≠ 0 −109≤ai≤109,ai≠0 109ai109,ai=0) — the description of Kolya’s array.

Output

Print the minimum number of integers you have to insert into Kolya’s array in such a way that the resulting array doesn’t contain any subsegments with the sum 0.

Examples
input
4
1 -5 3 2
output
1
input
5
4 -2 3 -9 2
output
0
input
9
-1 1 -1 1 -1 1 1 -1 -1
output
6
input
8
16 -5 -11 -15 10 5 4 -4
output
3
Note

Consider the first example. There is only one subsegment with the sum 0. It starts in the second element and ends in the fourth element. It’s enough to insert one element so the array doesn’t contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer 1 between second and third elements of the array.

There are no subsegments having sum 0 in the second example so you don’t need to do anything.

题意: 根据给出的数组计算任意子列的和,在这些子列和中,如果出现了和为0的情况,则在这些子列中插入一个数,使得它的和不为0,最后要求输出插入的这些数的和要最小。

题解: 用一个字典来记录该数组的前缀和,如果前缀和中出现了两次相同的值的情况或者时直接前缀和等于0的情况,我们在此位置插入一个数,使得它的前缀和不为0,然后继续从当前位置计算新的前缀和。详细逻辑见代码。

c++ AC 代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n;
ll t;
int main()
{
	cin >> n;
	map<ll, int> m; //用一个map来存储前面出现过的和
	m[0] = 1;
	ll sum = 0, ans = 0;
	for (int i = 1; i <= n; i++)
	{
		cin >> t;
		sum += t;
		if (m[sum] == 1) //如果sum已经出现过,又出现一次,说明出现了相加为0的情况 比如 16 -5 5 ,连续的两次16,必定是由中间数为0造成的
		{				 //或者是当前sum的和为0了,比如 16 -8 -8  相加为0
			ans++;		 //当出现上述两种情况之一时,说明之前的区间就需要插数
			m.clear();
			m[0] = 1; //前面的就不用管了,因此重置map,相当于起点变为从当前的t开始,然后再判断后面的数
			sum = t;
		}
		m[sum] = 1; //出现过的sum标记为1
	}
	cout << ans << endl;
	// system("pause");
	return 0;
}

文章参考自:https://blog.csdn.net/henulmh/article/details/108876241

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值