A. Unimodal Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Array of integers is unimodal, if:

  • it is strictly increasing in the beginning;
  • after that it is constant;
  • after that it is strictly decreasing.

The first block (increasing) and the last block (decreasing) may be absent. It is allowed that both of this blocks are absent.

For example, the following three arrays are unimodal: [5, 7, 11, 11, 2, 1], [4, 4, 2], [7], but the following three are not unimodal: [5, 5, 6, 6, 1], [1, 2, 1, 2], [4, 5, 5, 6].

Write a program that checks if an array is unimodal.

Input

The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the array.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1 000) — the elements of the array.

Output

Print "YES" if the given array is unimodal. Otherwise, print "NO".

You can output each letter in any case (upper or lower).

Examples
Input
6
1 5 5 5 4 2
Output
YES
Input
5
10 20 30 20 10
Output
YES
Input
4
1 2 1 2
Output
NO
Input
7
3 3 3 3 3 3 3
Output
YES
Note

In the first example the array is unimodal, because it is strictly increasing in the beginning (from position 1 to position 2, inclusively), that it is constant (from position 2 to position 4, inclusively) and then it is strictly decreasing (from position 4 to position 6, inclusively).

题意:给你一个序列,判断这个序列是否第一部分严格递增,第二部分相同 第三部分严格递减  第一第三部分可以省去

题解:迷之题意 如下代码可ac

 1 #pragma comment(linker, "/STACK:102400000,102400000")
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <algorithm>
 7 #include <cmath>
 8 #include <cctype>
 9 #include <map>
10 #include <set>
11 #include <queue>
12 #include <bitset>
13 #include <string>
14 #include <complex>
15 #define ll __int64
16 using namespace std;
17 int n;
18 int a[105];
19 int main()
20 {
21     scanf("%d",&n);
22     for(int i=1;i<=n;i++){
23         scanf("%d",&a[i]);
24     }
25     int l=1,r=n;
26     for(int i=1;i<n;i++)
27     {
28         if(a[i+1]>a[i]){
29            l++;
30         }
31         else
32             break;
33     }
34     for(int i=n;i>1;i--)
35     {
36         if(a[i-1]>a[i]){
37             r--;
38         }
39         else
40             break;
41     }
42     for(int i=l;i<r;i++)
43     {
44         if(a[i]!=a[i+1]){
45             printf("NO\n");
46             return 0;
47         }
48     }
49     printf("YES\n");
50     return 0;
51 }
B. Keyboard Layouts
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are two popular keyboard layouts in Berland, they differ only in letters positions. All the other keys are the same. In Berland they use alphabet with 26 letters which coincides with English alphabet.

You are given two strings consisting of 26 distinct letters each: all keys of the first and the second layouts in the same order.

You are also given some text consisting of small and capital English letters and digits. It is known that it was typed in the first layout, but the writer intended to type it in the second layout. Print the text if the same keys were pressed in the second layout.

Since all keys but letters are the same in both layouts, the capitalization of the letters should remain the same, as well as all other characters.

Input

The first line contains a string of length 26 consisting of distinct lowercase English letters. This is the first layout.

The second line contains a string of length 26 consisting of distinct lowercase English letters. This is the second layout.

The third line contains a non-empty string s consisting of lowercase and uppercase English letters and digits. This is the text typed in the first layout. The length of s does not exceed 1000.

Output

Print the text if the same keys were pressed in the second layout.

Examples
Input
qwertyuiopasdfghjklzxcvbnm
veamhjsgqocnrbfxdtwkylupzi
TwccpQZAvb2017
Output
HelloVKCup2017
Input
mnbvcxzlkjhgfdsapoiuytrewq
asdfghjklqwertyuiopzxcvbnm
7abaCABAABAcaba7
Output
7uduGUDUUDUgudu7
题意:给你两个乱序字母表 以及一个按照第一个字母表的一段输出 输出按照第二个字母表的输出
题解:map<char,char>
 1 #pragma comment(linker, "/STACK:102400000,102400000")
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <algorithm>
 7 #include <cmath>
 8 #include <cctype>
 9 #include <map>
