Educational Codeforces Round 52 (Rated for Div. 2) C. Make It Equal

C. Make It Equal

 

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

题意:给你n个矩形,每个矩形都是由1*1的小方块组成,你可以任选一行,包括该行在内以及上面的所有方块都可以消除,花费为消除方块的个数。规定每次消除不超过k的叫做good消除,问你最少多少次good消除,可以让所有矩形高度相等。

思路:思路很好想,实现略麻烦- -。首先排个序,很明显目标是把所有高度变成a[1]。

1.先求出消除第i行,所需的花费(就是一个行方块总数,仅仅这一行):双指针扫描即可,从最高的矩形开始,令now为当前位置,pos为比a[now]低的第一个位置,用n-pos即得该行总数,因为右边一定是连续的嘛。

2.还是从右往左扫描,累加求和就可以,当和sum超过k,此时计数good。里面细节比较多,多注意就行。

ps:顺便贴个样例  6 3

                              1 3 3 3 5 10          输出:4

​
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n,k,ans,a[200020],num[200020],x;
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        memset(a,0,sizeof(a));
        memset(num,0,sizeof(num));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a+1,a+n+1);
        int pos,now;
        now=n;
        pos=now-1;
        int d=a[1],h=a[n];   //d最小高度,h最大高度
        for(int i=h;i>d;i--)   //双指针扫描
        {
            while(a[pos]==a[now])pos--;
            num[i]=n-pos;
            a[now]--;
            if(a[pos]==a[now])
            {
                now=pos;
                pos=now-1;
            }
        }
        ans=0;
        int sum=0;
        if(h-d==1)   //一个特判,只消去一层时,手动判断
        {
            if(num[h]>k)printf("0\n");
            else printf("1\n");
            continue;
        }
        for(int i=h;i>d;i--)   //暴力扫描
        {
            sum+=num[i];
            if(sum>k)
            {
                i++;
                sum=0;
                ans++;
                if(num[i-1]>k)break;
            }
        }
        if(sum)   //sum有剩余
        {
            if(sum<=k)ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

​

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值