原题中需要求解的是按照它给定的操作次序,即每次删掉一个数字求删掉后每个区间段的和的最大值是多少。
  正面求解需要维护新形成的区间段,以及每段和,需要一些数据结构比如 map 和 set。 map<int, LL>interval2Sum来维护区间段(u->v),mulitset<LL>sum 来维护最大值。那么每次删除操作后,都需要去interval2Sum中找到对应区间,然后erase掉,
重新生成left -> delId -> right两个区间段,最后在maxSum中删掉原有的sum值 ,加入新形成的两短sum。正面刚的话,因为map 和 set的高效性 log级别所以速度还是不错的。(代码转自Codeforces : Ra16bit)
  

#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 ;
}
C. Destroying Array
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
input
4
1 3 2 5
3 4 1 2
output
5
4
3
0
input
5
1 2 3 4 5
4 2 3 5 1
output
6
5
5
1
0
input
8
5 5 4 4 6 6 5 5
5 2 8 7 1 3 4 6
output
18
16
11
8
8
6
6
0
Note

Consider the first sample:

  1. Third element is destroyed. Array is now 1 3  *  5. Segment with maximum sum 5 consists of one integer 5.
  2. Fourth element is destroyed. Array is now 1 3  *   * . Segment with maximum sum 4 consists of two integers 1 3.
  3. First element is destroyed. Array is now  *  3  *   * . Segment with maximum sum 3 consists of one integer 3.
  4. 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 -- 逆向思维的更多相关文章

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

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

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

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

  5. 二分 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D

    http://codeforces.com/contest/722/problem/D 题目大意:给你一个没有重复元素的Y集合,再给你一个没有重复元素X集合,X集合有如下操作 ①挑选某个元素*2 ②某 ...

  6. 线段树 或者 并查集 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C

    http://codeforces.com/contest/722/problem/C 题目大意:给你一个串,每次删除串中的一个pos,问剩下的串中,连续的最大和是多少. 思路一:正方向考虑问题,那么 ...

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

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

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

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

随机推荐

  1. Js—innerHTML和innerText的区别

    1.innerHTML属性和innerText属性 都是对元素的一个操作,简单讲,innerHTML可以在某种特定环境下重构某个元素节点的DOM结构,innerText只能修改文本值 在JavaScr ...

  2. 为什么Java中的密码优先使用 char[] 而不是String?

    可以看下壁虎的回答:https://www.zhihu.com/question/36734157 String是常量(即创建之后就无法更改),会保存到常量池中,如果有其他进程可以dump这个进程的内 ...

  3. Django cookie、session使用

    一.cookie Cookie是key-value结构,类似于一个python中的字典.随着服务器端的响应发送给客户端浏览器.然后客户端浏览器会把Cookie保存起来,当下一次再访问服务器时把Cook ...

  4. 【codeforces 509B】Painting Pebbles

    [题目链接]:http://codeforces.com/contest/509/problem/B [题意] 给n鹅卵石染色; 有k种颜色可供选择; 问你有没有染色方案; 使得各个堆的鹅卵石里面,第 ...

  5. Java基础学习总结(72)——提升 java 代码的运行效率

    前言 代码 优化 ,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没 ...

  6. Leetcode 49.字母异位词分组

    字母异位词分组 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 示例: 输入: ["eat", "tea", " ...

  7. CTF密码学总结

    CTF中那些脑洞大开的编码和加密 摘自:https://www.cnblogs.com/mq0036/p/6544055.html 0x00 前言 正文开始之前先闲扯几句吧,玩CTF的小伙伴也许会遇到 ...

  8. xth的第 12 枚硬币(codevs 1366)

    题目描述 Description 传说 xth 曾经拥有11枚完全相同硬币(你懂得),不过今年呢,rabbit又送了他一 枚硬币.这枚硬币和其他硬币外观相同,只有重量不同,或轻或重.Xth 一不小心, ...

  9. mysql grant 用户权限总结

    https://blog.csdn.net/anzhen0429/article/details/78296814

  10. 贪心算法 Heidi and Library (easy)

    A. Heidi and Library (easy) time limit per test 2 seconds memory limit per test 256 megabytes input ...