#2018-2019 ACM-ICPC, NEERC, Southern Subregional Contest, Qualification Stage (快乐场)

A. Coffee Break

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Recently Monocarp got a job. His working day lasts exactly mm minutes. During work, Monocarp wants to drink coffee at certain moments: there are nn minutes a1,a2,…,ana1,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 aiai, 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 nn 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 dd minutes pass between the end of any working day and the start of the following working day.

For each of the nn 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.

Input

The first line contains three integers nn, mm, dd (1≤n≤2⋅105,n≤m≤109,1≤d≤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 a1,a2,…,ana1,a2,…,an (1≤ai≤m)(1≤ai≤m), where aiai is some minute when Monocarp wants to have a coffee break.

Output

In the first line, write the minimum number of days required to make a coffee break in each of the nn given minutes.

In the second line, print nn space separated integers. The ii-th of integers should be the index of the day during which Monocarp should have a coffee break at minute aiai. Days are numbered from 11. If there are multiple optimal solutions, you may print any of them.

Examples

input

Copy

4 5 3
3 5 1 2

output

Copy

3
3 1 1 2 

input

Copy

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

output

Copy

2
2 1 1 2 2 1 2 1 1 2 

Note

In the first example, Monocarp can take two coffee breaks during the first day (during minutes 11 and 55, 33 minutes will pass between these breaks). One break during the second day (at minute 22), and one break during the third day (at minute 33).

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.

题目大意 :

你想在N个时间段喝咖啡, 每次喝咖啡需要1分钟, 但是你一天只有M分钟, 并且你喝咖啡的间隔必须大于K分钟, 输出一种最快喝完咖啡的方案 

思路 :

贪心, 用set保存每个时间点, 从第一天开始, 二分查找下一次可以喝的时间点,记录答案并且删除, 最后删完了退出循环就好, 并且天数 + 1, 直到set为空

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
 
set <ll> st;
ll n, m, k;
ll ans[MAXN];
unordered_map <ll, ll> ev;
 
int main()
{
	cin >> n >> m >> k;
	for (ll i = 1; i <= n; i++) {
		ll tmp; sc("%lld", &tmp);
		st.insert(tmp); ev[tmp] = i;
	}
	ll now = 1, num; // now表示天数
	while (SZ(st)) {
		num = *st.begin(); 
		while (true) {
			auto it = st.upper_bound(num + k);
			if (it == st.end()) {  // 查不到到下一天
				ans[ev[num]] = now;
				st.erase(num);
				break;
			}
			else {
				ans[ev[num]] = now;
				st.erase(num);
				num = *it;
			}
		}
		now++;
	}
	printf("%lld\n", now - 1);
	for (ll i = 1; i <= n; i++) printf("%lld ", ans[i]);
	cout << endl;
	return 0;  // 改数组大小!!!
}

B. Glider

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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<x2x1<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 OxOx axis, covering one unit of distance every second.

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<xi2≤109)(1≤xi1<xi2≤109) — the endpoints of the ii-th ascending air flow segment. No two segments intersect, and they are given in ascending order.

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

Copy

3 4
2 5
7 9
10 11

output

Copy

10

input

Copy

5 10
5 7
11 12
16 20
25 26
30 33

output

Copy

18

input

Copy

1 1000000000
1 1000000000

output

Copy

1999999999

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)=19999999991999999899−(−100)=1999999999.

题目大意 : 

一架飞机在高为H的位置飞行, , 他可以选择任意一个位置扔掉滑翔机, 滑翔机每单位时间下滑1单位距离,每个单位时间滑行1单位距离, 现在有N个上升气流, 可以使滑翔机不下滑, 输出滑翔机最多可以滑行的距离

思路 :

题目意思翻译一下就是, 让你在滑翔机到地面之前, 经过最多的上升气流的个数, 可以先用前缀和将气流的个数和经过的空隙的个数保存下来, 那么就满足单调性了, 这时候对于每一个空隙, 二分当前空隙的个数 + H之间有多少个气流, 每次更新最大值, 最后经过的气流最多的次数 + 高度即为答案

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
 
struct node
{
	int l, r;
}p[MAXN];
ll c[MAXN], b[MAXN], n, m;
 
