题目链接: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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Codeforces 221d D. Little Elephant and Array

    二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...

  4. 分享非常有用的Java程序 (关键代码)(五)---把 Array 转换成 Map

    原文:分享非常有用的Java程序 (关键代码)(五)---把 Array 转换成 Map import java.util.Map; import org.apache.commons.lang.Ar ...

  5. Codeforces 479E. Riding in a Lift (dp + 前缀和优化)

    题目链接:http://codeforces.com/contest/479/problem/E 题意:         给定一个启示的楼层a,有一个不能去的楼层b,对于你可以去的下一个楼层必须满足你 ...

  6. Codeforces Round #181 (Div. 2) A. Array 构造

    A. Array 题目连接: http://www.codeforces.com/contest/300/problem/A Description Vitaly has an array of n ...

  7. 1042.D Petya and Array 前缀 + 树状数组

    11.19.2018 1042.D Petya and ArrayNew Point: 前缀 + 树状数组 :树状数组逐个维护前缀个数 Describe: 给你一个数组,一个标记数,问你有多少区间[l ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. C++ builder 书籍推荐

    china-pub网上书店c++builder书籍专区,本专区专门为c++builder学习者提供目前最为畅销实用的c++builder技术书籍,通过对本专区c++builder书籍的了解,让您学习c ...

  2. ssm所需要的pom(jre8、tomcat8、spring4)

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  3. 11. Container With Most Water(装最多的水 双指针)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  4. HDU - 2844 Coins(多重背包+完全背包)

    题意 给n个币的价值和其数量,问能组合成\(1-m\)中多少个不同的值. 分析 对\(c[i]*a[i]>=m\)的币,相当于完全背包:\(c[i]*a[i]<m\)的币则是多重背包,考虑 ...

  5. OpenCV在未知相机内参数情况下的立体图像矫正方法及注意事项

    http://blog.sina.com.cn/s/blog_4298002e01013yb8.html 很多时候我们不知道摄像机的内参数矩阵,并且我们也不太关注内参数到底是多少,因为我们仅仅关心如何 ...

  6. 【转载】package-info

    本文是转载,原文地址:http://strong-life-126-com.iteye.com/blog/806246 package-info.java对于经常使用外部包的程序员来说应该是熟悉陌生人 ...

  7. P2043 质因子分解(阶乘的质因数分解)

    P2043 质因子分解 对$n!$进行质因数分解的一种高效算法 首先,筛出$<=n$的素数 蓝后,对$n$反复除以$prime$,同时$cnt+=n/prime$ $n!$中含有该$prime$ ...

  8. P1052 过河(离散化+dp)

    P1052 过河 dp不难,重点是要想到离散化. 石子个数$<=100$意味着有大量空间空置,我们可以缩掉这些空间. 实现的话自己yy下就差不多了. #include<iostream&g ...

  9. Linux内核分析第一周-通过分析汇编代码理解计算机是如何工作的

    首先,我们先写一个简单的C语言程序,如下: int g(int x) { return x +3; } int f(int x) { return g(x); } int main(void) { r ...

  10. c++ 列表删除元素(erase)

    #include <list> #include <iostream> #include <iterator> using namespace std; int m ...