A. Gabriel and Caterpillar
time limit per test
memory limit per test
input
output
9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.
a cm per hour by day and slips down byb
10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
Input
h1, h2 (1 ≤ h1 < h2 ≤ 105) — the heights of the position of the caterpillar and the apple in centimeters.
a, b (1 ≤ a, b ≤ 105) — the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Output
k
- 1.
Examples
input
10 30 2 1
output
1
input
10 13 1 1
output
0
input
10 19 1 2
output
-1
input
1 50 5 4
output
1
Note
10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
大意:毛毛虫爬树摘苹果,白天一小时爬acm,晚上每小时掉bcm,奇葩的是。有个人想看毛毛虫摘苹果,而且只能在每天下午2点之后才可以看到,问需要等待多少天可以看到毛毛虫摘苹果。
题目链接:点击打开链接
B. z-sort
time limit per test
memory limit per test
input
output
z-school found a kind of sorting called z-sort. The array a with n elements are z-sorted if two conditions hold:
- ai≥ai- 1 for all eveni,
- ai≤ai- 1 for all oddi> 1.
[1,2,1,2] and [1,1,1,1] are z-sorted while the array [1,2,3,4] isn’t z-sorted.
z-sorted?
Input
n (1 ≤ n ≤ 1000) — the number of elements in the array a.
n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.
Output
a z-sorted print n space separated integers ai — the elements after z-sort. Otherwise print the only word "Impossible".
Examples
input
4 1 2 2 1
output
1 2 1 2
input
5 1 3 2 2 5
output
1 5 2 3 2
思路:可以看出起伏线排列就是一个合法序列,不存在 impossible 的可能
题目链接:点击打开链接
C. Foe Pairs
time limit per test
memory limit per test
input
output
p of length n. Also you are given m foe pairs (ai, bi) (1 ≤ ai, bi ≤ n, ai ≠ bi).
(x, y) (1 ≤ x ≤ y ≤ n) that do not contain any foe pairs. So you shouldn't count intervals (x, y)
p = [1, 3, 2, 4] and foe pairs are {(3, 2), (4, 2)}. The interval (1, 3) is incorrect because it contains a foe pair (3, 2). The interval (1, 4) is also incorrect because it contains two foe pairs (3, 2) and (4, 2). But the interval (1, 2)
Input
n and m (1 ≤ n, m ≤ 3·105) — the length of the permutation p
n distinct integers pi (1 ≤ pi ≤ n) — the elements of the permutation p.
m lines contains two integers (ai, bi) (1 ≤ ai, bi ≤ n, ai ≠ bi) — the i-th foe pair. Note a foe pair can appear multiple times in the given list.
Output
c — the number of different intervals (x, y)
64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long
Examples
input
4 2 1 3 2 4 3 2 2 4
output
5
input
9 5 9 7 2 3 1 4 6 5 8 1 6 4 5 2 7 7 2 2 7
output
20
Note
(1, 1), (1, 2), (2, 2), (3, 3) and (4, 4).
大意:题目给出一个1到n的某一个排列,有m个二维点。问这n个数形成的所有子区间中,不包含m个点中任何一个的子区间有多少?