[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 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 sum1sum1, the sum of elements of the second part be sum2sum2 and the sum of elements of the third part be sum3sum3. Among all possible ways to split the array you have to choose a way such that sum1=sum3sum1=sum3and sum1sum1 is maximum possible.
More formally, if the first part of the array contains aa elements, the second part of the array contains bb elements and the third part contains cc elements, then:
The sum of an empty array is 00.
Your task is to find a way to split the array such that sum1=sum3sum1=sum3 and sum1sum1 is maximum possible.
Input
The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in the array dd.
The second line of the input contains nn integers d1,d2,…,dnd1,d2,…,dn (1≤di≤1091≤di≤109) — the elements of the array dd.
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],[ ].
很简单的一道题,开始的时候想多了,两边同时向里面
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
const int MAX = 2e5 + ;
long long n, a[MAX], x;
int main() { long long sum1 = ,sum2 = ,sum0 = ;
memset(a,,sizeof(a));
scanf("%d", &n );
for( int i = ; i <= n; i++ ) {
scanf("%d",&a[i]);
}
int left = , right = n+;
while(left<right){ if(sum1>sum2){
right--;
sum2 += a[right];
if(left==right)break;
}
if(sum2>sum1){
left++;
sum1 += a[left];
if(left==right)break;
}
if(sum1==sum2){
sum0 = sum1;
left++;
right--;
if(left==right)break;
sum1+=a[left];
sum2+=a[right];
} }
cout<<sum0<<endl; return ;
}
[codeForce-1006C]-Three Parts of the Array (简单题)的更多相关文章
- 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 ...
- 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 ...
- leetcode简单题6
今天的华师 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...
- BZOJ 2683: 简单题
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 913 Solved: 379[Submit][Status][Discuss] ...
- 【BZOJ-1176&2683】Mokia&简单题 CDQ分治
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 821[Submit][St ...
- Bzoj4066 简单题
Time Limit: 50 Sec Memory Limit: 20 MBSubmit: 2185 Solved: 581 Description 你有一个N*N的棋盘,每个格子内有一个整数,初 ...
- Bzoj2683 简单题
Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 1071 Solved: 428 Description 你有一个N*N的棋盘,每个格子内有一个整数, ...
- 这样leetcode简单题都更完了
这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...
- [BZOJ2683][BZOJ4066]简单题
[BZOJ2683][BZOJ4066]简单题 试题描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x ...
随机推荐
- Linux改变文件所有者
Linux改变文件所有者 #把当前路径下jsportal文件夹及下的所有文件的所有者改为appmanager组下的appmanager用户.chown -R -v appmanager:appmana ...
- Windows与Linux之间的文件自动同步
问题:在工作中遇到一个场景,需要每天定时将Linux机器上的文件自动同步到Windows机器上. 解决方案有两个: 1.在Windows 机器上设置共享目录,然后在Linux机器上挂载共享 目录,每天 ...
- 【转】修改Android解锁界面
背景 先说说背景吧,这是本人从WinCE系统转到Android之后,接到的第一个任务就是修改Android原生的解锁界面,之前看了两个星期的书和网络博客,Java的也有.Android应用开 ...
- CentOS中用Nexus搭建maven私服,为Hadoop编译提供本地镜像
系统: CentOS release 6.6 (Final) Nexus:nexus-2.8.1-bundle.tar.gz,下载地址:https://sonatype-download.global ...
- 第12章 GPIO输入-按键检测—零死角玩转STM32-F429系列
第12章 GPIO输入—按键检测 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fi ...
- iOS开发-Object-C获取手机设备信息(UIDevice)
一.获取UiDevice设备信息 // 获取设备名称 NSString *name = [[UIDevice currentDevice] name]; // 获取设备系统名称 NSString *s ...
- php对接网易云信视频直播
<?php/** * Created by PhpStorm. * User: lhl * Date: 2019/4/10 * Time: 17:31 */ namespace app\api\ ...
- mapreduce二次排序详解
什么是二次排序 待排序的数据具有多个字段,首先对第一个字段排序,再对第一字段相同的行按照第二字段排序,第二次排序不破坏第一次排序的结果,这个过程就称为二次排序. 如何在mapreduce中实现二次排序 ...
- AWVS使用手册
目录: 0×00.什么是Acunetix Web Vulnarability Scanner ( What is AWVS?) 0×01.AWVS安装过程.主要文件介绍.界面简介.主要操作区域简介(I ...
- 《Arduino 机器人制作指南》唐乐 分享 pdf下载
链接:https://pan.baidu.com/s/1cF7gaQoJXwfY0asnsimFrQ提取码:stq1