714-copying books--[Greedy, two-point search]

Source: Internet
Author: User

Before the invention of book-printing, it is very hard-make a copy of a book. All the contents hadto is re-written by hand and called Scribers. The scriber had been given a book and after severalmonths he finished its copy. One of the most famous Scribers lived in the 15th century and his Namewas Xaverius endricus remius ontius Xendrianus (Xero x). Anyway, the work is very annoying andboring. And the only-to-speed it up is to hire more scribers. Once upon a time, there was a theater ensemble the wanted to play famous antique tragedies. Thescripts of these plays were divided into many books and actors needed more copies of them, of course. So they hired many scribers to make copies of these books. Imagine you have M books (numbered1, 2, ..., m) that could have different number of pages (P1, p2, ..., PM) and you WA NT to make one copy ofeach of them. Your task is to divide these books among K scribes, K≤m. Each book can is AssignedTo a single scriber only, and every scribeR must get a continuous sequence of books. That's means, thereexists an increasing succession of numbers 0 = B0 < B1 < B2, ... < BK−1≤BK = m such that i-t H scribergets a sequence of books with numbers between bi−1 + 1 and bi. The time needed to make a copy ofall the books are determined by the Scriber who was assigned the most work. Therefore, our goal are tominimize the maximum number of pages assigned to a single scriber. Your task is to find the optimalassignment.InputThe input consists of N cases. The first line of the input contains only positive integer N. Then followthe cases. Each case consists of exactly-lines. At the first line, there is integers m and k,1≤k≤m≤500. At the second line, there is integers p1, p2, ..., PM separated by spaces. All thesevalues is positive and less than 10000000.OutputFor each case, print exactly one line. The line must contain the input succession P1, p2, ... pm dividedinto exactly k parts such that the maximum sum of a sin GLE part should is as small as possible. Usethe slash character ('/') to separate the parts. There must be exactly one space character between anytwo successive numbers and between the number and the slash.  If there is more than one solution, print the one of the minimizes the work assigned to the first scriber,then to the second Scriber etc. But each scriber must is assigned at least one book.Sample Input29 3100 200 300 400 500 600 700 800 9005 4100 100 100 100 100Sample Output100 200 300 400 500/600 700/800 900100/100/100/100 100

Problem Solving Ideas:
The optimization goal of the subject is to make the maximal continuous sub-sequence and the smallest , and in the maximum subsequence and the same case S1, S2 ... As small as possible. Then we can start from the right, as far as possible to the left, when the remaining number of books equals the remaining number of people, the remaining each book allocation strategy can only be one per person.

The code is as follows:

1#include <iostream>2#include <cstring>3#include <vector>4#include <cstdio>5#include <algorithm>6 using namespacestd;7 #defineMAXM 500+58typedefLong LongLL;9 intm,k;Ten intP[MAXM]; Onevector<int>s; A intANS[MAXM]; - LL M; -  the BOOLjudge (LL x) { -      - s.clear (); -     BOOLflag=true; +     intCnt=K; -      for(inti = m;i>0;){ +LL sum =0; A          while(I >0&& sum + p[i] <=x) { at             if(i +1= = CNT) Break; -Sum + = p[i--]; -         } - S.push_back (i); -cnt--; -         if(S.size () >k) { inFlag =false; -              Break; to         } +     } -     if(flag) { the         intj=0; *          for(inti = s.size ()-1; I >=0; i--){ $Ans[j++] =S[i];Panax Notoginseng         } -         return true; the     } +     Else return false; A } the intMainintargcConst Char*argv[]) { +Freopen ("/users/hujiacheng/desktop/input.txt","R", stdin); -     intN; $scanf"%d",&N); $      while(n--){ -m=0; -memset (ans,0,sizeofans); thescanf"%d%d",&m,&k); -          for(intI=1; i<=m;i++){Wuyiscanf"%d",&p[i]); theM + =P[i]; -         } WuLL l=1, r=M; -LL mid= (l+r)/2; About          while(l<R) { $             if(judge (mid)) { -R=mid; -Mid= (L+R)/2; -                  A             } +             Else { theL=mid+1; -Mid= (L+R)/2; $             } the         } the         intj=1; the          for(intI=1; i<=m;i++){ the             if(i!=1) cout<<" "; -cout<<P[i]; in             if(i==Ans[j]) { thecout<<" /"; theJ + +; About             } the         } thecout<<Endl; the     } +     return 0; -}

714-copying books--[Greedy, two-point search]

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.