【Codeforces 466C】Number of Ways
【链接】 我是链接,点我呀:)
【题意】
让你把数组分成3个连续的部分
每个部分的和要一样
问你有多少种分法
【题解】
先处理出来num[i]
表示i..n这里面有多少个j
满足aft[j] = aft[i]/2
这aft[i]=a[j]+a[j+1]..+a[n]
然后for从1..n
看看pre[i]*2=aft[i+1]是否成立。
如果成立的话那么答案就加上num[i+1]
【代码】
import java.io.*;
import java.util.*;
public class Main {
static int N = (int)5e5;
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
in = new InputReader();
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static class Task{
public void solve(InputReader in,PrintWriter out) {
int n = in.nextInt();
int []a = new int[N+10];
long []pre = new long[N+10];
long []aft = new long[N+10];
int []num = new int[N+10];
for (int i = 1;i <= n;i++) a[i] = in.nextInt();
for (int i = 1;i <= n;i++) pre[i] = pre[i-1]+a[i];
for (int i = n;i >= 1;i--) aft[i] = aft[i+1]+a[i];
Hashtable<Long, Integer> dic = new Hashtable<Long,Integer>();
for (int i = n;i >= 1;i--) {
if (aft[i]%2==0) {
long temp = aft[i]/2;
if (dic.get(temp)!=null)
num[i] = dic.get(temp);
}
Integer temp1 = dic.get(aft[i]);
if (temp1==null) {
dic.put( aft[i], 1);
}else {
dic.put(aft[i], temp1+1);
}
}
long ans = 0;
for (int i = 1;i <=n;i++) {
long cur = pre[i];
cur = pre[n]-cur;
if (cur!=pre[i]*2) {
continue;
}
ans = ans + num[i+1];
}
out.println(ans);
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader() {
br = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
【Codeforces 466C】Number of Ways的更多相关文章
- 【codeforces 235E】 Number Challenge
http://codeforces.com/problemset/problem/235/E (题目链接) 题意 给出${a,b,c}$,求${\sum_{i=1}^a\sum_{j=1}^b\sum ...
- 【CodeForces 660D】Number of Parallelograms(n个点所能组成的最多平行四边形数量)
You are given n points on a plane. All the points are distinct and no three of them lie on the same ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 514A】Chewbaсca and Number
[题目链接]:http://codeforces.com/contest/514/problem/A [题意] 允许你把每个数字翻转 ->x变成9-x 然后问你能够变成的最小的数字是什么; 不能 ...
- 【codeforces 805D】Minimum number of steps
[题目链接]:http://codeforces.com/contest/805/problem/D [题意] 给你一个字符串; 里面只包括a和b; 让你把里面的"ab"子串全都去 ...
- 【codeforces 546D】Soldier and Number Game
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 768C】Jon Snow and his Favourite Number
[题目链接]:http://codeforces.com/contest/768/problem/C [题意] 给你n个数字; 让你每次把这n个数字排序; 然后对奇数位的数字进行异或操作,然后对新生成 ...
- 【34.57%】【codeforces 557D】Vitaly and Cycle
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【47.95%】【codeforces 554C】Kyoya and Colored Balls
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- 【NOI2009】Bzoj1562&Codevs1843 变换序列
目录 List Description Input Output Sample Input Sample Output HINT Solution 官方题解%莫队 Code Position: htt ...
- [SDOI 2009] 晨跑
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1877 [算法] 不难看出,第一问要求的是最大流,第二问求的是最小费用最大流 注意建图 ...
- P3469 [POI2008]BLO-Blockade tarjan
好久没发博客了啊!自我反省1s...今天再捡起来. 这个题是一道有一点特殊的tarjan,用tarjan维护子树大小,然后判断是否有边多次连接,(就是非树边),然后就进行乘法计算就行了. 具体在代码里 ...
- [置顶]
Snow的追寻
题目描述 Snow终于得知母亲是谁,他现在要出发寻找母亲. 王国中的路由于某种特殊原因,成为了一棵有n个节点的根节点为1的树,但由于"Birds are everywhere.", ...
- Spark2.0 VS Spark 1.* -------SparkSession的区别
Spark .0以前版本: val sparkConf = new SparkConf().setAppName("soyo") val spark = new SparkCont ...
- 9.23 NOIP模拟题(数学专练)
数论基础 专题测试 命题人:清华大学 王赢绪 /* 水题 答案为C(n-k,m-1) 预处理阶乘和逆元,O(1)算答案 开始读错题了!!!朱一乐!!! */ #include<iostream ...
- 洛谷P1478 陶陶摘苹果(升级版)
题目数据范围小,开两个数组手写冒泡应该也能过,不过和之前在牛客上的一题类似用结构体数组就好了,主要是注意用结构体数组的排序 题目 题目描述 又是一年秋季时,陶陶家的苹果树结了n个果子.陶陶又跑去摘苹果 ...
- 如何看待B站疑似源码泄漏的问题?
今天突然看到关于B站源码泄漏事.网曝B站整个网站后台工程源码遭泄露,开源项目平台Github上疑似出现了Bilibili网站后台工程,内含部分用户名密码.目前官方还没对此事作出任何回应,所以还无法确定 ...
- JavaScript学习五
2019-06-02 09:53:42
- 用Python一键搭建Http服务器的方法
用Python一键搭建Http服务器的方法 Python3请看 python -m http.server 8000 & Python2请看 python -m SimpleHTTPServe ...