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 ...
随机推荐
- vue自定义拖动指令
1.在项目开发中,需要对div进行拖动.因为需要自定义组件 a>定义全局拖拽指令: 定义全局指令,需要在main.js中写入vue.directive('drag',{});即可.但是一般会在外 ...
- 15.Hibernate一对多双向关联映射+分页
1.创建如下数据库脚本 --创建用户信息表 --编号,用户名,密码,年龄,性别,昵称,手机,地址,管理员,图像地址 create table users ( id ) primary key, use ...
- Java基本排序算法
转载来自:https://www.jianshu.com/p/5e171281a387 一遍记住Java常用的八种排序算法与代码实现 1.直接插入排序 经常碰到这样一类排序问题:把新的数据插入到已 ...
- 【codeforces 483B】Friends and Presents
[链接] 我是链接,点我呀:) [题意] [题解] 我们可以二分n的值,设为mid 那么对于n=mid 我们可以算出来以下3个东西 temp1 = n/x; temp2 = n/y; temp3 = ...
- zuul 路由网关
一.阐述 Zuul 包含了对请求的路由和过滤两个主要的功能: 路由功能:负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础: 滤器功能:负责对请求的处理过程干预,是实现请求校验.服务 ...
- 洛谷 P1131 BZOJ 1060 [ZJOI2007]时态同步
题目描述 小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3….进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板的任何两个节点 ...
- POJ 1625
什么鬼,真的是有负数的吗?我在字符加上了128才过了.dp[i][j],经过i步到达j状态的路径数.转移很容易了,建个trie图就可以,由前一步可连通到和更新即可. 另外,要用到大数和AC自动机DP ...
- C++一些知识难点
什么是"引用"?申明和使用"引用"要注意哪些问题? 答:引用就是某个目标变量的"别名"(alias).相应用的操作与对变量直接操作效果全然同 ...
- 面向对象的三大特性之二——继承(含super的使用)
1.继承的语法:class 子类 extends 父类 例1: class Person { int age; String name; public void print1() { System.o ...
- luogu1541 乌龟棋 动态规划
题目大意 一行格子,每个格子里有数字.一些卡片,卡片上有1.2.3.4这几种数字.一开始你在格子1,随后每次选一个卡片,你可以前进卡片上的数字个格子,得到格子上的分数,然后讲该卡片丢弃.求取卡片的顺序 ...