Ultraviolet A 714-copying books

Source: Internet
Author: User

To minimize the maximum value, we need to first calculate the maximum value in binary mode, and then greedy to achieve output in the format of the smallest on the left.

CodeAs follows:

# Include <iostream> # include <cstdlib> # include <cstring> # include <cstdio> # include <cmath> using namespace STD; int main () {# ifdef test freopen ("in.txt", "r", stdin); # endif int A [505], B [505]; int K, M, num; scanf ("% d", & num); While (Num --) {long left = 0, Right = 0, CT, to, mid; memset (B, 0, sizeof (B); scanf ("% d", & M, & K); For (INT I = 0; I <m; I ++) {scanf ("% d", & A [I]); right + = A [I];} while (left <Right) // returns the maximum {Ct = 0, To = 0; Mid = (left + right)/2; for (INT I = 0; I <m; I ++) {to + = A [I]; if (a [I]> mid) // when a single element is greater than the central value, you need to increase the value of mid {Ct = K; // make it jump out of break;} If (to >= mid) {If (CT> = k) break; CT ++; to = A [I] ;}}if (CT> K-1) Left = Mid + 1; else right = mid ;}for (INT CCT = 0, TOT = 0, I = m-1; I> = 0; I --) // greedy {tot + = A [I]; if (a [I]> mid) // a small error may occur when the maximum value is obtained in binary mode. When a single element is slightly greater than the maximum value, use this method to update the maximum value. Mid = A [I]; If (CCT <k-1 & (TOT> mid | I <(k-1)-CCT )) // when the interval value is greater than the maximum value or the number of remaining elements is equal to the number of remaining partitions, Division is required. {B [I] = 1; Tot = A [I]; ++ CCT ;}} for (INT I = 0; I <m; I ++) {printf ("% d % C", a [I], I! M-1? '': '\ N'); If (B [I]) printf ("/") ;}} 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.