int main()
{
	cin >> n >> m;
	c[1] = 0;
	for (int i = 1; i <= n; i++) {
		sc("%d %d", &p[i].l, &p[i].r);
		if (i >= 2) c[i] = p[i].l - p[i - 1].r;
		b[i] = p[i].r - p[i].l;
		b[i] += b[i - 1], c[i] += c[i - 1];  // c表示空隙,b表示气流
	}
	ll max_ = 0;
	for (int i = 1; i <= n; i++) {
		int tmp = lower_bound(c + 1, c + n + 1, c[i] + m) - c;  // 二分
		ll ans = b[tmp - 1] - b[i - 1];
		Max(max_, ans);
	}
	cout << max_ + m << endl;
	return 0;  // 改数组大小!!!
}

C. Bacteria

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy

2
1 4

output

Copy

2

input

Copy

3
3 6 9

output

Copy

-1

input

Copy

7
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

output

Copy

1

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.

题目大意 :

有N个细菌, 每个细菌可以和自己相同重量的细菌合成一个大细菌, 重量为二者之和, 你可以添加一个任意重量的细菌, 输出最少需要添加多少细菌, 可以使输入的细菌合成一个, 如果无法合成, 输出-1

思路 :
不难想到每次要对最小的两个进行合成, 优先队列按照小的优先级大进行排序, 对于X, Y, 看需要多少个X才能合成一个Y, 令C = Y % X, 如果C不为0, 那么就无法合成,直接输出-1,观察后易知, X合成一次后是 X * pow(2, 1), 两次是X * pow(2, 2), N次是X * pow(2, N), 现在让你求N, 也就是求2的多少次方等于 C, 直接用log函数会有精度损失, 二分即可

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
 
priority_queue <ll, vector <ll>, greater <ll>> q;
ll ans;
ll fpow(ll a, ll b) {
	ll ans = 1, base = a;
	while (b) {
		if (b & 1) ans *= base;
		base *= base;
		b >>= 1;
	}
	return ans;
}
 
int main()
{
	ll n; cin >> n;
	for (ll i = 0; i < n; i++) {
		ll tmp; sc("%lld", &tmp);
		q.push(tmp);
	}
	if (n == 1) { printf("0\n"); return 0; }
	while (SZ(q) >= 2) {
		ll ans1 = q.top(); q.pop();
		ll ans2 = q.top(); q.pop();
		if (ans2 % ans1) { printf("-1\n"); return 0; }  // 退出
		ll cnt = ans2 / ans1, tot = -1;
		ll l = 0, r = 30, mid;  // 二分
		while (l <= r) {
			mid = (l + r) >> 1;
			if (fpow(2, mid) >= cnt) tot = mid, r = mid - 1;
			else l = mid + 1;
		}
		if (tot == -1) { printf("-1\n"); return 0; } // 没有答案退出
		ans += tot;
		q.push(2 * ans2);  // 加入队列
	}
	printf("%lld\n", ans);
	return 0;  // 改数组大小!!!
}

D. Masquerade strikes back

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Quite often the jury of Saratov SU use the problem "Masquerade" in different practice sessions before the contest. This problem is quite easy — all you need is to print the product of two integers which were read from the input stream.

As usual, the jury had prepared this problem once again. The jury had nn testcases, the ii-th testcase was a pair of positive integers aiai and bibi, both integers didn't exceed 107107. All testcases were pairwise distinct.

Unfortunately, something went wrong. Due to hardware issues all testcases have disappeared. All that the jury were able to restore are the number of testcases nn and the answers to these testcases, i. e. a sequence of nn numbers c1,c2,…,cnc1,c2,…,cn, such that ai⋅bi=ciai⋅bi=ci.

The jury ask you to help them. Can you provide any possible testset? Remember that all testcases were distinct and all numbers in each testcase were positive integers and didn't exceed 107107.

Input

First line contains one insteger nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of lost testcases.

Second line contains nn space-separated integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤1071≤ci≤107) — the answers to the testcases.

Output

If there is no such testset, print NO.

Otherwise, print YES in first line. Then print nn more lines, the ii-th of them should contain two space separated positive integers aiai and bibi not exceeding 107107. All pairs (ai,bi)(ai,bi) must be distinct, and, for each i∈[1,n]i∈[1,n], the condition ai⋅bi=ciai⋅bi=ci must be met.

Examples

input

Copy

4
1 3 3 7

output

Copy

YES
1 1
1 3
3 1
1 7

input

Copy

5
3 1 3 3 7

output

Copy

NO

input

Copy

6
9 10 9 10 9 10

output

Copy

YES
1 9
1 10
3 3
5 2
9 1
2 5

Note

