用并查集维护线段,从后往前枚举没个删除的位置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 并查集,逆向思维的更多相关文章

  1. CF722C. Destroying Array[并查集 离线]

    链接:Destroying Array C. Destroying Array time limit per test 1 second memory limit per test 256 megab ...

  2. CodeForces 722C Destroying Array (并查集)

    题意:给定 n 个数,然后每次破坏一个位置的数,那么剩下的连通块的和最大是多少. 析:用并查集来做,从后往前推,一开始什么也没有,如果破坏一个,那么我们就加上一个,然后判断它左右两侧是不是存在,如果存 ...

  3. CodeForces-722C Destroying Array 并查集 离线操作

    题目链接:https://cn.vjudge.net/problem/CodeForces-722C 题意 给个数组,每次删除一个元素,删除的元素作为一个隔断,问每次删除后该元素左右两边最大连续和 思 ...

  4. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  5. [并查集+逆向思维]Codeforces Round 722C Destroying Array

    Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  6. 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 ...

  7. CodeForces - 722C Destroying Array (并查集/集合的插入和删除)

    原题链接:https://vjudge.net/problem/511814/origin Description: You are given an array consisting of n no ...

  8. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维

    原题中需要求解的是按照它给定的操作次序,即每次删掉一个数字求删掉后每个区间段的和的最大值是多少. 正面求解需要维护新形成的区间段,以及每段和,需要一些数据结构比如 map 和 set. map< ...

  9. bzoj 1015: [JSOI2008]星球大战starwar (逆向思维+并查集)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1015 思路: 题目是要我们对当前图拆掉k个点,问,每拆一个点后图中有多少个联通块,我们可以逆 ...

随机推荐

  1. bzoj 2716 [Violet 3]天使玩偶——KDtree

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2716 第三道KDtree!仍旧是模板.还有CDQ分治做法,见下面. 数组迷之开大?(开6e5 ...

  2. cookie与web Storage

    一.cookie 1. http头与cookie cookie是HTTP Cookie的简称.该标准要求: (1)服务器的HTTP响应头包含 Set-Cookie字段 响应头Eg: HTTP/1.1 ...

  3. MySQL绿色版的安装步骤

    由于工作需要最近要开始研究MySQL了(看来学习都是逼出来的),本人对mysql没有研究,可以说一个小白. 下面就从安装开始吧,虽然网上关于这方面的东西很多,还是需要自己把操作过程写下来. 1.数据库 ...

  4. 转——AHRS(航姿参考系统)和IMU(惯性测量单元)的区别

    AHRS(航姿参考系统)和IMU(惯性测量单元)的区别  [测试测量] 发布时间:2010-05-09 16:52:09  http://bbs.ednchina.com/BLOG_ARTICLE_1 ...

  5. mahout过滤推荐结果 Recommender.recommend(long userID, int howMany, IDRescorer rescorer)

    Recommender.recommend(uid, RECOMMENDER_NUM, rescorer); Recommender.recommend(long userID, int howMan ...

  6. 5.6 安装SqlDeveloper

    首先,将安装包准备好: 打开终端: 这样,sqldeveloper就安装完毕了. 在Ubuntu中搜索,sql,会出现: 点击,可以进入:

  7. LeetCode第20题:有效的括号

    问题描述: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空 ...

  8. jQuery学习2

    1,jQuery中的$有什么意义. $是 JQuery 常用的一个回传函数. 很多人写jquery代码是这样开始的 $(function(){ // do something }); 事实上它是jqu ...

  9. Python开发【第六篇】:文件处理

    1. 文件   文件处理流程: 打开文件,获得文件句柄,并赋值 通过句柄对文件进行操作 关闭文件 1.1 打开文件   在 Python 中使用 open()函数打开文件,并返回文件对象: open( ...

  10. .net core 中使用NLog

    在.net standard 2.0.3 和.net core 2.1适用.其他版本的.net 应该也可以. 1.新建一个空白解决方案,再建一个类库 2.安装NLog.Config,会生成一个配置文件 ...