当前位置:网站首页>D. Non-zero Segments
D. Non-zero Segments
2022-08-08 16:37:00 【秦小咩】
D. Non-zero Segments
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Kolya got an integer array a1,a2,…,ana1,a2,…,an. The array can contain both positive and negative integers, but Kolya doesn't like 00, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be 00. 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 00. 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, 00, 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 00.
Input
The first line of the input contains one integer nn (2≤n≤2000002≤n≤200000) — the number of elements in Kolya's array.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109,ai≠0−109≤ai≤109,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 00.
Examples
input
Copy
4 1 -5 3 2
output
Copy
1
input
Copy
5 4 -2 3 -9 2
output
Copy
0
input
Copy
9 -1 1 -1 1 -1 1 1 -1 -1
output
Copy
6
input
Copy
8 16 -5 -11 -15 10 5 4 -4
output
Copy
3
Note
Consider the first example. There is only one subsegment with the sum 00. 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 11 between second and third elements of the array.
There are no subsegments having sum 00 in the second example so you don't need to do anything.
连续子段和为0无非两种情况,一种是1个或多个小区间不交叉,各自和为0,此时答案就是这些区间的个数。第二种就是交叉,包含的和为0的区间,此时我们可以发现,我们只需要从前往后扫,发现一个和为0的,就把答案加1,再从该位置重新计数。这样即使有包含情况存在,我们把被包含的改成巨大的数字,那么大区间也就不用修改了,即使有交叉的,我们把交叉的那部分修改成巨大数,再从新起点开始,那么这个交叉的也被瓦解了。这里体现的是一种贪心的思想,及能修改就修改,先修改一定最优。
判断区间和是0的快速方法是,哈希表存前缀,重复出现说明出现了当前第一个和是0的区间
#include<iostream>
# include<algorithm>
# include<unordered_map>
using namespace std;
typedef long long int ll;
unordered_map<ll,bool>mp;
int main ()
{
ll n,ans=0,x,nowsum=0;
cin>>n;
mp[0]=1;
for(int i=1;i<=n;i++)
{
cin>>x;
nowsum+=x;
if(mp.find(nowsum)==mp.end())
{
mp[nowsum]=1;
}
else
{
ans++;
mp.clear();
nowsum=x;
mp[x]=1;
mp[0]=1;
}
}
cout<<ans;
return 0;
}
边栏推荐
- Spam accounts are a lot of trouble, and device fingerprints are quickly found
- Redis哨兵的配置和原理
- 10分钟快速入门RDS【华为云至简致远】
- Acwing Week 63 [Unfinished]
- Understanding of redis slice cluster
- 【LeetCode】试题总结:深度优先搜索 (DFS)
- 调研阶段复盘
- 科创人·优锘科技COO孙岗:错误问题找不到正确答案,求索万物可视的大美未来
- 【数学模型】灰色关联分析
- Solve the inexplicable problem of MySQL violently - restart the service!
猜你喜欢

phar反序列化

科创人·优锘科技COO孙岗:错误问题找不到正确答案,求索万物可视的大美未来

【论文阅读】RAL 2022: Receding Moving Object Segmentation in 3D LiDAR Data Using Sparse 4D Convolutions

元宇宙医疗或将改变医疗格局

【MySQL哪些字段适合建索引,哪些查询条件会导致索引失效】
![ImportError: numpy.core.multiarray failed to import [cv2, matplotlib, PyTorch, pyinstaller ]](/img/4c/21f6707e771ca7a21e467331b9b8e5.jpg)
ImportError: numpy.core.multiarray failed to import [cv2, matplotlib, PyTorch, pyinstaller ]

二、junit接口自动化框架之二次开发

数字图像处理(六)—— 图像压缩

ctfshow七夕杯复现

英特尔两大 FPGA 产品已部署至中国创新中心:性能提高 45%,功耗降低 40%
随机推荐
The realization of the salary slip issuing function of WeChat public account + web background
ESP8266-Arduino编程实例-ADS1015(ADC)驱动
bzoj1097 [POI2007]旅游景点atr
ERROR Failed to compile with 1 error
维尔薇vs千劫
phar反序列化
Flutter的实现原理初探
bzoj5063 旅游
C语言学习概览(五)
Using PyGame's Bubble Sort Visualizer
ESP8266-Arduino编程实例-ADXL345三轴加速计驱动
FreeRTOS知识小结
【uniapp小程序】视图容器cover-view
【LeetCode】试题总结:深度优先搜索 (DFS)
使用 ansible-bender 构建容器镜像
Nervegrowold: machine advanced learning advice
字节一面:TCP 和 UDP 可以使用同一个端口吗?
用完华为云会议解决方案,我直接卸载了之前的会议软件【华为云至简致远】
Subject: Ordered Queue
NSSCTF部分复现