1 / 5

10026: Shoemaker's Problem

10026: Shoemaker's Problem. ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10026: Shoemaker's Problem 解題者:王振宇 解題日期: 2014 年 3 月 16 日 題意: 鞋匠有 N 件工作要完成。他 每天只能做一件工作 。並且他知道這些工作分別要幾個工作天才能完成 ( 1<= 工作天 <=10 3 ) 另外,他也知道每個工作被延誤一天所需被罰的罰金 ( 1<= 罰金 <=10 4 ) 。 延誤的天數為從今天算起到該工作開始的那天 (所以只有第一件工作沒有罰金)。

nell-long
Download Presentation

10026: Shoemaker's Problem

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 10026: Shoemaker's Problem • ★★★☆☆ • 題組:Problem Set Archive with Online Judge • 題號:10026: Shoemaker's Problem • 解題者:王振宇 • 解題日期:2014年3月16日 • 題意:鞋匠有N件工作要完成。他每天只能做一件工作。並且他知道這些工作分別要幾個工作天才能完成(1<=工作天<=103)另外,他也知道每個工作被延誤一天所需被罰的罰金(1<=罰金<=104)。 • 延誤的天數為從今天算起到該工作開始的那天(所以只有第一件工作沒有罰金)。 • 你的任務是寫一個程式幫助鞋匠找出完成這N個工作的先後順序,使得罰金最少。 1

  2. 題意範例: • 編號天數 罰金 • 134 • 211000 • 32 2 • 455 • 以第一組測試資料為例說明: • 若工作的順序是1 2 3 4,那罰金為 : 0*4 + 1000*3 + 4*2 + 6*5 =3038。 • 若工作的順序是2 1 3 4,則罰金為:0*1000 + 1*4 + 4*2 + 6*5 =42。 • 所以第二種工作順序的罰金較少(42 < 3038)。 • 輸出條件:對每組測試資料輸出一列,為這N個工作的順序使得罰金最少。工作之間以一空白字元分隔。 • 如果有不只一組答案,請輸出字典順序最小的那組。各組測試資料間亦請輸出一空白列。 2

  3. Discussion : • Solution : 排序,排程 • 如果有不只一組答案,請輸出字典順序最小的那組 • 工作編號最小的 • 不能用陣列的index來記錄,因為排序後就不是原來的編號了,因此需要新變數來記錄輸入順序的編號。 • 使用Struct記錄 1. 編號 2. 天數 3. 罰金 • 衡量權重? • 用性價比的概念 • 罰金 / 天數 : 找出只要一天不做會罰最多的先做 • 類似於貪婪的想法 • 在電腦的世界用除法會有誤差,可能被測資影響。 • 編號天數 罰金 性價 • 134 4/3 • 211000 1000 • 32 2 1 • 455 1 順序: 2 => 1 => 3 => 4 2

  4. 用排序+比較的想法 • std:: Sort <algorithm> • template <class RandomAccessIterator, class Compare> • void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp); • return value convertible to bool • return TRUE then => arg1 , arg2 • Not modify any of its args => use const for args • How do we define function ? • I . a天數*b罰金 vs. b天數*a罰金 • II. 當上述的值相同時,輸入編號的比較 • Imp : • If ( a天數*b罰金 == b天數*a罰金 ) • then return a編號 < b編號 • Else return a天數*b罰金 < b天數*a罰金 2

  5. Tips: • 輸出的每列資料用換行做間隔 • 第一個資料前不用換行 • 代替Struct且不須傳入Compare comp • 使用Operator Overloading • T = O ( N * log2 (N)) 2

More Related