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[];
- 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 ;
- }
1 second
256 megabytes
standard input
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.
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 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 ...
- 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 ...
- 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 ...
- 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 ...
- 二分 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D
http://codeforces.com/contest/722/problem/D 题目大意:给你一个没有重复元素的Y集合,再给你一个没有重复元素X集合,X集合有如下操作 ①挑选某个元素*2 ②某 ...
- 线段树 或者 并查集 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C
http://codeforces.com/contest/722/problem/C 题目大意:给你一个串,每次删除串中的一个pos,问剩下的串中,连续的最大和是多少. 思路一:正方向考虑问题,那么 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- js案例分析
名字取的高大上,其实只是我平时上网浏览遇到的一些我感觉还不错的小题目,再加上我或者是我在网上找到的一些理解,就保存到这里了. 2019/4/2 最新开了个新坑,是一个javascipt30的一些案例 ...
- jquery动态生成二维码添加自定义logo
动态生成二维码中间带logo. jquery.qrcode.js 动态生成二维码api很简单. 引入jquer(版本任意),引入jquery.qrcode.js 不需要中间带logo这样就可以了.带l ...
- c/c++排坑(3) -- c/c++中的switch语句
switch语句的简单介绍 一个 switch 语句允许测试一个变量等于多个值时的情况.每个值称为一个 case,且被测试的变量会对每个 switch case 进行检查. switch(expres ...
- vue数据绑定源码
思路分析 数据的双向绑定,就是数据变化了自动更新视图,视图变化了自动更新数据,实际上视图变化更新数据只要通过事件监听就可以实现了,并不是数据双向绑定的关键点.关键还是数据变化了驱动视图自动更新. 所有 ...
- Excel表格
自己一个一个试出来,并写上解释. 还不熟练,待多写代码多练习. #!/usr/bin/python # -*- coding:utf-8 -*- import os import xlwt impor ...
- 洋葱浏览器(Tor Browser)
第一,洋葱路由器简介 Tor Browser 是個內建「翻牆」功能的網路瀏覽器,藉由「洋蔥路由, The Onion Router (Tor)」匿名瀏覽技術,將上網時所傳遞的訊息層層加密保護,讓使用者 ...
- 第八节:numpy之四则运算与逻辑运算
- NLTK学习笔记(七):文本信息提取
目录 实体识别:分块技术 分块语法的构建 树状图 IOB标记 开发和评估分块器 命名实体识别和信息提取 如何构建一个系统,用于从非结构化的文本中提取结构化的信息和数据?哪些方法使用这类行为?哪些语料库 ...
- Maven pom 配置简介
1. groupId artifactId version 2. dependencies 3. plugins http://shmilyaw-hotmail-com.iteye.com/blog/ ...
- Maven学习总结(8)——使用Maven构建多模块项目
Maven学习总结(八)--使用Maven构建多模块项目 在平时的Javaweb项目开发中为了便于后期的维护,我们一般会进行分层开发,最常见的就是分为domain(域模型层).dao(数据库访问层). ...