UVA 714 Copying Books

Source: Internet
Author: User

Thinking about it:

After reading this question, there is really no idea, read the solution to understand. The minimum value is determined by the dichotomy before grouping. This way of thinking is beyond my expectation, and I have been moving towards DP. It seems that I need more discipline.

Reference:

"Algorithmic Competition Primer (2nd edition)"

Code:  

/** * AC @ Sep 5th * Run time:0.000s */#include <bits/stdc++.h>using namespace Std;typedef long Long ll;cons t int maxn = + 50;int value[maxn];int M, K;    LL sum, maxc;void read () {cin >> M >> K;    sum = MaxC = 0;        for (int i = 0; i < M; ++i) {cin >> value[i];        Sum + = Value[i]; MaxC = MaxC > Value[i]?    Maxc:value[i];    }}bool is_possile (int p) {int tmpvalue = 0, group = 1; for (int i = M-1; I >= 0; i.) {if (Tmpvalue + value[i] > P | | i+1 <= k-group) {tmpvalue =            Value[i];        Group + +;        } else {tmpvalue + = Value[i]; }} return group = = K;    void print (int pos, int group, int p) {std::vector<int> V;    int i;    LL sum = 0;        for (i = pos; sum + value[i] <= p && i+1>k-group;-I.) {sum + = Value[i];    V.push_back (Value[i]);  if (group = = K) {//the head for (int l = v.size ()-1; l > 0;--l) {          cout << V[l] << "";    } cout << v[0];        } else {print (I, group+1, p);        cout << "/";        for (int l = v.size ()-1; l >= 0;--l) {cout << "" << v[l];    }}}void output (int p) {print (M-1, 1, p); cout << Endl;}    void work () {//If left are initialized with 0, some problems'll occur LL = MaxC, right = SUM, mid;        while (right > left) {mid = (right + left) >> 1;        if (Is_possile (mid)) {right = Mid;        } else {left = mid + 1; }} output (right);}            int main (int argc, char const *argv[]) {Ios::sync_with_stdio (false);    Cin.tie (0);    int T;    Cin >> T;        while (T--) {read ();    Work (); } return 0;}

  

UVA 714 Copying Books

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.