AtCoder Grand Contest 023 A - Zero-Sum Ranges
Time limit : 2sec / Memory limit : 256MB
Score : 200 points
Problem Statement
We have an integer sequence A, whose length is N.
Find the number of the non-empty contiguous subsequences of A whose sums are 0. Note that we are counting the ways to take out subsequences. That is, even if the contents of some two subsequences are the same, they are counted individually if they are taken from different positions.
Constraints
- 1≤N≤2×105
- −109≤Ai≤109
All values in input are integers.
https://agc023.contest.atcoder.jp/tasks/agc023_a
题意:给一个数组求出数组中连续字串合为0的个数
Solution
n的平方算法会超时,一开始想着dp可是空间也不够;
首先看样例
6
1 3 -4 2 2 -2
我们计算前面的合为
1 4 0 2 4 2
我们可以发现相同的两个数之间之间经过的数的合一定是0;
例如第一个4 和第二个4 之间是 -4 + 2 +2=0所以我们发现规律只要找到相同的数的个数让他们之间两两连接就是个数
如果合为0代表从第一个开始到他的合就是0所以要额外加上0的个数
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=+;
ll num[maxn];
ll sum[maxn];
ll he(ll n){
if(n==)
return ;
if(n==)
return ;
return (n*(n-))/;
}
map<ll,int>mp;
int main()
{
//std::ios::sync_with_stdio(false);
int n;
scanf("%d",&n);
long long summ=;
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++){
scanf("%lld",&num[i]);
sum[i]=sum[i-]+num[i];
}
for(int i=;i<=n;i++){
if(mp[sum[i]]==)mp[sum[i]]++;
else{
mp[sum[i]]++;
} }
map<ll,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++){
//cout<<it->first<<" "<<it->second<<endl;
if(it->first==)
summ+=(he(it->second)+(it->second));
else if(it->second>){
if(it->first!=)
summ+=he(it->second); } }
printf("%lld\n",summ);
return ;
}
AtCoder Grand Contest 023 A - Zero-Sum Ranges的更多相关文章
- Atcoder Grand Contest 023 E - Inversions(线段树+扫描线)
洛谷题面传送门 & Atcoder 题面传送门 毒瘤 jxd 作业-- 首先我们不能直接对所有排列计算贡献对吧,这样复杂度肯定吃不消,因此我们考虑对每两个位置 \(x,y(x<y)\), ...
- AtCoder Grand Contest 023 E - Inversions
Description 给出长度为 \(n\) 序列 \(A_i\),求出所有长度为 \(n\) 的排列 \(P\),满足 \(P_i<=A_i\),求所有满足条件的 \(P\) 的逆序对数之和 ...
- AtCoder Grand Contest 023 C - Painting Machines
Description 一个长度为 \(n\) 的序列,初始都为 \(0\),你需要求出一个长度为 \(n-1\) 的排列 \(P\), 按照 \(1\) 到 \(n\) 的顺序,每次把 \(P_i\ ...
- AtCoder Grand Contest 023 F - 01 on Tree
Description 题面 Solution HNOI-day2-t2 复制上去,删点东西,即可 \(AC\) #include<bits/stdc++.h> using namespa ...
- Atcoder Grand Contest 023
A 略 B 略 C(计数) 题意: 有n个白球排成一行,故有n-1个空隙,我可以给一个空隙对应的两个白球都涂黑.n-1个空隙的一个排列就对应着一个涂黑顺序,定义这个涂黑顺序的价值是“将所有n个球都涂黑 ...
- AtCoder Grand Contest 011
AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...
- AtCoder Grand Contest 010
AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...
- AtCoder Grand Contest 009
AtCoder Grand Contest 009 A - Multiple Array 翻译 见洛谷 题解 从后往前考虑. #include<iostream> #include< ...
- AtCoder Grand Contest 008
AtCoder Grand Contest 008 A - Simple Calculator 翻译 有一个计算器,上面有一个显示按钮和两个其他的按钮.初始时,计算器上显示的数字是\(x\),现在想把 ...
随机推荐
- P1650 田忌赛马
题目描述 我国历史上有个著名的故事: 那是在2300年以前.齐国的大将军田忌喜欢赛马.他经常和齐王赛马.他和齐王都有三匹马:常规马,上级马,超级马.一共赛三局,每局的胜者可以从负者这里取得200银币. ...
- visio应用程序相关设置-选项-常规
1.用户名称,可读写 ApplicationSettings.UserName m_Visio.Window.Application.Settings.UserName = "BEIJING ...
- dva的基本用法
dva是一个状态管理工具,整合了redux,redux-saga,react-router,fetch等框架,目前只能用于react的状态管理 1. dva的models dva的主要作用还是整合了r ...
- 洛谷P2568 GCD (欧拉函数/莫比乌斯反演)
P2568 GCD 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 输入 ...
- Codeforces Round #520 (Div. 2) A. A Prank
A. A Prank time limit per test 1 second memory limit per test 256 megabytes 题目链接:https://codefo ...
- HDU 1698 Just a Hook(线段树
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- oracle的sql语句训练
--查询工资最高的人的名字select ename ,sal from emp where sal=(select max(sal) from emp );--求出员工的工资在所有人的平均工资之上的人 ...
- Ubuntu1604 install netease-cloud music
Two issue: 1. There is no voice on my computer, and the system was mute and cannot unmute. eric@E641 ...
- event对象和事件冒泡
<!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>无标 ...
- 深入探索 高效的Java异常处理框架
转载自:http://www.sunwei.org/archives/196 摘要:本文从Java异常最基本的概念.语法开始讲述了Java异常处理的基本知识,分析了Java异常体系结构,对比Sprin ...