青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

QuXiao

每天進步一點點!

  C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
  50 隨筆 :: 0 文章 :: 27 評論 :: 0 Trackbacks

解題報告

 

題目來源:

PKU 1505 Copying Books

 

算法分類:

DP

 

原文:

Copying Books

Time Limit: 3000MS


Memory Limit: 10000K

Total Submissions: 1806


Accepted: 404

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 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 ... m) that may have different number of pages (p1, p2 ... pm) and you want to make one copy of each of them. Your task is to divide these books among k scribes, k <= 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 = b0 < b1 < b2, ... < bk-1 <= bk = 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 <= k <= m <= 500. At the second line, there are integers p1, p2, ... pm separated by spaces. All these values are positive and less than 10000000.

Output

For each case, print exactly one line. The line must contain the input succession p1, p2, ... pm 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.

Sample Input

2

9 3

100 200 300 400 500 600 700 800 900

5 4

100 100 100 100 100

Sample Output

100 200 300 400 500 / 600 700 / 800 900

100 / 100 / 100 / 100 100

Source

Central Europe 1998

 

 

中文描述:

題目大意是給你m1…m)本書,每本書有Pm頁,用kk<=m)個員工來復(fù)印這些書。每本書只能分配給一個員工來復(fù)印,并且每個員工必須復(fù)印一段連續(xù)的書籍,每個員工復(fù)印的時間取決于所復(fù)印書籍的總頁數(shù)。讓你給出相應(yīng)的分配,使得分配給員工的書籍頁數(shù)的最大值盡量小。注意,如果有多種分配的方案,使得第一個員工的書籍頁數(shù)盡量少,其次是第二個、第三個……以此類推。

 

題目分析:

我們可以從后往前推,最后一個員工,也就是第k個員工,他至少要復(fù)印第m本書,至多可以復(fù)印第k本到第m本(因為至少要分配給前k-1個員工每人一本書)。假設(shè),第k名員工復(fù)制第ik<=i<=m)本書到第m本書,那么,所有員工復(fù)印書籍的最小時間就為第k名員工所需的時間以及前k-1名員工復(fù)制前i-1本書所需最小時間的較大的那個時間。這樣,問題的規(guī)模就從k個員工復(fù)印m本書減小到了k-1個員工復(fù)印i-1本書,而且求解過程中會不斷遇到前a個員工復(fù)印前b本書的最小時間。為了減小問題的規(guī)模以及記錄重復(fù)子問題的解,就可以用DP

但僅僅算出最小時間的不夠的,還要給出分配的方案,這個稍微有點繁瑣。因為題目中說,如果有多種最優(yōu)的分配方案,應(yīng)該讓前面的員工分配的頁數(shù)盡量少。那么,可以從后推,在當(dāng)前的員工所復(fù)印的書籍頁數(shù)沒有超過最大頁數(shù)的情況下,讓其復(fù)印的頁數(shù)最大化。如果超過了最大頁數(shù),就把這本書分配給前一名員工。最后再按順序?qū)⒎峙浣Y(jié)果輸出出來。

 

代碼:

#include <cstdio>

#include <climits>

#include <cstring>

 

const int MAX = 505;

int book[MAX];

__int64 total[MAX];                        //1~n本書的頁數(shù)

int k, m;

__int64 f[MAX][MAX];                  //f[i][j] = k 表示前i個人復(fù)制前j本書所需最少時間是k

__int64 max;

void Input ()

{

                scanf("%d%d", &m, &k);

                int i;

                for (i=1; i<=m; i++)

                                scanf("%d", &book[i]);

}

 

__int64 Sum (int s, int e)                                               //s本書到第e本書的總頁數(shù)

{

                return (total[e] - total[s-1]);

}

 

__int64 Max (__int64 a, __int64 b)

{

                return ( a>b?a:b );

}

 

__int64 Min (int x, int y)                                //x個人復(fù)制前y本書所需的最少時間        x<=y

