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 ...
随机推荐
- 【AnjularJS系列6 】 过滤器
第六篇,过滤器 AngularJS 过滤器可用于转换数据: 过滤器 描述 currency 格式化数字为货币格式. filter 从数组项中选择一个子集. lowercase 格式化字符串为小写. o ...
- Console.WriteLine 不会输出到unity控制台
1,Console.WriteLine() 是输出到控制台程序(console application)的命令 2,Unity中控制台是一个独立的程序,要想输出到Unity控制台需要使用Debug.L ...
- node——文件夹创建
//创建文件夹 var fs=require('fs'); //1.异步 fs.mkdir("./第一个目录",function(err){ if (err) { return c ...
- 在HTTP页面输入数据,Chrome 70将显示红色不安全警告
2018年10月17日,Chrome 70版本正式发布,该版本更新多项安全功能,再次升级对HTTP页面的不安全警告样式.当用户在HTTP页面输入数据时,Chrome 70将显示醒目的红色不安全警告,让 ...
- ZOJ 3911Prime Query [素数处理 + 线段树]
Time Limit: 5 Seconds Memory Limit: 196608 KBYou are given a simple task. Given a sequence A[i] with ...
- BZOJ 2754 [SCOI2012]喵星球上的点名 (AC自动机+map维护Trie树)
题目大意:略 由于字符集大,要用map维护Trie树 并不能用AC自动机的Trie图优化,不然内存会炸 所以我用AC自动机暴跳fail水过的 显然根据喵星人建AC自动机是不行的,所以要根据问题建 然而 ...
- Swoole 源码分析——进程管理 Swoole_Process
前言 swoole-1.7.2 增加了一个进程管理模块,用来替代 PHP 的 pcntl 扩展. PHP自带的pcntl,存在很多不足,如 pcntl 没有提供进程间通信的功能 pcntl 不支持重定 ...
- 【基于mini2440开发板的交叉编译环境及内核树配置.
在学习linux驱动开发过程中,交叉编译环境的配置及内核树的生成无疑是对linux不是十分了解的新人面前的一堵墙.高高大大的墙...笔者在初探这一方向时,就在这2个问题上苦恼了很久.查阅无数资料,大多 ...
- Java生成验证码_转
为了防止用户恶意,或者使用软件外挂提交一些内容,就得用验证码来阻止,虽然这个会影响用户体验,但为了避免一些问题很多网站都使用了验证码;今天下午参考文档弄了一个验证码,这里分享一下;这是一个web工程, ...
- Cocos2d-x 多点触摸
多点触摸的实现步骤与单点触摸类似,setTouchEnabled(true)开启触摸功能,注冊触摸事件,重载多点触摸函数:ccTouchesBegan(開始).ccTouchesMoved(移动).c ...