C. Make It Equal
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There is a toy building consisting of nn towers. Each tower consists of several cubes standing on each other. The ii-th tower consists of hihicubes, so it has height hihi.
Let's define operation slice on some height HH as following: for each tower ii, if its height is greater than HH, then remove some top cubes to make tower's height equal to HH. 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 kk (k≥nk≥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 nn and kk (1≤n≤2⋅1051≤n≤2⋅105, n≤k≤109n≤k≤109) — the number of towers and the restriction on slices, respectively.
The second line contains nn space separated integers h1,h2,…,hnh1,h2,…,hn (1≤hi≤2⋅1051≤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 22 slices. The first slice is on height 22 (its cost is 33), and the second one is on height 11 (its cost is 44).
一、原题地址
二、大致题意
给出n个有高度的格子,再给一个限制K。现在有一种操作可以选择一个高度然后把格子横切去上面的部分,但是每次被切去的部分不能超过给出的限制K。题目保证了每次落刀一定有一个可以满足的点。
询问最少需要切多少次才能将所有格子切成一样高。输出次数。
三、大致思路
离散化高度到y轴上,这样就可以利用线段树logn的统计两个高度之间有多少块格子。为了快速的找到下一次的落刀点,还需要二分来查找,否则超时。
四、代码
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<queue>
#include<set>
#include<map>
#include<unordered_set>
#include<vector>
using namespace std;
const int inf = 0x3f3f3f3f;
typedef long long LL;
const int N = 200005;
LL lazy[N << 2], K; //lazy用来记录该节点的每个数值应该加多少
int n;
struct Tree
{
int l, r;
LL sum;
int mid()
{
return (l + r) >> 1;
}
}tree[N << 2]; //tree[].l, tree[].r分别表示某个节点的左右区间,这里的区间是闭区间
void PushUp(int rt)
{
tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
}
void build(int l, int r, int rt)
{
tree[rt].l = l;
tree[rt].r = r;
lazy[rt] = 0;
if (l == r)
{
tree[rt].sum = 0;
return;
}
int m = tree[rt].mid();
build(l, m, (rt << 1));
build(m + 1, r, (rt << 1 | 1));
PushUp(rt);
}
void PushDown(int rt, int m)
{
if (lazy[rt])
{
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
tree[rt << 1].sum += lazy[rt] * (m - (m >> 1));
tree[rt << 1 | 1].sum += lazy[rt] * (m >> 1);
lazy[rt] = 0;
}
}
void update(LL c, int l, int r, int rt)//表示对区间[l,r]内的每个数均加c,rt是根节点
{
if (tree[rt].l == l&&tree[rt].r == r)
{
lazy[rt] += c;
tree[rt].sum += c*(r - l + 1);
return;
}
if (tree[rt].l == tree[rt].r)return;
int m = tree[rt].mid();
PushDown(rt, tree[rt].r - tree[rt].l + 1);
if (r <= m)update(c, l, r, rt << 1);
else if (l > m)update(c, l, r, rt << 1 | 1);
else
{
update(c, l, m, rt << 1);
update(c, m + 1, r, rt << 1 | 1);
}
PushUp(rt);
}
LL Query(int l, int r, int rt)
{
if (l == tree[rt].l&&r == tree[rt].r)
{
return tree[rt].sum;
}
int m = tree[rt].mid();
PushDown(rt, tree[rt].r - tree[rt].l + 1);
LL res = 0;
if (r <= m)res += Query(l, r, rt << 1);
else if (l > m)res += Query(l, r, rt << 1 | 1);
else
{
res += Query(l, m, rt << 1);
res += Query(m + 1, r, rt << 1 | 1);
}
return res;
}
int bir_search(int l, int r)
{
int mid, ret;
int nowh = r;
while (l <= r)
{
mid = (l + r) >> 1;
if (Query(mid + 1, nowh, 1) <= K)
{
ret = mid;
r = mid - 1;
}
else l = mid + 1;
}
return ret;
}
int maxh, minh, ans;
int main()
{
scanf("%d %lld", &n, &K);
maxh = -1; minh = inf;
build(1, 200000, 1);
for (int i = 1; i <= n; i++)
{
int h;
scanf("%d", &h);
maxh = max(maxh, h);
minh = min(minh, h);
update(1, 1, h, 1);
}
ans = 0;
while (maxh != minh)
{
maxh = bir_search(minh, maxh);
ans++;
}
printf("%d\n", ans);
getchar();
getchar();
}