Hdu-1050-moving Tables (c + + && greedy Preliminary)

Source: Internet
Author: User

Moving TablesTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 23147 Accepted Submission (s): 7779


Problem DescriptionThe Famous ACM (Advanced computer Maker) company have rented a floor of a building whose shape was in the Following figure.



The floor has a rooms each of the 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 was big, only one table can pass through the corridor. Some plan is needed to make the moving efficient. The manager figured out of the following plan:moving a table from a hostel to another the can is done within minutes. When moving a table from the class I to the Class J, the part of the corridor between the front of the class I and the front of the class J is Used. So, during each minutes, several moving between and rooms not sharing the same part of the corridor would be done Simult aneously. To make it clear the manager illustrated the possible cases and impossible cases of simultaneous moving.



For each of the either, at the most one table would be a 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, which represents the number of tables to move . Each of the following N lines contains-positive integers s and t, representing that a table was to move from the hostel numbe R S to the guest number T (each of the number appears at a once in the N lines). From the N+3-rd line, the remaining test cases is 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

Sample Output
102030

Sourceasia 2001, Taejon (South Korea)
Recommendwe carefully selected several similar problems for you:1051 1009 1052 1789 1045
5 days no water, the whole person is not water, see this English question also looked long time, this reminds me of last week's 6 level estimate is kneeling! Grass! Anyway
First explain the main idea: there are 200 rooms in the north and south of the corridor, and the company has recently made a plan to reform the system. The reform consisted of moving a lot of tables between these rooms (I began to interpret table as a table and the whole person was silly!). Because the corridor is very narrow, then all the tables are very large. Only one table at a time can pass through the corridor. The need for this plan is to make the process of moving the table more efficient. The manager figured out the plan. Definition: 1. Move the table from one room to another within 10 minutes. 2. When a table is moved from room I to room J, the part I to J of this corridor is used. Therefore, in this 10 minutes, the corridor between the two rooms can no longer be used at the same time. In order to understand the needs of the manager, the following illustration, is to cite a number of possible and impossible occurrences of the example!
This question seems to be greedy algorithm, but I refer to a more simple way of thinking, the implementation of the topic of a sentence, the largest crossing times is the number of rounds to be transported! As long as according to this sentence, this topic can be water over! (thank Wei_xiaohui classmate of the idea!) )

Suppose that the corridor from 1 to 400 (subscript starting from 1) is a line segment, and each time a table is moved, and if you move from room 15th to room 25th, the line 15->25 repeats once. And then to move another table, assuming that this time from room 20th moved to room 30th, the segment 20->30 repeated once, the same segment: 20->25 repeated two times, the problem is converted to the maximum number of repetitions.
That would be good, a 205-size array representing the corridor position (200 corridor position), initialized to 0, if 15->25 repeated once, the 15,16,17,18,19,20 corridor position is added 1, the second segment 20->30 Repeat, then 20 .... 30 of the position plus 1. When all is done, find the maximum location of the corridor and multiply it by 10 minutes, which is the time!
#include <iostream> #include <cstdio> #include <cstring>using namespace Std;int a[205];int main () {    int t,n;    scanf ("%d", &t);    while (t--)    {        memset (a,0,sizeof (a));        int start,finish;        scanf ("%d", &n);        for (int i=0;i<n;i++)        {            scanf ("%d%d", &start,&finish);            if (start>finish)            {                swap (start,finish);            }            Start = (start+1)/2;            finish = (finish+1)/2;            for (int j=start;j<=finish;j++)            {                a[j]++;            }        }        int MAX = 0;        for (int i=0;i<205;i++)        {            if (A[i]>max)            {                max=a[i];            }        }        printf ("%d\n", max*10);    }    return 0;}




Hdu-1050-moving Tables (c + + && greedy Preliminary)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.