当前位置:网站首页>CF 1333C Eugene and an array
CF 1333C Eugene and an array
2022-07-27 02:19:00 【ZaneBobo】
One . The question
Give you an array , I want you to find several good sub segments in the array ( Good sub segment : The does not contain any sub segments so that the sum of these sub segments is 0)
Two . Use Algorithm
The prefix and
3、 ... and . Ideas
Data range 2e5 It seems that it is probably a problem that can be solved by enumerating once
Let's start with the first place , Enumerating to each location, we make the answer += All good sub segments ending in the current position and , According to the idea of prefix and , This sub segment cannot contain both prefixes and equal points . So when enumerating the last position of the sub segment , Maintain the frontmost boundary that a sub segment can extend to each time .
So how to maintain this front boundary ( It is not advisable to ) Well ? The prefix and
During our enumeration , If the current prefix and = The prefixes and conditions enumerated above , Then the section between sum=0, Therefore, maintain the front boundary of the selected sub segment as the first prefix of the same prefix and the next position of the location .
Why is this position? For example
Array 2 -1 1 2 3
When i=3 here i=1 and i=3 The prefix and are equal At this time, sub segment -1 1 sum=0 Give Way pos=2, This position is nogood The beginning of the sub segment , The prefix and sum[i]- The prefix and sum[j] He is actually i+1 To j paragraph , So the i+1 If you can't take it, it won't appear behind sum=0 The situation of the .
For example i=3 The prefix and =6
stay i=7 The prefix and =6 that The top position is updated to pos=4.
So at this time good The sub segment can be ( Marked by position )567/67/7 That is to say i-pos Three sub segments ( Location pos Is a sub segment and can be 0 The boundary of is not desirable )
A knowledge :map In the past, you can also find out whether it has existed !!(.count(x))
Four . Code
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N = 2e5+10;
typedef long long LL;
LL sum;
map<LL,LL> cnt;
int main()
{
int n;
cin>>n;
LL pos=0;
cnt[0]=0;
LL ans=0;
for(int i=1;i<=n;i++)
{
int a;
cin>>a;
sum+=a;
if(cnt.count(sum))
{
pos=max(pos,cnt[sum]+1);
}
ans+=i-pos;
cnt[sum]=i;
}
cout<<ans<<endl;
}边栏推荐
- Flink1.13.6 detailed deployment method
- 关于编程的自我介绍和规划
- 2022zui new Tiktok 24-hour round robin live broadcast monitoring (I) live broadcast room start-up monitoring
- golang 实现 tcp-聊天室
- Timer interrupt experiment
- 【mysql】mysql启动关闭命令以及一些报错解决问题
- 初识网页设计
- Lora通信应用开发
- HCIA (network elementary comprehensive experimental exercise)
- Gan's training skills: alchemist cultivation plan - generative confrontation network training, participation and improvement
猜你喜欢

Experiment of OSPF in mGRE environment

The basic configuration of static routing (planning of IP address and configuration of static routing) realizes the accessibility of the whole network.

Ogeek meetup phase I, together with cubefs, is hot
![[MySQL] MySQL startup and shutdown commands and some error reports to solve problems](/img/23/b4c90604eba796692fbe049d2679d1.png)
[MySQL] MySQL startup and shutdown commands and some error reports to solve problems

The latest C language introduction and advanced - the most complete and detailed C language tutorial in history!! Section 1 - overview of C language

Text to image intensive reading of paper gr-gan: gradually refine text to image generation

HCIA Basics (1)

数字芯片的面积优化:第三届“华为杯”研究生创芯大赛数字方向上机题1详解

微信小程序:用户微信登录流程(附:流程图+源码)

数字集成电路:MOS管器件章(二)
随机推荐
静态综合实验(静态路由、环回接口、缺省路由、空接口、浮动静态的综合练习)
二层封装技术(HDLC、PPP--PAP\CHAP、GRE)实验练习
6.28 Dahua written examination
HCIA静态路由基础模拟实验
RISC-V工具链编译笔记
OSPF静态大实验
2022 open source work of the latest text generated image research (papers with code)
6.29 Zhong'an Summer Internship
机械硬盘选购指南——从选购经历谈起
WAN technology experiment
第五讲—按键控制LED
Nat网络地址转换实验
VLAN原理简述、具体实验配置
动态路由ofps协议配置
ESP8266Wi-Fi数据通讯
6.30 written examination of MediaTek
About unsafe problems such as fopen and strError encountered in vs2022 or advanced version running environment
静态路由基础配置(IP地址的规划、静态路由的配置),实现全网可达。
Dynamic routing ofps protocol configuration
JS max?