10 #include <set>
11 #include <queue>
12 #include <bitset>
13 #include <string>
14 #include <complex>
15 #define ll __int64
16 using namespace std;
17 char a[30];
18 char b[30];
19 char d[1005];
20 map<char,char> mp;
21 int main()
22 {
23     scanf("%s",a);
24     scanf("%s",b);
25     for(char i='0';i<='9';i++)
26         mp[i]=i;
27     for(int i=0;i<26;i++)
28     {
29         mp[a[i]]=b[i];
30         mp[a[i]-'a'+'A']=b[i]-'a'+'A';
31     }
32     scanf("%s",d);
33     int len=strlen(d);
34     for(int i=0;i<len;i++)
35     {
36         printf("%c",mp[d[i]]);
37     }
38     cout<<endl;
39     return 0;
40 }
C. Jury Marks
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain number of points (may be negative, i. e. points were subtracted). Initially the participant had some score, and each the marks were one by one added to his score. It is known that the i-th jury member gave ai points.

Polycarp does not remember how many points the participant had before this k marks were given, but he remembers that among the scores announced after each of the k judges rated the participant there were n (n ≤ k) values b1, b2, ..., bn (it is guaranteed that all values bj are distinct). It is possible that Polycarp remembers not all of the scores announced, i. e. n < k. Note that the initial score wasn't announced.

Your task is to determine the number of options for the score the participant could have before the judges rated the participant.

Input

The first line contains two integers k and n (1 ≤ n ≤ k ≤ 2 000) — the number of jury members and the number of scores Polycarp remembers.

The second line contains k integers a1, a2, ..., ak ( - 2 000 ≤ ai ≤ 2 000) — jury's marks in chronological order.

The third line contains n distinct integers b1, b2, ..., bn ( - 4 000 000 ≤ bj ≤ 4 000 000) — the values of points Polycarp remembers. Note that these values are not necessarily given in chronological order.

Output

Print the number of options for the score the participant could have before the judges rated the participant. If Polycarp messes something up and there is no options, print "0" (without quotes).

Examples
Input
4 1
-5 5 0 20
10
Output
3
Input
2 2
-2000 -2000
3998000 4000000
Output
1
Note

The answer for the first example is 3 because initially the participant could have  - 10, 10 or 15 points.

In the second example there is only one correct initial score equaling to 4 002 000.

题意:k个人依次打分(k个分数是按照打分的时间顺序给的) 给你n个在打分过程中出现过的分数 判断有多少种初始分的选择

题解:以n个打分过程中出现过的分数 做为第k个人打分后得到的分数 可以求得每个人打分可能对应哪些初始分  判断初始分的正确性即可 统计个数输出

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <cmath>
 7 #include <cctype>
 8 #include <map>
 9 #include <set>
10 #include <queue>
11 #include <bitset>
12 #include <string>
13 #include <complex>
14 #define ll __int64
15 using namespace std;
16 int n,k;
17 int sum[2003];
18 map<int,set<int>>m;
19 set<int>::iterator it;
20 int main()
21 {
22     scanf("%d %d",&k,&n);
23     sum[0]=0;
24     int exm;
25     for(int i=1; i<=k; i++)
26     {
27         scanf("%d",&exm);
28         sum[i]=sum[i-1]+exm;
29     }
30     for(int i=1; i<=n; i++)
31     {
32         scanf("%d",&exm);
33         for(int j=1; j<=k; j++)
34         {
35             m[i].insert(exm-sum[j]);
36         }
37     }
38     int ans=0;
39     for(it=m[1].begin(); it!=m[1].end(); it++)
40     {
41         int jishu=0;
42         for(int i=2;i<=n;i++){
43             if(m[i].find(*it)!=m[i].end()){
44                 jishu++;
45             }
46             else
47                 break;
48         }
49         if(jishu==n-1)
50             ans++;
51     }
52     printf("%d\n",ans);
53     return 0;
54 }
D. Office Keys
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.

You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.

Input

The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.

The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.

The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.

Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.

Output

Print the minimum time (in seconds) needed for all n to reach the office with keys.

Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note

In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.

题意:给你n个人的位置 k把钥匙的位置 以及办公室的位置p  每个人只能拿一把钥匙去开办公室的门 问最少需要多少时间才能使所有的人都到达办公室

题解:暴力 细想一下n个人应当取相邻的n把钥匙

 1 #pragma comment(linker, "/STACK:102400000,102400000")
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <algorithm>
 7 #include <cmath>
 8 #include <cctype>
 9 #include <map>
