Educational Codeforces Round 52 C. Make It Equal (细节+排序)

题目链接:http://codeforces.com/contest/1065/problem/C

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.

题意:

给你一排参差不齐的塔,让你水平方向依次切掉上面的,且每一次切的总数不能超过k,问你最少切多少刀使得全部塔一样高。

题解:

先从大到小排序,将每一层的个数保存到另一个数组,计算每一层方块数时注意细节,然后模拟切就行了。

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn = 2e5+55;
typedef long long ll;
ll a[maxn];
ll b[maxn];
int main()
{
    ll n,k;
    while(cin>>n>>k)
    {
        if(n==1)
        {
            cout<<0<<endl;
            continue;
        }
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        sort(a,a+n,greater<ll>());
        int m = 0;                             
        for(int i=0; i<n-1&&a[i]!=a[n-1];i++)   //计算比最小值高的每一层保存到b数组,a[i]!=a[n-1]
        {
            while(a[i+1]==a[i]) i++;        
            for(int j=1; j<=(a[i]-a[i+1]); j++) //比如 8 8 4 4 2,8和4差了4层,所以每一层都要保存
                b[m++] = i+1;
        }
        ll ans= 0;
        for(int i=0; i<m;)                    
        {
            ll sum = 0;
            while(sum+b[i]<=k)
            {
                sum+=b[i];
                i++;
            }
            ans++;
        }
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41157137/article/details/83021175