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 hihi cubes, 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).
题意:给出一个数组,如上图,a[x] = y表示在第x列中有y个1*1的正方形,我们每次可以沿着平行于x轴切一刀,舍弃上面部分,且舍弃部分的面积要小于k,求最少多少步。
思路:每次切都是横者切的,所以计算出每排的方格数,暴力从最上层一直切到最下层即可
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
using namespace std;
#define ll long long
int a[200005];
int cub[200005];
int main()
{
ll n,k;
while(cin >> n >> k)
{
int ans = 0;
memset(cub,0,sizeof(cub));
for(int i = 1;i <= n;i++)
cin >> a[i];
sort(a+1,a+n+1);
for(int i = n;i > 1&&a[i]!=a[1];i--)//a[i]!=a[1]表示最底层的方格数不计算
{
while(a[i] == a[i-1]) i--;
for(int j = 0;j < a[i]-a[i-1];j++)
cub[a[i]-j]+=n-i+1;
}
for(int i = a[n],sum = 0;i >= 1;i--)
{
sum+=cub[i];
if(sum > k)
sum=cub[i],ans++;
if(i == 1 && sum > 0)
ans++;//切到最后,如果还有剩下的没切掉,就要切最后一刀
}
cout << ans <<endl;
}
//cout << "AC" <<endl;
return 0;
}