POJ-3273 Monthly Expense---最小化最大值

题目链接:

https://cn.vjudge.net/problem/POJ-3273

题目大意:

给N个数,划分为M个块(不得打乱数顺序)。找到一个最好的划分方式,使得块的和的最大值 最小

解题思路:

首先是最大值最小

写出二分模板(需要确定上下界)

然后根据二分模板写chack函数

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 typedef long long ll;
 5 const int maxn = 1e6 + 10;
 6 int n, m;
 7 int a[maxn];
 8 bool judge(int x)
 9 {
10     int tot = 0, cnt = 1;
11     for(int i = 1; i <= n; i++)
12     {
13         if(tot + a[i] > x)
14         {
15             tot = a[i];
16             cnt++;
17         }
18         else tot += a[i];
19     }
20     //cout<<x<<":::"<<cnt<<endl;
21     return cnt <= m;
22 }
23 int main()
24 {
25     while(cin >> n >> m){
26     int l = 0, r = 0, ans;
27     for(int i = 1; i <= n; i++)
28         scanf("%d", &a[i]), r += a[i], l = max(l, a[i]);
29     //l 和 r的范围必须确定好
30
31     while(l <= r)
32     {
33         int mid = (l + r) / 2;
34         if(judge(mid))
35             ans = mid, r = mid - 1;
36         else l = mid + 1;
37     }
38     cout<<ans<<endl;
39     }
40     return 0;
41 }

原文地址:https://www.cnblogs.com/fzl194/p/9021455.html

时间: 2024-08-06 20:35:00

POJ-3273 Monthly Expense---最小化最大值的相关文章

[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

题目传送门 1 /* 2 题意:分成m个集合,使最大的集合值(求和)最小 3 二分搜索:二分集合大小,判断能否有m个集合. 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 1e5 + 10; 12 const int INF = 0x3f3f

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

POJ3273-Monthly Expense (最小化最大值)

题目链接:cid=80117#problem/E">click here~~ [题目大意] 农夫JF在n天中每天的花费,要求把这n天分作m组.每组的天数必定是连续的.要求分得各组的花费之和应该尽可能地小,最后输出各组花费之和中的最大值 [解题思路]: 经典的最小化最大值问题,要求连续的m个子序列,子序列的和最大值的最小,枚举满足条件的m的最小值即为答案.因此二分查找. 1.能否把序列划分为每一个序列之和不大于mid的m个子序列, 2.通过用当前的mid值能把天数分成几组, 3.比較mid和

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(二分)

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

poj 3273 Monthly Expense

题目链接:http://poj.org/problem?id=3273 题目大意:给你n个数把他们连续的分成m组,问最小的那一组的最大值. 思路:用最小的那一个元素和总和进行二分...然后判断最小那组为mid时,分组的个数. #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <cmath> using namespace s

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 <

poj 3273 Monthly Expense(二分搜索之最大化最小值)

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 over the

POJ 3273 Monthly Expense 【二分答案】

题意:给出n天的花费,需要将这n天的花费分成m组,使得每份的和尽量小,求出这个最小的和 看题目看了好久不懂题意,最后还是看了题解 二分答案,上界为这n天花费的总和,下界为这n天里面花费最多的那一天 如果mid>=m,说明mid偏小,l=mid+1, 如果mid<m,说明mid偏大,r=mid, 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath>