Make It Equal(思維+細心)
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;
}