C. Make It Equal 差分

There is a toy building consisting of n towers. Each tower consists of several cubes standing on each other. The i-th tower consists of hi cubes, so it has height hi

.

Let’s define operation slice on some height H
as following: for each tower i, if its height is greater than H, then remove some top cubes to make tower’s height equal to H

. Cost of one “slice” equals to the total number of removed cubes from all towers.

Let’s name slice as good one if its cost is lower or equal to k
(k≥n

).

Calculate the minimum number of good slices you have to do to make all towers have the same height. Of course, it is always possible to make it so.
Input

The first line contains two integers n
and k (1≤n≤2⋅105, n≤k≤109

) — the number of towers and the restriction on slices, respectively.

The second line contains n
space separated integers h1,h2,…,hn (1≤hi≤2⋅105

) — the initial heights of towers.
Output

Print one integer — the minimum number of good slices you have to do to make all towers have the same heigth.
Examples
Input
Copy

5 5
3 1 2 2 4

Output
Copy

2

Input
Copy

4 5
2 3 4 5

Output
Copy

2

Note

In the first example it’s optimal to make 2
slices. The first slice is on height 2 (its cost is 3), and the second one is on height 1 (its cost is 4).


用 h[ i ] 表示高度为 i 的木块有几个,那么我们用差分数组进行维护;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 300005
#define inf 0x3f3f3f3f
#define INF 999999999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 10000007;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-7
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }


int n, k;
int h[maxn+5];

int main()
{
	//ios::sync_with_stdio(false);
	rdint(n); rdint(k);
	int x;
	for (int i = 1; i <= n; i++) {
		rdint(x); h[1]++; h[x + 1]--;
	}
	for (int i = 1; i <= maxn; i++)h[i] += h[i - 1];
	int st = 0;
	int tot = 0;
	for (int i = maxn; i >= 1; i--) {
		if (h[i] == n)break;
		if (st + h[i] <= k) {
			st += h[i];
		}
		else {
			tot++; st = h[i];
		}
	}
	if (st)tot++;
	cout << tot << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40273481/article/details/83028162