题目大意就是给长度为 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的更多相关文章

  1. Codeforces 722C. Destroying Array

    C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. [并查集+逆向思维]Codeforces Round 722C Destroying Array

    Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  3. CF722C. Destroying Array[并查集 离线]

    链接:Destroying Array C. Destroying Array time limit per test 1 second memory limit per test 256 megab ...

  4. 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 ...

  5. 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 ...

  6. [codeforces722C]Destroying Array

    [codeforces722C]Destroying Array 试题描述 You are given an array consisting of n non-negative integers a ...

  7. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维

    原题中需要求解的是按照它给定的操作次序,即每次删掉一个数字求删掉后每个区间段的和的最大值是多少. 正面求解需要维护新形成的区间段,以及每段和,需要一些数据结构比如 map 和 set. map< ...

  8. [CF722C] Destroying Array

    C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. 【37.38%】【codeforces 722C】Destroying Array

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. mysql中的data下的数据文件(.FRM、.MYD、.MYI)恢复为数据

    记一次mysql中的data文件操作经历 想拿到一个项目的最新的数据,做功能升级使用,备份一份数据同时也作为本地测试数据,文件有些大,我直接通过远程的phpmyadmin程序导出,不能愉快的玩耍,直接 ...

  2. N最短路径分词

    N最短路径算法是一种基于词典的分词算法. 每个句子将生成一个有向无环图, 每个字作为图的一个定点, 边代表可能的分词. 在上图中, 边的起点为词的第一个字, 边的终点为词尾的下一个字. 边1表示&qu ...

  3. Django组件之cookie与session

    一.引子 http协议是无状态的,就是它不会记录请求和响应的任何信息,比如你访问一个服务器的一个网页时,先要你登录一下,然后进入网页,但当你要进入这个服务器的另一个网页时,它照常不会知道刚才你已经登录 ...

  4. centos每天自动备份mysql数据库

    #!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin export PATH #数据库用户名 db ...

  5. angularjs学习第九天笔记(指令作用域【隔离作用域】研究)

    您好,昨天学习了指令作用域为布尔型的情况, 今天主要研究其指针作用域为{}的情况 1.当作用域scope为{}时,子作用域完全创建一个独立的作用域, 此时,子做预约和外部作用域完全不数据交互 但是,在 ...

  6. babel-polyfill的引用和使用

    前两天一个首页项目,想用vue玩耍一下,就用vue-cli搭建了一套vue的开发框架 完成开发.联调和上线后,问题来了 chrome.ff浏览器下都能正常显示的页面,在百度浏览器下愣就显示不出来了 我 ...

  7. html特殊字体显示

    1.下载需要显示的ttf字体. 2.css样式调用. /* 微软雅黑 */ @font-face { font-family: microsoftyahei; src: url('${pageCont ...

  8. 【Mybatis】一对多实例

    ①创建数据库和表,数据库为mytest,表为teacher和student DROP TABLE IF EXISTS teacher; DROP TABLE IF EXISTS student; CR ...

  9. mysql5.7 安装和多源复制实践

    MySQL 5.7发布后,在复制方面有了很大的改进和提升.比如开始支持多源复制(multi-source)以及真正的支持多线程复制了.多源复制可以使用基于二进制日子的复制或者基于事务的复制.下面我们说 ...

  10. Button's four click events

    第一种:内部类的方式 1 package com.example.phonedialer; 2 3 import com.example.click2.R; 4 5 import android.ne ...