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 array consisting of n non-negative integers a1, a2, ..., an.
You are going to destroy integers in the array one by one. Thus, you are given the permutation of integers from 1 to n defining the order elements of the array are destroyed.
After each element is destroyed you have to find out the segment of the array, such that it contains no destroyed elements and the sum of its elements is maximum possible. The sum of elements in the empty segment is considered to be 0.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the length of the array.
The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).
The third line contains a permutation of integers from 1 to n — the order used to destroy elements.
Output
Print n lines. The i-th line should contain a single integer — the maximum possible sum of elements on the segment containing no destroyed elements, after first i operations are performed.
Sample Input
4
1 3 2 5
3 4 1 2
Sample Output
5
4
3
0
Hint
题意
有n个数,然后每个数的权值是a[i],现在按照顺序去摧毁n个元素,然后每次问你最大的连通块和是多少。
题解:
倒着做,然后用带权并查集去维护就好了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
long long fa[maxn],a[maxn],b[maxn],ans[maxn],vis[maxn],sum[maxn],n;
int fi(int x){return fa[x]==x?x:fa[x]=fi(fa[x]);}
void uni(int x,int y)
{
x=fi(x),y=fi(y);
fa[x]=y;sum[y]+=sum[x];
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=a[i];
fa[i]=i;
}
for(int i=1;i<=n;i++)scanf("%d",&b[i]);
long long tmp = 0;
for(int i=n;i>1;i--)
{
vis[b[i]]=1;
if(vis[b[i]-1])uni(b[i]-1,b[i]);
if(vis[b[i]+1])uni(b[i]+1,b[i]);
tmp=max(sum[fi(b[i])],tmp);
ans[i-1]=tmp;
}
for(int i=1;i<=n;i++)
cout<<ans[i]<<endl;
}
Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 带权并查集的更多相关文章
- Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]
传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) A B C D 水 模拟 并查集 优先队列
A. Broken Clock time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) B. Verse Pattern 水题
B. Verse Pattern 题目连接: http://codeforces.com/contest/722/problem/B Description You are given a text ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)
A. Broken Clock time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)(set容器里count函数以及加强for循环)
题目链接:http://codeforces.com/contest/722/problem/D 1 #include <bits/stdc++.h> #include <iostr ...
- 二分 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D
http://codeforces.com/contest/722/problem/D 题目大意:给你一个没有重复元素的Y集合,再给你一个没有重复元素X集合,X集合有如下操作 ①挑选某个元素*2 ②某 ...
- 线段树 或者 并查集 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C
http://codeforces.com/contest/722/problem/C 题目大意:给你一个串,每次删除串中的一个pos,问剩下的串中,连续的最大和是多少. 思路一:正方向考虑问题,那么 ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心
D. Generating Sets 题目连接: http://codeforces.com/contest/722/problem/D Description You are given a set ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) A. Broken Clock 水题
A. Broken Clock 题目连接: http://codeforces.com/contest/722/problem/A Description You are given a broken ...
随机推荐
- win10内建子系统Linux
从cmd中下载linux和linux终端安装程序一样 最新要从商店购买(当然是免费) ubuntu openSUSE SUSE 3个 创建用户
- Tornado实现多线程、多进程HTTP服务
背景 线上有一个相关百科的服务,返回一个query中提及的百科词条.该服务是用python实现的,以前通过thrift接口访问,现要将其改为通过HTTP访问.之前没有搭建HTTPServer的经验,因 ...
- Linux 并发链接数
并发数查看 查看 TCP 协议连接数 netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' SYN_RECV # ...
- Codeforces 237 div2 B. Marathon(关于精度损失的教训)
题目链接:http://codeforces.com/contest/404/problem/B?csrf_token=6292hf3e1h4g5e0d16a996ge6bgcg7g2 解题报告:一个 ...
- 第12月第1天 MASConstraintMaker crash
1. crash [valueLabel mas_makeConstraints:^(PAKitMASConstraintMaker *make) { make.left.equalTo(finish ...
- 【ARTS】01_07_左耳听风-20181224~1230
ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...
- 在Scrapy项目【内外】使用scrapy shell命令抓取 某网站首页的初步情况
Windows 10家庭中文版,Python 3.6.3,Scrapy 1.5.0, 时隔一月,再次玩Scrapy项目,希望这次可以玩的更进一步. 本文展示使用在 Scrapy项目内.项目外scrap ...
- MVC Autofac依赖注入
通过Dll实现全部类的属性注入,该演示实例主要通过多层架构中单一的对象方式来演示,没有采取接口的方式, 新建AutoFacHelper类,如下代码: public class AutoFacHelpe ...
- javascript-词法分析解析
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- java 异常使用指南
应该在以下情况使用异常: 1.)在恰当的级别处理问题.(在知道如何处理的情况下才捕获异常) 2.)解决问题并且重新调用异常的方法 3.)进行少许的修补,然后绕过异常发生的地方继续执行 4.)用别的数据 ...