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 ...
随机推荐
- ubuntu16.04(liunx) 离线安装 xgboost (anaconda3,anaconda2共存)
服务器ubuntu 系统同时安装了 anaconda3,anaconda2 ,但服务器没有连接外网,所以 想在python3 环境下安装离线安装xgboost. 主要分2步: 0:进入py3环境 ( ...
- Educational Codeforces Round 55 (Rated for Div. 2) Solution
A. Vasya and Book Solved. 三种方式取$Min$ #include <bits/stdc++.h> using namespace std; #define ll ...
- 《零起点,python大数据与量化交易》
<零起点,python大数据与量化交易>,这应该是国内第一部,关于python量化交易的书籍. 有出版社约稿,写本量化交易与大数据的书籍,因为好几年没写书了,再加上近期"前海智库 ...
- mongodb的存储引擎
mongodb版本为3.4 mongodb存储引起的一些概述 存储引擎是MongoDB的核心组件,负责管理数据如何存储在硬盘和内存上.从MongoDB 3.2 版本开始,MongoDB 支持多数据存储 ...
- Vue学习笔记之Webpack介绍
在这里我仅仅的是对webpack做个讲解,webpack这个工具非常强大,解决了我们前端很繁琐的一些工具流程繁琐的事情.如果感兴趣的同学,简易还是看官网吧. 中文链接地址:https://www.we ...
- linux及安全《Linux内核设计与实现》第二章——20135227黄晓妍
第二章:从内核出发 2.1获取源代码 2.1.1使用git Git:内核开发者们用来管理Linux内核源代码的控制系统. 我们使用git来下载和管理Linux源代码. 2.1.2安装内核源代码(如果使 ...
- 20145326实验四 Android开发基础
20145326实验四 Android开发基础 一.实验内容及步骤 安装 JDK 并配置 JDK 环境变量 找到之前path变量中的jdk文件所在位置并复制. 并用复制的变量名新建一个 JAVA_HO ...
- ExtJS发送POST请求 参数格式为JSON
背景 这要从我比较懒说起.技术框架ExtJS + resteasy,默认请求方式是ajax get,这后台方法就要写很多@QueryParam来获取参数.我比较喜欢前台用ajax post请求,后台方 ...
- Android移动软件开发总结
目录 Android实验参考目录 常用知识点总结 服务绑定bind Service ThreadService使用总结 Service用法总结 Broadcast Receiver用法 Intent使 ...
- UVa 11489 整数游戏
https://vjudge.net/problem/UVA-11489 题意: 给出一个数字串n,两个人轮流从中取出一个数字,要求每次取完之后剩下的数是3的倍数,不能取数者输. 思路: 要想取掉一个 ...