UVa 714 Copying Books(貪心 二分)
題意 把m數分成k組 使每組數的和的最大值最小 如果有多種分法 靠前的組的和儘量小
關鍵是找出那個最小的最大值 可以通過二分來找出 開始左端點為m個數中最大的數 右端點為m個數的和 若中點能將m個數分為小於等於k組 比它大的肯定都是可以的 中點變為右端點 否則中點變成左端點
然後就可以貪心逆向模擬了 從後往前每組選擇儘量多的數直到剩下的數等於組數
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 505; int a[N], d[N], m, k, maxi; ll s[N], le, ri, mid, mins, t; int divs() { int last = 0, cnt = 1; for(int i = 1; i <= m; ++i) { if(s[i] - s[last] > mid) last = i - 1, ++cnt; } return cnt; } int main() { int cas; scanf("%d", &cas); while(cas--) { scanf("%d %d", &m, &k); for(int i = maxi = 1; i <= m; ++i) { scanf("%d", &a[i]); s[i] = s[i - 1] + a[i]; if(a[i] > a[maxi]) maxi = i; } le = a[maxi], ri = s[m]; while(le <= ri) { mid = (le + ri) >> 1; if(divs() <= k) mins = mid, ri = mid - 1; else le = mid + 1; } t = 0; memset(d, 0, sizeof(d)); for(int i = m; i > 0; --i) { if(t + a[i] > mins) d[i] = k--, t = 0; if(k >= i) while(i > 0) d[--i] = 1; t = t + a[i]; } for(int i = 1; i <= m; ++i) { printf("%d%c", a[i], i < m ? ' ' : '\n'); if(d[i]) printf("/ "); } } return 0; }
Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had been given a book and after several months he finished its copy. One of the most famous scribers lived in the 15th century and his name was Xaverius Endricus Remius Ontius Xendrianus (Xerox
Once upon a time, there was a theater ensemble that wanted to play famous Antique Tragedies. The scripts 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
Input
The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly two lines. At the first line, there are two integersm and k, . At the second line, there are integers separated by spaces. All these values are positive and less than 10000000.
For each case, print exactly one line. The line must contain the input succession divided into exactly k parts such that the maximum sum of a single part should be as small as possible. Use the slash character (`/') to separate the parts. There must be exactly one space character between any two successive numbers and between the number and the slash.
If there is more than one solution, print the one that minimizes the work assigned to the first scriber, then to the second scriber etc. But each scriber must be assigned at least one book.
2 9 3 100 200 300 400 500 600 700 800 900 5 4 100 100 100 100 100
100 200 300 400 500 / 600 700 / 800 900 100 / 100 / 100 / 100 100
Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had been given a book and after several months he finished its copy. One of the most famous scribers lived in the 15th century and his name was Xaverius Endricus Remius Ontius Xendrianus (Xerox). Anyway, the work was very annoying and boring. And the only way to speed it up was to hire more scribers.
Once upon a time, there was a theater ensemble that wanted to play famous Antique Tragedies. The scripts 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 (numbered ) that may have different number of pages ( )
and you want to make one copy of each of them. Your task is to divide these books among k scribes, . Each book can be assigned
to a single scriber only, and every scriber must get a continuous sequence of books. That means, there exists an increasing succession of numbers such
that i-th scriber gets a sequence of books with numbers between bi-1+1 and bi. The time needed to make a copy of all the books is determined by the scriber who was assigned the most work. Therefore,
our goal is to minimize the maximum number of pages assigned to a single scriber. Your task is to find the optimal assignment.
Input
The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly two lines. At the first line, there are two integersm and k, . At the second line, there are integers separated by spaces. All these values are positive and less than 10000000.
For each case, print exactly one line. The line must contain the input succession divided into exactly k parts such that the maximum sum of a single part should be as small as possible. Use the slash character (`/') to separate the parts. There must be exactly one space character between any two successive numbers and between the number and the slash.
If there is more than one solution, print the one that minimizes the work assigned to the first scriber, then to the second scriber etc. But each scriber must be assigned at least one book.
2 9 3 100 200 300 400 500 600 700 800 900 5 4 100 100 100 100 100
100 200 300 400 500 / 600 700 / 800 900 100 / 100 / 100 / 100 100
相關推薦
UVa 714 Copying Books(貪心 二分)
題意 把m數分成k組 使每組數的和的最大值最小 如果有多種分法 靠前的組的和儘量小 關鍵是找出那個最小的最大值 可以通過二分來找出 開始左端點為m個數中最大的數 右端點為m個數的和 若中點能將m個數分為小於等於k組 比它大的肯定都是可以的 中點變為右端點
UVA 714 Copying Books(二分查詢)(未a)
Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so cal
UVa 714 Copying Books (最大值儘量小_二分+貪心)
原題: Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by
UVa 714 Copying Books 二分 + 貪心 (最大值最小化問題)
/** * 最大值最小化問題: 二分法。 劉汝佳那本白書上也說的很詳細。 * 先把最大值的可能區間計算出來,也就是[0, maxAns] maxAns 就是所有頁數總和。 * 然後再在答案區間裡先把“最大值最小化”。 * 得到最小化後的最大值以後,就可
uva 714 Copying Books(二分法求最大值最小化)
題目大意:將一個個數為n的序列分割成m份,要求這m份中的每份中值(該份中的元素和)最大值最小, 輸出切割方式,有多種情況輸出使得越前面越小的情況。 解題思路:二分法求解f(x), f(x) &l
UVa 714 Copying Books——二分最大值最小化
注意上界在累加時可能超過int範圍 #include <cstdio> #include <cstring> #include <iostream> #includ
UVA(714) Copying Books
最大值最小化應該是二分法中經典的題目,Copying Books就是一道最大值最小化的題目 題目大致的意思是要抄N本書,編號為1,2,3...N, 每本書有1<=x<=10000000頁, 把這些書分配給K個抄寫員,要求分配給某個抄寫員的那些書的編號必須是連續的。每個抄寫員的速度是相同的,求所有
UVa:714 Copying Books .
最大值最小化的問題,卡了兩天才做出來。 入門經典上有分析,但是隻提供了求最小的最大值的思路。 具體方法是在某區間上用二分法猜數字,最後猜的一個數x是使得【將輸入數列劃分成m個連續子序列使得所有S(i)均不超過x】成立的最小x,這就是最小的最大值。 這裡二分的並非陣列,
【ZOJ2002】Copying Books(二分+貪心)
題目連結 Copying Books Time Limit: 2 Seconds Memory Limit: 65536 KB Before the invention of book-printin
UVA - 714 Copying Book
題目連結: UVA - 714 Copying Book 題意: 給定一個m個數的序列,將這個序列分成k份連續子序列,要求子序列的最大值最小。 思路: 二分、貪心 這段序列不用再排序,先求出這段序列的和以及序列的最大值,然後從序列的最大值到序列的和開始二分查詢,找到劃分段數小於等於k的最小上
POJ 1505 (Copying Books)(經典二分)
Description Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by h
Copying Books(二分查詢)
在挑戰程式設計競賽上做過類似的,二分答案判斷是否合理。 #include <cstdio> #include <stack> using namespace std; ty
Uva714 Copying Books 【二分】【例題8-10】
題意:將m個序列分成k個連續的子序列,將各個子序列求和,分的子序列的和的最大值儘量小。 思路: (1)列舉子序列和的最大值,從m個序列中最大值~m個序列的和 之間的數二分列舉最大值,每次列舉的值看能劃分的個數與k值比較,如果<=k 列舉值小縮小,否則增大,直到二分結
Copying Books +uva+二分+貪心
Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so c
ZOJ——Copying Books 最大值最小化問題 (貪心 + 二分)
題目連結: #include <cstdio> #include <cmath> #include<vector> #include<cstring> #include<algorithm> #include<cmath>
POJ-1505&&UVA-714 抄書(貪心+二分)
題目傳送門:http://poj.org/problem?id=1505 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8
[貪心&&二分]uva714 Copying Books
Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so
Copying Books(貪心 最大值最小化 二分)
題目描述開頭一大堆屁話,我還仔細看了半天。。其實就最後2句管用。意思就是給出n本書然後要分成k份,每份總頁數的最大值要最小。問你分配方案,如果最小值相同情況下有多種分配方案,輸出前面份數小的,就像字典序輸出從小到大一樣的意思。 這裡用到貪心的方法,定義f(x)為真的條件是滿
UVa 714 抄書 二分答案
題意: 把一個包含m個正整數的序列劃分成k個(1≤k≤m≤500)非空的連續子序列,使得每個正 整數恰好屬於一個序列。設第i個序列的各數之和為S(i),你的任務是讓所有S(i)的最大值盡 量小。例如,
【二分與三分03】Copying Books
題目來源: 題目大意: 將n本頁數為p1,p2,……,pm(順序排列)的書分給k個抄寫員抄寫,每個抄寫員速度一樣且每個抄寫員只能抄寫編號相鄰的書,求抄寫時間最少的分組。 解題思路: 二分法與貪心。 通過二分法求出一個抄寫員最多需要抄寫的頁數,然後按照這個頁數來分組