[CF722C] 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.
如果正着考虑我们不好处理删除元素;
所以时光倒流, 我们从后往前加元素, 这样题就变成了加入元素;
于是我们可以用并查集维护联通性, 每次加入一个元素, 我们就可以把它两边的联通块合并起来;
然后每次取max就是当前的答案;
有一个细节,就是你不能合并一个还未“出现”的元素, 所以我们要开一个bool数组判断是否出现过;
#include <iostream>
#include <cstdio>
using namespace std;
int n, a[], p[];
int fa[];
long long val[];
int Find(int x){return x==fa[x]?x:fa[x]=Find(fa[x]);}
long long ans;
long long put[];
bool des[];
int main()
{
scanf("%d", &n);
for (register int i = ; i <= n ; i ++) scanf("%d", &a[i]);
for (register int i = ; i <= n ; i ++) scanf("%d", &p[i]);
for (register int i = ; i <= n + ; i ++) fa[i] = i;
for (register int i = n ; i >= ; i --){
put[i] = ans;
des[p[i]] = ;
int l = p[i] - , r = p[i] + ;
int fl = Find(l), fr = Find(r), fp = Find(p[i]);
if (fl != fp and des[l])fa[fl] = fp, val[fp] += val[fl];
if (fr != fp and des[r])fa[fr] = fp, val[fp] += val[fr];
val[fp] += a[p[i]];
ans = max(ans, val[fp]);
}
for (register int i = ; i <= n ; i ++) printf("%lld\n", put[i]);
return ;
}
[CF722C] Destroying Array的更多相关文章
- CF722C. Destroying Array[并查集 离线]
链接:Destroying Array C. Destroying Array time limit per test 1 second memory limit per test 256 megab ...
- Codeforces 722C. Destroying Array
C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 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
C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...
- [codeforces722C]Destroying Array
[codeforces722C]Destroying Array 试题描述 You are given an array consisting of n non-negative integers a ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维
原题中需要求解的是按照它给定的操作次序,即每次删掉一个数字求删掉后每个区间段的和的最大值是多少. 正面求解需要维护新形成的区间段,以及每段和,需要一些数据结构比如 map 和 set. map< ...
- [并查集+逆向思维]Codeforces Round 722C Destroying Array
Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- 【37.38%】【codeforces 722C】Destroying Array
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- CodeForces - 722C Destroying Array (并查集/集合的插入和删除)
原题链接:https://vjudge.net/problem/511814/origin Description: You are given an array consisting of n no ...
随机推荐
- Tempter of the Bone(DFS+剪枝)
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
- Nginx正确配置Location
文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. 之前已经讲过Nginx的基本配置,本篇文章主要对Nginx中Location指令的作用进行介绍.本篇文章主要对 ...
- linux中必备常用支持库的安装(CentOS)
在CentOS安装软件的时候,可能缺少一部分支持库,而报错.这里首先安装系统常用的支持库.那么在安装的时候就会减少很多的错误的出现 yum install -y gcc gdb strace gcc- ...
- Windows上部署MySql
下载安装包 最新版本可以在 MySQL 下载中下载. 下载完后,我们将 zip 包解压到相应的目录,这里我将解压后的文件夹放在 D:\Program Files\Java\mysql-8.0.16-w ...
- jquery的api以及用法总结-数据/操作/事件
数据 .data() 在匹配元素上存储任意相关数据或返回匹配的元素集合中的第一个元素的给定名称的数据存储的值 .data(obj) 一个用于更新数据的键/值对 .data()方法允许我们再dom元素上 ...
- Kafka系列一之架构介绍和安装
Kafka架构介绍和安装 写在前面 还是那句话,当你学习一个新的东西之前,你总得知道这个东西是什么?这个东西可以用来做什么?然后你才会去学习它,使用它.简单来说,kafka既是一个消息队列,如今,它也 ...
- MapReduce之Job提交流程源码和切片源码分析
hadoop2.7.2 MapReduce Job提交源码及切片源码分析 首先从waitForCompletion函数进入 boolean result = job.waitForCompletion ...
- 学习WEBAPI第一天
目录 WebApi: 通过操作对象来实现操作标签的目的 一.DOM 中常用的操作 二.doucument对象 三.获取元素 四.注册事件 五.操作元素的属性 六.当页面加载完时,script代码已经执 ...
- 设计模式之UML类图以及类间关系
类图是描述系统中的类,以及各个类之间的关系的静态视图.能够让我们在正确编写代码以前对系统有一个全面的认识.类图是一种模型类型,确切的说,是一种静态模型类型.类图表示类.接口和它们之间的协作关系. 以下 ...
- myql忽略大小写问题解决
linux系统下启动mysql默认是区分大小写的,如果刚好项目中使用的表名与数据库中表名大小写有冲突,此时就需要忽略mysql表名大小写了. 解决方式一: 1.关闭数据库 mysqladmin -ur ...