In the first example one of the possible testsets is (a1=1a1=1, b1=1b1=1), (a2=1a2=1, b2=3b2=3), (a3=3a3=3, b3=1b3=1), (a4=1a4=1, b4=7b4=7).

In the second example a testset consisting of distinct tests doesn't exist.

题目大意 :

输入一个序列, 输出序列当中每个数Xi的U 和 V, 满足U * V == 数Xi, 并且U和V在答案中不能重复,可调换位置

思路 :

先预处理保存所有的对, 并把每个数包含的答案对数记下来, 如果该数出现的次数 > 对数, 那么就一定会重复, 最后输出答案

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int MAXM = 1e7 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
 
vector <pair <int, int>> e[MAXM];
int p[MAXN], num[MAXM], n;
 
int main()
{
	cin >> n; bool flag = true;
	for (int i = 1; i <= n; i++) {
		sc("%d", &p[i]); int tmp = p[i];
		if (!flag) continue;
		if (!num[tmp]) {
			for (int j = 1; j <= sqrt(tmp); j++) {
				if (tmp % j == 0) {
					e[tmp].push_back(make_pair(j, tmp / j)); // 保存因子
					if (j * j != tmp) e[tmp].push_back(make_pair(tmp / j, j));
				}
			}
		}
		num[tmp]++;
		if (num[tmp] > SZ(e[tmp])) flag = false; // 多就重复了
	}
	if (!flag) { printf("NO\n"); return 0; }
	printf("YES\n");
	for (int i = 1; i <= n; i++) {
		int tmp = p[i];
		printf("%d %d\n", e[tmp][0].first, e[tmp][0].second);
		e[tmp].erase(e[tmp].begin());
	}
	return 0;  // 改数组大小!!!
}

E. Painting the Fence

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There is a beautiful fence near Monocarp's house. The fence consists of nn planks numbered from left to right. The ii-th plank has color aiai.

Monocarp's father have decided to give his son mm orders. Each order is a color cjcj. After each order Monocarp finds leftmost and rightmost planks currently having color cjcj and repaints all planks between them into color cjcj.

For example, if, at first, fence looked like (from left to right) [1,2,3,1,4,1,5,6][1,2,3,1,4,1,5,6], then after fulfilling an order with color 11 fence will look like [1,1,1,1,1,1,5,6][1,1,1,1,1,1,5,6].

Assume that Monocarp fulfills all orders in the order they come, one by one.

Note that if current order is about color xx and there is no more than one plank in the fence having color xx, then Monocarp doesn't repaint anything, so he can skip this order and skip to the next one.

Find out the color of each plank after Monocarp has done all the given orders.

Input

The first line contains one integer nn (1≤n≤3⋅105)(1≤n≤3⋅105) — the number of planks in the fence.

The second line contains nn space-separated integers a1,a2,…,ana1,a2,…,an (1≤ai≤3⋅105)(1≤ai≤3⋅105), where aiai is the initial color of the ii-th plank.

The third line contains one integer mm (1≤m≤3⋅105)(1≤m≤3⋅105) — the number of orders.

The fourth line contains mm space-separated integers c1,c2,…,cmc1,c2,…,cm (1≤cj≤3⋅105)(1≤cj≤3⋅105), where cjcj is the color of the jj-th order.

Output

Print nn space-separated integers — the colors of planks in the fence after processing all mm orders.

Examples

input

Copy

4
1 2 1 2
2
2 1

output

Copy

1 2 2 2 

input

Copy

8
7 1 7 1 23 9 23 1
4
23 4 7 1

output

Copy

7 7 7 1 1 1 1 1 

Note

In the first example initial appearance of the fence is [1,2,1,2][1,2,1,2]. After the first order (color 22) fence will look like [1,2,2,2][1,2,2,2]. After the second order (color 11) appearance of the fence will not change.

In the second example initial appearance of the fence is [7,1,7,1,23,9,23,1][7,1,7,1,23,9,23,1]. After the first order (color 2323) the fence will look like [7,1,7,1,23,23,23,1][7,1,7,1,23,23,23,1]. After the second order (color 44) appearance of the fence will not change. After the third order (color 77) the fence will look like [7,7,7,1,23,23,23,1][7,7,7,1,23,23,23,1]. After the fourth order (color 11) the fence will look like [7,7,7,1,1,1,1,1][7,7,7,1,1,1,1,1].

题目大意 :

有N个栅栏, 每个栅栏都有各自的颜色, 现在操作M次, 把最左边的和最右边的颜色为X的栅栏之间的栅栏, 全部染成X色,最后输出操作完的颜色情况

