C. Destroying Array 并查集,逆向思维
用并查集维护线段,从后往前枚举没个删除的位置id[i]
那么,现在删除了这个,就是没有了的,但是上一个id[i + 1]就是还没删除的。
然后现在进行合并
int left = id[i + 1];(相当于每次都加入一个元素a[left])
它在这个位置,如果能和左右的合并,就是左右邻居都有元素,那么当然是合并最好,因为元素都是大于0的,越长越好。
合并的时候再记录一个数组sum[pos]表示以这个为根的总和是多少。
按并查集的思路更新整个并查集即可、
注意一点的就是,
要求ans[i]
那么ans[i + 1]是在没有id[i + 1]这个元素的前提下的最大值,现在有了这个元素,但是不见得会变大。
所以
ans[i] = max(ans[i + 1], sum[find(id[i + 1)]);
要和前一个比较一下
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
int fa[maxn];
LL sum[maxn];
int find(int u) {
if (fa[u] == u) return fa[u];
else return fa[u] = find(fa[u]);
}
void merge(int x, int y) {
x = find(x);
y = find(y);
if (x != y) {
fa[y] = x;
sum[x] += sum[y];
}
}
int a[maxn];
int id[maxn];
bool has[maxn];
LL ans[maxn];
void work() {
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) fa[i] = i;
for (int i = ; i <= n; ++i) scanf("%d", &a[i]);
for (int i = ; i <= n; ++i) scanf("%d", &id[i]);
ans[n] = ;
for (int i = n - ; i >= ; --i) {
int left = id[i + ];
sum[left] = a[left];
if (has[left + ]) {
merge(left, left + );
}
if (has[left - ]) {
merge(left, left - );
}
ans[i] = max(ans[i + ], sum[find(left)]);
has[left] = ;
}
for (int i = ; i <= n; ++i) {
printf("%I64d\n", ans[i]);
}
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}
C. Destroying Array 并查集,逆向思维的更多相关文章
- CF722C. Destroying Array[并查集 离线]
链接:Destroying Array C. Destroying Array time limit per test 1 second memory limit per test 256 megab ...
- CodeForces 722C Destroying Array (并查集)
题意:给定 n 个数,然后每次破坏一个位置的数,那么剩下的连通块的和最大是多少. 析:用并查集来做,从后往前推,一开始什么也没有,如果破坏一个,那么我们就加上一个,然后判断它左右两侧是不是存在,如果存 ...
- CodeForces-722C Destroying Array 并查集 离线操作
题目链接:https://cn.vjudge.net/problem/CodeForces-722C 题意 给个数组,每次删除一个元素,删除的元素作为一个隔断,问每次删除后该元素左右两边最大连续和 思 ...
- HDU 4496 并查集 逆向思维
给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...
- [并查集+逆向思维]Codeforces Round 722C Destroying Array
Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 带权并查集
C. Destroying Array 题目连接: http://codeforces.com/contest/722/problem/C Description You are given an a ...
- CodeForces - 722C Destroying Array (并查集/集合的插入和删除)
原题链接:https://vjudge.net/problem/511814/origin Description: You are given an array consisting of n no ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维
原题中需要求解的是按照它给定的操作次序,即每次删掉一个数字求删掉后每个区间段的和的最大值是多少. 正面求解需要维护新形成的区间段,以及每段和,需要一些数据结构比如 map 和 set. map< ...
- bzoj 1015: [JSOI2008]星球大战starwar (逆向思维+并查集)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1015 思路: 题目是要我们对当前图拆掉k个点,问,每拆一个点后图中有多少个联通块,我们可以逆 ...
随机推荐
- C# 性能总结
尽量使用using语句块和finally (实现IDisposable) 尽量使用单个大程序集而不是多个小程序集 (使用NGen.exe) 使用sealed关键字 权衡虚函数 使用弱引用
- poj3414Pots(倒水BFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13231 Accepted: 5553 Special J ...
- influxdb api
https://influxdb-python.readthedocs.io/en/latest/examples.html
- tomcat部署虚拟主机-搭建两个应用以及httpd和Nginx的反向代理
实验环境:CentOS7 前提:已经安装好tomcat,未安装请查看http://www.cnblogs.com/wzhuo/p/7111135.html: 目的:基于主机名访问两个应用: [root ...
- javadoc 工具生成开发API文档
=====================先来一点成就感===================== package com.springMybatis.dao; import com.springMy ...
- java.util.Date、java.sql.Date、java.sql.Time、java.sql.Timestamp区别和联系
java.util.Date.java.sql.Date.java.sql.Time.java.sql.Timestamp区别和联系 栏目:Java基础 作者:admin 日期:2015-04-19 ...
- 洛谷-铺地毯-NOIP2011提高组复赛
题目描述 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有 n 张地毯,编号从 1 到n .现在将这些地毯按照编号从小到大的顺序平行于 ...
- Windchill 配置LOG文件,使开发中的代码能显示打印的信息
如开发代码的类HomeLogic.java, 包路径在pnt.report.home 需求:需监控此类的打印数据 方法:配置D:\ptc\Windchill_10.1\Windchill\codeba ...
- This account is currently not available
今天在linux下切换用户发现提示This account is currently not available,说是无效用户了后来网上查了一下发现是用户的shell禁止登录了,解决方法只要开启she ...
- 14.Nginx 文件名逻辑漏洞(CVE-2013-4547)
由于博主在渗透网站时发现现在Nginx搭建的网站是越来越多 所以对Nginx的漏洞来一个全面性的复习,本次从Nginx较早的漏洞开始分析. 2013年底,nginx再次爆出漏洞(CVE-2013-45 ...