Codeforces Round #333(Div.2) (5/5) (Div.1) (3/5)

maksyuki 发表于 比赛 分类,标签:
0

A Two Bases

After seeing the "ALL YOUR BASE ARE BELONG TO US" meme for the first time, numbers X and Y realised that they have different bases, which complicated their relations.

You're given a number X represented in base bx and a number Y represented in base by. Compare those two numbers.

Input

The first line of the input contains two space-separated integers n and bx (1 ≤ n ≤ 10, 2 ≤ bx ≤ 40), where n is the number of digits in the bx-based representation of X.

The second line contains n space-separated integers x1, x2, ..., xn (0 ≤ xi < bx) — the digits of X. They are given in the order from the most significant digit to the least significant one.

The following two lines describe Y in the same way: the third line contains two space-separated integers m and by(1 ≤ m ≤ 10, 2 ≤ by ≤ 40, bx ≠ by), where m is the number of digits in the by-based representation of Y, and the fourth line contains m space-separated integers y1, y2, ..., ym (0 ≤ yi < by) — the digits of Y.

There will be no leading zeroes. Both X and Y will be positive. All digits of both numbers are given in the standard decimal numeral system.

Output

Output a single character (quotes for clarity):

  • '<' if X < Y
  • '>' if X > Y
  • '=' if X = Y

Sample test(s)

input

6 2
1 0 1 1 1 1
2 10
4 7

output

=

input

3 3
1 0 2
2 5
2 4

output

<

input

7 16
15 15 4 0 0 7 10
7 9
4 8 0 3 1 5 0

output

>

Note

In the first sample, X = 1011112 = 4710 = Y.

In the second sample, X = 1023 = 215 and Y = 245 = 1123, thus X < Y.

In the third sample,  and Y = 48031509. We may notice that X starts with much larger digits andbx is much larger than by, so X is clearly larger than Y.

 

题目类型:暴力枚举

算法分析:直接将两个数转换成10进制然后比较

 

B Approximating a Constant Range

When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?

You're given a sequence of n data points a1, ..., an. There aren't any big jumps between consecutive data points — for each 1 ≤ i < n, it's guaranteed that |ai + 1 - ai| ≤ 1.

A range [l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be the maximum and m the minimum value of ai for l ≤ i ≤ r; the range[l, r] is almost constant if M - m ≤ 1.

Find the length of the longest almost constant range.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of data points.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).

Output

Print a single number — the maximum length of an almost constant range of the given sequence.

Sample test(s)

input

5
1 2 3 3 2

output

4

input

11
5 4 5 5 6 7 8 8 8 7 6

output

5

Note

In the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.

In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].

 

题目类型:区间最值查询

算法分析:使用两个指针pa和pb指向一个序列的头和尾,每次若新增的数使得当前的子序列合法,则pb指针自加1,否则使得pa指针自加1。在前面更新子序列的过程中还要更新最小值和最大值及其的位置,序列的最大值(答案)

 

算法分析:另一种常数更小的方法是从左到右扫一遍,以当前数为基准向其左右扩展(满足相邻数字差最大为1),不断更新res。每次更新完之后,子序列的左指针直接跳到右指针所在的位置或者是其后一个位置

 

C The Two Routes

In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. There is also an absurdly simple road network — for each pair of different towns x and y, there is a bidirectional road between towns x and y if and only if there is no railway between them. Travelling to a different town using one railway or one road always takes exactly one hour.

A train and a bus leave town 1 at the same time. They both have the same destination, town n, and don't make any stops on the way (but they can wait in town n). The train can move only along railways and the bus can move only along roads.

You've been asked to plan out routes for the vehicles; each route can use any road/railway multiple times. One of the most important aspects to consider is safety — in order to avoid accidents at railway crossings, the train and the bus must not arrive at the same town (except town n) simultaneously.

Under these constraints, what is the minimum number of hours needed for both vehicles to reach town n (the maximum of arrival times of the bus and the train)? Note, that bus and train are not required to arrive to the town n at the same moment of time, but are allowed to do so.

Input

The first line of the input contains two integers n and m (2 ≤ n ≤ 400, 0 ≤ m ≤ n(n - 1) / 2) — the number of towns and the number of railways respectively.

Each of the next m lines contains two integers u and v, denoting a railway between towns u and v (1 ≤ u, v ≤ n,u ≠ v).

You may assume that there is at most one railway connecting any two towns.

Output

Output one integer — the smallest possible time of the later vehicle's arrival in town n. If it's impossible for at least one of the vehicles to reach town n, output  - 1.

Sample test(s)

input

4 2
1 3
3 4

output

2

input

4 6
1 2
1 3
1 4
2 3
2 4
3 4

output

-1

