Honk's pool(二分模板题)

题意:有n个水池,每个水池有a[i]单位水,有k次操作,每次操作将水量最多的水池减少一单位水,水量最少的水池增加一单位水,问最后水量最大的水池和水量最少的水池相差的水量。

思路:二分最后的最大水量和最小水量,特别的,模拟一下可以发现如果总水量sum%n==0,则最大值的下界和最小值的上界均为sum/n,若sum%n!=0,则最大值的下界为sum/n+1,最小值上界为sum/n。二分时注意选取区间是左闭右开还是左开右闭。

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
const int maxn=5e5+5;
ll a[maxn];
ll n,k;
bool check1(ll p){
    ll res=0;
    for(int i=1;i<=n;i++)
        if(a[i]>p) res+=a[i]-p;
    if(res<=k) return 1;
    else return 0;
}
bool check2(ll p){
    ll res=0;
    for(int i=1;i<=n;i++)
        if(a[i]<p) res+=p-a[i];
    if(res<=k) return 1;
    else return 0;
}
int main(){
    while(~scanf("%lld%lld",&n,&k) ){
        ll sum=0;
        for(int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
            sum+=a[i];
        }
        ll l,r,mid;//左开右闭区间
        r=1e9+9;
        if(sum%n==0)
            l=sum/n-1;
        else l=sum/n;
        while(r-l>1){
            mid=(r+l+1)/2;
            if(check1(mid))
                r=mid;
            else l=mid;
        }
        ll up=r;//>=k
        l=0;r=sum/n+1;//左闭右开
        while(r-l>1){
            mid=(r+l+1)/2;
            if(check2(mid))
                l=mid;
            else r=mid;
        }
        ll down=l;
        printf("%lld\n",up-down);
    }
}

Honk's pool(二分模板题)

原文地址:https://www.cnblogs.com/ucprer/p/11544522.html

时间: 2024-08-30 02:38:14

Honk's pool(二分模板题)的相关文章

Aizu ITP2_6_A(二分模板)

For a given sequence $A = \{a_0, a_1, ..., a_{n-1}\}$ which is sorted by ascending order, find a specific value $k$ given as a query. Input The input is given in the following format. $n$ $a_0 \; a_1 \; ,..., \; a_{n-1}$ $q$ $k_1$ $k_2$ : $k_q$ The n

hdu 2255 二分图带权匹配 模板题

模板+注解在 http://blog.csdn.net/u011026968/article/details/38276945 hdu 2255 代码: //KM×î´ó×îСƥÅä #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define INF 0x0fffffff const int MAXN

HDU2063(二分匹配入门模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9322    Accepted Submission(s): 4108 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求

poj 3041Asteroids 二分匹配求最小点覆盖模板题

//最大匹配=最小覆盖 //这题是求最小覆盖点的模板题 #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 510; int line[maxn][maxn]; int match[maxn]; int vis[maxn]; int N , K; int find(int start) { for(int i = 1;i <=  N;

poj 3692 Kindergarten (最大团模板题)

题目链接:http://poj.org/problem?id=3692 Kindergarten Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5156   Accepted: 2512 Description In a kindergarten, there are a lot of kids. All girls of the kids know each other and all boys also know e

HDU 4347 - The Closest M Points - [KDTree模板题]

本文参考: https://www.cnblogs.com/GerynOhenz/p/8727415.html kuangbin的ACM模板(新) 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4347 Problem Description The course of Software Design and Development Practice is objectionable. ZLC is facing a serious problem

The Preliminary Contest for ICPC Asia Shenyang 2019 F. Honk&#39;s pool

题目链接:https://nanti.jisuanke.com/t/41406 思路:如果k的天数足够大,那么所有水池一定会趋于两种情况: ① 所有水池都是一样的水位,即平均水位 ② 最高水位的水池和最低水位的水池高度只相差一个高度,且最低水位一定是平均水位 如果k给了个限制: 我们当然需要先算出所有水池高度的平均值. 然后从低到高排序,二分小于平均值的水位,二分高于平均值的水位, 然后判断二分的预期值需要的天数是否小于等于k.然后二分找出最低水位的最大值, 最高水位的最小值,两者相减就是答案了

hdu 2966 In case of failure kdtree模板题

问求每个点距离平方的最小的点 kd-tree模板题…… 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a)) 3 #define debug(x) cerr<<#x<<"=="<<(x)<<endl 4 using namespace std; 5 typedef long long ll; 6 typedef pair<int,int>

几道树剖模板题

寒假后半段一直都在外出旅游..颓了好久..qaq 旅游期间写了几道树剖模板题,贴上来.. BZOJ 1036 没啥好说的,裸题 1 #include <cstdio> 2 #include <algorithm> 3 4 #define LEFT (segt[cur].l) 5 #define RIGHT (segt[cur].r) 6 #define MID (segt[cur].mid) 7 #define SUM (segt[cur].Sum) 8 #define MAX (