CodeForces - 722C Destroying Array (set)
2017-01-23 14:32
363 查看
题意:
给出一个长度为 N 的数串,每次从数串中删去一个数,问每次操作后数串中最大连续和是多少?
思路:
利用 set 维护 断点集 O(logn)
利用 multiset 维护 线段长度集 O(logn)
利用 从数串头开始累加计算的区间和,来便于求出任意区间和 O(1)
在 添加断点时不断维护 断点集,线段长度集即可。
代码:
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int MAXN=1e5;
multiset <LL> ans;
multiset <LL>::iterator itt;
set <LL> cut;
set <LL>::iterator it;
int n,x,st,en;
LL sum[MAXN+1000],a[MAXN+1000];
void ini(){
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++)//........................................计算从头开始到每个 i 的区间和
sum[i]=a[i]+sum[i-1];
ans.clear();cut.clear();
cut.insert(0);cut.insert(n+1);//...............................初始断点 0 和 n + 1
ans.insert(sum
);//..........................................初始线段长为 sum
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>n){
for(int i=1;i<=n;i++)
cin>>a[i];
ini();
while(n--){
cin>>x;
it=cut.upper_bound(x);//...............................查找断点并维护
en=*it;
st=*(--it);
itt=ans.find(sum[en-1]-sum[st]);
ans.erase(itt);//......................................删除旧线段
ans.insert(sum[en-1]-sum[x]);//........................添加新线段
ans.insert(sum[x-1]-sum[st]);
cut.insert(x);//.......................................添加新断点
itt=ans.end();//.......................................multiset 自排序
cout<<*(--itt)<<endl;
}
}
}
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.
Example
Input
Output
Input
Output
Input
Output
Note
Consider the first sample:
Third element is destroyed. Array is now 1 3 * 5. Segment with maximum sum 5consists of one integer 5.
Fourth element is destroyed. Array is now 1 3 * * . Segment with maximum sum4 consists of two integers 1 3.
First element is destroyed. Array is now * 3 * * . Segment with maximum sum3 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.
给出一个长度为 N 的数串,每次从数串中删去一个数,问每次操作后数串中最大连续和是多少?
思路:
利用 set 维护 断点集 O(logn)
利用 multiset 维护 线段长度集 O(logn)
利用 从数串头开始累加计算的区间和,来便于求出任意区间和 O(1)
在 添加断点时不断维护 断点集,线段长度集即可。
代码:
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int MAXN=1e5;
multiset <LL> ans;
multiset <LL>::iterator itt;
set <LL> cut;
set <LL>::iterator it;
int n,x,st,en;
LL sum[MAXN+1000],a[MAXN+1000];
void ini(){
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++)//........................................计算从头开始到每个 i 的区间和
sum[i]=a[i]+sum[i-1];
ans.clear();cut.clear();
cut.insert(0);cut.insert(n+1);//...............................初始断点 0 和 n + 1
ans.insert(sum
);//..........................................初始线段长为 sum
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>n){
for(int i=1;i<=n;i++)
cin>>a[i];
ini();
while(n--){
cin>>x;
it=cut.upper_bound(x);//...............................查找断点并维护
en=*it;
st=*(--it);
itt=ans.find(sum[en-1]-sum[st]);
ans.erase(itt);//......................................删除旧线段
ans.insert(sum[en-1]-sum[x]);//........................添加新线段
ans.insert(sum[x-1]-sum[st]);
cut.insert(x);//.......................................添加新断点
itt=ans.end();//.......................................multiset 自排序
cout<<*(--itt)<<endl;
}
}
}
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.
Example
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:
Third element is destroyed. Array is now 1 3 * 5. Segment with maximum sum 5consists of one integer 5.
Fourth element is destroyed. Array is now 1 3 * * . Segment with maximum sum4 consists of two integers 1 3.
First element is destroyed. Array is now * 3 * * . Segment with maximum sum3 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.
相关文章推荐
- CodeForces 274A k-Multiple Free Set
- Codeforces 387E George and Cards 树状数组 + 集合set查询
- codeforces 577B Modulo Sum(DP||set)
- codeforces 460D:Little Victor and Set
- Codeforces 527C Glass Carving<set集合和multiset集合的使用>
- Codeforces 527C Glass Carving(Set)
- CodeForces 274A k-Multiple Free Set【思维】
- codeforces 898C. Phone Numbers(模拟+map,set,vector)
- [Codeforces 460D] Little Victor and Set (构造)
- Codeforces 34C-Page Numbers(set+vector+暴力乱搞)
- CodeForces 305C--Ivan and Powers of Two (STL:set)
- codeforces 274A k-Multiple Free Set
- Codeforces 274A k-Multiple Free Set
- codeforces-Glass Carving(527C)std::set用法
- CodeForces 705C 我被set.erase(*it)套路了
- codeforces 246 E. Blood Cousins Return (set+dsu on the tree)
- Codeforces 245G Suggested Friends - 交集set_intersection()
- CodeForces 527C. Glass Carving (SBT/线段树/std::set)
- CodeForces - 864B Polycarp and Letters 水题 set
- codeforces 878C 及部分set的用法