题目链接:

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. docker 离线安装

    适用于: 1.内网安装docker 2.内网升级docker debian 8 sudo apt-get updatesudo apt-get install -d apt-transport-htt ...

  2. 【详●析】[GXOI/GZOI2019]逼死强迫症

    [详●析][GXOI/GZOI2019]逼死强迫症 脑子不够用了... [题目大意] 在\(2\times N\)的方格中用\(N-1\)块\(2\times 1\)的方砖和\(2\)块\(1\tim ...

  3. Django reverse函数

    1.总urls.py内容如下: from django.contrib import admin from django.urls import path from django.conf.urls ...

  4. c#导出word文档

    为方便下次遇到不知道去哪找先把它存放在这里,以下是保存导出word主要类方法 public class BiultReportForm { /// <summary>word 应用对象 & ...

  5. asp网页无法打开

    环境:Window 2003.IIS6.Framework1.1 .VS2003 一个WebForm项目里面包含一些asp网页  运行后发现asp页面无法访问 提示:无法找到该页 解决方案: 1. [ ...

  6. jar包、war包、ear包傻傻分不清?

    在工作中,需要在jboss上deploy一个health check的war包,因此了解一下: Jar文件(扩展名为. Jar,Java Application Archive)包含Java类的普通库 ...

  7. [NOIP2002] 提高组 洛谷P1033 自由落体

    题目描述 在高为 H 的天花板上有 n 个小球,体积不计,位置分别为 0,1,2,….n-1.在地面上有一个小车(长为 L,高为 K,距原点距离为 S1).已知小球下落距离计算公式为 d=1/2*g* ...

  8. Codevs 2693 上学路线(施工)

    时间限制: 2 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题目描述 Description 问题描述 你所在的城市街道好像一个棋盘,有a条南北方向的街道和b条东西方向的街道. 南 ...

  9. CSY版最大团,速度快一倍

    #include <bits/stdc++.h> using namespace std; #define REP(i, n) for(int i(0); i < (n); ++i) ...

  10. CodeForces 599B Spongebob and Joke

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...