2017-5-18-Train: Codeforces Round #332 (Div. 2)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2017-5-18-Train: Codeforces Round #332 (Div. 2)相关的知识,希望对你有一定的参考价值。

A. Patrick and Shopping(模拟题)

Today Patrick waits for a visit from his friend Spongebob. To prepare for the visit, Patrick needs to buy some goodies in two stores located near his house. There is a d1 meter long road between his house and the first shop and a d2 meter long road between his house and the second shop. Also, there is a road of length d3 directly connecting these two shops to each other. Help Patrick calculate the minimum distance that he needs to walk in order to go to both shops and return to his house.

技术分享

Patrick always starts at his house. He should visit both shops moving only along the three existing roads and return back to his house. He doesn‘t mind visiting the same shop or passing the same road multiple times. The only goal is to minimize the total distance traveled.

Input

The first line of the input contains three integers d1, d2, d3 (1 ≤ d1, d2, d3 ≤ 108) — the lengths of the paths.

  • d1 is the length of the path connecting Patrick‘s house and the first shop;

  • d2 is the length of the path connecting Patrick‘s house and the second shop;

  • d3 is the length of the path connecting both shops.

Output

Print the minimum distance that Patrick will have to walk in order to visit both shops and return to his house.

Examples

input

10 20 30

output

60

input

1 1 5

output

4

Note

The first sample is shown on the picture in the problem statement. One of the optimal routes is: house 技术分享 first shop 技术分享 second shop 技术分享house.

In the second sample one of the optimal routes is: house 技术分享 first shop 技术分享 house 技术分享 second shop 技术分享 house.

技术分享

Code:

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 int main()
 5 {
 6     LL d1 , d2 , d3;
 7     scanf("%I64d%I64d%I64d" , &d1 , &d2 , &d3);
 8     if(d1 > d2)
 9         swap(d1 , d2);
10     LL ans = min(2 * (d1 + d2) , min(d1 + d2 + d3 , 2 * (d1 + d3)));
11     printf("%I64d" , ans);
12 }
View Code

B. Spongebob and Joke(模拟)

While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick‘s personal stuff and found a sequence a1, a2, ..., a**m of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence f1, f2, ..., f**n of length n and for each number a**i got number b**i = fai. To finish the prank he erased the initial sequence a**i.

It‘s hard to express how sad Patrick was when he returned home from shopping! We will just say that Spongebob immediately got really sorry about what he has done and he is now trying to restore the original sequence. Help him do this or determine that this is impossible.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) — the lengths of sequences f**i and b**i respectively.

The second line contains n integers, determining sequence f1, f2, ..., f**n (1 ≤ f**i ≤ n).

The last line contains m integers, determining sequence b1, b2, ..., b**m (1 ≤ b**i ≤ n).

Output

Print "Possible" if there is exactly one sequence a**i, such that b**i = fai for all i from 1 to m. Then print m integers a1, a2, ..., a**m.

If there are multiple suitable sequences a**i, print "Ambiguity".

If Spongebob has made a mistake in his calculations and no suitable sequence a**i exists, print "Impossible".

Examples

input

3 3
3 2 1
1 2 3

output

Possible
3 2 1 

input

3 3
1 1 1
1 1 1

output

Ambiguity

input

3 3
1 2 1
3 3 3

output

Impossible

Note

In the first sample 3 is replaced by 1 and vice versa, while 2 never changes. The answer exists and is unique.

In the second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence.

In the third sample f**i ≠ 3 for all i, so no sequence a**i transforms into such b**i and we can say for sure that Spongebob has made a mistake.

技术分享

Code:

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 static const int MAXN = 1e5 + 10;
 4 int b[MAXN];
 5 vector<int> ans;
 6 struct Node
 7 {
 8     int v , w;
 9 }pos[MAXN];;
