Uva714-copying books (minimized maximum value)

Source: Internet
Author: User

Question: uva714-copying books (minimum maximum value)


N books are given. The value of each book represents the number of pages in the book. Given M scribers, each scriber must copy at least one book or several successive books. The workload of each scriber is equal to the sum of the pages of the book he wants to copy. Ask how to minimize the maximum workload of scribers. The answer is required to output the preceding and smaller division if there are multiple answers.


Solution: minimize the maximum value.

The possible maximum value of a binary test. If the maximum value can be divided, it means that the maximum value may be this value or smaller. If not, the maximum value is not large enough.

When dividing, make sure that the latter is relatively large.


Code:

#include <stdio.h>#include <string.h>const int N = 505;typedef long long ll;int n, m;ll max_num, min_num;int books[N];int visit[N];ll Min (const ll a, const ll b) { return a < b ? a : b; } int divide (ll value) {int i = n - 1;int count = 0;ll sum;while (i >= 0) {sum = 0;if (sum + books[i] > value)return m + 1;while (i >= 0 && sum + books[i] <= value) {sum += books[i--];}if (i >= 0)visit[i] = 1;count++;}return count;}int bsearch () {ll left = min_num;ll right = max_num;ll mid;while (left < right) {   mid = left + ((right - left)>>1);   if (divide (mid) <= m)   right = mid;   else   left = mid + 1; }return right;}void solve () {ll ans = bsearch();    memset (visit, 0, sizeof (visit));    int cnt =  divide (ans);for (int i = 0; i < n - 1 && cnt < m; i++) {if (!visit[i]) {visit[i] = 1;cnt++;}}}int main () {int t;scanf ("%d", &t);while (t--) {max_num = 0;scanf ("%d%d", &n, &m);for (int i = 0; i < n; i++) {scanf ("%d", &books[i]);max_num += books[i];if (i == 0)min_num = books[i];elsemin_num = Min(min_num, books[i]);}memset (visit, 0, sizeof (visit));if (m != 1)  solve();for (int i = 0; i < n - 1; i++) {printf ("%d ", books[i]);if (visit[i])printf ("/ ");}printf ("%d\n", books[n - 1]);}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.