Codeforces 1006C:Three Parts of the Array(前缀和+map)
题目链接:http://codeforces.com/problemset/problem/1006/C
(CSDN又改版了,复制粘贴来过来的题目没有排版了,好难看,以后就截图+题目链接了)
题目截图:
题意:一个长度为n的数组,按顺序分成三部分,要求第一部分和第三部分元素的和相等(每一部分都可以没有元素,没有元素的部分元素和为0),求出第一部分和第三部分最大的相等的元素和,如果没有输出0
实现:开两个数组记录下数组的前缀和,后缀和(不知道这样叫对不对),然后用map记录下每个前缀和,后缀和指向的位置,用vis对后缀和的元素标记,然后利用vis查找前缀和中是否有和后缀和相同的位置,并且比较这两个位置的大小,如果前缀和的位置小于后缀和的位置,更新ans的值,否则停止循环
(说的好乱,完全不知道在说什么,以上废话自动忽略直接看代码吧,请自动忽略代码中的注释)
还有vis标记要用map标记!!!不要用数组,数太大,超范围了!!!
AC代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e6+10;
using namespace std;
ll a[maxn],b[maxn];
ll first[maxn],last[maxn];//前缀和,后缀和
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
map<ll,int>mmp;//标记前缀和的位置
map<ll,int>mmmp;//标记后缀和的位置
map<ll,int>vis;//记录后缀和是否出现
vis.clear();
ll n;
cin>>n;
ms(first);
ms(last);
for(int i=1;i<=n;i++)
{
cin>>a[i];
first[i]=first[i-1]+a[i];
mmp[first[i]]=i;//第i个数的前缀和指向i
b[n-i+1]=a[i];//翻转a数组,存入b中
}
for(int i=1;i<=n;i++)
{
last[i]=last[i-1]+b[i];
mmmp[last[i]]=n-i+1;
vis[last[i]]=1;//记录下后缀和,
}
ll ans=0;
for(int i=1;i<=n;i++)
{
if(vis[first[i]])//如果第i个数的前缀和在last中出现过
{
int x=mmp[first[i]];
int y=mmmp[first[i]];
if(x<y)//如果first和last数组无重叠元素
{
ans=max(ans,first[i]);
}
else
break;
}
}
cout<<ans<<endl;
return 0;
}
Codeforces 1006C:Three Parts of the Array(前缀和+map)的更多相关文章
- 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 ...
- Codeforces 221d D. Little Elephant and Array
二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...
- 分享非常有用的Java程序 (关键代码)(五)---把 Array 转换成 Map
原文:分享非常有用的Java程序 (关键代码)(五)---把 Array 转换成 Map import java.util.Map; import org.apache.commons.lang.Ar ...
- Codeforces 479E. Riding in a Lift (dp + 前缀和优化)
题目链接:http://codeforces.com/contest/479/problem/E 题意: 给定一个启示的楼层a,有一个不能去的楼层b,对于你可以去的下一个楼层必须满足你 ...
- Codeforces Round #181 (Div. 2) A. Array 构造
A. Array 题目连接: http://www.codeforces.com/contest/300/problem/A Description Vitaly has an array of n ...
- 1042.D Petya and Array 前缀 + 树状数组
11.19.2018 1042.D Petya and ArrayNew Point: 前缀 + 树状数组 :树状数组逐个维护前缀个数 Describe: 给你一个数组,一个标记数,问你有多少区间[l ...
- [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 ...
- Codeforces Round #284 (Div. 1) C. Array and Operations 二分图最大匹配
题目链接: http://codeforces.com/problemset/problem/498/C C. Array and Operations time limit per test1 se ...
随机推荐
- rest-framework框架的基本组件
快速实例 Quickstart 大致步骤 (1)创建表,数据迁移 (2)创建表序列化类BookSerializer class BookSerializer(serializers.Hyperlink ...
- array2xml xml2array
array2xml/** * * 将简单数组转化为简单的xml * @param string $data 要进行转化的数组 * @param string $tag ...
- js事件处理-整理
<!-- 作者:gentiana@163.com 时间:2016-3-10 描述:js事件处理 --> <!DOCTYPE html> <html> <hea ...
- ACM ICPC, JUST Collegiate Programming Contest (2018) Solution
A:Zero Array 题意:两种操作, 1 p v 将第p个位置的值改成v 2 查询最少的操作数使得所有数都变为0 操作为可以从原序列中选一个非0的数使得所有非0的数减去它,并且所有数不能 ...
- Restoring Numbers
D. Restoring Numbers ...
- 基于SSH RSA的信任关系
RSA 非对称加密算法 client ---> server 私钥 公钥 1. 客户端生成密钥对 ssh-keygen -t rsa 执行后产生的密钥对会分别追加写到当前用户家目录下的以下文 ...
- Web前端学习笔记之前端跨域知识总结
0x00 前言 相信每一个前端er对于跨域这两个字都不会陌生,在实际项目中应用也是比较多的.但跨域方法的多种多样实在让人目不暇接.老规矩,碰到这种情况,就只能自己总结一篇博客,作为记录. 0x01 什 ...
- P1757 通天之分组背包 / hdu1712 ACboy needs your help (分组背包入门)
P1757 通天之分组背包 hdu1712 ACboy needs your help hdu1712题意:A[i][j]表示用j天学习第i个课程能够得到A[i][j]的收益,求m天内获得的收益最大值 ...
- Git WorkBehavior
https://tortoisegit.org/docs/tortoisegit/tgit-dug-showlog.html Repository Demo https://github.com/Ch ...
- JS判断输入值为正整数
JS中的test是原来是JS中检测字符串中是否存在的一种模式,JS输入值是否为判断正整数代码: <script type=”text/javascript”> function test( ...