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 x x x. For example, if x x x = 4 4 4 and there were 3 3 3 keyboards in the store, then the devices had indices 4 4 4 , 5 5 5 and 6 6 6 , and if x = 10 x = 10 x=10 and there were 7 7 7 of them then the keyboards had indices 10 10 10 , 11 11 11 , 12 12 12 , 13 13 13 , 14 14 14 , 15 15 15 and 16 16 16 .
After the heist, only n n n keyboards remain, and they have indices a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,…,an . Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither x x x nor the number of keyboards in the store before the heist.
The first line contains single integer n n n ( 1 ≤ n ≤ 1   000 ) 1 \le n \le 1\,000) 1≤n≤1000) — the number of keyboards in the store that remained after the heist.
The second line contains n n n distinct integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,…,an( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^{9} 1≤ai≤109) — the indices of the remaining keyboards. The integers a i a_i ai are given in arbitrary order and are pairwise distinct.
Print the minimum possible number of keyboards that have been stolen if the staff remember neither x x x nor the number of keyboards in the store before the heist.
4
10 13 12 8
2
5
7 5 6 4 8
0
In the first example, if x = 8 x=8 x=8 then minimum number of stolen keyboards is equal to 2 2 2 . The keyboards with indices 9 9 9 and 11 11 11 were stolen during the heist.
In the second example, if x = 4 x=4 x=4 then nothing was stolen during the heist.
直接记录 m a x , m i n max, min max,min或者排下序就好了。 博主直接排序… 缅怀因为 m a x , m i n max,min max,min初值设成 1 0 − 7 , 1 0 7 10^-7,10^7 10−7,107而 f s t fst fst的llppdd同学。
代码如下:
#include
#include
#include
#include
#include
#include
#define R register
#define IN inline
#define W while
#define gc getchar()
template <class T>
IN void in(T &x)
{
x = 0; R char c = gc;
for (; !isdigit(c); c = gc);
for (; isdigit(c); c = gc)
x = (x << 1) + (x << 3) + c - 48;
}
int dat[1005], num, ans;
int main(void)
{
in(num);
for (R int i = 1; i <= num; ++i) in(dat[i]);
std::sort(dat + 1, dat + 1 + num);
for (R int i = 2; i <= num; ++i)
ans += dat[i] - dat[i - 1] - 1;
printf("%d", ans);
}
Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a a a and screen height not greater than b b b . Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w w w , and the height of the screen is h h h , then the following condition should be met: w h = x y \frac{w}{h} = \frac{x}{y} hw=yx .
There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w w w and hhthere is a TV set with screen width w w w and height h h h in the shop.
Monocarp isn’t ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w w w and h h h , beforehand, such that ( w ≤ a w \le a w≤a) , ( h ≤ b h \le b h≤b) and ( w h = x y \frac{w}{h} = \frac{x}{y} hw=yx) .
In other words, Monocarp wants to determine the number of TV sets having aspect ratio x y \frac{x}{y} yx, screen width not exceeding a a a , and screen height not exceeding b b b . Two TV sets are considered different if they have different screen width or different screen height.
The first line contains four integers a a a, b b b , x x x , y y y ( 1 ≤ a , b , x , y ≤ 1 0 18 1 \le a, b, x, y \le 10^{18} 1≤a,b,x,y≤1018 ) — the constraints on the screen width and height, and on the aspect ratio.
Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints.
17 15 5 3
3
14 16 7 22
0
4 2 6 4
1
1000000000000000000 1000000000000000000 999999866000004473 999999822000007597
1000000063
In the first example, there are 3 3 3 possible variants: ( 5 , 3 ) (5, 3) (5,3) , ( 10 , 6 ) (10, 6) (10,6) , ( 15 , 9 ) (15, 9) (15,9) .
In the second example, there is no TV set meeting the constraints.
In the third example, there is only one variant: ( 3 , 2 ) (3, 2) (3,2) .
显然我们把 x , y x,y x,y除以它们的 g c d gcd gcd, a n s = m a x ( a x , b y ) ans = max(\frac{a}{x}, \frac{b}{y}) ans=max(xa,yb)就行了。
代码如下:
#include
#include
#include
#include
#include
#include
#define R register
#define IN inline
#define W while
#define gc getchar()
#define ll long long
template <class T>
IN void in(T &x)
{
x = 0; R char c = gc;
for (; !isdigit(c); c = gc);
for (; isdigit(c); c = gc)
x = (x << 1) + (x << 3) + c - 48;
}
ll a, b, x, y, g;
ll gcd(ll x, ll y) {return y ? gcd(y, x % y) : x;}
int main(void)
{
in(a), in(b), in(x), in(y);
g = gcd(x, y);
x /= g; y /= g;
printf("%I64d", std::min(a / x, b / y));
}
Recently Monocarp got a job. His working day lasts exactly mm minutes. During work, Monocarp wants to drink coffee at certain moments: there are n n n minutes a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,…,an , when he is able and willing to take a coffee break (for the sake of simplicity let’s consider that each coffee break lasts exactly one minute).
However, Monocarp’s boss doesn’t like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute a i a_i ai , Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least dd minutes pass between any two coffee breaks. Monocarp also wants to take these n n n coffee breaks in a minimum possible number of working days (he doesn’t count days when he is not at work, and he doesn’t take coffee breaks on such days). Take into account that more than d d d minutes pass between the end of any working day and the start of the following working day.
For each of the n n n given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent.
The first line contains three integers n n n , m m m , d d d ( 1 ≤ n ≤ 2 ⋅ 1 0 5 , n ≤ m ≤ 1 0 9 , 1 ≤ d ≤ m 1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m 1≤n≤2⋅105,n≤m≤109,1≤d≤m) — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks.
The second line contains nn distinct integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,…,an ( 1 ≤ a i ≤ m 1 \le a_i \le m 1≤ai≤m), where a i a_i ai is some minute when Monocarp wants to have a coffee break.
In the first line, write the minimum number of days required to make a coffee break in each of the n n n given minutes.
In the second line, print n n n space separated integers. The i i i -th of integers should be the index of the day during which Monocarp should have a coffee break at minute a i a_i ai . Days are numbered from 1 1 1 . If there are multiple optimal solutions, you may print any of them.
4 5 3
3 5 1 2
3
3 1 1 2
10 10 1
10 5 7 4 6 3 2 1 9 8
2
2 1 1 2 2 1 2 1 1 2
In the first example, Monocarp can take two coffee breaks during the first day (during minutes 1 1 1 and 5 5 5 , 3 3 3minutes will pass between these breaks). One break during the second day (at minute 2 2 2 ), and one break during the third day (at minute 3 3 3 ).
In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
我们用 s e t set set维护时间, 每次先取最小的一个, 然后往后贪心取间隔尽量接近 d d d的时间点就好了。
注意间隔 d d d是指两个时间点之间有 d d d个时间点。
代码如下:
#include
#include
#include
#include
#include
#include
#include
#define R register
#define IN inline
#define gc getchar()
#define W while
#define MX 200050
template <class T>
IN void in(T &x)
{
x = 0; R char c = gc;
for (; !isdigit(c); c = gc);
for (; isdigit(c); c = gc)
x = (x << 1) + (x << 3) + c - 48;
}
int bel[MX], num, cnt, halt, lent;
struct INFO {int tim, id;} now;
IN bool operator < (const INFO &x, const INFO &y) {return x.tim < y.tim;}
IN bool operator == (const INFO &x, const INFO &y) {return x.tim == y.tim;}
std::set <INFO> st;
std::set <INFO>:: iterator it;
int main(void)
{
int a;
in(num), in(lent), in(halt);
for (R int i = 1; i <= num; ++i)
in(a), st.insert({a, i});
W (!st.empty())
{
++cnt;
it = st.begin(); now = *it;
bel[now.id] = cnt;
st.erase(it);
W (!st.empty())
{
it = st.lower_bound({now.tim + halt + 1, 0});
if(it == st.end()) break;
now = *it; bel[now.id] = cnt;
st.erase(it);
}
}
printf("%d\n", cnt);
for (R int i = 1; i <= num; ++i) printf("%d ", bel[i]);
}
A plane is flying at a constant height of h h h meters above the ground surface. Let’s consider that it is flying from the point ( − 1 0 9 , h ) (-10^9, h) (−109,h) to the point ( 1 0 9 , h ) (10^9, h) (109,h) parallel with O x Ox Ox 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 O x Ox Ox 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 x 1 x_1 x1 and x 2 x_2 x2( x 1 < x 2 x_1 < x_2 x1<x2 ) representing its endpoints. No two segments share any common points. When the glider is inside one of such segments, he doesn’t descend, so his second coordinate stays the same each second. The glider still flies along O x Ox Ox axis, covering one unit of distance every second.
If the glider jumps out at 1 1 1, he will stop at 10 10 10 . Otherwise, if he jumps out at 2 2 2 , he will stop at 12 12 12 .Determine the maximum distance along O x Ox Ox 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 0 0 0 .
The first line contains two integers n n n and h h h ( 1 ≤ n ≤ 2 ⋅ 1 0 5 , 1 ≤ h ≤ 1 0 9 1 \le n \le 2\cdot10^{5}, 1 \le h \le 10^{9} 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 n n n lines contains two integers x i 1 x_{i1} xi1 and x i 2 x_{i2} xi2 ( 1 ≤ x i 1 < x i 2 ≤ 1 0 9 1 \le x_{i1} < x_{i2} \le 10^{9} 1≤xi1<xi2≤109) — the endpoints of the i i i -th ascending air flow segment. No two segments intersect, and they are given in ascending order.
Print one integer — the maximum distance along O x Ox Ox 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.
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
In the first example if the glider can jump out at ( 2 , 4 ) (2, 4) (2,4) , then the landing point is ( 12 , 0 ) (12, 0) (12,0) , so the distance is 12 − 2 = 10 12-2 = 10 12−2=10 .
In the second example the glider can fly from ( 16 , 10 ) (16,10) (16,10) to ( 34 , 0 ) (34,0) (34,0) , and the distance is 34 − 16 = 18 34-16=18 34−16=18 .
In the third example the glider can fly from ( − 100 , 1000000000 ) (-100,1000000000) (−100,1000000000) to ( 1999999899 , 0 ) (1999999899,0) (1999999899,0) , so the distance is 1999999899 − ( − 100 ) = 1999999999 1999999899-(-100)=1999999999 1999999899−(−100)=1999999999 .
贪心地想, 我们在每一段平行飞行的段的起点开始飞行不会更差, 所以我们从左到右枚举这个位置, t w o p o i n t e r two\ pointer two pointer维护另一端的位置即可。
代码如下:
#include
#include
#include
#include
#include
#include
#include
#define R register
#define IN inline
#define W while
#define gc getchar()
#define MX 200050
#define ll long long
#define INF 1000000000
bool neg;
template <class T>
IN void in(T &x)
{
x = 0; R char c = gc;
for (; !isdigit(c); c = gc)
if(c == '-') neg = true;
for (; isdigit(c); c = gc)
x = (x << 1) + (x << 3) + c - 48;
if(neg) neg = false, x = -x;
}
struct Seg {ll from, to;} seg[MX];
ll inter[MX], ans, h;
int num;
int main(void)
{
in(num); in(h); R int now = 1, from = 1;
for (R int i = 1; i <= num; ++i) in(seg[i].from), in(seg[i].to);
for (R int i = 2; i <= num; ++i) inter[i - 1] = seg[i].from - seg[i - 1].to;
inter[num] = INF;
for (R int i = 2; i <= num; ++i) inter[i] += inter[i - 1];
for (from = 0; from < num; ++from)
{
W (inter[now] - inter[from] < h) ++now;
ans = std::max(ans, seg[now].to - seg[from + 1].from + (h - (inter[now - 1] - inter[from])));
}
printf("%I64d", ans);
}
Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from 1 1 1 to n n n . For every edge e e e of this tree, Monocarp has written two numbers: the maximum indices of the vertices of the two components formed if the edge e e e (and only this edge) is erased from the tree.
Monocarp has given you a list of n − 1 n - 1 n−1 pairs of numbers. He wants you to provide an example of a tree that will produce the said list if this tree exists. If such tree does not exist, say so.
The first line contains one integer n n n ( 2 ≤ n ≤ 1   000 2 \le n \le 1\,000 2≤n≤1000) — the number of vertices in the tree.
Each of the next n − 1 n-1 n−1 lines contains two integers a i a_i ai and b i b_i bi each ( 1 ≤ a i < b i ≤ n 1 \le a_i < b_i \le n 1≤ai<bi≤n ) — the maximal indices of vertices in the components formed if the i i i -th edge is removed.
If there is no such tree that can produce the given list of pairs, print “NO
” (without quotes).
Otherwise print “YES
” (without quotes) in the first line and the edges of the tree in the next n − 1 n - 1 n−1 lines. Each of the last n − 1 n - 1 n−1 lines should contain two integers x i x_i xi and y i y_i yi ( 1 ≤ x i , y i ≤ n 1 \le x_i, y_i \le n 1≤xi,yi≤n ) — vertices connected by an edge.
Note: The numeration of edges doesn’t matter for this task. Your solution will be considered correct if your tree produces the same pairs as given in the input file (possibly reordered). That means that you can print the edges of the tree you reconstructed in any order.
4
3 4
1 4
3 4
YES
1 3
3 2
2 4
3
1 3
1 3
NO
3
1 2
2 3
NO
Possible tree from the first example. Dotted lines show edges you need to remove to get appropriate pairs.
显然每一条边都有一个值为 n n n, 先特盘掉。
既然每个都有 n n n, 我们不妨把 n n n号点放在中间, 贪心放其他的点。 如果 i i i号点出现了 k k k次, 那么中间就有 k − 1 k-1 k−1个比 i i i小的节点, 我们贪心从小向大填充就行了。
当然也可以构造链的情况, 把标号最大的点放在链的一端即可。
代码如下:
#include
#include
#include
#include
#include
#include
#include
#define R register
#define IN inline
#define gc getchar()
#define W while
#define MX 1050
template <class T>
IN void in(T &x)
{
x = 0; R char c = gc;
for (; !isdigit(c); c = gc);
for (; isdigit(c); c = gc)
x = (x << 1) + (x << 3) + c - 48;
}
int cnt[MX];
bool vis[MX];
int num, ct;
std::set <int> st, avai;
struct Edge {int from, to;} edge[MX];
int main(void)
{
in(num); int a, b;
for (R int i = 1; i < num; ++i)
{
in(a), in(b);
if(a < b) std::swap(a, b);
if(a != num) return puts("NO"), 0;
if(b == num) return puts("NO"), 0;
cnt[b]++; st.insert(b); vis[b] = true;
}
for (R int i = 1; i <= num; ++i) if(!vis[i]) avai.insert(i);
int now, tar, fir;
W (!st.empty())
{
now = *st.begin(); st.erase(st.begin());
if(cnt[now] == 1)
{
edge[++ct] = {now, num};
continue;
}
fir = *avai.begin(); avai.erase(avai.begin());
if(fir > now) return puts("NO"), 0;
edge[++ct] = {num, fir};
for (R int i = 2; i < cnt[now]; ++i)
{
tar = *avai.begin(); avai.erase(avai.begin());
if(tar > now) return puts("NO"), 0;
edge[++ct] = {tar, fir}, fir = tar;
}
edge[++ct] = {now, fir};
}
puts("YES");
for (R int i = 1; i <= ct; ++i) printf("%d %d\n", edge[i].from, edge[i].to);
}
You are given a tube which is reflective inside represented as two non-coinciding, but parallel to O x Ox Ox lines. Each line has some special integer points — positions of sensors on sides of the tube.
You are going to emit a laser ray in the tube. To do so, you have to choose two integer points A A A and B B B on the first and the second line respectively (coordinates can be negative): the point A A A is responsible for the position of the laser, and the point B B B — for the direction of the laser ray. The laser ray is a ray starting at A A A and directed at B B B which will reflect from the sides of the tube (it doesn’t matter if there are any sensors at a reflection point or not). A sensor will only register the ray if the ray hits exactly at the position of the sensor.
Examples of laser rays. Note that image contains two examples. The 3 3 3 sensors (denoted by black bold points on the tube sides) will register the blue ray but only 2 2 2 will register the red.Calculate the maximum number of sensors which can register your ray if you choose points A A A and B B B on the first and the second lines respectively.
The first line contains two integers n n n and y 1 y_1 y1 ( 1 ≤ n ≤ 1 0 5 1 \le n \le 10^5 1≤n≤105 , 0 ≤ y 1 ≤ 1 0 9 0 \le y_1 \le 10^9 0≤y1≤109 ) — number of sensors on the first line and its y y y coordinate.
The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an ( 0 ≤ a i ≤ 1 0 9 0 \le a_i \le 10^9 0≤ai≤109 ) — x x x coordinates of the sensors on the first line in the ascending order.
The third line contains two integers m m m and y 2 y_2 y2 ( 1 ≤ m ≤ 1 0 5 1 \le m \le 10^5 1≤m≤105 , y 1 < y 2 ≤ 1 0 9 y_1 < y_2 \le 10^9 y1<y2≤109 ) — number of sensors on the second line and its y y y coordinate.
The fourth line contains m m m integers b 1 , b 2 , … , b m b_1, b_2, \ldots, b_m b1,b2,…,bm ( 0 ≤ b i ≤ 1 0 9 0 \le b_i \le 10^9 0≤bi≤109 ) — x x x coordinates of the sensors on the second line in the ascending order.
Print the only integer — the maximum number of sensors which can register the ray.
3 1
1 5 6
1 3
3
3
One of the solutions illustrated on the image by pair A 2 A_2 A2 and B 2 B_2 B2 .
首先我们可以发现当 x B − x A x_B-x_A xB−xA为奇数的时候可以用 x B − x A = 1 x_B-x_A=1 xB−xA=1的情况代替, 而当 x B − x A = 2 k x^B-x^A=2^k xB−xA=2k的情况不能通过其他凑出来。 所以我们枚举这个间隔, 每次统计。
但在间隔为 2 k 2^k 2k的情况下, 本质不同的起点有 2 k + 1 2^{k+1} 2k+1个, 我们怎么统计每种情况的答案? 其实很简单, 我们保存从 m i n ( x 1 , y 1 ) min(x_1,y_1) min(x1,y1)到 x i x_i xi、 y i y_i yi的距离 m o d 2 k + 1 mod\ 2^{k+1} mod 2k+1的值, 统计完后遍历 m a p map map统计答案即可。
代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define R register
#define IN inline
#define W while
#define gc getchar()
#define ll long long
#define MX 200500
bool neg;
template <class T>
IN void in(T &x)
{
x = 0; R char c = gc;
for (; !isdigit(c); c = gc)
if(c == '-') neg = true;
for (; isdigit(c); c = gc)
x = (x << 1) + (x << 3) + c - 48;
if(neg) neg = false, x = -x;
}
int numu, numd, up[MX], down[MX], pmin = INT_MAX, pmax = -INT_MAX, yu, yd, ans, res, pos;
std::map <ll, int> mpd, mpu;
std::map <ll, int> :: iterator it;
IN int calc(ll val)
{
int ret = 0;
mpd.clear(); mpu.clear(); ll jp = val << 1, sur;
for (R int i = 1; i <= numd; ++i) mpd[(down[i] - pmin) % jp]++;
for (R int i = 1; i <= numu; ++i) mpu[(up[i] - pmin) % jp]++;
for (it = mpd.begin(); it != mpd.end(); it++)
{
sur = it -> first;
ret = std::max(ret, it -> second + mpu[(sur + val) % jp]);
}
for (it = mpu.begin(); it != mpu.end(); it++)//注意可能只去上面或只取下面的情况, 所以两边都要枚举到
{
sur = it -> first;
ret = std::max(ret, it -> second + mpd[(sur + val) % jp]);
}
return ret;
}
int main(void)
{
in(numd); in(yd);
for (R int i = 1; i <= numd; ++i) in(down[i]), pmin = std::min(down[i], pmin), pmax = std::max(down[i], pmax);
in(numu); in(yu);
for (R int i = 1; i <= numu; ++i) in(up[i]), pmin = std::min(up[i], pmin), pmax = std::max(up[i], pmax);
int t = log2(pmax - pmin);
if(pmax == pmin) return puts("2"), 0;
for (R int i = 0; i <= t; ++i) ans = std::max(ans, calc(1ll << i));
printf("%d", ans);
}