[codeforces722C]Destroying Array

试题描述

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

输入示例

  1.  

输出示例

  1.  

数据规模及约定

见“输入

题解

倒着做,用并查集。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <stack>
  6. #include <vector>
  7. #include <queue>
  8. #include <cstring>
  9. #include <string>
  10. #include <map>
  11. #include <set>
  12. using namespace std;
  13.  
  14. const int BufferSize = 1 << 16;
  15. char buffer[BufferSize], *Head, *Tail;
  16. inline char Getchar() {
  17. if(Head == Tail) {
  18. int l = fread(buffer, 1, BufferSize, stdin);
  19. Tail = (Head = buffer) + l;
  20. }
  21. return *Head++;
  22. }
  23. int read() {
  24. int x = 0, f = 1; char c = Getchar();
  25. while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
  26. while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
  27. return x * f;
  28. }
  29.  
  30. #define maxn 100010
  31. #define LL long long
  32. int val[maxn], ord[maxn];
  33. LL ans[maxn];
  34.  
  35. int fa[maxn];
  36. LL sum[maxn];
  37. int findset(int x) { return x == fa[x] ? x : fa[x] = findset(fa[x]); }
  38.  
  39. int main() {
  40. int n = read();
  41. for(int i = 1; i <= n; i++) val[i] = read();
  42. for(int i = 1; i <= n; i++) ord[i] = read();
  43.  
  44. for(int i = 1; i <= n; i++) sum[i] = val[i], fa[i] = 0;
  45. LL mx = 0;
  46. for(int i = n; i; i--) {
  47. ans[i] = mx;
  48. int u = ord[i];
  49. fa[u] = u;
  50. if(fa[u-1]) {
  51. int v = findset(u - 1);
  52. fa[v] = u; sum[u] += sum[v];
  53. }
  54. if(fa[u+1]) {
  55. int v = findset(u + 1);
  56. fa[v] = u; sum[u] += sum[v];
  57. }
  58. mx = max(mx, sum[u]);
  59. }
  60.  
  61. for(int i = 1; i <= n; i++) printf("%I64d\n", ans[i]);
  62.  
  63. return 0;
  64. }

[codeforces722C]Destroying Array的更多相关文章

  1. CodeForces-722C Destroying Array 并查集 离线操作

    题目链接:https://cn.vjudge.net/problem/CodeForces-722C 题意 给个数组,每次删除一个元素,删除的元素作为一个隔断,问每次删除后该元素左右两边最大连续和 思 ...

  2. CodeForces722C Destroying Array【瞎搞】

    题意: 先给你一个序列,然后给你n个1-n的一个数,让你求前i个元素销毁的时候,区间字段和区间最大: 思路: 离线处理,维护新区间首尾位置的起点和终点,倒着处理: #include <bits/ ...

  3. Codeforces 722C. Destroying Array

    C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. CF722C. Destroying Array[并查集 离线]

    链接:Destroying Array C. Destroying Array time limit per test 1 second memory limit per test 256 megab ...

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

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

  7. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维

    原题中需要求解的是按照它给定的操作次序,即每次删掉一个数字求删掉后每个区间段的和的最大值是多少. 正面求解需要维护新形成的区间段,以及每段和,需要一些数据结构比如 map 和 set. map< ...

  8. [CF722C] Destroying Array

    C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. [并查集+逆向思维]Codeforces Round 722C Destroying Array

    Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. Windows下降权MYSQL和apche的运行级别(普通用户权限运行)

    1.MYSQL的降权运行  新建立一个用户比如mysql  net user mysql microsoft /add  net localgroup users mysql /del  不属于任何组 ...

  2. Bootstrap中的datetimepicker浅谈

    从古至今,人们都习惯用某个时间来标记某个事件的发生.我们在写管理后台的时候,重中之中也是这个时间的设置.在问题出现的时候,我们是查看日志的时候,就可以根据这个时间段来查找这个问题出现点. 在使用时间控 ...

  3. IOS存储目录documents你在哪里啊

    iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. documents,tmp,app,Library. (NSHomeDirectory ...

  4. 无法登录phpmyadmin,报1130错误

    分析过程及解决方案: mysql的1130错误是远程连接的用户无远程权限问题导致.解决方案:在本机登入mysql后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从”loc ...

  5. SQL Server中的事务日志管理的阶梯,级别1:事务日志概述

    SQL Server中的事务日志管理的阶梯,级别1:事务日志概述 翻译:刘琼滨 谢雪妮 许雅莉 赖慧芳 级别1:事务日志概述 事务日志是一个文件,其中SQL服务器存储了所有与日志文件关联的数据库执行的 ...

  6. UVA 11419 SAM I AM (最小点覆盖,匈牙利算法)

    题意:给一个r*c的矩阵,某些格子中可能有一些怪物,可以在一行或一列防止一枚大炮,大炮会扫光整行/列的怪,问最少需要多少炮?输出炮的位置. 思路: 先每行和列都放一个炮,把炮当成点,把怪当成边,一边连 ...

  7. COGS 2098. Asm.Def的病毒

    ★☆   输入文件:asm_virus.in   输出文件:asm_virus.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] “这就是我们最新研制的,世界上第一种可持 ...

  8. Android(java)学习笔记166:上下文的区分

    1.两种上下文:  (1)Activity.this                               界面的上下文 (2)getApplicationContext()         整 ...

  9. liunx中安装软件的几种方式

    服务器安装包一般有四种方式 1.源代码包安装 自由度高  需要预编译,安装速度慢    2.rpm包手动安装   安装的缺点是文件的关联性太大 3. 二进制tar.gz格式 直接解压即可 如tomca ...

  10. VBA Promming——入门教程

    VBA Visual Basic for Applications(VBA)是Visual Basic的一种宏语言,是微软开发出来在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言.主要能用来 ...