C. Destroying Array
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

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.

Examples
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

思路:并查集;

倒着来做,每次我们向原来的序列中加点,那么我们如果知道他的左边i-1和右边i+1,是否已经存在,那么我们就可以将他们合并,这个用并查集维护就可以了,然后,用当前加入的点,所构成的段去更新最大值即可。

 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<set>
7 #include<math.h>
8 #include<map>
9 using namespace std;
10 typedef long long LL;
11 LL ans[100005];
12 int bns[100005];
13 int bin[100005];
14 int du[100005];
15 LL ask[100005];
16 bool flag[100005];
17 LL cost[100005];
18 int main(void)
19 {
20 int n;
21 scanf("%d",&n);
22 {
23 int i,j;memset(cost,0,sizeof(cost));
24 for(i = 1; i <= n; i++)
25 {
26 scanf("%lld",&ans[i]);
27 }
28 for(i = 1; i <= n; i++)
29 {
30 scanf("%d",&bns[i]);
31 }
32 for(i = 0; i <= 100000; i++)
33 {
34 bin[i] = i;
35 du[i] = 1;
36 }
37 LL maxx = 0;
38 for(i = n; i >= 1; i--)
39 {
40 ask[i] = maxx;
41 flag[bns[i]] = true;
42 cost[bns[i]] = ans[bns[i]];
43 maxx =max(maxx,ans[bns[i]]);
44 if(flag[bns[i]-1])
45 {
46 int xx = bns[i];
47 int yy = bns[i]-1;
48 int x,y;
49 for(x = xx; x!=bin[x];)
50 x = bin[x];
51 for(y = yy; y!=bin[y];)
52 y = bin[y];
53 if(du[x]>du[y])
54 {
55 bin[y] = x;
56 du[x]+=du[y];
57 cost[x]+=cost[y];
58 maxx = max(maxx,cost[x]);
59 }
60 else
61 {
62 bin[x] = y;
63 du[y]+=du[x];
64 cost[y]+=cost[x];
65 maxx = max(maxx,cost[y]);
66 }
67 }
68 if(flag[bns[i]+1])
69 {
70 int xx = bns[i];
71 int yy = bns[i]+1;
72 int x,y;
73 for(x = xx; x!=bin[x];)
74 x = bin[x];
75 for(y = yy; y!=bin[y];)
76 y = bin[y];
77 if(du[x]>du[y])
78 {
79 bin[y] = x;
80 du[x]+=du[y];
81 cost[x]+=cost[y];
82 maxx = max(maxx,cost[x]);
83 }
84 else
85 {
86 bin[x] = y;
87 du[y]+=du[x];
88 cost[y]+=cost[x];
89 maxx = max(maxx,cost[y]);
90 }
91 }
92 }
93 for(i = 1;i <= n;i++)
94 {
95 printf("%lld\n",ask[i]);
96 }
97 }
98 return 0;
99 }

C

D. Generating Sets
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a set Y of n distinct positive integers y1, y2, ..., yn.

Set X of n distinct positive integers x1, x2, ..., xn is said to generate set Y if one can transform X to Y by applying some number of the following two operation to integers in X:

  1. Take any integer xi and multiply it by two, i.e. replace xi with 2·xi.
  2. Take any integer xi, multiply it by two and add one, i.e. replace xi with 2·xi + 1.

Note that integers in X are not required to be distinct after each operation.

Two sets of distinct integers X and Y are equal if they are equal as sets. In other words, if we write elements of the sets in the array in the increasing order, these arrays would be equal.

Note, that any set of integers (or its permutation) generates itself.

You are given a set Y and have to find a set X that generates Y and the maximum element of X is mininum possible.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 50 000) — the number of elements in Y.

The second line contains n integers y1, ..., yn (1 ≤ yi ≤ 109), that are guaranteed to be distinct.

Output

Print n integers — set of distinct integers that generate Y and the maximum element of which is minimum possible. If there are several such sets, print any of them.

Examples
Input
5
1 2 3 4 5
Output
4 5 2 3 1 
Input
6
15 14 3 13 1 12
Output
12 13 14 7 3 1 
Input
6
9 7 13 17 5 11
Output
4 5 2 6 3 1 
思路:贪心;
每次选取当前数中最大的进行除2,当队列中没有的时候将新的数加入队列继续,如果存在则一直进行下去,如果得到0,那么说明当前的数已经不能分解了.复杂度(n*log(n)^2)
 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<set>
