Codeforces 722C Destroying Array(并查集)
来源:互联网 发布:尚学堂oa源码 编辑:程序博客网 时间:2024/05/21 11:27
C. Destroying Arraytime limit per test1 secondmemory limit per test256 megabytes
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 from1 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 integern (1 ≤ n ≤ 100 000) — the length of the array.
The second line contains n integersa1, a2, ..., an (0 ≤ ai ≤ 109).
The third line contains a permutation of integers from1 to n — the order used to destroy elements.
Print n lines. Thei-th line should contain a single integer — the maximum possible sum of elements on the segment containing no destroyed elements, after firsti operations are performed.
41 3 2 53 4 1 2
5430
51 2 3 4 54 2 3 5 1
65510
85 5 4 4 6 6 5 55 2 8 7 1 3 4 6
18161188660
题意:给出n个非负数,再给出n个位置,从第一个位置开始,依次删除这个位置上的数,每删除一个数,求该n个数最大连续子序列和
思路;用并查集,逆向来做,每次添加一个数,只有可能把他左边或右边的那两个数连接起来,所以用一个标记来判断相邻的两个数是否在之前已被添加,如果被添加,用并查集把这两个点合并,然后更新最大值。
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;typedef long long ll;const int MAXN = 1e5+100;ll sum[MAXN],ans[MAXN],a[MAXN];int pos[MAXN],pre[MAXN];bool have[MAXN];int find(int x){if(pre[x]==x){return pre[x];}else{pre[x]=find(pre[x]);return pre[x];}}int main(void){int n;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%I64d",&a[i]);}for(int i=1;i<=n;i++){scanf("%d",&pos[i]);}for(int i=1;i<=n;i++){pre[i]=i;}ans[n]=0;for(int i=n;i>1;i--){int p=pos[i];sum[p]=a[p];if(have[p-1]){int p1=find(p),p2=find(p-1);pre[p1]=p2;sum[p2]+=sum[p1];}if(have[p+1]){int p1=find(p),p2=find(p+1);pre[p1]=p2;sum[p2]+=sum[p1];}have[p]=1;ans[i-1]=max(ans[i],sum[find(p)]);}for(int i=1;i<=n;i++){printf("%I64d\n",ans[i]);}return 0;}
- codeforces 722C Destroying Array ( 并查集 / Multiset )
- codeforces 722C. Destroying Array(并查集||set)
- 【Codeforces 722 C Destroying Array】+ 并查集
- Codeforces 722C Destroying Array(并查集)
- [codeforces]C. Destroying Array——并查集
- 并查集巧妙用法(codeforces)C. Destroying Array
- Codeforces Round 722C:Destroying Array(并查集,好题)
- Codeforces 722C Destroying Array【逆序(离线)思维+并查集】
- CodeForces 722C. Destroying Array(并查集 好题)
- CodeForces 722C Destroying Array(并查集和我奇怪的二叉树)
- codeforces 722C Destroying Array 并查集带权
- 并查集(Destroying Array,cf 722C)
- codeforces 722C Destroying Array
- codeforces(722c)-Destroying Array
- Codeforces 722C. Destroying Array
- codeforces 722C. Destroying Array
- Codeforces722 C. Destroying Array (并查集)
- Destroying Array(并查集)
- 谈谈混合云传输安全,怎样用正确的姿势连接云上云下?
- python random模块
- JS新API标准 地理定位(navigator.geolocation)
- MFC串口助手实现
- Android wifi休眠策略
- Codeforces 722C Destroying Array(并查集)
- 神兽保佑,永无BUG
- LWC 55:712. Minimum ASCII Delete Sum for Two Strings
- Servlet运行机制与生命周期
- 如何在 Git 里撤销(几乎)任何操作
- android studio2.3.3双击exe文件打不开的问题
- 社交系统ThinkSNS-plus(TS+)Android端APP V1.0.4更新播报
- usb设备的probe全过程
- linux关于bashrc与profile