用并查集维护线段,从后往前枚举没个删除的位置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. UIButton常见用法

    //UIButton是iOS中用来响应用户点击的控件,既可以显示文字,也可以显示图片,也可以处理用户交互 //UIButton的创建,一般采用类方法来创建,不需要释放 //UIButton 也是UIC ...

  2. 【转】 Pro Android学习笔记(六十):Preferences(4):MultiSelect List Preference

    目录(?)[-] XML文件 在设备中保存 读出信息 ListPreference提供单选列表,我们可以通过CheckBoxPreference提供多选列表.此外,Android在3.0后提供Mult ...

  3. 修改sharepoint列表样式

    1.将sharepoint 的样式修改为阴影 2.用designer打开列表的AllItems.aspx文件,将下面的样式拷贝到里面     <style type="text/css ...

  4. 关于导入excel报错的处理(xls,xlsx)

    关于导入excel报错的处理(xls,xlsx) 最近在做一个将excel导入到dataGriview中的小功能在做的过程中遇到以下问题: 链接excel的链接串是这样写的 string strCon ...

  5. eclipse个人觉得有用的快捷键

    CTRL+SHIFT+F 自动整理代码格式 CTRL+M 最大/还原当前编辑窗口 CTRL+/ 注释当前行 CTRL+1 快速修复 CTRL+D 删除当前行 SHIFT+ENTER 在当前行前面插入空 ...

  6. java多线程无锁和工具类

    1 无锁 (1) cas (compare and swap) 设置值的时候,会比较当前值和当时拿到的值是否相同,如果相同则设值,不同则拿新值重复过程:注意,在设置值的时候,取值+比较+设值 是一条c ...

  7. vue 上传二进制图片

    1.前段代码 <el-form-item label="证件照片" prop="idImage"> <input @change='ss' t ...

  8. Learning Python 010 函数 2

    Python 函数 2 函数的参数 位置参数(普通,正常的参数) 随便编写一个求x^n的值的函数power(x, n): def power(x, n): s = 1 while n > 0: ...

  9. ps -ef | grep java 输出的具体含义是什么?

      uid        pid    ppid # PID 这个程序的 ID 下面的 PPID 则是父程序的 ID: PS是LINUX下最常用的也是非常强大的进程查看命令 常见的使用方法是检查一个进 ...

  10. IOS开发学习 碎片S

    非常感谢提供一下内容的人和组织! 字符串编码:http://www.cnblogs.com/KevinYang/archive/2010/06/18/1760597.html Foundation框架 ...