当前位置:网站首页>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;
}边栏推荐
- 第三讲--GPIO输入输出库函数使用以及相关例程
- 2022zui new Tiktok 24-hour round robin live broadcast monitoring (I) live broadcast room start-up monitoring
- 初识网页设计
- [Database Course Design] SQLSERVER database course design (student dormitory management), course design report + source code + database diagram
- Brief introduction of VLAN principle and specific experimental configuration
- STM32入门教程第二讲
- 数字芯片的面积优化:第三届“华为杯”研究生创芯大赛数字方向上机题1详解
- Ospf基础配置应用( 综合实验: 干涉选举 缺省路由 区域汇总 认证--接口认证)
- About unsafe problems such as fopen and strError encountered in vs2022 or advanced version running environment
- 识时务者常用网址大全
猜你喜欢

OSPF静态大实验

初识C语言(2)

Brief introduction of VLAN principle and specific experimental configuration

ensp中的简单静态路由

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

初识C语言(1)

HCIA动态路由OSPF实验

ACM mode input and output exercise

HCIA Basics (1)

HCIA静态路由基础模拟实验
随机推荐
golang 实现 tcp-聊天室
Solution: various error reports and pit stepping and pit avoidance records encountered in the alchemist cultivation plan pytoch+deeplearning (II)
Republishing and routing strategy of OSPF
Lecture 3 - GPIO input / output library function usage and related routines
ospf协议概述以及基础概念
动态路由rip协议实验
Nb-iot access to cloud platform
TCP的三次握手与四次挥手(简述)
数字集成电路:CMOS反相器(一)静态特性
Pseudo class of a element
Static routing default routing VLAN experiment
7.16 written examination of Duoyi network
Text to image paper intensive reading rat-gan: recursive affine transformation for text to image synthesis
二层封装技术(HDLC、PPP--PAP\CHAP、GRE)实验练习
2022 latest Tiktok live broadcast monitoring (II) streaming media download in live broadcast room
Beyond hidden display ellipsis
识时务者常用网址大全
(CF1691D) Max GEQ Sum
Dynamic routing ofps protocol configuration
7.13 Weilai approved the written examination in advance