A plane is flying at a constant height of hh meters above the ground surface. Let's consider that it is flying from the point (−109,h)(−109,h) to the point (109,h)(109,h) parallel with OxOx axis.
A glider is inside the plane, ready to start his flight at any moment (for the sake of simplicity let's consider that he may start only when the plane's coordinates are integers). After jumping from the plane, he will fly in the same direction as the plane, parallel to OxOx axis, covering a unit of distance every second. Naturally, he will also descend; thus his second coordinate will decrease by one unit every second.
There are ascending air flows on certain segments, each such segment is characterized by two numbers x1x1 and x2x2 (x1 If the glider jumps out at 11, he will stop at 1010. Otherwise, if he jumps out at 22, he will stop at 1212. Determine the maximum distance along OxOx axis from the point where the glider's flight starts to the point where his flight ends if the glider can choose any integer coordinate to jump from the plane and start his flight. After touching the ground the glider stops altogether, so he cannot glide through an ascending airflow segment if his second coordinate is 00. Input The first line contains two integers nn and hh (1≤n≤2⋅105,1≤h≤109)(1≤n≤2⋅105,1≤h≤109) — the number of ascending air flow segments and the altitude at which the plane is flying, respectively. Each of the next nn lines contains two integers xi1xi1 and xi2xi2 (1≤xi1 Output Print one integer — the maximum distance along OxOx axis that the glider can fly from the point where he jumps off the plane to the point where he lands if he can start his flight at any integer coordinate. Examples Input Output Input Output Input Output Note In the first example if the glider can jump out at (2,4)(2,4), then the landing point is (12,0)(12,0), so the distance is 12−2=1012−2=10. In the second example the glider can fly from (16,10)(16,10) to (34,0)(34,0), and the distance is 34−16=1834−16=18. In the third example the glider can fly from (−100,1000000000)(−100,1000000000) to (1999999899,0)(1999999899,0), so the distance is 1999999899−(−100)=199999999 题意: 从飞机上跳伞,正常情况下每秒向右移动一格,向下落一格,当遇到气流时,不会降落,问最远可以飞多远 我们可以想到两个气流区域中间的部分其实就是会降落的高度,所以问题就转化为了在不超过h的空区域之和的情况下,通过的气流的长度+高度h。 所以我们可以用前缀和来维护空区域的长度和气流的长度,在通过lower_bound函数查找从每一个起点(即各个气流区域的起点)开始的空区域之和小于H的中最长的即可。 Recently Monocarp has created his own mini-laboratory! The laboratory contains nn bacteria. Monocarp knows that he can merge any two bacteria having equal sizes, and the resulting bacterium will have the size equal to the sum of sizes of merged bacteria. For example, if two bacteria having sizes equal to 77 merge, one bacterium with size 1414 is the result. It becomes hard to watch for many bacteria, so Monocarp wants to merge all of them into one bacterium. It may not be possible to do this with the bacteria Monocarp has, so he can buy any number of bacteria of any possible integer sizes in a special store. You have to determine the minimum number of bacteria Monocarp has to buy to merge them with the nn bacteria his laboratory contains into exactly one bacterium. Input The first line contains one integer nn (1≤n≤2⋅105)(1≤n≤2⋅105) — the number of bacteria Monocarp's laboratory contains. The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109), where aiai is the size of the ii-th bacterium in the laboratory. Output If it is impossible to merge the bacteria (possibly after buying some) into only one bacterium, print -1. Otherwise print the minimum number of bacteria Monocarp has to buy to merge them with the nn bacteria his laboratory contains into exactly one bacterium. Examples Input Output Input Output Input Output Note In the first example Monocarp should buy one bacterium having size 11 and one bacterium having size 22. Then Monocarp will have 44 bacteria having sizes [1,4,1,2][1,4,1,2]. Then two bacteria having sizes 11 can be merged into one having size 22. Then Monocarp will have 33 bacteria having sizes [2,4,2][2,4,2]. Then two bacteria having sizes 22 can be merged into one having size 44. Then Monocarp will have 22 bacteria having sizes [4,4][4,4], which can be merged into one having size 88. In the second example no matter which bacteria Monocarp will buy, he cannot merge all his bacteria. In the third example Monocarp needs to buy one bacterium having size 10000000001000000000. 题意:两个同样大小的细菌可以合并,如果没有一样大的,需要买一个。。。直到合并为一个,问至少需要买几个,若始终不能合并成一个,输出-1 很明显,不能合并成一个的条件就是最后剩下的两个不相等并且当小的那个数乘2大于另一个数,这样是无论如何都不能合并成一个的; 这里用优先队列维护即可。 H - Theater Square The Theater Square can be represented as a rectangle having height nn and length mm, divided into square 1×11×1 cells. Let's denote the cell located at the intersection of ii-th row and jj-th column as (i,j)(i,j). The rows are numbered from top to bottom, the columns — from left to right. There is a rectangular fountain inside the Teather Square. The cell in its left upper corner is (x1,y1)(x1,y1), the cell in its right lower corner is (x2,y2)(x2,y2). The Theater Square soon will be paved with tiles having height 11 and length 22. Every cell (except cells inside the fountain) should be paved, and no cell should be covered by more than one tile. All tiles will be laid out horizontally, so the cells covered by each tile are in the same row. To pave the whole Theater Square it might be necessary to break some tiles. After breaking a tile, two new tiles of size 1×11×1are formed (which cannot be broken further). You may consider that the mayor, who ordered the paving of the Theater Square, has infinite number of tiles 1×21×2. Since broken tiles are not beautiful, among all possible ways to pave the Theater Square the mayor wants to choose a way such that the number of tiles to be broken into two lesser tiles is minimum possible. Pay attention that tiles should be laid horizontally, no tile can cover cells in different rows. Help the mayor! Tell him the minimum possible number of tiles to be broken. Input The first line contains two integers nn and mm (1≤n,m≤2⋅105)(1≤n,m≤2⋅105) — the height and the length of the Theater Square, respectively. The second line contains four numbers x1,y1,x2,y2 (1≤x1≤x2≤n,1≤y1≤y2≤m)x1,y1,x2,y2 (1≤x1≤x2≤n,1≤y1≤y2≤m) — the coordinates of left upper corner and right lower corner of the fountain. Output Print one number — minimum possible number of tiles mayor has to break in order to pave the whole Theater Square. Examples Input Output Input Output Input Output Note One of the optimal ways to pave the Theater Square in the first example: 55 tiles are to be broken. 题意:有一个n×m的广场,需要铺设1×2的砖,广场里还有一个喷泉,1×2的砖可以分成1×1的砖,问至少需要几块1×2的砖分成1×1的砖? ,这个题可以先求需要的1×1的砖的数量,最后除2向上取整即可,1×1的砖按照喷泉的左右剩下的列数判断即可,奇数就需要一列,另外喷泉没有占到的行也判断一下,列数如果是奇数,也需要一列。 There was an electronic store heist last night. All keyboards which were in the store yesterday were numbered in ascending order from some integer number xx. For example, if x=4x=4 and there were 33 keyboards in the store, then the devices had indices 44, 55 and 66, and if x=10x=10 and there were 77 of them then the keyboards had indices 1010, 1111, 1212, 1313, 1414, 1515 and 1616. After the heist, only nn keyboards remain, and they have indices a1,a2,…,ana1,a2,…,an. Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither xx nor the number of keyboards in the store before the heist. Input The first line contains single integer nn (1≤n≤1000)(1≤n≤1000) — the number of keyboards in the store that remained after the heist. The second line contains nn distinct integers a1,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109) — the indices of the remaining keyboards. The integers aiai are given in arbitrary order and are pairwise distinct. Output Print the minimum possible number of keyboards that have been stolen if the staff remember neither xx nor the number of keyboards in the store before the heist. Examples Input Output Input Output Note In the first example, if x=8x=8 then minimum number of stolen keyboards is equal to 22. The keyboards with indices 99 and 1111 were stolen during the heist. In the second example, if x=4x=4 then nothing was stolen during the heist. 题意:商店里有下标递增的键盘,突然商店被盗,有一些键盘丢失,问最少丢了多少个键盘 水题,找出剩下的键盘中下标最大的和最小的,max-min+1即可求出个数,在减去n即可。 3 4
2 5
7 9
10 11
10
5 10
5 7
11 12
16 20
25 26
30 33
18
1 1000000000
1 1000000000
1999999999
#include
C - Bacteria
2
1 4
2
3
3 6 9
-1
7
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
1
#include
6 5
1 2 3 4
5
6 1
3 1 4 1
2
1 12
1 3 1 8
0
#include
I - Heist
4
10 13 12 8
2
5
7 5 6 4 8
0
#include