您的位置:首页 > 其它

Codeforces Round #424(Div.2)

2017-07-14 13:36 176 查看
题目链接

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


【思路】

求序列是否单峰,暴力即可。时间复杂度O(n)

【程序】

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <ctime>
#include <fstream>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <vector>
using namespace std;
int a[110],n;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int l = 1;
int r = n;
for(l=1;a[l] < a[l+1];l++);
for(r=n;a[r] < a[r-1];r--);
for(;l<r;l++)
if(a[l] != a[l+1])
{
printf("NO");
return 0;
}
printf("YES\n");
return 0;
}


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


【思路】

同样模拟即可。。。大水题。。。

【程序】

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <ctime>
#include <fstream>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <vector>
using namespace std;

char s1[1010],s2[1010],s[1010];
int a[30];
int len;

int main()
{
scanf("%s%s",s1,s2);
for(int i=0;i<26;i++)
a[s1[i]-'a'] = s2[i]-'a';

scanf("%s",s);
int len = strlen(s);

for(int i=0;i<len;i++)
if(s[i] >= 'A' && s[i] <= 'Z')
printf("%c",a[s[i]-'A']+'A');
else if(s[i] >= 'a' && s[i] <= 'z')
printf("%c",a[s[i]-'a'] + 'a');
else
printf("%c",s[i]);
return 0;
}


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


 【思路】

把a的前缀和与b分别排序,然后一个一个试就可以了(注意处理重叠的情况)

【程序】

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <ctime>
#include <fstream>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <vector>
using namespace std;
#define Maxn 2010
#define OFFSET 8000010

int n,k,ans;
int a[Maxn],b[Maxn];
bool bo[OFFSET << 1];

int main()
{
scanf("%d%d",&k,&n);

int x;
for(int i=1;i<=k;i++)
{
scanf("%d",&x);
a[i] = a[i-1] + x;
}
for(int i=1;i<=n;i++) scanf("%d",&b[i]);
sort(a+1,a+k+1); sort(b+1,b+n+1);

for(int i=1;i<=k;i++)
{
int st = b[1] - a[i],p = 2;
for(int j=i+1;j<=k;j++)
if(st + a[j] == b[p])
p++;

if(p == n + 1)
{
if(!bo[st + OFFSET]) ans++;
bo[st + OFFSET] = true;
}
}

printf("%d\n",ans);
return 0;
}


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


【思路】
这很明显是一道Dp题。先把人和钥匙按大小排序,然后设f[i][j]为前i个人有j个钥匙的最少时间。则:f[i][j] = min(f[i][j-1] , max(f[i-1][j-1] , dis(a[i],b[j]) + dist(b[j],p)));

时间复杂度O(nk)。

【程序】

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <ctime>
#include <fstream>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;
#define Maxn 1010
#define Maxk 2010
#define INF 10000000000LL

LL n,k,p;
LL a[Maxn];
LL b[Maxk];
LL f[Maxn][Maxk];

int main()
{
cin>>n>>k>>p;
for(int i=1;i<=n;i++)
cin >> a[i];
for(int i=1;i<=k;i++)
cin >> b[i];

sort(a+1,a+n+1); sort(b+1,b+k+1);

memset(f,0x3f3f3f3f,sizeof(f));
memset(f[0],0,sizeof(f[0]));

for(int i=1;i<=n;i++)
for(int j=1;j<=k;j++)
f[i][j] = min(f[i][j],min(f[i][j-1],max(f[i-1][j-1],abs(a[i]-b[j]) + abs(b[j]-p))));

cout<<f
[k]<<endl;

return 0;
}


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


【思路】

解法1:暴力模拟,对于每一个要拿出的数,暴力算出与上一个拿出的之间共有多少数。时间复杂度O(n^2)。

解法2:从解法1可以看出,这相当于求动态区间里有多少数,用树状数组优化即可。时间复杂度O(nlogn)。

【程序】

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <cstdlib>
using namespace std;
typedef long long LL;
#define Maxn 200010
#define lowbit(x) x & (-x)

vector<int> g[Maxn];

int n;
int a[Maxn];

LL ans;

int f[Maxn];
void Add(int x,int d)
{
for(;x<=n;x += lowbit(x)) f[x] += d;
}
int Sum(int x)
{
int s = 0;
for(;x;x -= lowbit(x)) s += f[x];
return s;
}

int dist(int l,int r)
{
if(l<r) return Sum(r) - Sum(l);
else return Sum(n) - Sum(l) + Sum(r);
}

int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
Add(i,1);
g[a[i]].push_back(i);
}

int now = 0;
for(int i=0;i<Maxn;i++)
if(!g[i].empty())
{
int Maxx = 0,id = 0 , l;
int siz = g[i].size();

for(int j=0;j < siz;j++)
{
l = dist(now,g[i][j]);
if(l > Maxx)
{
Maxx = l;
id = j;
}
}

ans = ans + 0LL + Maxx;
now = g[i][id];

for(int j=0;j < siz;j++)
Add(g[i][j] , -1);
}

cout << ans << endl;
return 0;
}


F. Bamboo Partition

time limit per test
2 seconds

memory limit per test
512 megabytes

input
standard input

output
standard output

Vladimir wants to modernize partitions in his office. To make the office more comfortable he decided to remove a partition and plant several bamboos in a row. He thinks it would be nice if there are n bamboos
in a row, and the i-th from the left is ai meters
high.

Vladimir has just planted n bamboos in a row, each of which has height 0 meters
right now, but they grow 1 meter each day. In order to make the partition nice Vladimir can cut each bamboo once at any height (no greater
that the height of the bamboo), and then the bamboo will stop growing.

Vladimir wants to check the bamboos each d days (i.e. d days
after he planted, then after 2d days and so on), and cut the bamboos that reached the required height. Vladimir wants the total length
of bamboo parts he will cut off to be no greater than k meters.

What is the maximum value d he can choose so that he can achieve what he wants without cutting off more than k meters
of bamboo?

Input

The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 1011) —
the number of bamboos and the maximum total length of cut parts, in meters.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) —
the required heights of bamboos, in meters.

Output

Print a single integer — the maximum value of d such that Vladimir can reach his goal.

Examples

input
3 4
1 3 5


output
3


input
3 40
10 30 50


output
32


Note

In the first example Vladimir can check bamboos each 3 days. Then he will cut the first and the second bamboos after 3 days,
and the third bamboo after 6 days. The total length of cut parts is 2 + 0 + 1 = 3 meters.

【思路】

设每个d天看一次。则总共需要剪掉的米数就是


化简得:


同时除以d:

(设S = k + sum(ai))

由此,如果有

 且


,可以得到:



则如果当d取d1时原不等式成立,则d2也一定成立。

求出所有的d2,并判断该方案是否可行即可。时间复杂度


【程序】

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
#define Maxn 110
typedef long long LL;

LL a[Maxn];
int n;

LL k,Maxx,ans;

bool check(LL d)
{
LL sum = 0;
for(int i=0;i<n;i++)
sum += (a[i] + d - 1LL) / d;
sum *= d;
return sum <= Maxx;
}

int main()
{
cin >> n >> k;
Maxx = k;
for(int i=0;i<n;i++)
{
cin >> a[i];
Maxx += a[i];
}
for(LL st=1,en;st <= Maxx;st = en + 1LL)
{
en = Maxx / (Maxx / st);
if(check(en)) ans = en;
}
cout << ans << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces 解题报告