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组数据,因要取最小的mid所以继续取左区间直都l=r;

#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
ll n,m,a[100005];
ll r,l,mid;
bool judge(ll d){
    ll t=0,sum=a[0];
    for(int i=1;i<n;i++){
     if(sum>d) return 1;
     sum+=a[i];
     if(sum>d){
        sum=a[i];
        t++;
     }
    }
    if(sum) t++;
    return t>m;
}
int bfind(){
while(l<r){
    mid=(l+r)/2;
    if(judge(mid)) l=mid+1;
    else  r=mid;
}
return l;
}
int main(){
    while(cin>>n>>m){
    r=0,l=1<<30;
    for(int i=0;i<n;i++){
    scanf("%d",&a[i]);
    r+=a[i];
    l=min(l,a[i]);
    }
    cout<<bfind()<<endl;
    }
return 0;
}

  

原文地址:https://www.cnblogs.com/WELOTX/p/11376617.html

时间: 2024-10-26 11:40:08

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

POJ 3273 Monthly Expense 二分答案

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

POJ 3273 Monthly Expense (二分枚举)

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

poj 3273 Monthly Expense 二分

题目链接:http://poj.org/problem?id=3273 因为规定天数连续 所以从前到后贪心即可 裸二分 二分专题的特点: 1.很容易写 2.很容易写错 所以要注意: 上下界的取值是否太大太小 加起来会不会爆 最后能不能跳出循环 当对中值进行判定以后 应该如何调整lb和ub 注意lb那个地方应该赋值为mid+1 否则会T到死 #include <cstdio> #include <cstdlib> #include <ctime> #include <

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=

[ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)

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

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

BZOJ 1639: [Usaco2007 Mar]Monthly Expense 月度开支( 二分答案 )

直接二分答案然后判断. ----------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define rep( i , n ) for( int i = 0 ;  i < n ; ++i ) #define clr

poj3273——经典的二分优化

poj3273——经典的二分优化 Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16522   Accepted: 6570 Description Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already