{

//考慮特殊情況

                if ( f[x][y] != -1 )

                                return f[x][y];

                if ( y == 0 )

                                return ( f[x][y] = 0 );

                if ( x == 0 )

                                return ( f[x][y] = INT_MAX );

 

                int i;

                __int64 temp;

                f[x][y] = INT_MAX;

                for (i=x-1; i<y; i++)

                {

//x個人復(fù)制第i+1到第y本書與前x-1個人復(fù)制前i本書的時間較大的時間

                                temp = Max( Min(x-1, i), Sum(i+1, y) );                 

                                if ( temp < f[x][y] )

                                {

                                                f[x][y] = temp;

                                }

                }

                return f[x][y];

}

 

void Output ()

{

                int i, p;

                __int64 temp;

                int slash[MAX];

                max = f[k][m];

                memset(slash, 0, sizeof(slash));

                temp = 0;

                p = k;

                for (i=m; i>0; i--)

                {

                                //讓后面的員工盡量復(fù)印最多的書籍

                                if ( temp + book[i] > max || i < p )

                                {

                                                slash[i] = 1;

                                                temp = book[i];

                                                p --;

                                }

                                else

                                {

                                                temp += book[i];

                                }

                }

 

                for (i=1; i<=m; i++)

                {

                                printf("%d", book[i]);

                                if ( slash[i] == 1 )

                                                printf(" / ");

                                else if ( i != m )

                                                printf(" ");

                }

                printf("\n");

}

 

void Solve ()

{

                int i, j;

                //預(yù)處理書籍頁數(shù)的和

                total[0] = 0;

                for (i=1; i<=m; i++)

                                total[i] = total[i-1] + book[i];

 

                memset(f, -1, sizeof(f));

               

                Min(k, m);

               

                Output();

}

 

int main ()

{

                int test;

                scanf("%d", &test);

                while ( test-- )

                {

                                Input ();

                                Solve ();

                }

 

                return 0;

}

 

 

程序分析與心得:

時間復(fù)雜度O(n2),空間復(fù)雜度O(n2)

在用記憶化搜索解決DP問題時,往往比較符合人的思維,容易想到模型,編程比較簡單。在解題過程中,除了可以按照常理順著推,也可以嘗試逆向思維,從最后的狀態(tài)倒著推,這樣可以使問題想得更加透徹,有比較好的效果。

