SDUT 2778-James's budget (two answers), sdut2778-

Source: Internet
Author: User

SDUT 2778-James's budget (two answers), sdut2778-

James's budget Time Limit: 1000 ms Memory limit: 65536 K any questions? Click Here ^_^ James finally found a job, but the boss was a strange person. He didn't pay the salary every month. He thought you could get it whenever you wanted, he did not pay for the first few months in a row, and James happened to be a great guy, so he hoped that every time he made the money would be enough for the next n months. So let you divide the n months into exactly m groups. Each group contains at least one month, and the months in each group must be consecutive. Please group them to minimize the total cost of each group. The first line of the input is two integers, n (1 ≤ n ≤ 100,000) and m (1 ≤ m ≤ n). The next n rows are the cost of n consecutive months, the I + 1 row is the cost of the I month. The total cost of the Group with the smallest output meeting the maximum total cost. Sample Input
5 332941
Sample output
9
In simple words, the second answer is the answer to a question. We can roughly determine a range and then divide the answers in this range. Why can we do this? In fact, there are requirements, and the answer should be monotonous. That is to say, after judging the mid, we can determine whether the answer must be on the left or right of the mid. Binary answers are often used to find the minimum value in the maximum value and the maximum value in the minimum value ..
#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <string>#include <cctype>#include <vector>#include <cstdio>#include <cmath>#include <deque>#include <stack>#include <map>#include <set>#define ll long long#define maxn 100010#define pp pair<int,int>#define  max(x,y) ( ((x) > (y)) ? (x) : (y) )#define  min(x,y) ( ((x) > (y)) ? (y) : (x) )using namespace std;int n, m, a[maxn], high, low;bool jduge(int mid){int cnt = 1, s = 0;for (int i = 0; i < n; i++) {if (s + a[i] <= mid) {s += a[i];} else {cnt++;s = a[i];}}if (cnt <= m) {return 1;} else {return 0;}}void solve(){int mid;while (low <= high) {mid = (low + high) / 2;if (jduge(mid)) {high = mid - 1;} else {low = mid + 1;}}printf("%d\n", mid);}int main(){while (~scanf("%d%d", &n, &m)) {high = 0;low = 0;for (int i = 0; i < n; i++) {scanf("%d", a + i);low = max(a[i], low);high += a[i];}solve();}return 0;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.