题目链接:

http://codeforces.com/problemset/problem/466/C

题意:

给一个长度为n的数组,将其分成连续的三段使三段的和相等。求有几种这种组合

分析:

从头扫到尾。将全部的前缀和为(sum/3)的点统计起来。然后再从尾開始统计。找到统计全部后缀和为(sum/3)的节点 然后这样的方案的数为

这个点之前全部前缀和为sum/3的个数

代码例如以下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std; const int maxn = 500010;
typedef long long LL; LL a[maxn];
int cnt[maxn];
int main()
{
int n;
LL x;
while(~scanf("%d",&n)){
LL s=0,p=0;
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
s+=a[i];
}
if(s%3){puts("0");continue;}
s/=3;
int com=0;
for(int i=1;i<=n;i++){
p+=a[i];
if(p==s)
cnt[com++]=i;
}
LL ans = 0;
p=0;
for(int i=n;i>=1;i--){
p+=a[i];
// cout<<"p "<<p<<endl;
if(p==s){
int pos=lower_bound(cnt,cnt+com,i-1)-cnt;
// cout<<"pos "<<pos<<endl;
ans+=pos;
}
}
cout<<ans<<endl;
}
return 0;
}

Codeforces466C 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. codeforces 466C. Number of Ways 解题报告

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

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

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

  8. [Codeforces 466C] Number of Ways

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

  9. 【Codeforces 466C】Number of Ways

    [链接] 我是链接,点我呀:) [题意] 让你把数组分成3个连续的部分 每个部分的和要一样 问你有多少种分法 [题解] 先处理出来num[i] 表示i..n这里面有多少个j 满足aft[j] = af ...

随机推荐

  1. POI导出,开发中经常会遇到数据导出这样的问题,下面是我在开发中采用的解决方法,大家可以参考,具体的实现害的结合你自身的业务逻辑

    @RequestMapping(value = "/drawPayFailExport",method = RequestMethod.GET,produces = "a ...

  2. webpack 之 plugin

    loader和plugins的区别是:前者处理单个文件,而后者作用于整个打包过程 内置插件:BannerPlugin 可以在打包后的文件上方添加备注信息,如版权说明等 // webpack.confi ...

  3. 项目:开发->测试->上线:流程规范

    1) 项目分析 2) 项目经理分工协作 程序就具体流程: 1: 按项目名称: 建git, 数据库, 线上测试虚拟机 2: 按项目经理分配的大的模块. 自行划分工作阶段 a: 划分为小的模块 b: 预估 ...

  4. day15-python之变量和递归

    1.局部变量与全局变量 #!/usr/bin/env python # -*- coding:utf-8 -*- # name='lhf' # def change_name(): # global ...

  5. LeetCode(67) Add Binary

    题目 Given two binary strings, return their sum (also a binary string). For example, a = "11" ...

  6. xmind 8 readme

    xmind 8 readme   README     LICENSE   XMind 3 is dual licensed under 2 open source licenses: the Ecl ...

  7. mysql汉字转拼音函数

    -- 创建汉字拼音对照临时表 CREATE TABLE IF NOT EXISTS `t_base_pinyin` ( `pin_yin_` ) CHARACTER SET gbk NOT NULL, ...

  8. Leetcode 229.求众数II

    求众数II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: ...

  9. 【java基础 4】树形结构数据呈现的非递归算法(循环)实现

    一.基本概况 上一篇博客介绍到用递归实现树结构数据的查找,那么这篇博客,我就结合自己对于树的理解,然后用一种非递归的方式进行树结构数据的处理.首先,改造数据库表设计,加入度的概念: 首先,layer的 ...

  10. 【思维+贪心】codeforces Game of the Rows

    http://codeforces.com/contest/839/problem/B [题意] 给定n组人,告诉每组人的人数,这些人要在飞机上坐座位 飞机上座位的分布看图可以知道,12  3456 ...