CodeForces-722C Destroying Array 并查集 离线操作
题目链接: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 并查集 离线操作的更多相关文章
- CodeForces 722C Destroying Array (并查集)
题意:给定 n 个数,然后每次破坏一个位置的数,那么剩下的连通块的和最大是多少. 析:用并查集来做,从后往前推,一开始什么也没有,如果破坏一个,那么我们就加上一个,然后判断它左右两侧是不是存在,如果存 ...
- CodeForces - 722C Destroying Array (并查集/集合的插入和删除)
原题链接:https://vjudge.net/problem/511814/origin Description: You are given an array consisting of n no ...
- Codeforces 722C. Destroying Array
C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CF722C. Destroying Array[并查集 离线]
链接:Destroying Array C. Destroying Array time limit per test 1 second memory limit per test 256 megab ...
- C. Destroying Array 并查集,逆向思维
用并查集维护线段,从后往前枚举没个删除的位置id[i] 那么,现在删除了这个,就是没有了的,但是上一个id[i + 1]就是还没删除的. 然后现在进行合并 int left = id[i + 1];( ...
- [并查集+逆向思维]Codeforces Round 722C Destroying Array
Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces Gym 100463E Spies 并查集
Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...
- Codeforces 859E Desk Disorder 并查集找环,乘法原理
题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...
- Codeforces - 828C String Reconstruction —— 并查集find()函数
题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...
随机推荐
- Sublime 是自动检测而非自动设置缩进
以为是自动设置规范化的缩进 以前一直认为是:识别出文件类型后,设置统一的缩进规范.比如说 识别为CSS,就把缩进设成2个空格 其实是自动检测然后与你保持统一 亲测发现,根据你文本里用的是几个空格的缩进 ...
- Springboot统一异常处理(@ControllerAdvice)
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind ...
- 如何在使用layer.prompt在输入值为空的情况下点击确定继续执行逻辑?
突然发现在使用LayUI时,用到弹出层layer.prompt时,如果文本框输入值是空的话点击确定没有反应,不能向下执行. 但是我又需要在这种情况下去继续执行判断或逻辑时该怎么做?? 示例:原代码如下 ...
- 解决PL/SQL管理工具database下拉为空和登录出现ORA-12154
前言:昨天捣鼓了一下午,终于可以用plsql连接上oracle了... 测试环境:win10 注意问题: (一).环境变量 我发现按网上别人说的那一大推环境配置,很容易出错,我把它们全删了,就留了两个 ...
- Server初见——python
import socketphone = socket.socket(socket.AF_INET,socket.SOCK_STREAM)phone.bind(('127.0.0.1',8080))p ...
- java 公开内部类无法实例化 no enclosing instance 解决办法
因为B类不是A类的静态内部类,所以B累也只能像A类的成员一样通过new A()的实例访问,new(new A()).B(),这显然不是我们想要的方式,于是需要在B类的前边加上static,变成下边这样 ...
- sed将上下两行并列
#cat log5 mmmm 1234 nnnn 2344 #sed -n '{N;s/\n/\t/p}' log5 mmmm 1234 nnnn 2344
- 《Exception》第八次团队作业:Alpha冲刺(第四天)
一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.掌握软件测试基础技术.2.学习迭代式增量软 ...
- IDEA Maven 打包运行 jar java.io.FileNotFoundException: 问题?
当 使用 idea maven 将项目打包运行的时候,能够成功运行,但是总会跑到 xxx\xxx\lib 下 找jar包 如下异常: java.io.FileNotFoundException: D: ...
- CF735E Ostap and Tree
比较毒瘤的树形DP,子状态难想.这是主要是搬运一篇题解. 用\(f[i][j]\)表示\(i\)的子树中离\(i\)最近黑点的距离为\(j\),且距离超过\(j\)的点都被满足的方案数.转移时新建一个 ...