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, const int, const int);
10 bool C(const int, const int, const int);
11
12 int main(void)
13 {
14     int days, set_sum;
15     while (~scanf("%d%d", &days, &set_sum))
16     {
17         for (int i = 0; i < days; i++)
18             scanf("%d", &money_set[i]);
19         Search(days, set_sum, 100000);
20     }
21     return 0;
22 }
23
24 void Search(const int days, const int set_sum,const int max_m)
25 {
26     int lb = 0, rb = max_m, mid;
27
28     while (rb - lb > 1)
29     {
30         mid = (rb + lb) / 2;
31         if (C(mid, set_sum, days))//二分逼近,最后的rb即为所求
32             rb = mid;
33         else
34             lb = mid;
35     }
36     printf("%d\n", rb);
37 }
38
39 bool C(const int x, const int set_sum, const int days)
40 {
41     //这次是最小化最大值,注意变通
42     int tmp_m, pos = 0, uesd;
43
44     for (uesd = 0; pos < days && uesd < set_sum; uesd++)
45     {
46         for (tmp_m = 0; pos < days && money_set[pos] + tmp_m <= x; pos++)
47         {
48             tmp_m += money_set[pos];
49             if (x < money_set[pos])
50                 return false;
51         }
52     }
53     if (uesd < set_sum ||(uesd == set_sum && pos == days))
54         return true;
55     else return false;
56 }

时间: 2024-11-10 01:03:07

Divide and conquer:Monthly Expense(POJ 3273)的相关文章

Divide and conquer:Telephone Lines(POJ 3662)

电话线 题目大意:一堆电话线要你接,现在有N个接口,总线已经在1端,要你想办法接到N端去,电话公司发好心免费送你几段不用拉网线,剩下的费用等于剩余最长电话线的长度,要你求出最小的费用. 这一看又是一个最小化最大值的问题(也可以看成是最大化最小值的问题),常规方法一样的就是把这个费用二分就好,但是这道题是道图论题,不一定经过所有的点,那我们就以二分基准长度为界限,把小于基准长度的那一部分看成是0,大于等于基准长度的看成是1,这样我们只用SPFA算法算最短路径就可以了,非常的巧妙 参考:http:/

Divide and conquer:Aggressive Cows(POJ 2456)

侵略性的牛 题目大意:C头牛最大化他们的最短距离 常规题,二分法即可 1 #include <iostream> 2 #include <algorithm> 3 #include <functional> 4 5 using namespace std; 6 7 static int pos[100000]; 8 9 bool judge(const int, const int,const int); 10 void solve(const int, const i

Divide and Conquer:River Hopscotch(POJ 3258)

 去掉石头 题目大意:一群牛在河上的石头上跳来跳去,现在问你如何通过去掉M个石头,使得牛跳过石头的最短距离变得最大? 这一题比较经典,分治法的经典,二分法可以很方便处理这个问题,我们只要明白比较函数这个东西就可以了. 模板: while (……) { mid = (lb + rb) / 2; if (Judge_C(……)) else rb = mid; } while判断条件可以根据是整形还是浮点型灵活变换,Judge_C就是比较函数,几乎所有的分治算法都可以这样归纳,我们只要找到合适的比较函

二分搜索 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

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

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 Expence 简单二分

1 /** 2 大意: 有连续的n天,每一天有一定的花费,将其分成m份,每一份占一天或者连续的几天,求这m份中的最大值 3 思路: 二分其最大上限,看在此最大上线,能分成多少份,若大于m份,说明上限过小,需要扩大上限 4 若小于m份,则说明,下限过大,需要缩小上限. 5 **/ 6 #include <iostream> 7 8 using namespace std; 9 int c[100010]; // 记录,每天的花费 10 int main() 11 { 12 int n,m; 13