POJ 3273Monthly Expense每月的费用

本文介绍了一种通过二分查找法解决预算分配问题的算法。该算法旨在将连续的天数划分为若干组,使得各组总花费最小,并输出最高花费的一组作为预算上限。文章提供了详细的代码实现,包括边界条件处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

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.

给出农夫在n天中每天的花费,要求把这n天分作m组,每组的天数必然是连续的,要求分得各组的花费之和应该尽可能地小,最后输出各组花费之和中的最大值

输入输出格式

输入格式:

 

 

 

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

 

输出格式:

 

 

 

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

 

输入输出样例

输入样例#1: 复制

7 5
100
400
300
100
500
101
400

输出样例#1: 复制

500

 

 

 

说明

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.

裸的二分:

#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<cctype>  
#include<cmath>  
#include<iostream>  
#include<sstream>  
#include<iterator>  
#include<algorithm>  
#include<string>  
#include<vector>  
#include<set>  
#include<map>  
#include<stack>  
#include<deque>  
#include<queue>  
#include<list>  
using namespace std;  
const double eps = 1e-8;  
typedef long long LL;  
typedef unsigned long long ULL;  
const int INT_INF = 0x3f3f3f3f;  
const int INT_M_INF = 0x7f7f7f7f;  
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;  
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;  
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};  
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};  
const int MOD = 1e9 + 7;  
const double pi = acos(-1.0);  
const int MAXN = 1e5 + 10;  
const int MAXT = 10000 + 10;  
#define N 100005
int n,m;
long long a[N];
int cal(long long mid)
{
	long long sum=0;
	int x=0;
	for(int i=1;i<=n;i++)
	{
		sum+=a[i];
		if(sum>mid)
		{
			if(sum-a[i]!=0)   //这里也可以直接写sum=a[i]
                                         //需要考虑如果一个值大于mid
				i--;
			x++;
			sum=0;
			
		}
	
	}
	if(sum!=0) x++;        //这里考虑最后一个值,如果上面sum=a[i]这里也省去了,
       
	return x;
}
int main()  
{  
      
    while(scanf("%d%d",&n,&m)!=EOF)  
    {  
    	long long sum=0,maxx=0;
	     for(int i=1;i<=n;i++)
	     	{
	     		scanf("%lld",&a[i]);
	     	sum+=a[i];
	     	maxx=max(a[i],maxx);
	     	}
	     	long long l=maxx,r=sum,mid,ans;
	     	while(l<=r)
			{
				mid=(l+r)/2;
				
				  
				int x=cal(mid);
				
				 if(m>=x)     //表示分少了,mid值偏大
				 {      
				 	 r=mid-1;
                     ans=mid;
				 }
				 else 
				 {
				 	 l=mid+1;
				 	
				 }
				 
			}
			printf("%lld\n",ans);
		 
    }  
    return 0;  
      
}  
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main {
	public static void main(String[] args) throws IOException {
		Scanner in = new Scanner (new BufferedInputStream(System.in));
		//Scanner in = new Scanner(new BufferedInputStream(System.in));
	    Reader.init(System.in);
	    int n=in.nextInt();
	    int m=in.nextInt();
	    
	    int [] a=new int[n+5];
	    int sum=0;
	    int max=0;
	   for(int i=1;i<=n;i++)
	    {
	    	a[i]=in.nextInt();
	    	max=Math.max(a[i], max);
	    	sum+=a[i];
	    }
	   
	   //Arrays.sort(a,1,n+1);
	   int l=max,r=sum+1;///注意下界范围,如果从0开始的话下面统计函数需要重新考虑一下
	   while(l<r)
	   {
		   int mid=(l+r)>>1;
	       if(f(a,mid,n)<=m)
              r=mid;
	       else
	    	  l=mid+1;
	   }
	   System.out.println(l);
	}

	private static int f(int[] a, int mid, int n) {
		// TODO 自动生成的方法存根
		int sum=0;
		int temp=0;
		for(int i=1;i<=n;i++)
		{
			if(temp+a[i]<=mid)
			{
				//if(mid==4)
				//System.out.println(pos+"    "+a[i]);
				temp+=a[i];
			}
			else
			{
				sum++;
				temp=a[i];
			}
		}
		sum++;
		//System.out.println(mid+" "+sum);
		return sum;
	}		
}
class Reader 
{ 
    static BufferedReader reader; 
    static StringTokenizer tokenizer; 
 
    static void init(InputStream input) 
    { 
        reader = new BufferedReader(new InputStreamReader(input)); 
        tokenizer = new StringTokenizer(""); 
    } 
 
    static String next() throws IOException 
    { 
        while(!tokenizer.hasMoreTokens()) 
        { 
            String str=reader.readLine();
            //System.out.println(str);
            tokenizer = new StringTokenizer(str); 
        } 
        return tokenizer.nextToken(); 
    } 
 
    static int nextInt() throws  IOException 
    { 
        return Integer.parseInt(next()); 
    } 
 
    static double nextDouble() throws IOException 
    { 
        return Double.parseDouble(next()); 
 
    } 
} 

 

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main {
	public static void main(String[] args) throws IOException {
		Scanner in = new Scanner (new BufferedInputStream(System.in));
		//Scanner in = new Scanner(new BufferedInputStream(System.in));
	    Reader.init(System.in);
	    int n=in.nextInt();
	    int m=in.nextInt();
	    
	    int [] a=new int[n+5];
	    int sum=0;
	    int max=0;
	   for(int i=1;i<=n;i++)
	    {
	    	a[i]=in.nextInt();
	    	max=Math.max(a[i], max);
	    	sum+=a[i];
	    }
	   
	   //Arrays.sort(a,1,n+1);
	   int l=0,r=sum+1;
	   while(l<r)
	   {
		   int mid=(l+r)>>1;
	       if(f(a,mid,n)<=m)
              r=mid;
	       else
	    	  l=mid+1;
	   }
	   System.out.println(l);
	}

	private static int f(int[] a, int mid, int n) {
		// TODO 自动生成的方法存根
		int sum=0;
		int temp=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i]>mid)
				return (1<<30);
			if(temp+a[i]<=mid)
			{
				//if(mid==4)
				//System.out.println(pos+"    "+a[i]);
				temp+=a[i];
			}
			else
			{
				sum++;
				temp=a[i];
			}
		}
		sum++;
		//System.out.println(mid+" "+sum);
		return sum;
	}		
}
class Reader 
{ 
    static BufferedReader reader; 
    static StringTokenizer tokenizer; 
 
    static void init(InputStream input) 
    { 
        reader = new BufferedReader(new InputStreamReader(input)); 
        tokenizer = new StringTokenizer(""); 
    } 
 
    static String next() throws IOException 
    { 
        while(!tokenizer.hasMoreTokens()) 
        { 
            String str=reader.readLine();
            //System.out.println(str);
            tokenizer = new StringTokenizer(str); 
        } 
        return tokenizer.nextToken(); 
    } 
 
    static int nextInt() throws  IOException 
    { 
        return Integer.parseInt(next()); 
    } 
 
    static double nextDouble() throws IOException 
    { 
        return Double.parseDouble(next()); 
 
    } 
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值