Destroying Array CF 722C
题目大意就是给长度为 n 一个数列,有 n 每次删除,每一次删除第 i 个位置上的数,求每一次删除后剩余不连续数列的最大区间和。
输入样例
4
1 3 2 5
3 4 1 2
输出样例
5
4
3
0
第二行是原来的数列,第三行是删除第 i 个数。
这道题的正解是用并查集来做。要将删除的顺序存下来,并倒序操作,这样就相当于每一次加上第 i 数。然后判断加上的数的左右两边是否有数,有就合并,并尝试用合并的新的区间和更新答案。对了,答案也要存下来,再倒序输出,才是真正的答案。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + ;
ll a[maxn], b[maxn], sum[maxn], ans[maxn], maxm = -;
int p[maxn], n;
bool vis[maxn]; //vis[i]判断第i个位置上的数是否存在
void init() //并查集初始化:每一个点都是自己的父亲节点
{
for(int i = ; i < maxn; ++i) {sum[i] = ; p[i] = i;}
return;
}
int Find(int x)
{
return p[x] == x ? x : p[x] = Find(p[x]);
}
void merge(int x, int y) //合并
{
int px = Find(x), py = Find(y);
//肯定不联通
p[px] = py;
sum[py] += sum[px];
return;
}
int main()
{
init();
scanf("%d", &n);
for(int i = ; i <= n; ++i) scanf("%lld", &a[i]);
for(int i = ; i <= n; ++i) scanf("%lld", &b[i]);
for(int i = n; i > ; --i)
{
vis[b[i]] = ;
sum[b[i]] = a[b[i]];
if(vis[b[i] - ]) merge(b[i], b[i] - ); //左边是否有数
if(vis[b[i] + ]) merge(b[i], b[i] + ); //右边是否有数
if(sum[Find(b[i])] > maxm) maxm = sum[Find(b[i])]; //尝试更新最大区间和
ans[i - ] = maxm;
}
for(int i = ; i <= n; ++i) printf("%lld\n", ans[i]);
return ;
}
Destroying Array CF 722C的更多相关文章
- Codeforces 722C. Destroying Array
C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...
- [并查集+逆向思维]Codeforces Round 722C Destroying Array
Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- CF722C. Destroying Array[并查集 离线]
链接:Destroying Array C. Destroying Array time limit per test 1 second memory limit per test 256 megab ...
- 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) C. Destroying Array
C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...
- [codeforces722C]Destroying Array
[codeforces722C]Destroying Array 试题描述 You are given an array consisting of n non-negative integers a ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维
原题中需要求解的是按照它给定的操作次序,即每次删掉一个数字求删掉后每个区间段的和的最大值是多少. 正面求解需要维护新形成的区间段,以及每段和,需要一些数据结构比如 map 和 set. map< ...
- [CF722C] Destroying Array
C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 【37.38%】【codeforces 722C】Destroying Array
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- iomanip的作用 C++
c++程序里面经常见到下面的头文件 #include <iomanip> 这里面iomanip的作用比较多: 主要是对cin,cout之类的一些操纵运算子,比如setfill,setw,s ...
- Best way to learn android and java?
Question: I have been getting my feet wet with android sdk, eclipse, and other various beginner step ...
- 深入理解Java 8 Lambda(语言篇)
State of Lambda by Brian Goetz 原文链接:http://lucida.me/blog/java-8-lambdas-insideout-language-features ...
- 如何在UWP中统一处理不同设备间的页面回退逻辑
已经有一段时间没有写博客来记录自己的学习点滴了.现在回想起来确实有些惭愧,期间经历了一些事情,到目前为止算是平息了,是时候该收收心来充实自己了. 在本篇缪文中,楼主打算给UWP开发的初学者讲述一个在开 ...
- BUGList
Django : a. MySQL数据表还未创建时,不可在视图内直接使用模型类对象,产生报错 django.db.utils.ProgrammingError: (1146, "Table ...
- 【读书笔记】iOS-加速计与陀螺仪
一,数据的“滤波” 直接从加速度计获得的原始数据,往往不能直接使用,而是需要去除一些干扰数据,这个过程称为“滤波”.“滤波”一词来源于无线电技术中对无线电信号的处理过程.事实上从数学角度而言它们是一样 ...
- PHP会话(Session)实现用户登陆功能 转自#落人间#
对比起 Cookie,Session 是存储在服务器端的会话,相对安全,并且不像 Cookie 那样有存储长度限制,本文简单介绍 Session 的使用. 由于 Session 是以文本文件形式存储在 ...
- tomcat 取消项目名访问路径
在server.xml 里,<host>...</host>的标签之间添加<Context path="" docBase="projec ...
- loadrunner11中文版破解文档+录制脚本(图/文)
下面是整理后的loadrunner11破解版的所有文件及操作步骤. LoadRunner11安装 1.Loadrunner11下载链接:https://pan.baidu.com/s/15Svt3kt ...
- python 3.3.2报错:No module named 'urllib2'
ModuleNotFoundError: No module named 'urllib3' 1. ImportError: No module named 'cookielib'1 Python3中 ...