ACM--移動桌子--貪心--HDOJ 1050--Moving Tables

來源:互聯網
上載者:User

標籤:時間   i++   computer   OWIN   panel   possible   manager   sizeof   uil   

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Problem DescriptionThe famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure. 



The floor has 200 rooms each on the north side and south side along the corridor. Recently the Company made a plan to reform its system. The reform includes moving a lot of tables between rooms. Because the corridor is narrow and all the tables are big, only one table can pass through the corridor. Some plan is needed to make the moving efficient. The manager figured out the following plan: Moving a table from a room to another room can be done within 10 minutes. When moving a table from room i to room j, the part of the corridor between the front of room i and the front of room j is used. So, during each 10 minutes, several moving between two rooms not sharing the same part of the corridor will be done simultaneously. To make it clear the manager illustrated the possible cases and impossible cases of simultaneous moving. 



For each room, at most one table will be either moved in or moved out. Now, the manager seeks out a method to minimize the time to move all the tables. Your job is to write a program to solve the manager’s problem. 

 

InputThe input consists of T test cases. The number of test cases ) (T is given in the first line of the input. Each test case begins with a line containing an integer N , 1<=N<=200 , that represents the number of tables to move. Each of the following N lines contains two positive integers s and t, representing that a table is to move from room number s to room number t (each room number appears at most once in the N lines). From the N+3-rd line, the remaining test cases are listed in the same manner as above. 

 

OutputThe output should contain the minimum time in minutes to complete the moving, one per line. 

 

Sample Input

3 4 10 20 30 40 50 60 70 80 2 1 3 2 200 3 10 100 20 80 30 50 
 

 

Sample Output

102030

題意:在一個狹窄的走廊裡將桌子從一個房間移動到另一個房間,走廊的寬度只能允許一個桌子通過。給出t,

   表示有t組測試資料。再給出n,表示要移動n個桌子。n下面有n行,每行兩個數字,表示將桌子從a房間

   移到b房間。走廊的分布圖如一圖所示,每移動一個桌子到達目的地房間需要花10分鐘,問移動n個桌子

   所需要的時間。

分析:

若移動多個桌子時,所需要經過的走廊沒有重合處,即可以同時移動。若有一段走廊有m個桌子都

     要經過,一次只能經過一個桌子,則需要m*10的時間移動桌子。設一個數組,下標值即為房間號。

     桌子經過房間時,該房間號為下標對應的數組值即加10。最後找到最大的數組值,即為移動完桌子

     需要的最短時間。

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6  7 int main() 8 { 9     int t,n,repeat[405],i,from,to,temp;//repeat存放重複次數,from為出發點,to為目的地10 11     scanf("%d",&t);12 13     while (t--)14     {15         scanf("%d",&n);16         memset(repeat,0,sizeof(repeat));17         while (n--)18         {19             scanf("%d %d",&from,&to);20 21             if (from>to)//可能出發位置比目的地房間大。無論大小,我們都可以看做從小的房間移動到大的房間  22             {23                 temp=to;24                 to=from;25                 from=temp;26             }27 28             if (from%2==0)//考慮實際情況,出發房間為偶數時減一,可參照題中給出的圖29             {30                 from=from-1;31             }32 33             if (to%2==1)//目的地房間為奇數時加一34             {35                 to+=1;36             }37 38             for (i=from;i<=to;i++)39             {40                 repeat[i]+=10;41             }42         }43 44         printf("%d\n",*max_element(repeat,repeat+400));//STL中尋找數列最大值函數45     }46 47     return 0;48 }

 

 

 

 

ACM--移動桌子--貪心--HDOJ 1050--Moving Tables

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.