题意:给定 n 个数,然后每次破坏一个位置的数,那么剩下的连通块的和最大是多少。

析:用并查集来做,从后往前推,一开始什么也没有,如果破坏一个,那么我们就加上一个,然后判断它左右两侧是不是存在,如果存在,那么就合并起来,

然后不断最大值,因为这个最大值肯定是不递减,所以我们一直更新就好。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int a[maxn], b[maxn], p[maxn];
bool num[maxn];
vector<LL> ans;
LL ran[maxn]; int Find(int x) { return x == p[x] ? x : p[x] = Find(p[x]); } int main(){
while(scanf("%d", &n) == 1){
for(int i = 1; i <= n; ++i){ scanf("%d", a+i); p[i] = i; }
for(int i = 1; i <= n; ++i) scanf("%d", b+i); ans.clear();
memset(num, false, sizeof num);
LL cnt = 0;
for(int i = n; i > 0; --i){
ans.push_back(cnt);
int x = b[i];
num[x] = true;
ran[x] = a[x];
if(num[x+1]){
int y = Find(x+1);
p[y] = x;
ran[x] += ran[y];
}
if(num[x-1]){
int y = Find(x-1);
p[y] = x;
ran[x] += ran[y];
}
cnt = Max(cnt, ran[x]);
} for(int i = ans.size()-1; i >= 0; --i)
printf("%I64d\n", ans[i]);
}
return 0;
}

CodeForces 722C Destroying Array (并查集)的更多相关文章

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

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

  2. Codeforces 722C. Destroying Array

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

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

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

  4. C. Destroying Array 并查集,逆向思维

    用并查集维护线段,从后往前枚举没个删除的位置id[i] 那么,现在删除了这个,就是没有了的,但是上一个id[i + 1]就是还没删除的. 然后现在进行合并 int left = id[i + 1];( ...

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

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

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

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

  7. Codeforces Gym 100463E Spies 并查集

    Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...

  8. Codeforces 859E Desk Disorder 并查集找环,乘法原理

    题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...

  9. Codeforces - 828C String Reconstruction —— 并查集find()函数

    题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...

随机推荐

  1. linux上配置spark集群

    环境: linux spark1.6.0 hadoop2.2.0 一.安装scala(每台机器)   1.下载scala-2.11.0.tgz   放在目录: /opt下,tar -zxvf scal ...

  2. iOS FMDB 无法更新二进制数据的问题

    使用FMDB很方便的实现了(通过数据库字段名而不是字段索引)数据的读取,插入,更新,删除.但是我在更新图片时发现通过格式化字符(@“%@”,data/NSData/)传入的二进制数据更新到数据库后不能 ...

  3. [Bzoj1030][JSOI2007]文本生成器(AC自动机)(dp)

    1030: [JSOI2007]文本生成器 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 5254  Solved: 2172[Submit][Stat ...

  4. [Bzoj1821][JSOI2010]Group 部落划分 Group(并查集)(二分答案)

    1821: [JSOI2010]Group 部落划分 Group Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2949  Solved: 1392[S ...

  5. MySQL Study之--Percona Server版本号

    MySQL Study之--Percona Server版本号 1.简单介绍      Percona 为 MySQL 数据库server进行了改进.在功能和性能上较 MySQL 有着非常显著的提升. ...

  6. HDU3459:Rubik 2&#215;2&#215;2(IDA)

    Problem Description Sonny is probably the only computer science Ph.D. student who cannot solve a Rub ...

  7. 使用literal语法格式化字符串

    支持arm64之后,格式化字符串的时候会遇到一些问题,主要与NSInteger的定义有关: #if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET ...

  8. HDU4763 Theme Section 【KMP】

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  9. Android开发之自己定义Spinner样式的效果实现(源码实现)

    android系统自带的Spinner样式是远远满足不了我们实际开发过程中对Spinner UI风格的要求,因此我们肯定须要为了切合整个应用的风格,改动我们的Spinner样式.系统给我们提供了两种常 ...

  10. mac WebStorm 破解

    摘要:因为想要学习HTML所以需要一个工具,同事推荐了webstorm.下载以后再网上搜破解方法.搜索到一个很简单的. 一.下载链接https://www.jetbrains.com/webstorm ...