zoukankan      html  css  js  c++  java
  • poj 3273 Monthly Expense (二分)

    //最大值最小
    //天数的a[i]值是固定的 不能改变顺序
    # include <algorithm>
    # include <string.h>
    # include <stdio.h>
    using namespace std;
    int n,m;
    int a[100010];
    int judge(int x)
    {
    	int ans=1;//分成了几组
    	int tmp=0;
    	for(int i=0;i<n;i++)
    	{
    		tmp+=a[i];
    		if(tmp>x)
    		{
    			tmp=a[i]; //前i-1天小于等于m,把前i-1天作为一组,第i天作为下一组的第一天  
    			ans++;
    		}
    	}	
    	if(ans>m)//大于m组,mid太小
    		return false;
    	return true;
    }
    int main()
    {
    
    	int i,sum,low;
    	while(~scanf("%d%d",&n,&m))
    	{
    		sum=0;
    		low=0;
    		for(i=0;i<n;i++)
    		{
    			scanf("%d",&a[i]);
    			sum+=a[i];
    			if(low<a[i])
    				low=a[i];
    		}
    		int l=low;//最小边界为单个里最大的
    		int r=sum;
    		int mid;
    		while(l<=r)
    		{
    			 mid=(l+r)/2;
    			if(judge(mid))
    				r=mid-1;			
    			else
    				l=mid+1;
    		}
    		printf("%d
    ",l);
    	}
    	return 0;
    }

  • 相关阅读:
    [面试题]什么是面向对象编程
    面向对象编程的新手理解
    Object of type type is not JSON serializable
    STL---map
    STL---llist
    Div标签使用inline-block有间距
    STL---vector
    KMP算法
    算法06
    算法05
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4311963.html
Copyright © 2011-2022 走看看