input

5 5
4 2
3 5
4 5
5 1
1 2

output

3

Note

In the first sample, the train can take the route  and the bus can take the route . Note that they can arrive at town 4 at the same time.

In the second sample, Absurdistan is ruled by railwaymen. There are no roads, so there's no way for the bus to reach town 4.

 

题目类型:BFS

算法分析:根据题意可知,火车道或者高速公路都一定会存在一个直接从1到n的边,所以根本就不会出现同时相撞的情况(除了n)。则直接判断通过两种交通工具是否能够同时从1到达n,不能的话直接输出-1,否则直接跑一个非直接连接1至n的线路的BFS

 

D Lipshitz Sequence

A function  is called Lipschitz continuous if there is a real constant K such that the inequality|f(x) - f(y)| ≤ K·|x - y| holds for all . We'll deal with a more... discrete version of this term.

For an array , we define it's Lipschitz constant  as follows:

  • if n < 2,
  • if n ≥ 2,  over all 1 ≤ i < j ≤ n

In other words,  is the smallest non-negative integer such that |h[i] - h[j]| ≤ L·|i - j| holds for all1 ≤ i, j ≤ n.

You are given an array  of size n and q queries of the form [l, r]. For each query, consider the subarray ; determine the sum of Lipschitz constants of all subarrays of .

Input

The first line of the input contains two space-separated integers n and q (2 ≤ n ≤ 100 000 and 1 ≤ q ≤ 100) — the number of elements in array  and the number of queries respectively.

The second line contains n space-separated integers  ().

The following q lines describe queries. The i-th of those lines contains two space-separated integers li and ri(1 ≤ li < ri ≤ n).

Output

Print the answers to all queries in the order in which they are given in the input. For the i-th query, print one line containing a single integer — the sum of Lipschitz constants of all subarrays of .

Sample test(s)

input

10 4
1 5 2 9 1 3 4 2 1 7
2 4
3 8
7 10
1 9

output

17
82
23
210

input

7 6
5 7 7 4 6 6 2
1 2
2 3
2 6
1 7
4 7
3 5

output

2
0
22
59
16
8

Note

In the first query of the first sample, the Lipschitz constants of subarrays of  with length at least 2 are:

The answer to the query is their sum.

 

题目类型:几何+区间最值查询

算法分析:首先一个隐含的条件是相邻两个点间的斜率要比跨过好多个点间的斜率要大,之后可以直接使用线段树查询每段的最值,这里使用一种更高效的做法:看每段的最值最终控制多长的区间,则最后的结果就是该最值乘上跨过这个段的子序列的个数,而控制区间的长度可以通过预处理得到

 

E Kleofáš and the n-thlon

Kleofáš is participating in an n-thlon - a tournament consisting of n different competitions in n different disciplines (numbered 1 through n). There are m participants in the n-thlon and each of them participates in all competitions.

In each of these n competitions, the participants are given ranks from 1 to m in such a way that no two participants are given the same rank - in other words, the ranks in each competition form a permutation of numbers from 1 to m. The score of a participant in a competition is equal to his/her rank in it.

The overall score of each participant is computed as the sum of that participant's scores in all competitions.

The overall rank of each participant is equal to 1 + k, where k is the number of participants with strictly smalleroverall score.

The n-thlon is over now, but the results haven't been published yet. Kleofáš still remembers his ranks in each particular competition; however, he doesn't remember anything about how well the other participants did. Therefore, Kleofáš would like to know his expected overall rank.

All competitors are equally good at each discipline, so all rankings (permutations of ranks of everyone except Kleofáš) in each competition are equiprobable.

Input

The first line of the input contains two space-separated integers n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 1000) — the number of competitions and the number of participants respectively.

Then, n lines follow. The i-th of them contains one integer xi (1 ≤ xi ≤ m) — the rank of Kleofáš in the i-th competition.

Output

Output a single real number – the expected overall rank of Kleofáš. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9.

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

Sample test(s)

input

4 10
2
1
2
1

output

1.0000000000000000

input

5 5
1
2
3
4
5

output

2.7500000000000000

input

3 6
2
4
2

output

1.6799999999999999

Note

In the first sample, Kleofáš has overall score 6. Nobody else can have overall score less than 6 (but it's possible for one other person to have overall score 6 as well), so his overall rank must be 1.

 

题目类型:概率DP

算法分析:简单来说就是用dp[i][j]表示经过前i个考试,一个人能够得到j分的概率。则状态转移方程为dp[i][j] = {Sigma(dp[i-1][k]) - dp[i-1][j-aa[i]]} / (m - 1),这里使用前缀和进行优化,详细求解过程可以参考:

http://blog.csdn.net/snowy_smile/article/details/50061111