posted on 2008-03-10 15:11 quxiao 閱讀(568) 評論(0)  編輯 收藏 引用 所屬分類: ACM
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美激情一区二区三区| 欧美久久综合| 欧美成人免费播放| 亚洲国产精品久久久久| 理论片一区二区在线| 久久免费视频在线| 国产麻豆综合| 欧美一区2区视频在线观看| 亚洲综合第一| 国产自产2019最新不卡| 欧美高清在线播放| 欧美激情五月| 欧美在线播放高清精品| 久久久精品tv| 亚洲一区二区三区影院| 一本高清dvd不卡在线观看| 国产精品自拍在线| 欧美成年网站| 国产精品大片| 美女爽到呻吟久久久久| 欧美四级剧情无删版影片| 香蕉尹人综合在线观看| 美女网站久久| 欧美一区二区观看视频| 牛牛影视久久网| 午夜老司机精品| 蜜乳av另类精品一区二区| 亚洲图片欧美一区| 久久精品2019中文字幕| 一区二区欧美日韩视频| 欧美怡红院视频| 夜夜嗨av一区二区三区免费区| 亚洲一级片在线观看| 亚洲精品综合| 久久精品视频在线免费观看| 亚洲视频在线二区| 噜噜噜噜噜久久久久久91| 亚洲欧美视频在线观看视频| 农村妇女精品| 久久天天躁狠狠躁夜夜爽蜜月| 欧美日韩亚洲一区二区三区在线| 久久精品中文| 国产美女精品视频免费观看| 亚洲国产美女精品久久久久∴| 欧美日韩三级视频| 99ri日韩精品视频| 国产亚洲欧美另类中文| 在线一区亚洲| 亚洲欧美精品一区| 91久久国产综合久久91精品网站| 欧美在现视频| 久久久久久九九九九| 国产精品美女久久| 夜夜嗨一区二区三区| 欧美一区影院| 欧美亚洲一区二区在线观看| 欧美破处大片在线视频| 欧美激情欧美激情在线五月| 韩国成人精品a∨在线观看| 亚洲男女毛片无遮挡| 亚洲一区国产视频| 国产精品黄视频| 制服丝袜激情欧洲亚洲| 欧美日韩天堂| 亚洲免费高清| 亚洲一区亚洲| 国产精品色婷婷久久58| 亚洲午夜视频在线观看| 亚洲在线一区二区三区| 国产精品国产自产拍高清av王其| 99热这里只有精品8| 亚洲天堂av在线免费| 欧美日韩在线视频一区二区| 一本到12不卡视频在线dvd| 亚洲永久免费观看| 国产精品丝袜久久久久久app| 亚洲一二三级电影| 久久久久久黄| 亚洲精品美女| 欧美日韩精品免费观看| 亚洲视频第一页| 久久精品免费| 欧美美女视频| 亚洲先锋成人| 久久手机免费观看| 一本到12不卡视频在线dvd| 欧美色综合网| 久久久www成人免费精品| 欧美电影在线观看| 亚洲伊人一本大道中文字幕| 国产精品综合| 欧美成人午夜激情视频| 99国产精品久久久久老师| 午夜久久久久| 最新亚洲视频| 国产欧美日韩视频一区二区| 久久久久久久网| 亚洲美女黄网| 久久亚洲精品一区二区| 一个色综合av| 一区二区视频欧美| 欧美亚洲不卡| 久久综合色播五月| 欧美国产亚洲视频| 亚洲你懂的在线视频| 伊人久久大香线蕉av超碰演员| 欧美日韩国产三级| 久久精品成人欧美大片古装| 亚洲精品乱码久久久久久按摩观| 亚洲在线一区二区三区| 亚洲国产欧美国产综合一区| 国产精品美女在线| 欧美日本免费| 久久亚洲精品伦理| 亚洲综合国产| 亚洲视频精选在线| 亚洲国产精品久久久| 久久精品国产99国产精品| 一二三四社区欧美黄| 91久久久在线| 亚洲国产99| 狠狠色综合一区二区| 国产精品av一区二区| 六月丁香综合| 久久深夜福利| 久久精品理论片| 西西裸体人体做爰大胆久久久| 日韩午夜在线视频| 亚洲精品乱码久久久久久日本蜜臀| 久久一区二区视频| 久久精品人人做人人综合| 亚洲一级片在线观看| 日韩一级免费观看| 日韩视频一区二区三区在线播放免费观看 | 亚洲激情在线视频| 亚洲第一免费播放区| 欧美成人免费网| 欧美大胆成人| 欧美成人免费视频| 亚洲国产精品成人va在线观看| 六月婷婷一区| 欧美福利视频在线观看| 免费成人小视频| 亚洲成人在线网站| 亚洲一区二区在| 中文网丁香综合网| 亚洲欧美日韩在线一区| 亚洲尤物视频在线| 久久激五月天综合精品| 久久成人免费网| 久久综合一区二区| 欧美成人午夜激情| 亚洲三级性片| 正在播放欧美一区| 香蕉乱码成人久久天堂爱免费| 午夜精品影院| 久久欧美肥婆一二区| 免费成人av在线看| 欧美日韩天天操| 国产日韩专区| 亚洲国产女人aaa毛片在线| 亚洲免费观看| 欧美一区二区在线免费观看| 久久亚洲综合色| 亚洲国产成人porn| 在线视频亚洲欧美| 久久精品一区| 欧美剧在线观看| 国产情人综合久久777777| 黄色日韩网站| 一区二区三区欧美在线观看| 亚洲一区二区三区免费观看| 久久精品亚洲精品国产欧美kt∨| 久久在线观看视频| 日韩亚洲国产精品| 欧美中文字幕在线视频| 欧美日本高清一区| 国产一区二区黄色| 一区二区高清在线| 久久久久在线| 夜夜嗨av一区二区三区免费区| 亚洲一区在线观看视频| 免费日韩av| 国产精品一区免费视频| 亚洲人成毛片在线播放女女| 午夜精品成人在线| 亚洲国产综合视频在线观看| 亚洲欧美精品伊人久久| 欧美精品免费看| 在线免费高清一区二区三区| 亚洲小说区图片区| 欧美韩国在线| 久久av最新网址| 国产精品国产三级国产aⅴ9色| 一色屋精品亚洲香蕉网站| 亚洲欧美日韩综合| 亚洲人成毛片在线播放| 久久久www成人免费毛片麻豆| 国产精品人成在线观看免费 | 欧美日韩在线影院|