zoukankan      html  css  js  c++  java
  • POJ 3273:Monthly Expense 二分好题啊啊啊啊啊啊

    Monthly Expense
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 19207   Accepted: 7630

    Description

    Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over the next N (1 ≤ N ≤ 100,000) days.

    FJ wants to create a budget for a sequential set of exactly M (1 ≤ M ≤ N) fiscal periods called "fajomonths". Each of these fajomonths contains a set of 1 or more consecutive days. Every day is contained in exactly one fajomonth.

    FJ's goal is to arrange the fajomonths so as to minimize the expenses of the fajomonth with the highest spending and thus determine his monthly spending limit.

    Input

    Line 1: Two space-separated integers: N and M 
    Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day

    Output

    Line 1: The smallest possible monthly limit Farmer John can afford to live with.

    Sample Input

    7 5
    100
    400
    300
    100
    500
    101
    400

    Sample Output

    500

    Hint

    If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.

    题意是给出一系列数,将这些数按顺序分成M组,求每组和的最小值是多少。

    枚举答案,left是一个数一个组,即最大值。right是所有数的和。

    每次去和M比较来判断mid是多是少。

    代码:

    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <string>
    #include <cstring>
    #pragma warning(disable:4996)
    using namespace std;
    
    int N,M;
    int value[100000];
    
    int solve(int mid)
    {
    	int i,sum=0,result=1;
    	if(mid==501)
    		i=1;
    	for(i=1;i<=N;i++)
    	{
    		if(value[i]+sum<=mid)
    		{
    			sum += value[i];
    		}
    		else
    		{
    			sum=value[i];
    			result++;
    		}
    	}
        if(result>M)
    	{
    		return 1;
    	}
    	else
    		return -1;
    }
    
    int main()
    {
    	int i,sum,max_x;
    	cin>>N>>M;
    
    	sum=0;
    	max_x=0;
    	for(i=1;i<=N;i++)
    	{
    		cin>>value[i];
    		sum += value[i];
    		max_x=max(max_x,value[i]);
    	}
    	
    	int mid=(sum+max_x)/2;
    	while(max_x<sum)
    	{
    		int temp=solve(mid);
    		if(solve(mid)==-1)
    		{
    			sum = mid-1;
    		}
    		else
    		{
    			max_x = mid+1;
    		}
    		mid = (max_x+sum)/2;
    	}
    
    	cout<<mid<<endl;
    	return 0;
    }


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    jQuery Mobile-jquery Mobile 怎么用ajax提交表单
    Passing data between pages in JQuery Mobile | Ram's Blog
    jQuery Mobile Docs
    jquery mobile header title左对齐 button右对齐
    jquery mobile -role
    jQueryMobile$(document).ready 等价于?( jQuery mobi...
    HTML5,jQuery Mobile 可视化生成器 Tiggzi
    WebApi系列~开放的CORS,跨域资源访问对所有人开放
    WebApi系列~目录
    WebApi系列~基于单请求封装多请求的设计~请求的安全性设计与实现
  • 原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4928113.html
Copyright © 2011-2022 走看看