10 #include <set>
11 #include <queue>
12 #include <bitset>
13 #include <string>
14 #include <complex>
15 #define ll __int64
16 using namespace std;
17 ll n,k,p;
18 ll a[1003];
19 ll b[2003];
20 int main()
21 {
22     scanf("%I64d %I64d %I64d",&n,&k,&p);
23     for(int i=1;i<=n;i++)
24         scanf("%I64d",&a[i]);
25     for(int i=1;i<=k;i++)
26         scanf("%I64d",&b[i]);
27     sort(a+1,a+1+n);
28     sort(b+1,b+1+k);
29     ll ans=1e18;
30     for(int i=1,j=n;j<=k;i++,j++)
31     {
32         ll maxn=0;
33         for(int l=i;l<=j;l++)
34         maxn=max(maxn,abs(a[l-i+1]-b[l])+abs(b[l]-p));
35         ans=min(ans,maxn);
36     }
37     printf("%I64d\n",ans);
38     return 0;
39 }
E. Cards Sorting
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100 000, inclusive. It is possible that some cards have the same integers on them.

Vasily decided to sort the cards. To do this, he repeatedly takes the top card from the deck, and if the number on it equals the minimum number written on the cards in the deck, then he places the card away. Otherwise, he puts it under the deck and takes the next card from the top, and so on. The process ends as soon as there are no cards in the deck. You can assume that Vasily always knows the minimum number written on some card in the remaining deck, but doesn't know where this card (or these cards) is.

You are to determine the total number of times Vasily takes the top card from the deck.

Input

The first line contains single integer n (1 ≤ n ≤ 100 000) — the number of cards in the deck.

The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000), where ai is the number written on the i-th from top card in the deck.

Output

Print the total number of times Vasily takes the top card from the deck.

Examples
Input
4
6 3 1 2
Output
7
Input
1
1000
Output
1
Input
7
3 3 3 3 3 3 3
Output
7
Note

In the first example Vasily at first looks at the card with number 6 on it, puts it under the deck, then on the card with number 3, puts it under the deck, and then on the card with number 1. He places away the card with 1, because the number written on it is the minimum among the remaining cards. After that the cards from top to bottom are [2, 6, 3]. Then Vasily looks at the top card with number 2 and puts it away. After that the cards from top to bottom are [6, 3]. Then Vasily looks at card 6, puts it under the deck, then at card 3 and puts it away. Then there is only one card with number 6 on it, and Vasily looks at it and puts it away. Thus, in total Vasily looks at 7 cards.

题意:给你一些card上面写着数字 每次操作 如果当前数字是所有牌中最小的值则将这个牌扔掉 否则放到底部 问最少要进行多少次操作能够扔掉所有的牌

题解:树状数组+二分  注意一点 将数字i都扔掉之后 是从最后一个数字i的右边开始 扔掉 数字i+1 这就是为什么要在数字i+1的所有位置中二分一个第一个大于be的位置

 1 #pragma comment(linker, "/STACK:102400000,102400000")
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <algorithm>
 7 #include <cmath>
 8 #include <cctype>
 9 #include <map>
10 #include <set>
11 #include <queue>
12 #include <bitset>
13 #include <string>
14 #include <complex>
15 #define ll __int64
16 using namespace std;
17 ll tree[100005];
18 ll n;
19 ll a[100005];
20 map<ll,set<ll> >mp;
21 set<ll> ::iterator it,mid;
22 ll lowbit(ll x)
23 {
24     return x&(-x);
25 }
26 void add(ll x,ll y)
27 {
28     while(x<n+1)
29     {
30         tree[x]+=y;
31         x+=lowbit(x);
32     }
33 }
34 ll sum(ll x){
35     ll res=0;
36     while(x>0)
37     {
38         res+=tree[x];
39         x-=lowbit(x);
40     }
41     return res;
42 }
43 int main()
44 {
45     scanf("%I64d",&n);
46     for(int i=1;i<=n;i++)
47     {
48         scanf("%I64d",&a[i]);
49         mp[a[i]].insert(i);//将数a[i]出现的位置存下来
50         add(i,1);//将位置加到树状数组
51     }
52     ll be=0;
53     ll j=0;
54     ll ans=0;
55     for(int i=1;i<=100000;i++)//从小到大枚举每个值
56     {
57          mid=mp[i].upper_bound(be);//找到第一个大于be的位置
58          for(it=mid;it!=mp[i].end();it++)//先扔掉后面位置的
59          {
60              j++;
61              add(*it,-1);
62              be=*it;
63          }
64          for(it=mp[i].begin();it!=mid;it++)//扔掉前面的
65          {
66              if(it==mp[i].begin())//这里说明 已经结束了一轮 计算上一轮的ans贡献
67              {
68                  ans=ans+j+sum(n);
69                  j=0;
70              }
71                  add(*it,-1);
72                  j++;
73                  be=*it;
74          }
75     }
76     printf("%I64d\n",ans+j);
77     return 0;
78 }