Codeforces466C Number of Ways
题目链接:
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的更多相关文章
- cf466C Number of Ways
C. Number of Ways time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 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 ...
- 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 ...
- 【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 ...
- codeforce Number of Ways(暴力)
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ...
- codeforces 466C. Number of Ways 解题报告
题目链接:http://codeforces.com/problemset/problem/466/C 题目意思:给出一个 n 个数的序列你,问通过将序列分成三段,使得每段的和都相等的分法有多少种. ...
- Codeforces - 466C - Number of Ways - 组合数学
https://codeforces.com/problemset/problem/466/C 要把数据分为均等的非空的三组,那么每次确定第二个分割点的时候把(除此之外的)第一个分割点的数目加上就可以 ...
- [Codeforces 466C] Number of Ways
[题目链接] https://codeforces.com/contest/466/problem/C [算法] 维护序列前缀和 , 枚举中间一段即可 , 详见代码 时间复杂度 : O(N) [代码] ...
- 【Codeforces 466C】Number of Ways
[链接] 我是链接,点我呀:) [题意] 让你把数组分成3个连续的部分 每个部分的和要一样 问你有多少种分法 [题解] 先处理出来num[i] 表示i..n这里面有多少个j 满足aft[j] = af ...
随机推荐
- 关闭Visual Studio 2015 关闭单击打开文件的功能
工具-->选项-->环境-->选项卡和窗口-->预览选项卡 去掉“在解决方案资源管理器中预览选定的文件(在按住Alt的同时单击可避免预览)(X)”的勾选
- OERR: ORA-1410 "invalid ROWID" Master Note / Troubleshooting, Diagnostic and Solution (文档ID 1410.1)
OERR: ORA-1410 "invalid ROWID" Master Note / Troubleshooting, Diagnostic and Solution (文档I ...
- MariaDB数据库(二)
1. MariaDB数据类型 MariaDB数据类型可以分为数字,日期和时间以及字符串值. 使用数据类型的原则:够用就行,尽量使用范围小的,而不用大的. 1.1 常用的数据类型 整数:int,bit ...
- Python中比元组更好用的namedtuple
一.思考 1.什么是元组? 不可变的序列类型 "不能修改的列表" 2.元组支持哪些操作? 元组是序列类型,支持序列类型的所有操作 通过索引取值 one_tuple = (" ...
- 牛客网 牛可乐发红包脱单ACM赛 B题 小a的旅行计划
[题解] 题意其实就是把n个物品分成4个集合,其中三个集合不可以为空(只属于A.只属于B.AB的交),一个集合空或者非空都可以(不属于A也不属于B),问有多少种方案. 考虑容斥,4个集合都不为空的方案 ...
- ArrayList集合的遍历
ArrayLIstDemo3.java import java.util.ArrayList; public class ArrayListDemo3 { public static void mai ...
- Python接口测试之moco
在现在的软件开发过程中,特别是app的部分,需要的很多数据以及内容,都是来自server端的API,但是不能保证 在客户端开发的时候,api在server端已经开发完成,专门等着前端来调用,理想的情况 ...
- selenium grid使用(windows+centos7.4)
windows作为hub,centos7.4作为node. firefox用的centos7自带的52版本,懒得更新. vm虚拟机必须设置成bridge模式上网,否则报错.具体参见博文:Vmware改 ...
- 大数据学习——采集目录到HDFS
采集需求:某服务器的某特定目录下,会不断产生新的文件,每当有新文件出现,就需要把文件采集到HDFS中去 根据需求,首先定义以下3大要素 l 采集源,即source——监控文件目录 : spoold ...
- 大数据学习——hdfs客户端操作
package cn.itcast.hdfs; import org.apache.commons.io.IOUtils; import org.apache.hadoop.conf.Configur ...