<><B>[例10] LITTLE SHOP OF FLOWERS (IOI’99)</B></P>
<><B><FONT face=Arial size=4>ROBLEM</FONT></B> </P>
<P>You want to arrange the window of your flower shop in a most pleasant way. You have <I>F </I>bunches of flowers, each being of a different kind, and at least as many vases ordered in a row. The vases are glued onto the shelf and are numbered consecutively 1 through <I>V</I>, where <I>V</I> is the number of vases, from left to right so that the vase 1 is the leftmost, and the vase <I>V</I> is the rightmost vase. The bunches are moveable and are uniquely identified by integers between 1 and <I>F</I>. These id-numbers have a significance: They determine the required order of appearance of the flower bunches in the row of vases so that the bunch <I>i</I> must be in a vase to the left of the vase containing bunch <I>j</I> whenever <I>i</I> < <I>j</I>. Suppose, for example, you have a bunch of azaleas (id-number=1), a bunch of begonias (id-number=2) and a bunch of carnations (id-number=3). Now, all the bunches must be put into the vases keeping their id-numbers in order. The bunch of azaleas must be in a vase to the left of begonias, and the bunch of begonias must be in a vase to the left of carnations. If there are more vases than bunches of flowers then the excess will be left empty. A vase can hold only one bunch of flowers.</P>
<P>Each vase has a distinct characteristic (just like flowers do). Hence, putting a bunch of flowers in a vase results in a certain aesthetic value, expressed by an integer. The aesthetic values are presented in a table as shown below. Leaving a vase empty has an aesthetic value of 0.</P>
<TABLE cellSpacing=0 cellPadding=7 align=center borderColorLight=#ffffff border=1>
<TR>
<TD vAlign=top colSpan=2 rowSpan=2> </TD>
<TD colSpan=5><B>V A S E S </B></TD></TR>
<TR>
<TD vAlign=top align=middle><B>1 </B></TD>
<TD vAlign=top align=middle><B>2 </B></TD>
<TD vAlign=top align=middle><B>3</B> </TD>
<TD vAlign=top align=middle><B>4</B> </TD>
<TD vAlign=top align=middle><B>5</B> </TD></TR>
<TR>
<TD vAlign=center height=26 rowSpan=3><B>Bunches </B></TD>
<TD vAlign=center height=26><B>1 (azaleas) </B></TD>
<TD vAlign=center align=middle height=26><FONT size=3>7</FONT></TD>
<TD vAlign=center align=middle height=26><FONT size=3>23</FONT></TD>
<TD vAlign=center align=middle height=26><FONT size=3>-5</FONT></TD>
<TD vAlign=center align=middle height=26><FONT size=3>-24</FONT></TD>
<TD vAlign=center align=middle height=26><FONT size=3>16</FONT></TD></TR>
<TR>
<TD vAlign=center height=26><B>2 (begonias) </B></TD>
<TD vAlign=center align=middle height=26><FONT size=3>5</FONT></TD>
<TD vAlign=center align=middle height=26><FONT size=3>21</FONT></TD>
<TD vAlign=center align=middle height=26><FONT size=3>-4</FONT></TD>
<TD vAlign=center align=middle height=26><FONT size=3>10</FONT></TD>
<TD vAlign=center align=middle height=26><FONT size=3>23</FONT></TD></TR>
<TR>
<TD vAlign=center height=26><B>3 (carnations) </B></TD>
<TD vAlign=center align=middle height=26><FONT size=3>-21 </FONT></TD>
<TD vAlign=center align=middle height=26><FONT size=3>5</FONT></TD>
<TD vAlign=center align=middle height=26><FONT size=3>-4</FONT></TD>
<TD vAlign=center align=middle height=26><FONT size=3>-20</FONT></TD>
<TD vAlign=center align=middle height=26><FONT size=3>20</FONT></TD></TR></TABLE>
<P>According to the table, azaleas, for example, would look great in vase 2, but they would look awful in vase 4.</P>
<P>To achieve the most pleasant effect you have to maximize the sum of aesthetic values for the arrangement while keeping the required ordering of the flowers. If more than one arrangement has the maximal sum value, any one of them will be acceptable. You have to produce exactly one arrangement.</P>
<P><B><FONT face=Arial size=4>ASSUMPTIONS</FONT></B></P>
<UL>
<LI>1 <= <I>F</I> <= 100 where <I>F</I> is the number of the bunches of flowers. The bunches are numbered 1 through <I>F</I>.
<LI><I>F</I> <= <I>V</I> <= 100 where <I>V</I> is the number of vases.
<LI>-50 <= <I>A<SUB>ij</SUB> </I><= 50 where <I>A<SUB>ij </SUB></I>is the aesthetic value obtained by putting the flower bunch <I>i</I> into the vase <I>j</I>. </LI></UL>
<P><B><FONT face=Arial size=4>INPUT</FONT></B> </P>
<P>The input is a text file named <B>flower.inp</B>.
<UL>
<LI>The first line contains two numbers: <I>F</I>, <I>V</I>.
<LI>The following <I>F</I> lines: Each of these lines contains <I>V</I> integers, so that <I>A<SUB>ij</SUB></I> is given as the <I>j<SUP>th</SUP></I> number on the (<I>i</I>+1)<SUP><I>st</I></SUP> line of the input file. </LI></UL>
<P><B><FONT face=Arial size=4>OUTPUT</FONT></B> </P>
<P>The output must be a text file named <B>flower.out</B> consisting of two lines:
<UL>
<LI>The first line will contain the sum of aesthetic values for your arrangement.
<LI>The second line must present the arrangement as a list of <I>F</I> numbers, so that the <I>k</I>’th number on this line identifies the vase in which the bunch <I>k</I> is put. </LI></UL>
<P><B><FONT face=Arial size=4>EXAMPLE</FONT></B> </P>
<P>flower.inp:</P>
<TABLE cellSpacing=0 borderColorLight=#ffffff border=1>
<TR>
<TD><PRE>3 57 23 -5 -24 165 21 -4 10 23-21 5 -4 -20 20</PRE></TD></TR></TABLE>
<P>flower.out:</P>
<TABLE cellSpacing=0 borderColorLight=#ffffff border=1>
<TR>
<TD><PRE>532 4 5</PRE></TD></TR></TABLE>
<P><B><FONT face=Arial size=4>EVALUATION</FONT></B> </P>
<UL>
<LI>Your program will be allowed to run 2 seconds.
<LI>No partial credit can be obtained for a test case. </LI></UL>
<P>本题虽然是IOI’99中较为简单的一题,但其中大有文章可作。说它简单,是因为它有序,因此我们一眼便可看出这题应该用动态规划来解决。但是,如何动态规划呢?如何划分阶段,又如何选择状态呢?</P>
<P><B><方法1></B></P>
<P>以花束的编号来划分阶段。在这里,第k阶段布置第k束花,共有F束花,有F+1个阶段,增加第F+1阶段是为了计算的方便;状态变量x<SUB>k</SUB>表示第k束花所在的花瓶。而对于每一个状态x<SUB>k</SUB>,决策u<SUB>k</SUB>就是第k+1束花放置的花瓶号;最优指标函数f<SUB>k</SUB>(x<SUB>k</SUB>)表示从第k束花到第n束花所得到的最大美学值;A(i,j)是花束i插在花瓶j中的美学值,V是花瓶总数,F是花的总数。</P>
<P>状态转移方程为 <IMG src="http://algorithm.myrice.com/algorithm/technique/dynamic_programming/images/exp81.gif"></P>
<P>规划方程为</P>
<BLOCKQUOTE>
<P><IMG src="http://algorithm.myrice.com/algorithm/technique/dynamic_programming/images/f1.gif"></P></BLOCKQUOTE>
<P>边界条件为:
<BLOCKQUOTE>
<P><IMG src="http://algorithm.myrice.com/algorithm/technique/dynamic_programming/images/f2.gif">,</P></BLOCKQUOTE>
<P>事实上这是一个虚拟的边界。</P>
<P>最后要求的最大美学价值是</P>
<BLOCKQUOTE>
<P><IMG src="http://algorithm.myrice.com/algorithm/technique/dynamic_programming/images/f3.gif"></P></BLOCKQUOTE>
<P><B><方法2></B></P>
<P>方法1的规划方程中的允许决策空间:x<SUB>k+1</SUB>≤u<SUB>k</SUB>≤V-(F-k)+1 比较麻烦,因此有待改进。还是以花束的编号来划分阶段,第k阶段布置第k束花;状态变量x<SUB>k</SUB>表示第k束花所在的花瓶;注意,这里我们考虑倒过来布置花瓶,即从第F束花开始布置到第1束花。于是状态变量u<SUB>k</SUB>表示第k-1束花所在的花瓶;最优指标f<SUB>k</SUB>(x<SUB>k</SUB>)表示从第一束花到第k束花所获得的美学价值;A(i,j)是花束i插在花瓶j中的美学值,V是花瓶总数,F是花的总数。则状态转移方程为:</P>
<BLOCKQUOTE>
<P><IMG src="http://algorithm.myrice.com/algorithm/technique/dynamic_programming/images/f4.gif"></P></BLOCKQUOTE>
<P>规划方程为:</P>
<BLOCKQUOTE>
<P><IMG src="http://algorithm.myrice.com/algorithm/technique/dynamic_programming/images/f5.gif"></P></BLOCKQUOTE>
<P>增加的虚拟边界条件为:</P>
<BLOCKQUOTE>
<P><IMG src="http://algorithm.myrice.com/algorithm/technique/dynamic_programming/images/f6.gif"></P></BLOCKQUOTE>
<P>最后要求的最大美学价值是:</P>
<BLOCKQUOTE>
<P><IMG src="http://algorithm.myrice.com/algorithm/technique/dynamic_programming/images/f7.gif"></P></BLOCKQUOTE>
<P>可以看出,这种方法实质上和方法1没有区别,但是允许决策空间的表示变得简单了。</P>
<P><B><方法3></B></P>
<P>以花瓶的数目来划分阶段,第k个阶段决定花瓶k中是否放花;状态变量x<SUB>k</SUB>表示前k个花瓶中放了多少花;而对于任意一个状态x<SUB>k</SUB>,决策就是第x<SUB>k</SUB>束花是否放在第k个花瓶中,用变量u<SUB>k</SUB>=1或0来表示。最优指标函数f<SUB>k</SUB>(x<SUB>k</SUB>)表示前k个花瓶中插了x<SUB>k</SUB>束花,所能取得的最大美学值。注意,这里仍然是倒过来考虑。</P>
<P>状态转移方程为</P>
<BLOCKQUOTE>
<P><IMG src="http://algorithm.myrice.com/algorithm/technique/dynamic_programming/images/f8.gif"></P></BLOCKQUOTE>
<P>规划方程为</P>
<BLOCKQUOTE>
<P><IMG src="http://algorithm.myrice.com/algorithm/technique/dynamic_programming/images/f9.gif"></P></BLOCKQUOTE>
<P>边界条件为</P>
<BLOCKQUOTE>
<P><IMG src="http://algorithm.myrice.com/algorithm/technique/dynamic_programming/images/f10.gif"></P></BLOCKQUOTE>
<P>三种不同的方法都成功地解决了问题,只不过因为阶段的划分不同,状态的表示不同,决策的选择有多有少,所以算法的时间复杂度也就不同。</P>
<P>这个例子具有很大的普遍性。有很多的多阶段决策问题都有着不止一种的阶段划分方法,因而往往就有不止一种的规划方法。有时各种方法所产生的效果是差不多的,但更多的时候,就像我们的例子一样,两种方法会在某个方面有些区别。所以,在用动态规划解题的时候,可以多想一想是否有其它的解法。对于不同的解法,要注意比较,好的算法好在哪里,差一点的算法差在哪里。从各种不同算法的比较中,我们可以更深刻地领会动态规划的构思技巧。</P><!-- #EndEditable --> |