洛谷P2884 [USACO07MAR]Monthly Expense S
題目描述
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.
給出農夫在n天中每天的花費,要求把這n天分作m組,每組的天數必然是連續的,要求分得各組的花費之和應該盡可能地小,最后輸出各組花費之和中的最大值
輸入格式
Line 1: Two space-separated integers: N and M
Lines 2…N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
輸出格式
Line 1: The smallest possible monthly limit Farmer John can afford to live with.
輸入輸出樣例
輸入 #1復制
7 5
100
400
300
100
500
101
400
輸出 #1復制
500
說明/提示
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.
題目大意:有n個數,要分成m份,每份的和要盡可能小,求這個情況下和最大的一份的和。
解題思路:
二分+貪心
代碼如下:
#include <iostream> using namespace std; typedef long long LL; int n, k; const int N = 100010; int a[N];bool check(int mid) {int p = 1;LL sum = 0;for (int i = 0; i < n; i++) {if (a[i] + sum <= mid) {sum += a[i];} else {p++;sum = a[i];if (sum > mid)return false;}}return p <= k; }int main() {LL sum = 0;cin >> n >> k;for (int i = 0; i < n; i++) {cin >> a[i];sum += a[i];}LL l = 1, r = sum;while (l < r) {LL mid = l + r >> 1;if (check(mid)) {r = mid;} else {l = mid + 1;}}cout << r << endl;return 0; }總結
以上是生活随笔為你收集整理的洛谷P2884 [USACO07MAR]Monthly Expense S的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么煤质量好什么样的煤质量好
- 下一篇: 如何快速将应用迁移到新电脑换电脑怎么把软