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 ...
随机推荐
- Django之视图层
一.视图函数 一个视图函数,简称视图,是一个简单的python函数,接收web请求并返回web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误等.在函数中必须写一个request的参 ...
- C++ 输入、输出运算符重载
C++ 能够使用流提取运算符 >> 和流插入运算符 << 来输入和输出内置的数据类型.我们可以重载流提取运算符和流插入运算符来操作对象等用户自定义的数据类型. 在这里,有一点很 ...
- 可能会导致循环或多重级联路径。请指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。
错误提示:可能会导致循环或多重级联路径.请指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束. 原因:自表连接(同一张表 ...
- C#基础知识回顾-- 反射(4)
从程序集获得类型 先说点题外话,现在技术真的发展太快了.我这边还很多东西半生不熟 呢,那边又出现了好多有趣的新东西让你眼红不已.学还是不学这还真是 个问题.Node.js .bootstrap,我最近 ...
- [转]使用@Test 也可以从spring容器中获取依赖注入
转自:http://blog.csdn.net/u010987379/article/details/52091790 @RunWith(SpringJUnit4ClassRunner.class) ...
- 【Spring】18、springMVC对异常处理的支持
无论做什么项目,进行异常处理都是非常有必要的,而且你不能把一些只有程序员才能看懂的错误代码抛给用户去看,所以这时候进行统一的异常处理,展现一个比较友好的错误页面就显得很有必要了.跟其他MVC框架一样, ...
- DataGridView 多列排序功能
System.Data.DataTable dt = new System.Data.DataTable(); private void FillDataGridView() { dt.Columns ...
- JVM内存:年轻代、老年代、永久代(推荐 转)
参考文章: 1.Java 新生代.老年代.持久代.元空间 2.Java内存与垃圾回收调优 3.方法区的Class信息,又称为永久代,是否属于Java堆? Java 中的堆是 JVM 所管理的最大的一块 ...
- HDU3567
Eight II Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 130000/65536 K (Java/Others)Total S ...
- jquery弹窗时禁止body滚动条滚动
当弹出一个jq提示窗口的时候,一般窗口右边还会有进度条的情况,禁止进度条方法禁止浏览器滚动条滚动: $('body').css({ "overflow-x":"hidde ...