POJ 1065-Wooden Sticks 貪心問題

來源:互聯網
上載者:User

題目來源: http://acm.pku.edu.cn/JudgeOnline/problem?id=1065

 

 

解題報告:

 

本題採用貪心演算法, 先從一堆sticks中找到長度最小的stick, 對長度相等的, 找到weight最小的,  找到後, 設長度為l, 重量為w, 然後, 從剩下的sticks中找到長度、重量都大於等於l和w的sticks中長度及重量最小的stick,再依次找下去,如果不存在,則就直接找長度、重量最小的stick,且時間+1.

 

接下來,證明, 貪心演算法的可行性.

 

第一步, 證明總能找到一組最優解是用貪心策略得到的。

 

設該題有一個最優解 ()(l,w)()

 

設其中,(l,w)為最小的stick,則l,w一定小於之前的調用的stick的l'和w',即在調用這根stick時,set up時間一定要加一。因此,將(l,w)挪到最前方,不會減少也不會增加調用這根stick的set up time。然後,對l',w'都大於等於l,w的sticks中的l',w'最小的stick,如果它在原最優解中,不需算set up time, 那麼把它挪到(l,w)最小的那根stick後,也不會減少、不會增加set up time。如果,它在原最優解中,需要計算set up time,那麼它一定不是最優解,否則,將它挪到(l,w)後,可以減少一個set up time。 依次歸納下去,可以得到一定有一個最優解可以通過貪心策略得到。

 

第二步,證明子問題也需要最優解,顯然,如果子問題不是最優的,那麼總問題的解一定也不是最優的,因此子問題也需要最優解。

 

 #include <iostream><br />using namespace std;</p><p>int *l, *w;<br />int cnt;</p><p>#define INF 1000000</p><p>int min(int begin, int end, int pl, int pw) //找出從l[begin], w[begin]到l[end], w[end]中l,w都大於pl,pr<br />//中的l的最小值的位置,若不存在,則直接返回l的最小值的位置<br />{<br />int minl=INF, minw=INF;<br />int locate=-1;<br />for(int i=begin;i<end;i++)<br />{<br />if(l[i]>=pl && w[i]>=pw)<br />{<br />if(minl>l[i])<br />{<br />minl=l[i];<br />minw=w[i];<br />locate=i;<br />}<br />else if(minl==l[i])<br />{<br />if(minw > w[i])<br />{<br />minw=w[i];<br />locate=i;<br />}<br />}<br />}<br />}<br />if(locate==-1)<br />{<br />cnt++;<br />for(int i=begin;i<end;i++)<br />{<br />if(minl>l[i])<br />{<br />minl=l[i];<br />minw=w[i];<br />locate=i;<br />}<br />else if(minl==l[i])<br />{<br />if(minw > w[i])<br />{<br />minw=w[i];<br />locate=i;<br />}<br />}<br />}<br />}<br />return locate;<br />}</p><p>void exchange(int i, int j)<br />{<br />int temp1=l[i];<br />int temp2=w[i];<br />w[i]=w[j];<br />w[j]=temp2;<br />l[i]=l[j];<br />l[j]=temp1;<br />}</p><p>int main()<br />{<br />int n;<br />cin >> n;<br />while(n--)<br />{<br />int m;<br />cin >> m;<br />l=new int[m];<br />w=new int[m];<br />cnt=1;<br />for(int i=0;i<m;i++)<br />scanf("%d%d",&l[i],&w[i]);<br />int pl=0,pw=0;<br />for(int i=0;i<m;i++)<br />{<br />int t=min(i,m,pl,pw);<br />pl=l[t];<br />pw=w[t];<br />exchange(i,t);<br />}<br />delete [] l;<br />delete [] w;<br />cout << cnt << endl;<br />}<br />return 0;<br />}

 

 

附錄:

 

Wooden Sticks
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10697   Accepted: 4337

Description

There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows:
(a) The setup time for the first wooden stick is 1 minute.
(b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l' and weight w' if l <= l' and w <= w'. Otherwise, it will need 1 minute for setup.
You are to find the minimum setup time to process a given pile of n wooden sticks. For example, if you have five sticks whose pairs of length and weight are ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ) , and ( 4 , 1 ) , then the minimum setup time should be 2 minutes since there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1 , 2 ) , ( 2 , 5 ) .

Input

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of two lines: The first line has an integer n , 1 <= n <= 5000 , that represents the number of wooden sticks in the test case, and the second line contains 2n positive integers l1 , w1 , l2 , w2 ,..., ln , wn , each of magnitude at most 10000 , where li and wi are the length and weight of the i th wooden stick, respectively. The 2n integers are delimited by one or more spaces.

Output

The output should contain the minimum setup time in minutes, one per line.

Sample Input

3 5 4 9 5 2 2 1 3 5 1 4 3 2 2 1 1 2 2 3 1 3 2 2 3 1 

Sample Output

213

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.