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 ...
随机推荐
- day15 函数对象以及作用域
目录 函数对象 函数对象的四大功能 函数的嵌套 名称空间和作用域 名称空间 考试必考 作用域 global nonlacal LEGB原则 函数对象 ==Python中一切皆对象== 函数名也可以看成 ...
- day05-2 变量、常量、注释以及内存管理
目录 什么是变量 Python中定义变量 定义变量名的命名规范 什么是常量 定义常量 注释是什么 注释有什么用 内存管理(重要) 引用计数 垃圾回收机制 小整数池 定义变量的三个特征 什么是变量 变量 ...
- Pyhton学习——Day36
#异步IO——Asynchronous#异步效率最高,特点:全程无阻塞# 在说明synchronous IO和asynchronous IO的区别之前,需要先给出两者的定义.# Stevens给出的定 ...
- 支持Openflow 1.3的wireshark插件安装教程
目前为止,我们使用openflow wiki里提供的minient镜像里集成的wireshark只支持openflow1.0,我们通过wireshark上 菜单 help-->about wir ...
- 利用 ST-LINK Utility软件下载程序
先在电脑上安装STM32 ST-LINK Utility,软件安装一路Next就可以了,安装好软件之后界面如下: 下载程序只需要使用3个图标就可以了 第一个图标Connect to the ta ...
- Mysql学习总结(31)——MySql使用建议,尽量避免这些问题
做服务器端开发的同学们,相信对于mysql应该是十分熟悉,但是一旦真正出现问题,你是否能够快速的发现问题的起因,并且解决呢?一旦问题涉及到数据库层面,往往不是那么好解决的,通常来说,我们需要提前做应对 ...
- Maven学习总结(23)——Maven常用命令介绍
1.生成eclipse项目:mvn eclipse:eclipse 2.清除eclipse的一些系统设置:mvn eclipse:clean 3.mvn tomcat:run 在tomcat里面运行 ...
- if判断语句
6)if判断语句 if ... then else end if; if ... then elsif ... then elsif ... then else ...
- windows server 2008开机自动登陆无密码,关机不必写原因
运行secpol.sec接下来,在弹出的“本地安全策略”对话框中,依次展开左边树图到“本地策略”-“安全选项”,在右边可以找到“交互式登录 无须按 Ctrl+Alt+Del”,双击该项设置为“已启用” ...
- 开源ETL工具kettle--数据迁移
背景 因为项目的需求,须要将数据从Oracle迁移到MSSQL,不是简单的数据复制,而是表结构和字段名都不一样.甚至须要处理编码规范不一致的情况,例如以下图所看到的 watermark/2/text/ ...