1. 程式人生 > >uva oj 714解題報告

uva oj 714解題報告

二分查詢+貪心 ||degree - 3

1 . 題目描述

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 $1, 2, /dots, m$) that may have different number of pages ( $p_1, p_2, /dots, p_m$) and you want to make one copy of each of them. Your task is to divide these books among k scribes, $k /le m$. 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 $0 = b_0 < b_1 < b_2, /dots < b_{k-1} /le b_k = m$
 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 integers m and k,$1 /le k /le m /le 500$. At the second line, there are integers $p_1, p_2, /dots p_m$ 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 $p_1, p_2, /dots p_m$ 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

2 . 測試用例

input: 3
12 5
48 65 53 3 90 3 90 30 52 75 54 22
10 4
82 36 6 11 33 33 25 79 9 7
9 5
64 16 75 91 1 71 43 81 13



output: 48 65 / 53 3 90 / 3 90 30 / 52 75 / 54 22
82 / 36 6 11 / 33 33 25 / 79 9 7
64 / 16 75 / 91 1 / 71 43 / 81 13


3 .  演算法描述

    1. 主體思路:

             題目要求算出單個“抄寫者”能夠抄寫的最大值(在這裡記為max),問題1怎麼找出這個max?              問題1解答:具體實現為先計算出所有pages的合sum,然後在0-sum之間進行二分查詢,問題2就是怎樣判斷當前的二分查詢的值(mid)就是目標值(max)呢,或者當前的值是大於還是小於目標值?              問題2解答:這裡用到了貪心演算法,對於每一個scriber,按順序(從後往前,因為題目要求如果有相似解的情況,後面scriber抄寫的頁數應該大於前面的)依次累加page,直到超過max的值。迴圈計算直到第一個scriber,計算餘下pages的和,如果這個合的值大於當前的max,那麼證明當前max的值偏小,二分查詢應當向後搜尋了;如果這個合的值小於當前max,則證明當前max的偏大的,二分查詢向前搜尋;如果找到則返回查詢成功

    2. 演算法實現細節

             從上面的描述可以看到程式中最關鍵的實現在於怎樣確定二分查詢的mid值與我們想要找到的max值之間的關係.  

4 . 程式中遇到的問題

        實現這個程式中遇到的最大問題就是:計算部分合的過程中,出現有的抄寫者沒有分到書的情況(eg : input :5 4,100 100 100 100 100),這種情況在計算部分和的時候就有可能出現第四個與第三個抄寫者分配到了200頁但是第一個抄寫者沒有分配到書的情況。。         解決辦法:         在對“抄寫者”計算“頁數部分合”的時候,除了要滿足“部分合”少於“當前最大值”,還要滿足餘下的書必須能夠保證足夠分配給剩下的“抄寫者”。主要是下面程式碼的判斷部分......          

5 . 需要改進的地方

          無

相關推薦

uva oj 714解題報告

二分查詢+貪心 ||degree - 3 1 . 題目描述 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

UVa 1451 Average 解題報告(斜率優化)

1451 - Average Time limit: 3.000 seconds A DNA sequence consists of four letters, A, C, G, and T. The GC-ratio of a DNA sequence is the

uva oj 111、674、562、11137、10534、10130解題報告

111 - DP LCS - degree1 2.解題思路:利用動態規劃的LCS演算法(演算法連結),特別注意要看清題目,第一行輸入為事件“envent”的數目,第二行為正確的事件"envent"的順序,第三行為學生的輸入 3.輸入輸出:sample的input、o

UVA 11995 (STL_E題)解題報告

esp pri 題目 close 判斷 結構 uva spa div 題目鏈接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a

UVA 10815 (STL_C題)解題報告

col play pla put 大寫 != 應該 lose ans 題目鏈接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a

UVA 156(STL_G題)解題報告

etl show ret return problem open main span 輸出 題目鏈接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=sho

UVA 12034(遞推&遞歸_I題)解題報告

txt ron get con void 技術分享 size 一次 -s 題目鏈接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem

UVA 12627(遞推&遞歸_H題)解題報告

get tdi 圖片 mat ble log gpo b- lin 題目鏈接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&am

uva 133解題報告

題目描述 為了縮短領救濟品的隊伍,NNGLRP決定了以下策略:每天所有來申請救濟品的人會被放在一個大圓圈,面朝裡面。選定一個人為編號 1 號,其他的就從那個人開始逆時針開始編號直到 N。一個官員一開始逆時針數,數 k 個申請者,然後另一個官員第 N 個始順時針方向數 m 個申請者,這兩個人就被送去再教育。如

UVA - 213解題報告

輸入 報告 題目 編碼 nbsp 每一個 一個 uva pro 題目鏈接:https://cn.vjudge.net/problem/UVA-213 Sample input TNM AEIOU 0010101100011 1010001001110110011 11000

容斥原理(UVA 10325---The Lottery) 解題報告

題目: The Sports Association of Bangladesh is in great problem with their latest lottery `Jodi laiga Jai'. There are so many participants this time th

UVA 11478 查分約束+二分答案 解題報告

讓我看到你們的雙手 Halum Description You are given a directed graph G(V; E) with a set of vertices and edges. Each edge (i; j) that conne

UVA 11090 SPFA+二分答案 解題報告

讓我看到你們的雙手 Going in Cycle!! Description You are given a weighted directed graph with n vertices and m edges. Each cycle in the gr

UVa 1595 解題報告

運用 map 將隨機的行數轉換為按照第一次輸入的順序進行轉換,不會影響對稱性,而且方便儲存。 #include <bits/stdc++.h> using namespace std; double findsym(vector <int>v,

UVA 10420 解題報告

題目意思: 簡單來說,就是直接統計國家名字出現的次數。 使用 c++的 map 就可以很方便的 ac 這道題。 輸出時候因為 map 內部實現的特性,所以直接用迭代器輸出的時候就是按照 key 的

九度OJ 1207質因數的個數解題報告

題目描述:求正整數N(N>1)的質因數的個數。相同的質因數需要重複計算。如120=2*2*2*3*5,共有5個質因數。輸入:可能有多組測試資料,每組測試資料的輸入是一個正整數N,(1<N&l

UVA 10559解題報告

一般的動態規劃題,都是當前的決策不會影響到未來。而這類動態規劃的題目中,當前的決策是會影響到未來的,並且這道題中,不僅僅是當前的決策影響到未來,因為價值是加起來的平方,所以以前的決策會和現在的決策一起影響未來的。因此不能簡單的把之前的決策的影響在以前就消去,而是

解題報告 UVA 272,10082, 457

哎……距離上次發做題的報告已經近10個月了。這學期沒什麼進步,感覺不太好。 現在處在臨近考試的階段,但個人保持緩慢地看《演算法競賽入門經典》——by 劉汝佳,效果還好。雖然進步緩慢,但起碼在一直都在進步著。我想,現在的一個問題是,很少能堅持、專注地思考一個問題。這是當代人共

UVa 10935 解題報告

關於佇列運用的一道簡單題 PE 兩次 注意·格式 #include <bits/stdc++.h> using namespace std; int main() {     while(1){         int n;         cin >&

杭電OJ題 1280 前m大的數 解題報告

前m大的數 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6149    Accepted Submission