Codeforces 722C. Destroying Array
1 second
256 megabytes
standard input
standard output
You are given an array consisting of n non-negative integers a1, a2, ..., an.
You are going to destroy integers in the array one by one. Thus, you are given the permutation of integers from 1 to n defining the order elements of the array are destroyed.
After each element is destroyed you have to find out the segment of the array, such that it contains no destroyed elements and the sum of its elements is maximum possible. The sum of elements in the empty segment is considered to be 0.
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the length of the array.
The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).
The third line contains a permutation of integers from 1 to n — the order used to destroy elements.
Print n lines. The i-th line should contain a single integer — the maximum possible sum of elements on the segment containing no destroyed elements, after first i operations are performed.
4
1 3 2 5
3 4 1 2
5
4
3
0
5
1 2 3 4 5
4 2 3 5 1
6
5
5
1
0
8
5 5 4 4 6 6 5 5
5 2 8 7 1 3 4 6
18
16
11
8
8
6
6
0
Consider the first sample:
- Third element is destroyed. Array is now 1 3 * 5. Segment with maximum sum 5 consists of one integer 5.
- Fourth element is destroyed. Array is now 1 3 * * . Segment with maximum sum 4 consists of two integers 1 3.
- First element is destroyed. Array is now * 3 * * . Segment with maximum sum 3 consists of one integer 3.
- Last element is destroyed. At this moment there are no valid nonempty segments left in this array, so the answer is equal to 0.
题目大意:给出一个长度为n的序列,每次删除一个(删除之后序列断开),求最大连续子段和。(序列中数为正整数)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
#include<ctime>
#include<cstring>
#define yyj(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout);
#define llg long long
#define maxn 100010
#define md 50000
#define inf 0x7fffffff
using namespace std;
llg i,j,k,n,m,a[maxn],f1,f2,maxl,c[maxn],ans[maxn],dad[maxn],bj[maxn],x,val[maxn]; llg find (llg x)
{
return dad[x]==x?x:dad[x]=find(dad[x]);
} int main()
{
// yyj("c");
cin>>n;
for (i=;i<=n;i++) scanf("%I64d",&a[i]),dad[i]=i;
for (i=;i<=n;i++) scanf("%I64d",&c[i]);
for (i=n;i>=;i--)
{
maxl=max(maxl,a[c[i]]);
bj[c[i]]=; val[c[i]]+=a[c[i]];
x=c[i];
if (bj[x-]!= && bj[x+]!=)
{
f1=find(x-);
dad[find(x)]=f1;
f2=find(x+);
dad[f2]=f1;
val[f1]+=a[x]+val[x+];
maxl=max(maxl,val[f1]);
}
else
if (bj[x-]!=)
{
f1=find(x-);
dad[find(x)]=f1;
val[f1]+=val[x];
maxl=max(maxl,val[f1]);
}
else
if (bj[x+]!=)
{
f2=find(x+);
dad[f2]=find(x);
val[find(x)]+=val[f2];
maxl=max(maxl,val[find(x)]);
}
else
{
dad[x]=x;
val[x]=a[x];
maxl=max(maxl,a[x]);
}
ans[i]=maxl;
}
for (i=;i<=n;i++) cout<<ans[i]<<endl;
cout<<;
return ;
}
Codeforces 722C. Destroying Array的更多相关文章
- CodeForces - 722C Destroying Array (并查集/集合的插入和删除)
原题链接:https://vjudge.net/problem/511814/origin Description: You are given an array consisting of n no ...
- CodeForces 722C Destroying Array (并查集)
题意:给定 n 个数,然后每次破坏一个位置的数,那么剩下的连通块的和最大是多少. 析:用并查集来做,从后往前推,一开始什么也没有,如果破坏一个,那么我们就加上一个,然后判断它左右两侧是不是存在,如果存 ...
- [并查集+逆向思维]Codeforces Round 722C Destroying Array
Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 带权并查集
C. Destroying Array 题目连接: http://codeforces.com/contest/722/problem/C Description You are given an a ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维
原题中需要求解的是按照它给定的操作次序,即每次删掉一个数字求删掉后每个区间段的和的最大值是多少. 正面求解需要维护新形成的区间段,以及每段和,需要一些数据结构比如 map 和 set. map< ...
- CF722C. Destroying Array[并查集 离线]
链接:Destroying Array C. Destroying Array time limit per test 1 second memory limit per test 256 megab ...
- Codeforces 482B Interesting Array(线段树)
题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...
- Codeforces 1077C Good Array 坑 C
Codeforces 1077C Good Array https://vjudge.net/problem/CodeForces-1077C 题目: Let's call an array good ...
- codeforces 482B. Interesting Array【线段树区间更新】
题目:codeforces 482B. Interesting Array 题意:给你一个值n和m中操作,每种操作就是三个数 l ,r,val. 就是区间l---r上的与的值为val,最后问你原来的数 ...
随机推荐
- 我的AngularJS 学习之旅
我的AngularJS 学习之旅 基础篇 1.Angular的 起源 2.比较Web 页面实现的几种方式 3.一些基本术语 4.Angular与其他框架的兼容性 5.总结 6.综合实例 很早之前就 ...
- Android Animation(动画)
前言 Android 平台提供实现动画的解决方案(三种) 一.3.0以前,android支持两种动画: (1)Frame Animation:顺序播放事先做好的图像,与gif图片原理类似,是一种逐帧动 ...
- SpringMVC之控制器的单例和多例管理
版权声明:本文为博主原创文章,未经博主允许不得转载. 在使用Spring3对控制器Controller进行bean管理时,如果要对控制器是否单例进行管理. 有两种方式配置多例模式: 1.springX ...
- WCF入门-项目间调用服务
练习WCF入库,添加了一个WCF项目,同时添加了一个控制台程序,在控制台程序启动时,调用WCF服务,报错. 控制台程序代码为: namespace WcfConsumer { class Progra ...
- 用node-inspector调试Node.js(转自NOANYLOVE'S BLOG)
原文地址:http://www.noanylove.com/2011/12/node-the-inspector-debugging-node-js/ 用node-inspector调试Node.js ...
- 关于xfce桌面程序启动失败
当双击桌面图标的时候,出现如下错误信息:Process org.xfce.FileManager exited with status 1 于是做出如下尝试: 1. ps aux | grep Fil ...
- yii2输出sql语句
yii2如何输出具体的查询的sql语句: $query = User::find() ->where(['id'=>[1,2,3,4]) ->select(['username']) ...
- 图说js中的this——深入理解javascript中this指针
没搞错吧!js写了那么多年,this还是会搞错!没搞错,javascript就是回搞错! ………… 文章来源自——周陆军的个人网站:http://zhoulujun.cn/zhoulujun/html ...
- margin padding
margin and padding有时候可以实现相同的效果但是 用padding的场合更多 2者的区别和用法的细节我还是没掌握好
- Scala基础语法
/* 学慕课网上<Scala程序设计>课程跟着敲的代码 作为代码参考也是很好的 在scala_ide.org上下载eclipse IDE,新建一个worksheet,就可以像在xcode的 ...