题目链接: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. performSelector与objc_msgSend

    - perform:(SEL)aSelector { if (aSelector) return objc_msgSend(self, aSelector); else return [self er ...

  2. Java使用反射通过对象属性获取属性的值

    代码: // 通过属性获取传入对象的指定属性的值 public String getValueByPropName(Student student, String propName) { String ...

  3. (1)安装----anaconda3下配置pyspark【单机】

    1.确保已经安装jdk和anaconda3.[我安装的jdk为1.8,anaconda的python为3.6] 2.安装spark,到官网 http://spark.apache.org/downlo ...

  4. Thinking in file encoding and decoding?

    > General file encoding ways We most know, computer stores files with binary coding like abc\xe4\ ...

  5. 【洛谷4941】War2 状压Dp

    简单的状压DP,和NOIP2017 Day2 找宝藏 代码几乎一样.(比那个稍微简单一点) f[i][j] ,i代表点的状态,j是当前选择的点,枚举上一个选到的点k 然后从f[i-(1<< ...

  6. 线性回归(regression)

    简介 回归分析只涉及到两个变量的,称一元回归分析.一元回归的主要任务是从两个相关变量中的一个变量去估计另一个变量,被估计的变量,称因变量,可设为Y:估计出的变量,称自变量,设为X. 回归分析就是要找出 ...

  7. 高级函数-sign

    ==========sign函数介绍(补充)===========   sign(n):判断n>0返回1;n=0返回0;n<0返回-1.   select sign(10),sign(0) ...

  8. 2016 10 28考试 dp 乱搞 树状数组

    2016 10 28 考试 时间 7:50 AM to 11:15 AM 下载链接: 试题 考试包 这次考试对自己的表现非常不满意!! T1看出来是dp题目,但是在考试过程中并没有推出转移方程,考虑了 ...

  9. SQL SERVER-常用命令2

    1.创建新表 create table mytest ( id int primary key identity(1,1),--主键,自动+1 name varchar(20) unique notn ...

  10. POSIX 线程编程(一)简介

    简介 在共享内存的多处理器结构中,可以用线程来实现并行.对于UNIX系统, IEEE POSIX 1003.1c标准规定了C语言线程编程接口的标准.这份标准的实现就是POSIX threads, 或者 ...