Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维
#include <bits/stdc++.h>
using namespace std;
int n,i,a,le,ri;
long long s[];
map<int, long long> all;
multiset<long long> sum;
int main() {
scanf("%d",&n);
for (i=; i<=n; i++) {
scanf("%d",&a);
s[i]=s[i-]+a;
}
all[]=n;
sum.insert(-s[n]);
for (i=; i<=n; i++) {
scanf("%d",&a);
auto it=all.lower_bound(a);
it--;
le=it->first;
ri=it->second;
sum.erase(sum.find(s[le]-s[ri]));
all.erase(it);
if (le+<a) {
all[le]=a-;
sum.insert(s[le]-s[a-]);
}
if (a<ri) {
all[a]=ri;
sum.insert(s[a]-s[ri]);
}
if (i==n) puts(""); else printf("%I64d\n",-*sum.begin());
}
return ;
}
反向思路,从删掉了最后一个元素开始。一个个恢复。判断左右是否有已经恢复的元素然后在区间内归并元素,用并查集来判断区间所属。那么区间段值不断增大,最后得到结果。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#define LL long long
using namespace std; const int Maxn = ;
int n, delId[Maxn], par[Maxn], used[Maxn];
LL a[Maxn], b[Maxn], ans[Maxn]; int find(int x)
{
return (par[x] == x)?x: find(par[x]);
} int main()
{
cin>>n;
for(int i = ; i < n; i ++){
scanf("%lld",&a[i]);
par[i] = i;
used[i] = ;
}for(int i = ; i < n; i ++){
scanf("%d",&delId[i]);
delId[i] --;
}
for(int i = n - ; i >= ; i --){
int index = delId[i];
used[index] = ;
b[index] += a[index];
if(used[index - ] && index){
int fa = find(index - );
b[fa] += a[index];
par[index] = fa;
}if(used[index + ] && index != n - ){
int newFa = find(index), oldFa = find(index + );
par[oldFa] = newFa;
b[newFa] += b[oldFa];
}
ans[i] = max(ans[i + ], b[find(index)]);
}
for(int i = ; i <= n; i ++){
cout<<ans[i]<<" ";
}cout<<endl;
return ;
}
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 1 to 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 integers 1 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.
Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维的更多相关文章
- 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 ...
随机推荐
- 3.Linux的远程管理及网络下载
3.1 Linux的远程管理 3.1.1 远程管理概述 什么是远程管理: 1.为什么需要远程管理: 服务器通常是Linux系统,而服务器不可能一直在身边,所以就需要远程来操作服务器 企业中通常需要集群 ...
- js里的深度克隆
ES6 数组克隆 let arr = [1,2,3,4,5]; let arr1 = [...a]; arr1 = ["a","b","c" ...
- Opencv学习之路—Opencv下基于HOG特征的KNN算法分类训练
在计算机视觉研究当中,HOG算法和LBP算法算是基础算法,但是却十分重要.后期很多图像特征提取的算法都是基于HOG和LBP,所以了解和掌握HOG,是学习计算机视觉的前提和基础. HOG算法的原理很多资 ...
- BZOJ 1602 牧场行走
直接写一波Lca就好了 #include<cstdio> #include<cmath> #include<algorithm> using namespace s ...
- 络谷 P2865 [USACO06NOV]路障Roadblocks
P2865 [USACO06NOV]路障Roadblocks 题目描述 Bessie has moved to a small farm and sometimes enjoys returning ...
- spring-cloud-feign使用@RequetParam错误:QueryMap parameter must be a Map: int
错误: QueryMap parameter must be a Map: int spring-cloud-feign处理@RequestParam和Spring MVC的不一样,Spring MV ...
- COCOS2D 学习笔记
cc.Node.scheduleUpdate:function () * schedules the "update" method. ...
- Html5笔记 表格 布局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- OpenCV:Visual Studio 2013 Ultimate中OpenCV 2.4.8 配置
配置环境: 操作系统:Win8.1 64位 IDE平台:Visual Studio 2013 Ultimate 一.准备OpenCV 2.4.8 1.下载:从官网下载 OpenCV2.4.8: ...
- SharePoint 2013 改动表单认证登录页面
前 言 之前的博客我们介绍了怎样为SharePoint配置表单登陆,可是.登陆页面是丑.非常丑.非常丑.特别非常丑! 我们如今就介绍一下怎样定制SharePoint表单登陆页面! SharePoint ...