poj3273 - Monthly Expense

这是一个二分穷举问题,通过二分寻找最小值,不断枚举,但一定要二分结束,否则只要找到m组就结束,不一定是最优解。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=100000+10;
int a[maxn];
int m,n;
bool judge(int mid)
{   int g=1,sum=0;
    for(int i=1;i<=n;i++)
    {    if(sum+a[i]<=mid)
              sum+=a[i];
         else {sum=a[i],g++;}//一组总和大于mid,重新分组,组数加一
    }
    if(g<=m) return true;
    else return false;
}
int main()
{
    int r=0,l=0;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {    scanf("%d",&a[i]);
         if(l<a[i]) l=a[i];//记录max,最为二分最左边的值,因为分组后的最小值必定大于单个的最大值
        r+=a[i];//取总和作为最右边的值
    }
    int mid;
    while(l<r)
    {   mid=(l+r)/2;//此处的mid就是一组的最大值
        if(judge(mid))
        r=mid-1;//能找到<=m组,去取更小的右边界,看能不能找到更小的mid
        else
            l=mid+1;//超过m组,扩大左边界,找够m组

    }
    printf("%d\n",mid);

    return 0;
}
时间: 2024-10-29 19:11:50

poj3273 - Monthly Expense的相关文章

POJ3273 Monthly Expense —— 二分

题目链接:http://poj.org/problem?id=3273 Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29231   Accepted: 11104 Description Farmer John is an astounding accounting wizard and has realized he might run out of money to run the

poj3273 Monthly Expense 二分查找

题目链接 题意:给出n组数据,每组有一个数,有多种方法通过将连续的几组数据并为一组使得组数从n减少至k,每组数据的值为其组中数据之和,每种方法得到的k组数据都有最大的一组数据和m,求这些方法中最小的m 解法:二分查找 以n组中最小的数为左边界l,n组数据之和为右边界r进行二分查找,取中间值mid=(l+r)/2,mid即为k组数据中最大的一组数据和,若mid可将n组数据分成多于k组数据,则mid偏小取右间, 若可将n组数据分成小于k组数据,则mid偏小取左区间,若恰好可分成k组数据,因要取最小的

1639: [Usaco2007 Mar]Monthly Expense 月度开支

1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 593  Solved: 295[Submit][Status] Description Farmer John是一个令人惊讶的会计学天才,他已经明白了他可能会花光他的钱,这些钱本来是要维持农场每个月的正常运转的.他已经计算了他以后N(1<=N<=100,000)个工作日中每一天的花费moneyi(1<=money

[BZOJ] 1639: [Usaco2007 Mar]Monthly Expense 月度开支

1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1077  Solved: 533[Submit][Status][Discuss] Description Farmer John是一个令人惊讶的会计学天才,他已经明白了他可能会花光他的钱,这些钱本来是要维持农场每个月的正常运转的.他已经计算了他以后N(1<=N<=100,000)个工作日中每一天的花费moneyi(1

[BZOJ1639][Usaco2007 Mar]Monthly Expense 月度开支

1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1069  Solved: 530 [Submit][Status][Discuss] Description Farmer John是一个令人惊讶的会计学天才,他已经明白了他可能会花光他的钱,这些钱本来是要维持农场每个月的正常运转的.他已经计算了他以后N(1<=N<=100,000)个工作日中每一天的花费moneyi

POJ 3273 Monthly Expense(二分)

Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17465   Accepted: 6961 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 r

Monthly Expense

Problem 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

Divide and conquer:Monthly Expense(POJ 3273)

Monthly Expense 题目大意:不废话,最小化最大值 还是直接套模板,不过这次要注意,是最小化最大值,而不是最大化最小值,判断的时候要注意 联动3258 1 #include <iostream> 2 #include <functional> 3 #include <algorithm> 4 5 using namespace std; 6 7 static int money_set[100010]; 8 9 void Search(const int,

BZOJ【1639】: [Usaco2007 Mar]Monthly Expense 月度开支

1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 700  Solved: 347[Submit][Status][Discuss] Description Farmer John是一个令人惊讶的会计学天才,他已经明白了他可能会花光他的钱,这些钱本来是要维持农场每个月的正常运转的.他已经计算了他以后N(1<=N<=100,000)个工作日中每一天的花费moneyi(1&