Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array
1 second
256 megabytes
standard input
standard output
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.
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.
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.
4
1 3 2 5
3 4 1 2
5
4
3
0
5
1 2 3 4 5
4 2 3 5 1
6
5
5
1
0
8
5 5 4 4 6 6 5 5
5 2 8 7 1 3 4 6
18
16
11
8
8
6
6
0
Consider the first sample:
- Third element is destroyed. Array is now 1 3 * 5. Segment with maximum sum 5 consists of one integer 5.
- Fourth element is destroyed. Array is now 1 3 * * . Segment with maximum sum 4 consists of two integers 1 3.
- First element is destroyed. Array is now * 3 * * . Segment with maximum sum 3 consists of one integer 3.
- Last element is destroyed. At this moment there are no valid nonempty segments left in this array, so the answer is equal to 0.
题意:n个数,删除点的顺序,求每次最大的连续和;
思路:并查集,倒着写;线段树也可以;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=2e5+,M=4e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
int fa[N],flag[N];
ll sum[N],ans[N],a[N],b[N];
int fa18(int x)
{
return x==fa[x]?x:fa[x]=fa18(fa[x]);
}
void update(int u,int v,int i)
{
int x=fa18(u);
int y=fa18(v);
if(x!=y)
{
fa[x]=y;
sum[y]+=sum[x];
ans[i]=max(ans[i+],max(ans[i],sum[y]));
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
scanf("%d",&b[i]);
for(int i=;i<=n;i++)
fa[i]=i,sum[i]=a[i];
for(int i=n;i>=;i--)
{
if(b[i]==fa[b[i]])
ans[i]=max(ans[i+],max(sum[b[i]],ans[i]));
if(flag[b[i]-])update(b[i]-,b[i],i);
if(flag[b[i]+])update(b[i]+,b[i],i);
flag[b[i]]=;
}
for(int i=;i<=n+;i++)
{
printf("%lld ",ans[i]);
}
return ;
}
Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array的更多相关文章
- 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) C. Destroying Array 带权并查集
C. Destroying Array 题目连接: http://codeforces.com/contest/722/problem/C Description You are given an a ...
- 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 ...
随机推荐
- 【BZOJ1880】[Sdoi2009]Elaxia的路线 最短路+DP
[BZOJ1880][Sdoi2009]Elaxia的路线 Description 最近,Elaxia和w**的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起 ...
- 截取字符(pos,copy,Leftstr,MidStr,RightStr)以逗号为准把字符串拆分,判断字符串是否有数字、字母(大小写), 去掉字符串空格
1.copy(a,b,c) 举个例子: str := “123456”;str1 := Copy(Str,2,3);结果就是 str1 等于 234.Copy有3个参数,第一个是你要处理的字符串,第二 ...
- hive表信息查询:查看表结构、表操作等
转自网友的,主要是自己备份下 有时候不记得! 问题导读:1.如何查看hive表结构?2.如何查看表结构信息?3.如何查看分区信息?4.哪个命令可以模糊搜索表 1.hive模糊搜索表 show tabl ...
- iOS响应超出View范围点击事件
// 在view中重写以下方法,其中self.button就是那个希望被触发点击事件的按钮 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent ...
- 解决IE,z-index失效
在影响显示顺序的模块加上: style="position:relative;z-index:-1;" 解决IE,z-index失效
- JS上传图片预览及图片限制
HTML代码: <form action="__SELF__" method="post" enctype='multipart/form-data'&g ...
- 爬虫四 selenium模块详细参数
selenium元素定位方法 一.访问页面并获取网页html from selenium import webdriver browser = webdriver.Chrome() browser.g ...
- 曾经跳过的坑------replace、替换斜杠反斜杠、时间格式化处理
JAVA 中: 坑一: replace没有用对象进行接收.直接使用 dateStr.replaceAll("\\/", "-"); 是不行的,至少得加上 &qu ...
- Python 5 面对对象编程
面向对象编程: 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 面向过程编程 ...
- JAVA 文件转字节数组转字符串
public static void main(String[] args) throws IOException { byte[] bytes = FileUtils.readFileToByteA ...