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.pswp.cn/news/256288.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/256288.shtml
英文地址,請注明出處:http://en.pswp.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;可以一起創建多個"名稱 / 值對"。 { “…