美文网首页
2020-09-17 砍树

2020-09-17 砍树

作者: JalorOo | 来源:发表于2020-09-17 21:09 被阅读0次

    https://www.luogu.com.cn/problem/P1873

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <sstream>
    #include <algorithm>
    #include <cstring>
    #include <vector>
    #include <queue>
    using namespace std;
    
    //template<typename DataType>
    //DataType qmi(DataType m, int k)
    //{
    //    DataType res = 1, t = m;
    //    while (k)
    //    {
    //        if (k&1) res = res * t;
    //        t = t * t;
    //        k >>= 1;
    //    }
    //    return res;
    //}
    
    
    int qmi(int m, int k)
    {
        int res = 1, t = m;
        while (k)
        {
            if (k&1) res = res * t;
            t = t * t;
            k >>= 1;
        }
        return res;
    }
    
    int read(){
        int x = 0,f = 1;
        char c = getchar();
        while (c<'0'||c>'9') {
            if (c=='-') {
                f = -1;
            }
            c = getchar();
        }
        while (c>='0'&&c<='9') {
            x = x*10+c-'0';
            c = getchar();
        }
        return x*f;
    }
    
    #define fi(a,b) for(int i = a; i < b; i++)
    #define fie(a,b) for(int i = a; i <= b; i++)
    #define fj(a,b) for(int j = a; j >= b; j--)
    
    struct priceAndCnt{
        int price,cnt;
    };
    
    void quickSort(priceAndCnt *a,int left,int right){
        int i,j;
        
        priceAndCnt temp,t;
        
        temp = a[(left+right)/2];//基准值
        
        i = left;
        j = right;
        
        while(i<=j){
            while (a[j].price > temp.price) {
                j--;
            }
            
            while (a[i].price < temp.price) {
                i++;
            }
            
            if (i<=j) {
                t = a[i];
                a[i] = a[j];
                a[j] = t;
                //继续下一步
                i++;
                j--;
            }
        }
        
        if(left<j)quickSort(a,left, j);//继续分治
        if(i<right)quickSort(a,i, right);//继续分治
    }
    
    
    int n;
    long long m;
    long long lef,rig = 0;
    long long mid;//看上面的解释啊
    int total,tim;//这个不用管,后面会提到的
    
    bool Judge(long long x,long long * a)
    {
        long long ans = 0;
        fie(1,n){//这里大家应该看得出,是在用贪心来看一看这个最大值是否能为x
            long long t = a[i] - x;
            if (t > 0) {
                ans += t;
            }
        }
        
        return ans < m;//返回结果}
    }
    
    int main()
    {
        cin>>n>>m;
        
        long long a[n];
        
        fie(1, n){
            cin>>a[i];
            rig=max(rig,a[i]);//找到最长木材
        }
        
        lef = 0;
        while (lef <= rig){//非递归式二分正常向写法,可理解为一般框架
            mid = (lef + rig) / 2;//这再看不出是啥意思可以退群了
            if ( Judge(mid,a) ){//是否小了
                rig = mid - 1;
            } else {
                lef = mid + 1;
            }
        }
        cout<<lef - 1<<endl;
        return 0;
    }
    /*
    5 20
    4 42 40 26 46
    ============
    36
    */
    

    相关文章

      网友评论

          本文标题:2020-09-17 砍树

          本文链接:https://www.haomeiwen.com/subject/uepryktx.html