Poj1083-moving tables-Greedy

Source: Internet
Author: User
Tags one table
Moving Tables
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 30280 Accepted: 10090

DescriptionThe 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 file. Each test case is begins with a line containing an integer n, 1 <= N <=, which represents the number of tables to M Ove.
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 the very once in the N lines). From the 3 + n-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 
2 
1 3 
2 ( 
3 
) 30 50 + + 

Sample Output

Ten
30


Main Topic

There are 400 rooms on the first floor, 200 rooms in the north and south, to move a table from one room to another, to occupy all the corridors between the two rooms, 10 minutes each to move a table, and data for the table to be moved. Ask for the minimum number of minutes required to move all the tables.

Thinking of solving problems

1 and 2 public corridors 1,3 and 4 common corridors 2 and so on. And from 2 to 4 use Corridor 1 and Corridor 2, move 1 to 2 and not move 2-4.

This is the answer to the corridor where all operations need to be recorded, and the most frequently occupied is the need to divide into so many groups of operations.

Code

#include <cstring>
#include <cstdio>
#include <iostream>
const int maxn=0x3f3f3f3f;
using namespace std;

int main ()
{
    int t;
    scanf ("%d", &t);
    while (t--)
    {
        int n;
        int i,max=0;
        int a[210]={0};
        scanf ("%d", &n);
        while (n--)
        {
            int s,e;
            scanf ("%d%d", &s,&e);
            if (s%2) s=s+1;//smaller odd numbers and larger even numbers occupy a corridor such as 1 and 2
            if (e%2) e=e+1;
            if (s>e) swap (s,e);//may move from a large room to a small room for
            (i=s/2;i<=e/2;++i)
            {
                a[i]++;
                if (A[i]>max) max=a[i];
            }
        }
        printf ("%d\n", max*10);
    }
    return 0;

}


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.