zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    C语言 · 选择排序
    C语言 · 生物芯片
    C语言 · 猜灯谜
    C语言 · x的x次幂结果为10
    C语言 · LOG大侠
    C语言 · 成绩查询系统
    C语言 · C++中map的用法详解
    C语言 · 方程的解
    斯坦福大学公开课:监督学习应用,梯度下降
    斯坦福大学公开课:机器学习的动机与应用
  • 原文地址:https://www.cnblogs.com/wzl19981116/p/9381859.html
Copyright © 2011-2022 走看看