F - Make It Equal CodeForces - 1065C

题目大意:有n座塔,塔高h[i],每次给定高度H对他们进行削切,要求每次削掉的所有格子数不能超过k个,输出最少削几次才能使所有塔的高度相同。

思路一:差分+贪心

对于每一个高度h,用一个数组让1~h的数,每一个都加一。用差分求一下后缀和可以完成。

AC code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=2E5+7;
ll arr[N];
int main()
{
    ll n,k;
    cin>>n>>k;
    ll x;
    ll c=0;
    for(ll i=1;i<=n;i++){
        cin>>x;
        c=max(x,c);
        arr[x]++;
    }
    for(ll i=c;i>=1;i--){
        arr[i]+=arr[i+1];
    }
    ll sum=0;
    ll cnt=0;
    for(ll i=c;i>=1;i--){
        if(arr[i]==n){
            if(cnt!=0){
                sum++;
                break;
            }
            else break;
        }
        cnt+=arr[i];
        if(cnt>k){
            cnt=arr[i];
            sum++;
        }
    }
    cout<<sum<<endl;
    return 0;
}

用线段树加二分也可以过:https://blog.csdn.net/Amovement/article/details/83449446

原文地址:https://www.cnblogs.com/Accepting/p/12250745.html

时间: 2024-08-29 21:24:20

F - Make It Equal CodeForces - 1065C的相关文章

CF-1328 F. Make k Equal

F. Make k Equal 题目链接 题意 长度为n的序列,每次可以选择一个最大的数字将其减一或者选择一个最小的数字将其加一,问最少操作多少次可以使得序列中至少存在 k 个一样的数字 分析 官方题解:http://codeforces.com/blog/entry/75246 可以想到最后一样的数字,一定是在原序列里面出现的,所以将原数组离散化之后,枚举最后一样的数字,并努力把它凑够 k 个.如何凑?借助左侧或者右侧的数字.只要借助了某侧的数字,那么这一侧全部的数字都要先挪动它旁边的那个位置

Codeforces Round #629 (Div. 3) F - Make k Equal (离散化 树状数组维护前缀和)

https://codeforces.com/contest/1328/problem/F 首先把a数组处理成pair对(num,cnt),表示数字num有cnt个,然后按num升序排序离散化一下. 对于一个数x,若想使得小于x的数字都变成x,必须先把所有小于x的数变成x-1,然后再+1变成x. 同理,要使得大于x的数变成x,必须把所有大于x的数字变成x+1,然后再-1变成x. 以上是题意所要求的必须操作. 思路: 1. 用f[i]数组记录离散化后前i大的数字的总数,那么对于任意第i大数字,可以

Codeforces 1065C Make It Equal (差分+贪心)

题意:n个塔,第i个塔由$h_i$个cube组成,每次可以切去某高度h以上的最多k个cube,问你最少切多少次,可以让所有塔高度相等 k>=n, n<=2e5 思路:差分统计每个高度i有的方块数nh[i],然后从高到低贪心的切就行了 代码: #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include

F - Lost Root Shows CodeForces - 1061F(交互+概率)

Lost Root time limit per test 3 seconds memory limit per test 256 megabytes Problem Description The graph is called tree if it is connected and has no cycles. Suppose the tree is rooted at some vertex. Then tree is called to be perfect kk-ary tree if

F - Restoring the Expression CodeForces - 898F

字符串hash:  base设置为10 枚举'='可能出现的位置,从1/2处开始到大概1/3处结束,当然大概的1/3不用计算,直接到最后就行,因为本题必然有解,输出直接结束即可. 根据'='号位置,'+'最多有四种位置,因为 等式的和位数确定,有进位和不进位,左和右,最多2X2,然后剪掉j的非法位置(这里没计算除了len=3以外的j有无非法位置的可能,剪了再说) 比较需要注意的地方是等式中非个位数字不能以'0'开头,开始只以为不以'0'开头即可,WA在了 1+0=0 ,改了j后又WA了0+0=0

F - Link/Cut Tree CodeForces - 614A(水题)

Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure. Unfortunately, Rostislav is unable to understand the definition of this procedure

Educational Codeforces Round 76 F 折半枚举

Educational Codeforces Round 76 F 折半枚举 https://codeforces.com/contest/1257/problem/F 题意: 数组a,找到一个x使得a中每一个元素异或x后"二进制中1的个数"相同. 数组长度100,数字大小2^30. 思路: 折半枚举答案X,如分为X前15位和后15位.之后我们再枚举期望的"相同个数"是多少,通过hash看看能不能满足就好了. 代码: #include <bits/stdc++

Codeforces Round #619 (Div. 2) Ayoub&#39;s function

Ayoub thinks that he is a very smart person, so he created a function f(s)f(s) , where ss is a binary string (a string which contains only symbols "0" and "1"). The function f(s)f(s) is equal to the number of substrings in the string s

Scalaz(4)- typeclass:标准类型-Equal,Order,Show,Enum

Scalaz是由一堆的typeclass组成.每一个typeclass具备自己特殊的功能.用户可以通过随意多态(ad-hoc polymorphism)把这些功能施用在自己定义的类型上.scala这个编程语言借鉴了纯函数编程语言Haskell的许多概念.typeclass这个名字就是从Haskell里引用过来的.只不过在Haskell里用的名称是type class两个分开的字.因为scala是个OOP和FP多范畴语言,为了避免与OOP里的type和class发生混扰,所以就用了typeclas