CF 1006C Three Parts of the Array【双指针/前缀和/后缀和/二分】
You are given an array d1,d2,…,dn consisting of n integer numbers.
Your task is to split this array into three parts (some of which may be empty) in such a way that each element of the array belongs to exactly one of the three parts, and each of the parts forms a consecutive contiguous subsegment (possibly, empty) of the original array.
Let the sum of elements of the first part be sum1, the sum of elements of the second part be sum2 and the sum of elements of the third part be sum3. Among all possible ways to split the array you have to choose a way such that sum1=sum3 and sum1 is maximum possible.
More formally, if the first part of the array contains a elements, the second part of the array contains b elements and the third part contains c elements, then:
sum1=∑1≤i≤adi,
sum2=∑a+1≤i≤a+bdi,
sum3=∑a+b+1≤i≤a+b+cdi.
The sum of an empty array is 0.
Your task is to find a way to split the array such that sum1=sum3 and sum1 is maximum possible.
Input
The first line of the input contains one integer n (1≤n≤2⋅105) — the number of elements in the array d.
The second line of the input contains n integers d1,d2,…,dn (1≤di≤109) — the elements of the array d.
Output
Print a single integer — the maximum possible value of sum1, considering that the condition sum1=sum3 must be met.
Obviously, at least one valid way to split the array exists (use a=c=0 and b=n).
Examples
Input
5
1 3 1 1 4
Output
5
Input
5
1 3 2 1 4
Output
4
Input
3
4 1 2
Output
0
Note
In the first example there is only one possible splitting which maximizes sum1: [1,3,1],[ ],[1,4].
In the second example the only way to have sum1=4 is: [1,3],[2,1],[4].
In the third example there is only one way to split the array: [ ],[4,1,2],[ ].
【题意】:将一个长度为n的数组划分为a,b,c三段(每一段都可以为空)使得a段和c段的和相等,问a段的和的最大值是多少?
【分析】:双指针求前缀和与后缀和进行大小比较,一遇到相等时打擂台求最大。注意要求和所以用LL,不然会WA10!
【代码】:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
#define ll long long
ll n,m;
ll a[N];
/*
将一个长度为n的数组划分为a,b,c三段(每一段都可以为空)
使得a段和c段的和相等,问a段的和的最大值是多少?
*/
int main()
{
while(~scanf("%lld",&n))
{
ll Max = 0, ls, rs;
for(ll i=0; i<n; i++)
{
scanf("%lld",&a[i]);
}
ll L = 0, R = n-1;
ls = a[L];
rs = a[R];
while(L < R)
{
if(ls == rs)
{
Max = max(Max,ls);
L++;
R--;
ls += a[L];
rs += a[R];
}
else if(ls > rs)
{
R--;
rs += a[R];
}
else
{
L++;
ls += a[L];
}
}
cout<<Max<<endl;
}
}
【二分】:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2*1e5+10;
ll n,a[maxn];
ll pre[maxn],suf[maxn];
/*
求前缀和,后缀和。然后从大到小枚举后缀,在前缀中查找相等,如果能找到且a,c两段不重叠,那么就是答案。注意开long long
*/
int main()
{
while(~scanf("%lld",&n))
{
ll Max = 0;
memset(pre,0,sizeof(pre));
memset(suf,0,sizeof(suf));
for(int i=0; i<n;i++)
{
scanf("%lld",&a[i]);
}
pre[0] = a[0];
for(int i=1;i<n;i++)
pre[i] = pre[i-1] + a[i];
suf[n-1] = a[n-1];
for(int i=n-2; i>=0; i--)
suf[i] = suf[i+1] + a[i];
for(int i=0; i<n; i++)
{
int pos = lower_bound(pre,pre+n,suf[i])-pre;
if(pos < i && pre[pos] == suf[i])
{
Max = max(Max,suf[i]);
}
}
printf("%lld\n",Max);
}
}
/*
n-1:4
0 1 2 3 4
1 2 3 4 5
5 9 12
*/
CF 1006C Three Parts of the Array【双指针/前缀和/后缀和/二分】的更多相关文章
- Codeforces 1006C:Three Parts of the Array(前缀和+map)
题目链接:http://codeforces.com/problemset/problem/1006/C (CSDN又改版了,复制粘贴来过来的题目没有排版了,好难看,以后就截图+题目链接了) 题目截图 ...
- CodeForces1006C-Three Parts of the Array
C. Three Parts of the Array time limit per test 1 second memory limit per test 256 megabytes input s ...
- <二分查找+双指针+前缀和>解决子数组和排序后的区间和
<二分查找+双指针+前缀和>解决子数组和排序后的区间和 题目重现: 给你一个数组 nums ,它包含 n 个正整数.你需要计算所有非空连续子数组的和,并将它们按升序排序,得到一个新的包含 ...
- [codeForce-1006C]-Three Parts of the Array (简单题)
You are given an array d1,d2,…,dnd1,d2,…,dn consisting of nn integer numbers. Your task is to split ...
- 【CF】220B Little Elephant and Array
区间动态统计的好题. /* */ #include <iostream> #include <string> #include <map> #include < ...
- CF1006C 【Three Parts of the Array】
二分查找水题 记$sum[i]$为$d[i]$的前缀和数组 枚举第一段区间的结尾$i$ 然后二分出$lower$_$bound(sum[n]-sum[i])$的位置$x$,如果$sum[x]$与$su ...
- [BZOJ3277/BZOJ3473] 串 - 后缀数组,二分,双指针,ST表,均摊分析
[BZOJ3277] 串 Description 现在给定你n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串(注意包括本身). Solution 首先将所有串连 ...
- LeetCode Find Minimum in Rotated Sorted Array 旋转序列找最小值(二分查找)
题意:有一个有序序列A,其内部可能有部分被旋转了,比如A[1...n]被转成A[mid...n]+A[1...mid-1],如果被旋转,只有这种形式.问最小元素是?(假设没有重复元素) 思路:如果是序 ...
- 【BZOJ3277/3473】串/字符串 后缀数组+二分+RMQ+双指针
[BZOJ3277]串 Description 字符串是oi界常考的问题.现在给定你n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串(注意包括本身). Inpu ...
随机推荐
- 【bzoj4998】星球联盟 LCT+并查集
题目描述 在遥远的S星系中一共有N个星球,编号为1…N.其中的一些星球决定组成联盟,以方便相互间的交流.但是,组成联盟的首要条件就是交通条件.初始时,在这N个星球间有M条太空隧道.每条太空隧道连接两个 ...
- visio应用程序相关设置-选项-常规
1.用户名称,可读写 ApplicationSettings.UserName m_Visio.Window.Application.Settings.UserName = "BEIJING ...
- 雪碧图background-position的rem用法
background的雪碧图配合rem就正常写即可,要加上background-size,大小为sprites的原图尺寸,宽高为一帧的尺寸.例如: .player{ width: 2.32rem; / ...
- vue父组件中修改子组件样式
1. 使用全局样式 <style> /* 全局样式 */ </style> <style scoped> /* 本地样式 */ </style> 2. ...
- Vue根据URL传参来控制全局 console.log 的开关
如果你的项目中console.log了很多信息,但是发到生产环境上又不想打印这些信息,这时候就需要设置一个全局变量,如:debug, 用正则匹配一下参数: const getQueryStr = (n ...
- HDU 2126 01背包(求方案数)
Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- json解析之jackson
对于json格式的数据解析现在越来越多了,之前介绍了两种:fastjson和net.sf.json解析. 今天又有一个jackson解析.不过相对于之前两种,这种感觉稍微笨拙些.呵呵,还是了解下吧: ...
- JavaScript 被忽视的细节
语句/表达式 换个角度理解语句(statemaents)和表达式(expressions):表达式不会改变程序的运行状态,而语句会.还有一种叫做表达式语句,可以理解为表达式和语句的交集,如({a:1} ...
- Step-By-Step: Setting up Active Directory in Windows Server 2016
There are interesting new features now made available in Windows Server 2016 such as time based grou ...
- DOM操作的一个小坑
最近在苦读<JavaScript高级程序教程>,真不愧是前端圣经,学到了很多东西. nodeList.NameNodeMap.HTMLCollection这三个集合是动态的!每当文档发生变 ...