Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维
Posted 暴力的轮胎
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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[100100]; map<int, long long> all; multiset<long long> sum; int main() { scanf("%d",&n); for (i=1; i<=n; i++) { scanf("%d",&a); s[i]=s[i-1]+a; } all[0]=n; sum.insert(-s[n]); for (i=1; 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+1<a) { all[le]=a-1; sum.insert(s[le]-s[a-1]); } if (a<ri) { all[a]=ri; sum.insert(s[a]-s[ri]); } if (i==n) puts("0"); else printf("%I64d\n",-*sum.begin()); } return 0; }
反向思路,从删掉了最后一个元素开始。一个个恢复。判断左右是否有已经恢复的元素然后在区间内归并元素,用并查集来判断区间所属。那么区间段值不断增大,最后得到结果。
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #define LL long long using namespace std; const int Maxn = 100005; 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 = 0; i < n; i ++){ scanf("%lld",&a[i]); par[i] = i; used[i] = 0; }for(int i = 0; i < n; i ++){ scanf("%d",&delId[i]); delId[i] --; } for(int i = n - 1; i >= 0; i --){ int index = delId[i]; used[index] = 1; b[index] += a[index]; if(used[index - 1] && index){ int fa = find(index - 1); b[fa] += a[index]; par[index] = fa; }if(used[index + 1] && index != n - 1){ int newFa = find(index), oldFa = find(index + 1); par[oldFa] = newFa; b[newFa] += b[oldFa]; } ans[i] = max(ans[i + 1], b[find(index)]); } for(int i = 1; i <= n; i ++){ cout<<ans[i]<<" "; }cout<<endl; return 0; }
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 Final Round (Div. 1 + Div. 2, Combined) B
Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)
Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)
Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)题解报告
Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)
codeforces Intel Code Challenge Final Round (div.1 + div.2 combined)