zoukankan      html  css  js  c++  java
  • poj 3273 Monthly Expense 夜

    http://poj.org/problem?id=3273
    给你每天的花费,让你分成m组 要求各组的和中的最大值越小越好
    二分查找
    #include<iostream>
    using namespace std;
    const int N=100001;
    int n,m;
    bool toosmall(int k,int money[])
    {
        int count=1;//k吧花费分成的组数,开始为一组
        int sum=0;
        for(int i=1;i<=n;++i)
        {
            if(sum+money[i]>k)//如果超过k 应增加一组,所以count加一 sum更新重计
            {
                sum=money[i];
                ++count;
            }
            else
            {
                sum=sum+money[i];
            }
        }
        if(count>m)//组数太多说明mid太小
        return true;
        return false;
    
    }
    int main()
    {
        while(cin>>n>>m)
        {
            int money[N];
            int high,low;
            high=0;//上界
            low=0;//下界
            for(int i=1;i<=n;++i)
            {
                cin>>money[i];
                low=max(low,money[i]);//最大的那个为下界
                high=high+money[i];//和为上界
            }
            int mid=(high+low)/2;
            while(low<high)
            {
                if(toosmall(mid,money))//如果mid太小
                {
                    low=mid+1;
                }
                else//mid太大 或者正好
                {
                    high=mid;
                }
                mid=(high+low)/2;
            }
            cout<<mid<<endl;
        }
        return 0;
    }
  • 相关阅读:
    4.20 每日一练
    4.19 每日一练
    4.18 每日一练
    Python函数初
    Python的文件操作
    python购物车
    python深浅拷贝,集合以及数据类型的补充
    Python 代码块 小数据池
    Python字典
    Python 列表操作
  • 原文地址:https://www.cnblogs.com/liulangye/p/2485688.html
Copyright © 2011-2022 走看看