题目描述
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());
}
}