10 int n , m;
11 int flag;
12 int main()
13 {
14     flag = 1;
15     scanf("%d%d" , &n , &m);
16     for(int i = 1 ; i <= n ; ++i)
17     {
18         int x;
19         scanf("%d" , &x);
20         pos[x].v = i;
21         ++pos[x].w;
22     }
23 
24     for(int i = 1 ; i <= m ; ++i)
25     {
26         int x;
27         scanf("%d" , &b[i]);
28         if(pos[b[i]].w == 0)
29             flag = 3;
30     }
31 
32     if(flag == 3)
33     {
34         puts("Impossible");
35         return 0;
36     }
37 
38     for(int i = 1 ; i <= m ; ++i)
39     {
40         if(pos[b[i]].w > 1)
41         {
42             flag = 2;
43             break;
44         }
45         else
46         {
47             ans.push_back(pos[b[i]].v);
48         }
49     }
50 
51     if(flag == 2)
52         puts("Ambiguity");
53     else
54     {
55         puts("Possible");
56         for(auto i: ans)
57             printf("%d " , i);
58     }
59 
60 }
View Code

C. Day at the Beach(前缀后缀最大/最小值)

One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunately, the weather was bad, so the friends were unable to ride waves. However, they decided to spent their time building sand castles.

At the end of the day there were n castles built by friends. Castles are numbered from 1 to n, and the height of the i-th castle is equal toh**i. When friends were about to leave, Squidward noticed, that castles are not ordered by their height, and this looks ugly. Now friends are going to reorder the castles in a way to obtain that condition h**i ≤ h**i + 1 holds for all i from 1 to n - 1.

Squidward suggested the following process of sorting castles:

  • Castles are split into blocks — groups of consecutive castles. Therefore the block from i to j will include castles i, i + 1, ..., j. A block may consist of a single castle.

  • The partitioning is chosen in such a way that every castle is a part of exactly one block.

  • Each block is sorted independently from other blocks, that is the sequence h**i, h**i + 1, ..., h**j becomes sorted.

  • The partitioning should satisfy the condition that after each block is sorted, the sequence h**i becomes sorted too. This may always be achieved by saying that the whole sequence is a single block.

Even Patrick understands that increasing the number of blocks in partitioning will ease the sorting process. Now friends ask you to count the maximum possible number of blocks in a partitioning that satisfies all the above requirements.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of castles Spongebob, Patrick and Squidward made from sand during the day.

The next line contains n integers h**i (1 ≤ h**i ≤ 109). The i-th of these integers corresponds to the height of the i-th castle.

Output

Print the maximum possible number of blocks in a valid partitioning.

Examples

input

3
1 2 3

output

3

input

4
2 1 3 2

output

2

Note

In the first sample the partitioning looks like that: 1[3].

技术分享

In the second sample the partitioning is: 2, 1

技术分享

技术分享

Code:

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 static const int MAXN = 1e5 + 10;
 4 static const int OO = 0x3fffffff;
 5 int data[MAXN];
 6 int mx[MAXN];
 7 int mi[MAXN];
 8 int ans;
 9 int main()
10 {
11     int n;
12     scanf("%d" , &n);
13     for(int i = 1 ; i <= n ; ++i)
14     {
15         scanf("%d" , &data[i]);
16     }
17 
18     for(int i = 1 ; i <= n ; ++i)
19     {
20         mx[i] = max(data[i] , mx[i - 1]);
21     }
22     mi[n + 1] = OO;
23     for(int i = n ; i > 0 ; --i)
24     {
25         mi[i] = min(data[i] , mi[i + 1]);
26     }
27 
28     for(int i = 1 ; i <= n ; ++i)
29     {
30         if(mx[i] <= mi[i + 1])
31             ++ans;
32     }
33 
34     printf("%d" , ans);
35 }
View Code

 

以上是关于2017-5-18-Train: Codeforces Round #332 (Div. 2)的主要内容,如果未能解决你的问题,请参考以下文章

cf 1216a

[cf1138BCircus][枚举,列等式]

[cf920G][容斥原理+二分]

Codeforces - 102222C - Caesar Cipher

Sum of Remainders(数学题)

[Codeforces 1242B]0-1 MST