思路 :

一开始以为线段树, 但是并没有想到怎么写, 因为你保存的数据可能在某次操作就被删掉了, 网上有用线段树写的。删除操作的话用set比较合适, 一开始把所有颜色的下标保存下来, 操作时, 看该颜色是否合并了, 如果合并过了就不操作, 否则将除了左右两端点之间的颜色全部删掉, 下次遍历的时候, 直接从左端点跳到右端点就不会超时了

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 3e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
 
set <int> st[MAXN];
int p[MAXN], ans[MAXN], n, m;
bool vis[MAXN];
 
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++) {
		sc("%d", &p[i]); 
		st[p[i]].insert(i);  // 记录初始位置
	}
	cin >> m;
	for (int i = 0; i < m; i++) {
		int tmp; sc("%d", &tmp);
		if (vis[tmp]) continue;  // 合并过就不操作
		if (SZ(st[tmp]) < 2) { vis[tmp] = true; continue; } // 一个点也算合并过
		for (int j = *st[tmp].begin() + 1; j <= *st[tmp].rbegin() - 1; j++) {
			st[p[j]].erase(j);
			while (vis[p[j]] && SZ(st[p[j]])) { // 直接跳过中间已经删掉的
				j = *st[p[j]].begin();
				st[p[j]].erase(j);
			}
		}
		vis[tmp] = true;
	}
	for (int i = 1; i <= 3e5; i++) {
		if (SZ(st[i]) >= 2 && vis[i]) {
			for (int j = *st[i].begin(); j <= *st[i].rbegin(); j++) p[j] = i;
		}
	}
	for (int i = 1; i <= n; i++) 
		printf("%d ", p[i]);
	cout << endl;
	return 0;  // 改数组大小!!!
}

H. Theater Square

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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×1 are 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

Copy

6 5
1 2 3 4

output

Copy

5

input

Copy

6 1
3 1 4 1

output

Copy

2

input

Copy

1 12
1 3 1 8

output

Copy

0

Note

One of the optimal ways to pave the Theater Square in the first example:

题目大意 :

一个N * M的矩阵, 输入水池的坐标, 除了水池以外剩下所有的空地都用1 * 2 的砖块填充, 无法填充则将1 * 2的砖块断开填充, 1 * 2的砖块无限, 输出最少切断了多少砖块

思路 : 签到题, 找到水池的上, 下,左, 右的空地是否是奇数, 如果是奇数, 记录对应的长度或宽度, 最后输出向上取整就好

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
 ll n,m;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
 
int main()
{   scanf("%lld%lld",&n,&m);
    int x1,y1,x2,y2;
    scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);
    ll sum=0;
    ll l=0,r=0;
    if(m%2)l=n-x2+x1-1;
    r=((y1-1)%2+(m-y2)%2)*(x2-x1+1);
    sum=l+r;
    if(sum%2)sum++;
    printf("%lld",sum/2);
	return 0;  // 改数组大小!!!
}

I. Heist

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy

4
10 13 12 8

output

Copy

2

input

Copy

5
7 5 6 4 8

output

Copy

0

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.

题目大意 :

输入一个序列,输出该序列若是连续那么最多少了多少个数

思路 :

签到题, 找到该序列的最大长度, 然后减去N就好

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
 
ll n, ans, max_ = -INF, min_ = INF;
 
int main()
{
    cin >> n;
    for (ll i = 0; i < n; i++) {
        ll tmp; sc("%lld", &tmp);
        Min(min_, tmp);
        Max(max_, tmp);
    }
    ll ans = max_ - min_ + 1;;
    cout << ans - n << endl;
	return 0;  // 改数组大小!!!
}

J. Buying a TV Set

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 aa and screen height not greater than bb. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is ww, and the height of the screen is hh, then the following condition should be met: wh=xywh=xy.

There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers ww and hh there is a TV set with screen width ww and height hh 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 ww and hh, beforehand, such that (w≤a)(w≤a), (h≤b)(h≤b) and (wh=xy)(wh=xy).

In other words, Monocarp wants to determine the number of TV sets having aspect ratio xyxy, screen width not exceeding aa, and screen height not exceeding bb. Two TV sets are considered different if they have different screen width or different screen height.

Input

The first line contains four integers aa, bb, xx, yy (1≤a,b,x,y≤10181≤a,b,x,y≤1018) — the constraints on the screen width and height, and on the aspect ratio.

