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 ...
随机推荐
- 集成板的CodeBlocks编译器配置相关文档
如需安装包请后台留言!! 原文链接:https://w.url.cn/s/A1RSfZP 打开安装包进行安装,除安装路径大家可以自行调整外, 其他默认即可.最后安装完成,CodeBlocks的大门即将 ...
- According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by de
MySQL在高版本需要指明是否进行SSL连接 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/framework?characterEncoding ...
- Python实现英文文章加密传送,收到后进行解密
思路:将I Love You这样的字符串中的每一个字符,将他的Unicode码都就进行加或减去一个特定的数, 在传送过程中,如果被截获,获取的也是一段混乱的文章,当收到这段文章后,按相同的方式对Uni ...
- 自定义策略-简单实践 <一>
1.建立 netcore mvc 项目. 2.startup.cs 中添加服务 services.AddAuthorization(option=> { var requirements ...
- .net Core 图片验证码 基于SkiaSharp实现
public class ImageCaptcha { /// <summary> /// 干扰线的颜色集合 /// </summary> private List<SK ...
- python selenium4 模拟点击+拖动+保存验证码 测试对象+以验证码的返回ID保存命名 58同城验证码
#!/usr/bin/python # -*- coding: UTF-8 -*- # @Time : 2019/12/5 17:30 # @Author : shenghao/10347899@qq ...
- pthread 编程基础
Linux系统下的多线程遵循POSIX线程接口,称为pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.与vxworks上任务的概 ...
- 微信小程序手动实现select下拉框选择
在小程序中没有像h5中的下拉 标签的 picker又满足部了,那就自己动手写 <view class='list-msg'> <view class='list-msg1'> ...
- 安卓开发之HttpURLConnection类和Handler类的使用
package com.lidaochen.test; import java.io.ByteArrayOutputStream; import java.io.InputStream; public ...
- 17.SpringMVC核心技术-拦截器
SpringMVC 中的 Interceptor 拦截器是非常重要和相当有用的,它的主要作用是拦截指定 的用户请求, 并进行相应的预处理与后处理.其拦截的时间点在“处理器映射器根据用户提 交的请求映射 ...