Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C 倒序并查集
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 1to 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 integers1 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个正数,然后给你n个操作,每次都将其中一个数字划去,然后问你整个数组中连续的一段的的最大的和是多少。
并查集:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set> #define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define CT continue
#define SC scanf
const int N=1e5+10;
ll a[N],ans[N];
int op[N],use[N],f[N]; int findr(int u)
{
if(f[u]!=u)
f[u]=findr(f[u]);
return f[u];
} int main()
{
int n;
while(~SC("%d",&n))
{
for(int i=1;i<=n;i++)
{
SC("%lld",&a[i]);
f[i]=i;
}
for(int i=1;i<=n;i++) SC("%d",&op[i]);
MM(use,0);MM(ans,0);
for(int i=n-1;i>=1;i--)
{
int k=op[i+1];
use[k]=1;
if(use[k-1])
{
int par=findr(k-1);
a[k]+=a[par];
f[par]=k;
}
if(use[k+1])
{
int par=findr(k+1);
a[k]+=a[par];
f[par]=k;
}
ans[i]=max(a[k],ans[i+1]);
}
for(int i=1;i<=n;i++) printf("%lld\n",ans[i]);
}
return 0;
}
倒序并查集,合并。
Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C 倒序并查集的更多相关文章
- 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 ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array
C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...
随机推荐
- 分享一些JVM常见的面试题(转)
出处: 分享一些JVM常见的面试题 前言: 在遨游了一番 Java Web 的世界之后,发现了自己的一些缺失,所以就着一篇深度好文:知名互联网公司校招 Java 开发岗面试知识点解析 ,来好好的对 ...
- Class.getResources()和classLoader.getResources()区别
Class.getResource(String path) path不以’/'开头时,默认是从此类所在的包下取资源: path 以’/'开头时,则是从ClassPath根下获取: package t ...
- 作业12:List集合类
一 为什么使用List? 1 List与数组 List 数组 可变长度 固定长度 方便的接口(支持Java8的Stream) / 2 List的实现方式 数组:ArrayList 查询效率比较高 插入 ...
- js实现div转图片并保存
最近工作中遇到的需求,将div转成图片并保存. 1.准备需要用到的js插件jquery-1.8.2.js,html2canvas.min.js(将div转换为canvas),bluebird.js(用 ...
- VisualSVN Server 安装
1.去官网下载:https://www.visualsvn.com/server/download/ 2.安装: 修改为后: 3.开机自动启动.
- 数学模块 math 函数的调用
数学模块 math 模块名: math 注: linux下为内建模块 Mac OS下为标准库模块 数学模块用法: import math # 或 from math import * 数据 描述 ma ...
- Redis使用总结-基础篇
年底的时候开始尝试在重构的项目中使用redis,现在项目稳定运行也有一段时间了,这里做一下阶段性总结. 一.简介 首先,redis是什么意思呢,官方文档的FAQ里给出了答案:It means REmo ...
- windows下通过批处理脚本启动redis
三种启动方式的特点: 第一种方式 :根目录之命令窗口启动 特点:每次启动都要进入到redis的根目录,比较繁琐,并且占用一个窗口 第二种方式:bat脚本便捷启动 特点:可放在桌面便捷启动,占用窗口 第 ...
- 一图一知-TS之symbol
- Python3+Appium学习笔记04-自动下载apk
在项目中,app更新后,都会发布新的apk版本,然后去蒲公英对应的网站上扫码下载对应的apk进行测试.所以就在想,通过app参数进行安装时,是否可以先通过代码去拉取一遍最新的apk安装包. 因为学 ...