• C


    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.

    对钱在最大值和总钱数之间取二分,但是不知道之前为什么一直wa,后面改了一下写法就过了很奇怪

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include <iomanip>
    #include<cmath>
    #include<string.h>
    #include<algorithm>
    #define sf scanf
    #define pf printf
    #define pb push_back
    #define mm(x,b) memset((x),(b),sizeof(x))
    #include<vector>
    #include<map>
    #define for(i,a,b) for(int i=a;i<b;i++)
    typedef long long ll;
    typedef long double ld;
    const ll mod=1e12+100;
    using namespace std;
    const double pi=acos(-1.0);
    int a[100005],n,m; 
    bool judge(int mid)
    {
    	int k=1,ans=0;
    	for(i,0,n)
    	{
    		if(a[i]+ans>mid)
    		{
    			k++;
    			ans=a[i];
    		}else
    		ans+=a[i];
    	}
    	return k<=m;
    }
    int main()
    {
    	//freopen("output1.txt", "r", stdin);
    	cin>>n>>m;
    	int sum=0,Max=0;
    	for(i,0,n)
    	{
    		sf("%d",&a[i]);
    		Max=max(Max,a[i]);
    		sum+=a[i];
    	}
    	if(m==1) 
    	{
    		cout<<sum;
    		return 0;
    	}
    	int left=Max,right=sum,mid;
    	while(left<right)
    	{
    		mid=(left+right)/2;
    		if(judge(mid))
    		right=mid;
    		else
    		left=mid+1;
    	}
    	while(!judge(left))
    	left++;
    	cout<<left;
    	return 0;
    }
    
  • 相关阅读:
    BAT脚本打印空行的使用方法
    Shell脚本关于屏幕输出的字符进行颜色控制的问题
    shell脚本中切换用户执行相应的命令或者shell脚本的方法
    Linux下执行的java命令重定向到文件中的方法
    解决Firefox浏览器每次打开都弹出导入向导的问题
    解决Ruby在IE11中报Unable to get browser (Selenium::WebDriver::Error::NoSuchWindowError)的错误
    6月28日 Django form组件 和 modelform组件
    6月27日 ajax
    6月25日 Django 分页 cookie、session
    6月26日 Django 中间件
  • 原文地址:https://www.cnblogs.com/wzl19981116/p/9381859.html
Copyright © 2020-2023  润新知