HDU1050 (Moving Tables: greedy algorithm)

Source: Internet
Author: User

Problem Solving Ideas:

This approach is based on the hdu2037 approach, finding all the tables that can be transported at the same time, and then it is convenient to find the shortest total time.

There is also an easier way to go straight through to find the area with the most repetition times and repeat the number of *10 to get the result.

Moving Tables

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 28033 Accepted Submission (s): 9170

Problem Description

The famous ACM (Advanced computer Maker) company had 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.

Input

The 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.

Output

The 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

Source

Asia 2001, Taejon (South Korea)

Recommend

We have carefully selected several similar problems for you:1051 1052 1789 1045 1053

Statistic | Submit | Discuss | Note

1 //#define _crt_secure_no_deprecate2#include <stdio.h>3#include <stdlib.h>4#include <iostream>5#include <algorithm>6 7 using namespacestd;8 9 intcmpConst int*a,Const int*b) {Ten     if(a[0] = = b[0])returna[1] < b[1]; One     Else returna[0] < b[0]; A } -  - intMain () { the     intN; -CIN >>N; -      while(n--) { -         intT; +         int**the hostel ; -         intTime =0; +         intT1 =0; A         intflag[ the] = {0};//used to determine if the handling has been carried out.  athostel =New int*[ the]; -CIN >>T; -          for(inti =0; I < T; i++) { -Room[i] =New int[2]; -CIN >> room[i][0] >> room[i][1]; -             if(room[i][0] > room[i][1]) { in                 inttemp = room[i][0]; -room[i][0] = room[i][1]; toroom[i][1] =temp; +             } -             if(room[i][1] %2==1) room[i][1]++;//here is the pit point of the problem, because there are rooms on both sides of the corridor, so the             if(room[i][0] %2==0) room[i][0]--;//Rooms 3 and 4th have a common corridor.  *         } $Sort (Hostel, +T, CMP);Panax Notoginseng          for(inti =0; I < T; i++) { -             if(Flag[i] = =0) { theTime + =Ten; +                 intBoundary = room[i][1]; A                  for(intj = i +1; J < T; J + +) {//find out the number of times that can be carried out concurrently with the I handling the                     if(room[j][0] > Boundary && flag[j] = =0) { +Boundary = room[j][1]; -FLAG[J] =1; $                         /*printf ("%d%d%d\n", I, time, boundary); Error correction statements*/ $                     } -                 } -             } the         } -cout << Time <<Endl;Wuyi         /*for (int i = 0; i < T; i++) { the cout << room[i][0] << room[i][1] << Endl; -         }*/ Wu     } -     //System ("pause"); About     return 0; $}

HDU1050 (Moving Tables: greedy algorithm)

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.