Output

Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints.

Examples

input

Copy

17 15 5 3

output

Copy

3

input

Copy

14 16 7 22

output

Copy

0

input

Copy

4 2 6 4

output

Copy

1

input

Copy

1000000000000000000 1000000000000000000 999999866000004473 999999822000007597

output

Copy

1000000063

Note

In the first example, there are 33 possible variants: (5,3)(5,3), (10,6)(10,6), (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).

题目大意 :

你要购买一台电视机, 宽和高满足W / H = X / Y, 现在限制W <= a, H <= b, 输出最多有多少个满足条件的电视机

思路 :

先化简Y / X, 然后看求分子和分母各自可能出现的次数, 取最小值

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
 
ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}
 
int main()
{
    ll a, b, x, y;
    cin >>a >> b >>  x >> y;
    ll ans = gcd(x, y);
    ll xi = x / ans;
    ll yi = y / ans;
    ll ans1 = a / xi;
    ll ans2 = b / yi;
    cout << min(ans1, ans2) << endl;
	return 0;  // 改数组大小!!!
}

K. Medians and Partition

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Let median of some array be the number which would stand in the middle of this array if it was sorted beforehand. If the array has even length let median be smallest of of two middle elements. For example, median of the array [10,3,2,3,2][10,3,2,3,2] is 33 (i.e. [2,2,3–,3,10][2,2,3_,3,10]). Median of the array [1,5,8,1][1,5,8,1] is 11 (i.e. [1,1–,5,8][1,1_,5,8]).

Let array be mm-good if its median is greater or equal than mm.

Let the partition of array [a1,a2,…,an][a1,a2,…,an] be a set of subarrays {b1,b2,…,bk}{b1,b2,…,bk} such that b1=[a1,a2,…,ai1]b1=[a1,a2,…,ai1], b2=[ai1+1,ai1+2,…,ai2]b2=[ai1+1,ai1+2,…,ai2], ..., bk=[aik−1+1,aik−1+2,…,an]bk=[aik−1+1,aik−1+2,…,an]. For example, array [10,3,2,3,2][10,3,2,3,2] can be partitioned as follows: {[10,3,2,3,2]}{[10,3,2,3,2]} or {[10],[3],[2],[3],[2]}{[10],[3],[2],[3],[2]}, or {[10],[3,2,3,2]}{[10],[3,2,3,2]}, or {[10,3],[2],[3,2]}{[10,3],[2],[3,2]} and so on.

You are given array aa of length nn and integer mm. Find the partition of aa into maximum number of subarrays such that each subarray is mm-good.

Input

The first line contains two integers nn and mm (1≤n≤50001≤n≤5000, 1≤m≤50001≤m≤5000) — length of array aa and constant mm.

The second line contains nn integers a1a1, a2a2, ..., anan (1≤ai≤50001≤ai≤5000)— array aa.

Output

If there is no valid partition of array aa into mm-good subarrays, print 00. Otherwise print maximum number of subarrays in partition of array aa such that each subarray is mm-good.

Examples

input

Copy

5 2
10 3 2 3 2

output

Copy

5

input

Copy

5 3
10 3 2 3 2

output

Copy

1

input

Copy

5 4
10 3 2 3 2

output

Copy

0

Note

In the first example array can be partitioned into 55 subarrays: {[10],[3],[2],[3],[2]}{[10],[3],[2],[3],[2]}. Medians of each part greater of equal than 22.

In the second example we can't partition array into several subarrays since medians of [2][2], [3,2][3,2], [2,3,2][2,3,2] and [3,2,3,2][3,2,3,2] are less than 33.

题目大意 :

输入一个序列, 求在他的子序列当中, 存在多少个中数≥ M的

思路 :

观察后不难发现, 越靠后越有可能出现答案, 所以先排序, 对于每一个位置进行二分, 如果中数不满足条件, 那么往后二分 ,记录次数即可

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
 
int p[MAXN], n, m;
int L, R, ans;
 
int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++) sc("%d", &p[i]);
	sort(p + 1, p + n + 1);
	L = 1, R = 1;
	for (int i = 1; i <= n; i++) {
		int mid = (L + R) >> 1;
		if (p[mid] >= m) ans++, L = i + 1, R = i + 1;
		else R++;
	}
	cout << ans << endl;
	return 0;  // 改数组大小!!!
}

猜你喜欢

转载自blog.csdn.net/weixin_43851525/article/details/103046205