codeforces原题链接:https://codeforces.com/problemset/problem/466/C

CF466C Number of Ways

题目描述

You've got array $ a[1],a[2],...,a[n] $ , consisting of $ n $ integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same.

More formally, you need to find the number of such pairs of indices $ i,j $ $ (2<=i<=j<=n-1) $ , that .

输入格式

The first line contains integer $ n $ $ (1<=n<=5·10^{5}) $ , showing how many numbers are in the array. The second line contains $ n $ integers $ a[1] $ , $ a[2] $ , ..., $ a[n] $ $ (|a[i]|<=10^{9}) $ — the elements of array $ a $ .

输出格式

Print a single integer — the number of ways to split the array into three parts with the same sum.

输入输出样例 #1

输入 #1

5
1 2 3 0 3

输出 #1

2

输入输出样例 #2

输入 #2

4
0 1 -1 0

输出 #2

1

输入输出样例 #3

输入 #3

2
4 1

输出 #3

0

思路:

这道题不难发现,可以利用前缀和更快捷的划分区间求和,将数组划分为3份,说明需要找到两个断点,在第一个断点前面的数字的和为所有数字加起来的sum的三分之一,而从索引1到第二个断点之间的数字之和则是sum的三分之二,而当sum无法被3整除时候一定无解,所以在前面可以对sum先进行一个特判,同时要对两个断点设定好满足条件,上题解

题解

#include <bits/stdc++.h>
using namespace std;
const int N=5e5+10;
typedef long long ll;
int n;
int a[N];
ll presum[N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
ll sum=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum+=a[i];
presum[i]+=presum[i-1]+a[i]; }
if(sum%3)cout<<0<<endl;
else
{
ll ans=0;
ll cnt=0;
ll div= sum/3 ;
for(int i=1;i<n;i++)
{
if(presum[i]==(2*div)&&i<=n-1&&i>1)ans+=cnt;
if(presum[i]==div&&i<=n-2)cnt++;
}
cout<<ans<<endl;
} return 0;
}

CF466C Number of Ways (前缀和)的更多相关文章

  1. cf466C Number of Ways

    C. Number of Ways time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  2. Codeforces Round #266 (Div. 2) C. Number of Ways

    You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split ...

  3. LeetCode 5274. Number of Ways to Stay in the Same Place After Some Steps - Java - DP

    题目链接:5274. 停在原地的方案数 You have a pointer at index 0 in an array of size arrLen. At each step, you can ...

  4. 【leetcode】1269. Number of Ways to Stay in the Same Place After Some Steps

    题目如下: You have a pointer at index 0 in an array of size arrLen. At each step, you can move 1 positio ...

  5. codeforce Number of Ways(暴力)

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ...

  6. Codeforces466C Number of Ways

    题目链接: http://codeforces.com/problemset/problem/466/C 题意: 给一个长度为n的数组,将其分成连续的三段使三段的和相等.求有几种这种组合 分析: 从头 ...

  7. [Codeforces 466C] Number of Ways

    [题目链接] https://codeforces.com/contest/466/problem/C [算法] 维护序列前缀和 , 枚举中间一段即可 , 详见代码 时间复杂度 : O(N) [代码] ...

  8. HDU 4055 Number String:前缀和优化dp【增长趋势——处理重复选数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 题意: 给你一个由'I', 'D', '?'组成的字符串,长度为n,代表了一个1~n+1的排列中 ...

  9. codeforces 466C. Number of Ways 解题报告

    题目链接:http://codeforces.com/problemset/problem/466/C 题目意思:给出一个 n 个数的序列你,问通过将序列分成三段,使得每段的和都相等的分法有多少种. ...

  10. Codeforces - 466C - Number of Ways - 组合数学

    https://codeforces.com/problemset/problem/466/C 要把数据分为均等的非空的三组,那么每次确定第二个分割点的时候把(除此之外的)第一个分割点的数目加上就可以 ...

随机推荐

  1. RPC实战与核心原理之分布式环境下如何快速定位问题

    分布式环境下如何快速定位 回顾 如何建立可靠的安全体系,关键点就是"鉴权",我们可以通过统一的鉴权服务动态生成秘钥,提高 RPC 调用的安全性. 分布式环境下定位问题有哪些困难 举 ...

  2. 第9讲、深入理解Scaled Dot-Product Attention

    Scaled Dot-Product Attention是Transformer架构的核心组件,也是现代深度学习中最重要的注意力机制之一.本文将从原理.实现和应用三个方面深入剖析这一机制. 1. 基本 ...

  3. python实现字符输入实时读取

    原理:通过opencv中的waitKey来实现 示例代码: def key_control(): while 1: cv2.imshow('tmp', np.zeros(shape=(100, 100 ...

  4. P5749 [IOI2019] 排列鞋子

    算是一种新思路吧. 题目要求我们求最少的对调次数,想到了什么?求逆序对个数,我们只需将原来的 \(S_i\) 数组转化一下,求其逆序对个数即可. 转化规则为:从头开始,对于每个还未被赋值的 \(S_i ...

  5. Element-plus组件库的MessageBox 消息弹框组件自定义样式的坑

    一.问题描述: 在使用Element-plus组件库的MessageBox 消息弹框组件时,需要更改该组件的按钮样式,于是根据官网文档: 找到cancel-button-class.confirm-b ...

  6. ARCHIV_CREATE_FILE 员工头像上传

    *&---------------------------------------------------------------------* *& Report ZHRR_011 ...

  7. 数栈技术分享:详解FlinkX中的断点续传和实时采集

    数栈是云原生-站式数据中台PaaS,我们在github和gitee上有一个有趣的开源项目:FlinkX,FlinkX是一个基于Flink的批流统一的数据同步工具,既可以采集静态的数据,也可以采集实时变 ...

  8. HyperWorks一维单元创建与模型连接管理

    在HyperWorks的有限元分析中,一维单元是非常重要的概念.我们可以使用一维单元连接节点,或将不匹配的网格部件进行连接,进行载荷施加,以及用于建立焊接,螺栓,铆钉等等各类工程中经常运用的模型连接方 ...

  9. 深入研究使用DozerMapper复制List<Ojbect>前后元素类型不一致的问题

    背景 某项目某个功能点是接受前端传参,将其存入MongoDB.这个传参的核心数据是一个二维数组List<List<Object>>,可以放字符串.整型,也可以放null. 在测 ...

  10. CF757G Can Bash Save the Day? (复健 Day 1)

    先差分为 \(Q(r)-Q(l-1)\),\(Q(i)=\sum_{j=1}^{i} \operatorname{dis}(p_j, x)\). 树上在线路径优先考虑点分树,先想询问怎么做,我们记 \ ...