【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 ...
随机推荐
- Java —— 正则表达式
0. 注意 正则表达式里的点号(.),可以匹配除换行符之外的所有字符 Java 语言同其他语言中的正则表达式的不同在于: 对 \(反斜线)的不同处理 \\:其他语言中,表示在正则表达式中插入普通的反斜 ...
- 【HDU 4864】 Task
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4864 [算法] 贪心 不妨将两个数组分别按x从大到小排序 然后枚举每件物品,选择x值大于该物品的且 ...
- LCT教程
lct是一种动态树,用来维护一些动态加边删边的操作的东西.他主要用到几个操作,其实这个算法和树链刨分有点像,但是不能用线段树简单维护,所以我们要用多棵平衡树来维护树上的一个个子树,然后就进行一些很秀的 ...
- 杂项-JSP-Runoob:JSP 标准标签库(JSTL)
ylbtech-杂项-JSP-Runoob:JSP 标准标签库(JSTL) 1.返回顶部 1. JSP 标准标签库(JSTL) JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通 ...
- 使用centos 5.x 64位系统安装astgo 2014 v7.3教程(含全套安装文件)
版本特色: 全自动安装 安装过程中不用频繁输入yes或回车 自带完整号码归属地数据库 自带触屏版WAP ·首先确定你需要使用astgo 2014 7.0还是7.3: astgo 2014 v 7.0 ...
- VUE修改每个页面title
//index.js routes: [ { name:'home', path: '/home/:openname', component: Home, meta: { title: '首页' } ...
- @RequestParam 和 @RequestBody 接受的时间格式
这两个接受的时间格式不相同 首先看一下他们的区别 @RequestParam用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容.(Ht ...
- leetCode----day01---- 从排序数组中删除重复项
需求: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. ...
- NS2学习笔记(四)
这几天学习NS2,虽然国内很多人使用,但系统的教材资料不多,只能一边看中文教材,一边看英文手册,知识点也是零零散散.过段时间等能将所有知识点串上,再总结总结.现只讲一些零碎的点记录一下. 添加新的协议 ...
- 用CSS样式写选择框右侧小三角
直接上代码! <!DOCTYPE html><html lang="en"><head> <title>小三角</title& ...