题目链接:https://cn.vjudge.net/problem/CodeForces-722C

题意

给个数组,每次删除一个元素,删除的元素作为一个隔断,问每次删除后该元素左右两边最大连续和

思路

这个题的思路马上就想到的时候,别人直接抢答,还是比较厉害的人了

离线操作,删除变成添加,添加时注意左右两边元素的最大值即可

提交过程

WA 忘了为什么WA了
AC

代码

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=1e5+20, INF=0x3f3f3f3f;
long long n, nums[maxn], oper[maxn], out[maxn];
bool vis[maxn], flg;
struct Node{
int pre; long long sum;
Node(int pre=0, long long sum=0):
pre(pre), sum(sum) {}
}nodes[maxn]; int find(int x){
if (x==nodes[x].pre) return x;
nodes[nodes[x].pre].sum+=nodes[x].sum;
nodes[x].sum=0;
return nodes[x].pre=find(nodes[x].pre);
} void join(int a, int b){
a=find(a); b=find(b);
if (a==b) return;
nodes[a].pre=b;
} int main(void){
scanf("%d", &n);
for (int i=1; i<=n; i++) nodes[i]=Node(i, 0);
for (int i=1; i<=n; i++) scanf("%lld", &nums[i]);
for (int i=1; i<=n; i++) scanf("%lld", &oper[i]);
for (int i=n; i>=1; i--){
int idx=oper[i];
nodes[idx]=Node(idx, nums[idx]);
vis[idx]=true; out[i-1]=max(nums[idx], out[i]);
if (idx-1>=1 && vis[idx-1]){
join(idx-1, idx); // find(idx-1);
out[i-1]=max(out[i-1], nodes[find(idx-1)].sum);
}if (idx+1<=n && vis[idx+1]){
join(idx, idx+1); find(idx);
out[i-1]=max(out[i-1], nodes[find(idx+1)].sum);
}
}
for (int i=1; i<=n; i++) printf("%lld\n", out[i]); return 0;
}
Time Memory Length Lang Submitted

CodeForces-722C Destroying Array 并查集 离线操作的更多相关文章

  1. CodeForces 722C Destroying Array (并查集)

    题意:给定 n 个数,然后每次破坏一个位置的数,那么剩下的连通块的和最大是多少. 析:用并查集来做,从后往前推,一开始什么也没有,如果破坏一个,那么我们就加上一个,然后判断它左右两侧是不是存在,如果存 ...

  2. CodeForces - 722C Destroying Array (并查集/集合的插入和删除)

    原题链接:https://vjudge.net/problem/511814/origin Description: You are given an array consisting of n no ...

  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. C. Destroying Array 并查集,逆向思维

    用并查集维护线段,从后往前枚举没个删除的位置id[i] 那么,现在删除了这个,就是没有了的,但是上一个id[i + 1]就是还没删除的. 然后现在进行合并 int left = id[i + 1];( ...

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

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

  7. Codeforces Gym 100463E Spies 并查集

    Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...

  8. Codeforces 859E Desk Disorder 并查集找环,乘法原理

    题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...

  9. Codeforces - 828C String Reconstruction —— 并查集find()函数

    题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...

随机推荐

  1. Unity 自己旋转 方法

    transform.Rotate(Vector3.up * Time.deltaTime * 100);

  2. CorelDRAW结合Photoshop绘制女性服装效果图

    今天小编为大家分享CorelDRAW结合PS绘制女性服装效果图,教程真的很不错,很值得大家学习,有兴趣的朋友赶快行动起来吧! CDR下载:http://pan.baidu.com/s/1cD4buQ ...

  3. CF950B Intercepted Message_双指针法

    本来想直接上权值线段树维护区间最值,不过可以用双指针法,就使问题变得简洁. Code: #include<iostream> using namespace std; const int ...

  4. js类的使用

    brush示例 以d3的一个brush进行叙述,示例见: https://bl.ocks.org/xunhanliu/6f0b46789842e9e19e6cfe9bd0b16806 应用情形: 当页 ...

  5. Vue学习之路第七篇:跑马灯项目实现

    前面六篇讲解了Vue的一些基础知识,正所谓:学以致用,今天我们将用前六篇的基础知识,来实现类似跑马灯的项目. 学前准备: 需要掌握定时器的两个函数:setInterval和clearInterval以 ...

  6. VUE:列表的过滤与排序

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 【【henuacm2016级暑期训练】动态规划专题 I】Gargari and Permutations

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 注意这k个序列每个都是排列. 如果在每个序列中都满足y出现在x之后的话. 那么我们从x连一条有向边至y (有一个序列不满足就不连 ( ...

  8. java源码之HashSet

    1,HashSet介绍 1)HashSet 是一个没有重复元素的集合.2)它是由HashMap实现的,不保证元素的顺序,而且HashSet允许使用 null 元素.3)HashSet是非同步的.如果多 ...

  9. APP热更新方案(转)

    本文转载自[http://creator.cnblogs.com/] 博客地址:Zealot Yin 为什么要做热更新 当一个App发布之后,突然发现了一个严重bug需要进行紧急修复,这时候公司各方就 ...

  10. [Angular] Provide Feedback to Progress Events with Angular’s HttpRequest Object

    In some cases your application might need to upload large amounts of data, such as files. Obviously ...