cf Educational Codeforces Round 52 C. Make It Equal

原题:
C. Make It Equal
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There is a toy building consisting of n towers. Each tower consists of several cubes standing on each other. The i-th tower consists of hicubes, so it has height hi.Let’s define operation slice on some height H as following: for each tower i, if its height is greater than H, then remove some top cubes to make tower’s height equal to H. 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 k (k≥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 n and k (1≤n≤2⋅10^5, n≤k≤10 ^9) — the number of towers and the restriction on slices, respectively.
The second line contains n space separated integers h1,h2,…,hn (1≤hi≤2⋅10^5) — 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
5 5
3 1 2 2 4
output
2
input
4 5
2 3 4 5
output
2
Note
In the first example it’s optimal to make 2 slices. The first slice is on height 2 (its cost is 3), and the second one is on height
1 (its cost is 4).

中文:
给你一堆由正方形叠成的长条,每个长条的高度是hi,表示有hi个正方形叠成,现在给你一个操作,这个操作是选定一个高度,可以去掉这个高度以上的所有方形,但是这些方形的数量不能超过k,问你最少操作多少次,能使得所有长条高度相同。

代码:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
ll n,k;
ll h[200001];
ll sum[200001];
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>k)
    {
        memset(sum,0,sizeof(sum));
        for(int i=0;i<n;i++)
            cin>>h[i];
        sort(h,h+n);

        ll Max=h[n-1];

        for(int i=1;i<=Max;i++)
        {
            sum[i]=n-(ll)(lower_bound(h,h+n,i)-h);
        }
        if(sum[Max]==n)
        {
            cout<<0<<endl;
            continue;
        }
        ll Min=1;
        for(ll i=Max;i>=1;i--)
        if(sum[i]==n)
        {
            Min=i;
            break;
        }
        ll ans=0,tmp=0;
        int flag=0;

        for(int i=Max;i>Min;)
        {
            if(tmp+sum[i]<=k)
            {
                tmp+=sum[i];
                i--;
            }
            else
            {
                flag=1;
                tmp=0;
                ans++;
            }
        }

        if(tmp!=0)
            ans++;
        cout<<ans<<endl;
    }
    return 0;
}

解答:

设定数组sum[i],表示高度大于等于i的条有多少个,那么可以对所有条按照高度进行排序,然后枚举高度i,二分查找高度i所在的下标,用总条数n减去i就是有多少个高度大于等于i的条了。

然后依次贪心的去拿去不超过k个正方形,直到所有条的高度都相同为止。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值