POJ 3273 Monthly Expense
来源:互联网 发布:java实体是什么 编辑:程序博客网 时间:2024/06/04 19:36
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 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.
Input
Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Sample Input
7 5100400300100500101400
Sample Output
500
Hint
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.
题意:将序列划分为k段,要求找出k段中最大的资金和要在所有符合要求的划分中值最少
思路: 我们可以直到这个值必然在所有数中最大值与所有数的总和之间,那么只要再这个区间进行二分即可
#include <cstdio>#include <iostream>#include <algorithm>using namespace std;const int maxn = 100000 + 10;int main(){ int n, m; int a[maxn]; int max1, cnt, s, sum; scanf("%d%d", &n, &m); sum = 0, max1 = 0; for (int i = 0; i < n; i++){ scanf("%d", &a[i]); sum = sum + a[i]; if (a[i] > max1) max1 = a[i]; } while (max1 < sum){ s = 0, cnt = 0; int middle = (max1 + sum) / 2; for (int i = 0; i < n; i++){ s = s + a[i]; if (s > middle){ s = a[i]; cnt++; } } if (cnt < m) sum = middle; else max1 = middle + 1; } printf("%d\n", max1); return 0;}
- poj 3273 Monthly Expense
- poj-3273 Monthly Expense
- poj 3273 Monthly Expense
- poj 3273 Monthly Expense
- POJ 3273 Monthly Expense
- POJ 3273 Monthly Expense
- POJ 3273 Monthly Expense
- POJ 3273 Monthly Expense
- POJ-3273-Monthly Expense
- POJ 3273 Monthly Expense
- poj 3273 Monthly Expense
- POJ 3273 Monthly Expense
- poj 3273 Monthly Expense
- POJ 3273 Monthly Expense
- POJ 3273 Monthly Expense
- POJ - 3273 Monthly Expense
- POJ-3273-Monthly Expense
- POJ 3273 Monthly Expense
- FirewallD详解(转载)
- linux 下使用ndk-build编译android使用的c++静态库
- 红外自动切换播放语音听筒
- bitmap上直接进行图像处理报错
- 【BZOJ 3207】花神的嘲讽计划Ⅰ(Hash + 可持久化线段树)
- POJ 3273 Monthly Expense
- AC自动机
- Matlab 脚本编译成dll被c++调用
- TextView属性大全
- (六)原型模式
- 计算机网络总结笔记
- 锁
- vmware Tools 全系列版本下载及安装方法(vmware Tools 10~12)
- linux read