CF Destroying Array(容器的使用)
来源:互联网 发布:顾客旅程 绘图软件 编辑:程序博客网 时间:2024/06/05 06:22
You are given an array consisting of n non-negative integersa1, a2, ..., an.
You are going to destroy integers in the array one by one. Thus, you are given the permutation of integers from1 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 be0.
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 firsti operations are performed.
41 3 2 53 4 1 2
5430
51 2 3 4 54 2 3 5 1
65510
85 5 4 4 6 6 5 55 2 8 7 1 3 4 6
18161188660
Consider the first sample:
- Third element is destroyed. Array is now 1 3 * 5. Segment with maximum sum5 consists 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 to0.
题意:给你一个数组,每次删除一个元素,求最大连续段值(不包括被删除的元素),直到所有都被删除了。
解题思路:个人觉得思路很简单,不过当时没用set容器的l自带ower_bound()函数,一直TLE在第十三个数据。用一个set m存放所有段的和值,另外一个容器存放被删除的
位置。每次输入一个位置x,查询已经被删除位置的最前一个和最后面一个位置l、r,那么原来段(l,r)( ()代表不包含边界的开区间)被分成了(l,x) 和 (x,r)这两个段,
那么就要将m内(l,r)删除,插入(l,x) 和 (x,r)。这里都是值这个段的值和。
底下有两个AC代码,一个是用set,不过得注意,中间结果有可能有多个相同的值,需要记录它,因为set一个元素只能出现一次。也可以用multiset,它可以有多个相同元素。
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<set>#include<map>using namespace std;typedef long long ll;const int maxn=100010;ll a[maxn];set<ll> m;set<int> pos;map<ll,int> ma;int main(){ int n; while(~scanf("%d",&n)) { m.clear(); pos.clear(); ma.clear(); a[0]=0; int x; for(int i=1;i<=n;i++) { scanf("%d",&x); a[i]=a[i-1]+x; } a[n+1]=a[n]; m.insert(a[n]); ma[a[n]]++; pos.insert(0); pos.insert(n+1); set<int>::iterator it; set<ll>::iterator itt; for(int i=0;i<n;i++){ scanf("%d",&x); int l,r; ll mid; it=pos.lower_bound(x); r=*it; --it; l=*it; pos.insert(x); ///cout << r << " " << l << endl; mid=a[r-1]-a[l]; if(ma[mid]>1) ma[mid]--; else{ m.erase(mid); ma[mid]--; } ///cout << a[r-1]-a[x] << " " << a[x-1]-a[l] << endl; ///cout << mid << endl; ma[a[r-1]-a[x]]++; ma[a[x-1]-a[l]]++; m.insert(a[r-1]-a[x]); m.insert(a[x-1]-a[l]); itt=m.end(); itt--; printf("%I64d\n",*itt); } } return 0;}
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<set>#include<map>using namespace std;typedef long long ll;const int maxn=100010;ll a[maxn];multiset<ll> m; ///set<int> pos;int main(){ int n; while(~scanf("%d",&n)) { m.clear(); pos.clear(); a[0]=0; int x; for(int i=1;i<=n;i++) { scanf("%d",&x); a[i]=a[i-1]+x; } a[n+1]=a[n]; m.insert(a[n]); pos.insert(0); pos.insert(n+1); set<int>::iterator it; for(int i=0;i<n;i++){ scanf("%d",&x); int l,r; ll mid; it=pos.lower_bound(x); /// 如果用 it=lower_bound(pos.begin(), pos.end(), x) 这来查询会超时,因为set自带的lower_bound()比较快。 r=*it; --it; l=*it; pos.insert(x); ///cout << r << " " << l << endl; mid=a[r-1]-a[l]; ///multiset<ll>::iterator itt; ///for(itt=m.begin(); itt!=m.end(); itt++) /// cout << *itt << " "; ///cout << endl; m.erase(m.lower_bound(mid)); ///cout << mid << endl; ///cout << a[r-1]-a[x] << " " << a[x-1]-a[l] << endl; ///cout << mid << endl; m.insert(a[r-1]-a[x]); m.insert(a[x-1]-a[l]); printf("%lld\n",*m.rbegin()); } } return 0;}
- CF Destroying Array(容器的使用)
- 并查集(Destroying Array,cf 722C)
- Destroying Array
- CodeForces - 722C Destroying Array (set)
- Destroying Array(并查集)
- CodeForces 722C Destroying Array(并查集和我奇怪的二叉树)
- CF #302 Div2 D Destroying Roads(最短路)
- Codeforces722 C Destroying Array
- CodeForces722C Destroying Array【瞎搞】
- Destroying Array CodeForces
- Codeforces722 C. Destroying Array (并查集)
- Codeforces Round 722C:Destroying Array(离线)
- codeforces 722C. Destroying Array(并查集||set)
- 并查集巧妙用法(codeforces)C. Destroying Array
- Codeforces722C-Destroying Array(线段树 or 并查集)
- CodeForces 722C Destroying Array(数组删除区间最大值)
- CodeForces 722C. Destroying Array(逆向思维)
- Sort the Array (CF)
- onNewIntent调用时机
- UVA 1422 Processor(优先队列+贪心+二分)
- Item9 Prefer alias declarations to typedefs
- html5 canvas学习--移动坐标和旋转坐标
- jsp 9个内置对象的常用方法
- CF Destroying Array(容器的使用)
- JSP的组成
- 数据区,代码区,堆栈区,操作系统堆栈
- Android面试整理
- 选择排序
- 指针实现矩阵边缘元素求和
- 组件改变生活_揭开Vue组件的神秘面纱
- MVC/MVP/MVVM
- Python写入excel提示“excel中有不可读的内容”