Codeforces 722C. Destroying Array

C. Destroying Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.

Input

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.

Output

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.

Examples
input
4
1 3 2 5
3 4 1 2
output
5
4
3
0
input
5
1 2 3 4 5
4 2 3 5 1
output
6
5
5
1
0
input
8
5 5 4 4 6 6 5 5
5 2 8 7 1 3 4 6
output
18
16
11
8
8
6
6
0
Note

Consider the first sample:

  1. Third element is destroyed. Array is now 1 3  *  5. Segment with maximum sum 5 consists of one integer 5.
  2. Fourth element is destroyed. Array is now 1 3  *   * . Segment with maximum sum 4 consists of two integers 1 3.
  3. First element is destroyed. Array is now  *  3  *   * . Segment with maximum sum 3 consists of one integer 3.
  4. 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的序列,每次删除一个(删除之后序列断开),求最大连续子段和。(序列中数为正整数)


 


 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<vector>
 7 #include<cmath>
 8 #include<ctime>
 9 #include<cstring>
10 #define yyj(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout);
11 #define llg long long
12 #define maxn 100010
13 #define md 50000
14 #define inf 0x7fffffff
15 using namespace std;
16 llg i,j,k,n,m,a[maxn],f1,f2,maxl,c[maxn],ans[maxn],dad[maxn],bj[maxn],x,val[maxn];
17 
18 llg find (llg x)
19 {
20     return dad[x]==x?x:dad[x]=find(dad[x]);
21 }
22 
23 int main()
24 {
25 //    yyj("c");
26     cin>>n;
27     for (i=1;i<=n;i++) scanf("%I64d",&a[i]),dad[i]=i;
28     for (i=1;i<=n;i++) scanf("%I64d",&c[i]);
29     for (i=n;i>=1;i--)
30     {
31         maxl=max(maxl,a[c[i]]);
32         bj[c[i]]=1; val[c[i]]+=a[c[i]];
33         x=c[i];
34         if (bj[x-1]!=0 && bj[x+1]!=0)
35         {
36             f1=find(x-1);
37             dad[find(x)]=f1;
38             f2=find(x+1);
39                 dad[f2]=f1;
40             val[f1]+=a[x]+val[x+1];
41             maxl=max(maxl,val[f1]);
42         }
43         else
44             if (bj[x-1]!=0)
45             {
46                 f1=find(x-1);
47                 dad[find(x)]=f1;
48                 val[f1]+=val[x];
49                 maxl=max(maxl,val[f1]);
50             }
51         else
52             if (bj[x+1]!=0)
53             {
54                 f2=find(x+1);
55                 dad[f2]=find(x);
56                 val[find(x)]+=val[f2];
57                 maxl=max(maxl,val[find(x)]);
58             }
59             else
60             {
61                 dad[x]=x;
62                 val[x]=a[x];
63                 maxl=max(maxl,a[x]);
64             }
65         ans[i]=maxl;
66     }
67     for (i=2;i<=n;i++) cout<<ans[i]<<endl;
68     cout<<0;
69     return 0;
70 }

 

转载于:https://www.cnblogs.com/Dragon-Light/p/5927527.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/256288.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C#中数据类型及其转换知识点汇总

概念 C#中数据类型分为两大类&#xff0c;分别是值类型和引用类型。 值类型变量是从类 System.ValueType 中派生出来的&#xff0c;当声明一个值类型变量时&#xff0c;系统分配内存来存储值。 整形 包括8种类型&#xff0c;区别在于字节数和有无符号。 浮点型 float占用…

10亿个字符串的排序问题

一、问题描述 有一个大文件&#xff0c;里面有十亿个字符串&#xff0c;乱序的&#xff0c;要求将这些字符串以字典的顺序排好序 二、解决思路 将大文件切割成小文件&#xff0c;每个小文件内归并排序&#xff1b; 对所有的小文件进行归并排序——多重归并排序 三、解决方案 3.…

MVC学习IIS的不同版本(一)

一&#xff1a;IIS5.0运行在进程InetInfo.exe中&#xff0c;该进程寄宿着一个名为World Wide Publishing Service&#xff08;W3VC&#xff09;的window服务。 W3VC的主要功能&#xff1a;包括HTTP请求的监听、工作进程和配置管理 检测到HTTP 请求时&#xff1a; 根据扩展名判断…

Halcon中visualize_object_model_3d算子详解

