Poj-3273-Monthly Expense-binary

Source: Internet
Author: User

Question: Give You n m and then give you n numbers. Let you divide the n numbers into m parts, and each part is continuous. Ask the minimum value of the maximum value in all parts. Practice: binary. At the beginning, the previous session was the sum of n numbers. Indicates that only one group is allowed. The next session is the maximum number of n numbers. Indicates that the Group is divided into n groups. If the result is mid = (previous + next)/2, you can check the number of groups that can be divided by mid. If the number of groups is greater than m, the value of mid is smaller than the actual value. The next session becomes mid + 1. Otherwise, the previous session becomes mid-1; [html] # include <iostream> # include <stdio. h> # include <string. h> using namespace std; int a [100100]; int n, m; int sw (int mid) {int s, num, I; s = 0; num = 1; for (I = 0; I <n; I ++) {if (s + a [I] <= mid) {s + = a [I];} else {s = a [I]; num ++ ;}} if (num> m) return 1; else return 0 ;}int main () {int l, r, i, mid; cin> n> m; r = 0; l = 0; for (I = 0; I <n; I ++) {scanf ("% d", & a [I]); r + = a [I]; if (a [I]> l) l = a [I];} mid = (l + r)/2; while (l <r) {www.2cto.com if (sw (mid) l = mid + 1; else r = mid-1; mid = (l + r)/2;} cout <mid <endl; return 0 ;}

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.