Codeforces 722C - Destroying Array By Assassin

来源:互联网 发布:软件开发 需求文档 编辑:程序博客网 时间:2024/05/21 17:13
C. Destroying ArrayYou 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.InputThe first line of the input contains a single integer n (1 ≤ n ≤ 100000) — 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.OutputPrint n lines. The i-th line should contain a single integerthe maximum possible sum of elements on the segment containing no destroyed elements, after first i operations are performed.ExamplesInput41 3 2 53 4 1 2Output5430Input51 2 3 4 54 2 3 5 1Output65510Input85 5 4 4 6 6 5 55 2 8 7 1 3 4 6Output18161188660NoteConsider 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. 

这题目不是我想的。。。正常大家都是并查集搞过的,但是想不通,于是学习了一下STL的方法,简直开阔了新的视野!

我们用到了set和multiset这两个STL,建议大家自己动手学一学,可以看这里学习一下迭代器
set和multiset学习

然后我们用set(pair<.int,int>)记录每个从左到右联通的线段,用multiset记录已经写入的每一段的总和!

那么为什么用这个,第一用set记录线段,不重合所以能保证不会出现相同项,和用multiset首先因为可能和相同,并且我们只是要最大值,set和multiset都是默认升序的,那么我们想要最大值,只需要反向迭代器的开始就行了(PS:如果觉得迭代器蒙蔽,一定学习一下set,基本上就明白了)。注意这里set存入这样存make_pair(tail,head) (这个make_pair很直观,生成pair组),至于为什么后面就知道了。

那么我们每一次只要找到销毁的东西的位置在哪个线段就行,怎么找!包含第p个数的区间就是 l_bound (make_pair(p,0))
lower_bound() 返回指向大于(或等于)某值的第一个元器)))

然后注意删除sum中的值是需要find一下的,否则你这么的数就
了。。。

之后就是插入,这里注意我么你的记录方式!(i,j]是介样的!

代码就简了

#include<bits/stdc++.h>#define input freopen("input.txt","r",stdin)#define ll long long#define P pair<int,int>using namespace std;long long head[100005]={0};set<P>seg;multiset<ll>sum;void erase(int p){    set<P> :: iterator it = seg.lower_bound(make_pair(p,0));    sum.erase(sum.find(head[it->first]-head[it->second]));    seg.insert(make_pair(p-1,it->second)),sum.insert(head[p-1]-head[it->second]);    seg.insert(make_pair(it->first,p)),sum.insert(head[it->first]-head[p]);    seg.erase(it);}ll max(){    multiset<ll> :: reverse_iterator mit=sum.rbegin();    printf("%I64d\n",*mit);}int main(){    input;    int n,k;    while(scanf("%d",&n)!=EOF){        for(int i=1;i<=n;i++){            scanf("%d",&k);            head[i]=head[i-1]+k;        }        seg.insert(make_pair(n,0));  //seg的初始状态,将开始多的一段加入(0,n]         sum.insert(head[n]);         //sum加入第一个总和        for(int i=1;i<=n;i++){            scanf("%d",&k);            erase(k);            max();        }     }    return 0;} 

涨姿势!

0 0
原创粉丝点击