SPOJ-CRAN02 - Roommate Agreement(前缀和)
CRAN02 - Roommate Agreement
Leonard was always sickened by how Sheldon considered himself better than him. To decide once and for all who is better among them they decided to ask each other a puzzle. Sheldon pointed out that according to Roommate Agreement Sheldon will ask first. Leonard seeing an opportunity decided that the winner will get to rewrite the Roommate Agreement.
Sheldon thought for a moment then agreed to the terms thinking that Leonard will never be able to answer right. For Leonard, Sheldon thought of a puzzle which is as follows. He gave Leonard n numbers, which can be both positive and negative. Leonard had to find the number of continuous sequence of numbers such that their sum is zero.
For example if the sequence is- 5, 2, -2, 5, -5, 9
There are 3 such sequences
2, -2
5, -5
2, -2, 5, -5
Since this is a golden opportunity for Leonard to rewrite the Roommate Agreement and get rid of Sheldon's ridiculous clauses, he can't afford to lose. So he turns to you for help. Don't let him down.
Input
First line contains T - number of test cases
Second line contains n - the number of elements in a particular test case.
Next line contain n elements, ai (1<=i<= n) separated by spaces.
Output
The number of such sequences whose sum if zero.
Constraints
1<=t<=5
1<=n<=10^6
-10<= ai <= 10
Example
Input:
2
4
0 1 -1 0
6
5 2 -2 5 -5 9
Output:
6
3
题意
给你一个序列,里面n(10^6)个数字,问这些数字相加为0的区间有多少个
思路
看样例解释我们可以知道,可以利用前缀和来计算,a[i]=a[i]+a[i-1]这样,然后用map来存a[i]出现的次数,如果有x个a[i]出现,则说明其中存在序列和为0的情况,
并且可能的情况为1~x-1种,如果a[i]刚好=0,则还要加上当前这个。
/*
Name: hello world.cpp
Author: AA
Description: 唯代码与你不可辜负
*/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
LL a[n];
map<LL, LL> cnt;
cin >> a[];
cnt[a[]]++;
for(int i = ; i < n; i++) {
cin >> a[i];
a[i] += a[i - ];
cnt[a[i]]++;
}
map<LL, LL>::iterator it;
LL ans = ;
for(it = cnt.begin(); it != cnt.end(); it++) {
if(it->first == )
ans += it->second + it->second * (it->second - ) / ;
else
ans += it->second * (it->second - ) / ;
}
cout << ans << endl;
}
return ;
}
SPOJ-CRAN02 - Roommate Agreement(前缀和)的更多相关文章
- SPOJ Time Limit Exceeded(高维前缀和)
[题目链接] http://www.spoj.com/problems/TLE/en/ [题目大意] 给出n个数字c,求非负整数序列a,满足a<2^m 并且有a[i]&a[i+1]=0, ...
- SPOJ.TLE - Time Limit Exceeded(DP 高维前缀和)
题目链接 \(Description\) 给定长为\(n\)的数组\(c_i\)和\(m\),求长为\(n\)的序列\(a_i\)个数,满足:\(c_i\not\mid a_i,\quad a_i\& ...
- SPOJ:Fibonacci Polynomial(矩阵递推&前缀和)
Problem description. The Fibonacci numbers defined as f(n) = f(n-1) + f(n-2) where f0 = 0 and f1 = 1 ...
- [SPOJ] DIVCNT2 - Counting Divisors (square) (平方的约数个数前缀和 容斥 卡常)
题目 vjudge URL:Counting Divisors (square) Let σ0(n)\sigma_0(n)σ0(n) be the number of positive diviso ...
- SPOJ 7258 SUBLEX 后缀数组 + 二分答案 + 前缀和
Code: #include <cstdio> #include <algorithm> #include <cstring> #define setIO(s) f ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- SPOJ REPEATS 后缀数组
题目链接:http://www.spoj.com/problems/REPEATS/en/ 题意:首先定义了一个字符串的重复度.即一个字符串由一个子串重复k次构成.那么最大的k即是该字符串的重复度.现 ...
- SPOJ DISUBSTR 后缀数组
题目链接:http://www.spoj.com/problems/DISUBSTR/en/ 题意:给定一个字符串,求不相同的子串个数. 思路:直接根据09年oi论文<<后缀数组——出来字 ...
- SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
随机推荐
- jsp页面获取服务器Ip地址等数据
废话不多说,直接上代码 <%@ page language="java" import="java.util.*" pageEncoding=" ...
- C#学习笔记_02_数据类型
02_数据类型 基本数据类型 整型 有符号整型:最高位是正负号 字节型:sbyte 1byte:[-128,127] 短整型:short: 2byte:[-2^(位数-1),2^(位数-1)-1] 整 ...
- python笔记之json报错
写爬虫的过程中不免遇到处理json数据的情况,今天在爬取新华网新闻数据时发现使用json.loads函数时报错: json.decoder.JSONDecodeError: Expecting val ...
- Linux 常用密令总结 ------随用随记吧
ubuntu or uqilin 目录文件类ls 查看目录cd 转到目录ps -aux查看所有进程grep 查找| 管道符 用户账户类 su 切换用户sudo 以管理员权限运行命令重启机器 reboo ...
- SBC37x交叉编译平台QT+OPENCV
一.构建交叉编译器 [随机手册]Angstrom是一个界面友好的嵌入式发行版本,用亍手持设备,机顶盒和网络存储设备等嵌入式设备. 光盘的文件系统是定制的The Angstrom Distributio ...
- MyBatis学习总结(9)——使用MyBatis Generator自动创建代码
一.构建一个环境 1. 首先创建一个表: [sql] view plaincopy CREATE TABLE t_user ( USER_ID INT NOT NULL AUTO_INCREMENT, ...
- nginx tomcat glassfish session 复制配置
1.nginx配置 [root@www1-nlb1 ~]# cat /usr/local/nginx/conf/nginx.conf | grep -v ^$ | grep -v ^# user w ...
- 基于ActiveMQ的消息中间件系统逻辑与物理架构设计具体解释
1. 基本介绍与组件架构图 维基百科对消息中间件的定义是"Message-oriented Middleware is software infrastructure focused on ...
- 【Android归纳】Fragment生命周期-基于实验的最新总结
如今非常多应用的开发都是基于FragmentActivity中嵌套Fragment进行开发的,所以,假设我们可以清晰地知道他们的生命周期,那么会使我们的开发变的easy. 对于Activity的生命周 ...
- 【iOS开发-80】Quartz2D画图简单介绍:直线/圆形/椭圆/方形以及上下文栈管理CGContextSaveGState/CGContextRestoreGState
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2Vpc3ViYW8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...