7 #include<math.h>
8 #include<map>
9 using namespace std;
10 typedef long long LL;
11 int ans[50005];
12 int bns[50005];
13 typedef struct pp
14 {
15 int x;
16 bool operator<(const pp &cx)const
17 {
18 return cx.x>x;
19 }
20 } ss;
21 priority_queue<ss>que;
22 map<int,int>my;
23 int main(void)
24 {
25 int n;
26 while(scanf("%d",&n)!=EOF)
27 {
28 int i,j;int cn = 0;
29 my.clear();
30 while(!que.empty())
31 que.pop();
32 for(i = 0; i < n; i++)
33 {
34 scanf("%d",&ans[i]);
35 ss ak;
36 ak.x = ans[i];
37 que.push(ak);
38 my[ans[i]]++;
39 }
40 while(true&&!que.empty())
41 {
42 ss ak = que.top();
43 //printf("%d\n",ak.x);
44 que.pop();
45 int v = ak.x;
46 my[ak.x]--;
47 int c = ak.x/2;
48 while(my.count(c))
49 {
50 c/=2;
51 }
52 if(c == 0)
53 {
54 bns[cn++] = v; break;
55 }
56 else
57 {
58 ak.x = c;
59 que.push(ak);
60 my[c]++;
61 }
62 }
63 while(!que.empty())
64 {
65 bns[cn++] = que.top().x;
66 que.pop();
67 }
68 printf("%d",bns[0]);
69 for(i = 1;i < cn;i++)
70 {
71 printf(" %d",bns[i]);
72 }
73 printf("\n");
74 }
75 return 0;
76 }

D

codeforces(Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) )(C,D)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维

    原题中需要求解的是按照它给定的操作次序,即每次删掉一个数字求删掉后每个区间段的和的最大值是多少. 正面求解需要维护新形成的区间段,以及每段和,需要一些数据结构比如 map 和 set. map< ...

  6. 二分 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D

    http://codeforces.com/contest/722/problem/D 题目大意:给你一个没有重复元素的Y集合,再给你一个没有重复元素X集合,X集合有如下操作 ①挑选某个元素*2 ②某 ...

  7. 线段树 或者 并查集 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C

    http://codeforces.com/contest/722/problem/C 题目大意:给你一个串,每次删除串中的一个pos,问剩下的串中,连续的最大和是多少. 思路一:正方向考虑问题,那么 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. leetcode刷题之数组NO.4

    1.题目 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数 ...

  2. HelloWorldModelMBean

    package mbeanTest; import java.lang.reflect.Constructor; import javax.management.Descriptor; import ...

  3. oracle 存储过程及REF CURSOR的使用

    基本使用方法及示例 1.基本结构: CREATE OR REPLACE PROCEDURE 存储过程名字 (参数1 IN NUMBER,参数2 IN NUMBER) AS 变量1 INTEGER := ...

  4. Dockers启动Kafka

    首先安装 Confluent Platform Quick Start for Confluent Platform (Local install) Use this quick start to g ...

  5. Maven项目打包成war包并启动war包运行

    1 项目打包 1.1 右键点击所需要打包的项目,点击如图所示 Maven clean,这里 Maven 会清除掉之前对这个项目所有的打包信息. 1.2进行完 Maven clean 操作后,在ecli ...

  6. 全面解析 | 钥匙环服务的应用场景&商业价值

    在互联互通的场景驱动下,同一开发者旗下常常拥有多款应用或者多个应用形态,用户在同一设备的不同应用或端口登录时,即便使用同一帐号,仍需要重复输入密码进行验证,操作复杂,直接影响到用户的使用体验,而华为钥 ...

  7. 攻击科普:DDos

    目录 一.DDoS 攻击究竟是什么? 二.DDoS 攻击多少G是什么意思? 二.DDoS攻击种类 ICMP Flood UDP Flood NTP Flood SYN Flood CC攻击 DNS Q ...

  8. ABP VNext框架基础知识介绍(2)--微服务的网关

    ABP VNext框架如果不考虑在微服务上的应用,也就是开发单体应用解决方案,虽然也是模块化开发,但其集成使用的难度会降低一个层级,不过ABP VNext和ABP框架一样,基础内容都会设计很多内容,如 ...

  9. ios self.和_区别

    - "self."调用该类的setter或getter方法,"_"直接获取自己的实例变量.property 和 instance variable 是有区别的. ...

  10. 混沌映射初始化种群之Logistic映射

    Logstic混沌映射初始化种群 Step 1:     随机生成一个\(d\)维向量\({X_0}\),向量的每个分量在0-1之间. Step 2:     利用Logistic映射生成N个向量.L ...