概念 此函数用于显示3d模型。该函数功能很多,包括设置位姿,颜色,鼠标翻转、缩放、平移,选择和取消选择目标,降低鼠标灵敏度,切换检查模式等。 参数 visualize_object_model_3d( : : WindowHandle, ObjectModel3D, CamParam, PoseIn, GenParamName, GenParamValue, Tit…

random()模块随机函数的用法总结

random()是Python中生成随机数的函数&#xff0c;是由random模块控制&#xff0c;random()函数不能直接访问&#xff0c;需要导入random 模块&#xff0c;然后再通过相应的静态对象调用该方法才能实现相应的功能 目录 1. random.random() 2. random.uniform() 3. random.ra…

ansible命令应用示例

ansible命令应用示例 ping slave组ansible slave -m ping 用bruce 用户以root 身份pingansible slave -m ping -u bruce --sudo 用bruce 用户sudo 到batman 用户pingansible slave -m ping -u bruce --sudo --sudo-user batman 给slave组安装ftpan…

史上超全halcon常见3D算子汇总(一)

读取3D模型 read_object_model_3d 此算子用于读取3D对象。 read_object_model_3d( : : FileName, Scale, GenParamName, GenParamValue : ObjectModel3D, Status) FileName:文件名,halcon支持多种3d数据格式的读取,包括 .off, .ply, .dxf, .om3, .obj, .stl等格式。 1).…

Python:常用模块简介(1)

sys模块 >>> sys.platform #返回操作系统平台名称 win32 >>> sys.stdin #输入相关 <open file <stdin>, mode r at 0x000000000337B030> >>> sys.stdout #输出相关 <open file <stdout>, mode w at 0x000000000337…

【图像处理】——Python实现图像加噪(随机噪声、椒盐噪声、高斯噪声等)

目录 1、随机噪声 2、椒盐噪声 3、高斯噪声 补充:numpy.clip函数 4、其他噪声 1、随机噪声 随机噪声就是通过随机函数在图像上随机地

100NED

将生活融入编程转载于:https://www.cnblogs.com/nedhome/p/5036680.html

Windows10 VS2019下使用CMake3.20.1打开PCL1.11.0程序

安装CMake 为什么要使用cmake cmake 是kitware 公司以及一些开源开发者在开发几个工具套件(VTK)的过程中衍生品&#xff0c;成为一个独立的开放源代码项目。 CMake是一个很强大的编译配置工具&#xff0c;支持多种平台和编译器&#xff0c;通过编写CMakeLists.txt&#xff0c…

Java 并发---ConcurrentHashMap

concurrent包下的并发容器 JDK5中添加了新的concurrent包&#xff0c;相对同步容器而言&#xff0c;并发容器通过一些机制改进了并发性能。因为同步容器将所有对容器状态的访问都串行化了&#xff0c;这样保证了线程的安全性&#xff0c;所以这种方法的代价就是严重降低了并发性…

【图像处理】——图像滤波(Python+opencv实现三种方法:均值滤波、中值滤波、高斯滤波等)

目录 一、什么是滤波以及滤波的目的? 二、均值滤波(cv2.blur()) 1、原理 2、关键代码

UIScrollView事件拦截

在日常的开发中,我们经常会用到UIScrollView,然而,它是一个问题频出的控件,比如在nib中使用它就必须手动为它创建一个ContentView.当然了使用春代码的时候使用了懒加载机制使得它能够拥有一个contentView,今天我们不谈这个问题,我们来谈谈UIScrollView的事件拦截相关的知识. 在…

Windows10下安装QT5.14.2并用VS2019打开

安装 从官网下载&#xff1a;QT 安装方法仅需要注意&#xff1a; 1.最好不要安装在C盘。 2.根据开发需要安装功能模块&#xff0c;具体见参考文章。 https://jingyan.baidu.com/article/656db918d9292ae380249c4f.html 因为是用于PCL编程的&#xff0c;所以只选了msvc2017_64,…

【图像处理】——图像质量评价指标信噪比(PSNR)和结构相似性(SSIM)(含原理和Python代码)

目录 一、信噪比(PSNR) 1、信噪比的原理与计算公式 2、Python常规代码实现PSNR计算 3、TensorFlo

Error和Exception的区别

Error&#xff1a;值得是指与虚拟机相关的问题&#xff0c;比如虚拟机崩溃&#xff0c;虚拟机错误&#xff0c;内存空间不足&#xff0c;方法调用栈溢出。 对于这类错误应建议中断。 Exception&#xff1a;是指程序员可以处理的异常&#xff0c;可以捕获并且能够恢复&#xf…

JAVA TCP/IP网络通讯编程(二)

一个实例通过client端和server端通讯 客户端通过TCP/IP传输资源文件&#xff0c;比如图片&#xff0c;文字&#xff0c;音频&#xff0c;视频等..... 服务端接受到文件存入本地磁盘&#xff0c;返回接受到&#xff1a;“收到来自于"s.getInetAddress().getHostName()"…

C#中json序列化与反序列化

json格式概念 JSON(JavaScript Object Notation) 是一种轻量级的数据传输格式&#xff0c;其采用完全独立于语言的文本格式&#xff0c;使JSON成为理想的数据交换语言。 json由两种格式组成。 1.名称/值”对的集合&#xff0c;可以一起创建多个"名称 / 值对"。 { “…