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

來源:互聯網
上載者:User

標籤:codeforce   tmp   signed   const   nbsp   owb   force   names   ring   

題意: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<string>//#include<stack>#include<queue>#include<deque>#include<set>#include<vector>#include<map>#include<functional>    #define fst first#define sc second#define pb push_back#define mem(a,b) memset(a,b,sizeof(a))#define lson l,mid,root<<1#define rson mid+1,r,root<<1|1#define lc root<<1#define rc root<<1|1#define lowbit(x) ((x)&(-x)) using namespace std;typedef double db;typedef long double ldb;typedef long long ll;typedef unsigned long long ull;typedef pair<int,int> PI;typedef pair<ll,ll> PLL;const db eps = 1e-6;const int mod = 100003;const int maxn = 2e5+100;const int maxm = 2e5+100;const ll inf = 0x3f3f3f3f3f3f3f3f;const db pi = acos(-1.0);int nh[maxn];int main() {    int n;ll m;    scanf("%d %lld", &n, &m);    int vol = 0;    for(int i = 1; i <= n; i++){        int c;        scanf("%d", &c);        nh[0]++;nh[c+1]--;            }    for(int i = 1; i <= maxn; i++){        nh[i]+=nh[i-1];    }    int tmp = 0;    int ans = 0;    for(int i = maxn; i >= 0; i--){        if(nh[i]==n)break;        if(tmp+nh[i]>m){            tmp=nh[i];            ans++;        }        else tmp+=nh[i];    }    if(tmp>0)ans++;    printf("%d", ans);    return 0;}

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.