D - Moving Tables

Subject:

The famous ACM(Advanced Computer Maker) Company has rented a floor of a building whose shapeis in the following figure. 

 


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

 


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

***************************************************************************************************

Input

The input consists of T test cases. The number of testcases ) (T is given in the first line of the input. Each test case begins witha line containing an integer N , 1<=N<=200 , that represents the numberof tables to move. Each of the following N lines contains two positive integerss and t, representing that a table is to move from room number s to room numbert (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. 

Output

The output should contain the minimum time in minutes tocomplete 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

10

20

30

 ***************************************************************************************************

 题目:

      大致意思就是,搬房子,但是在搬房子的过程中,有冲突的搬房子街道不能一起搬,只能等待,搬房子街道不冲突,那么就可以一起搬,每一次搬房子都要10分钟,问至少要多久分钟才能搬完?

例如:

1 3 和2 4数据,当1搬到3的时候,1到4是不能动的,也就是街道被占用了,2到4的街道,不可以走。所以总共用时间为20分钟。

***************************************************************************************************

坑点主要在以下几点:

       (1)先要判断数据的大小,有可能数据是 4 3,也就是从4搬到3去。

        (2)一个在搬,确定哪些范围不能搬的。

从上面的图中我们可以看得出来。前面为偶数的,要改变为奇数,后面为奇数的,要改变为偶数。

例如:
2 4 。这一组数据在搬的时候,其实1 4是都不能动的。还有1 3 的这一组数据。其实代表街道 1 4之间的都不能动的。

***************************************************************************************************

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;

struct node
{
	int n,m,k;
}a[10005];

bool cmp(node n,node m)
{
	if(n.n!=m.n)
	{
		return n.n<m.n;
	}
	return n.m<m.m;
}

int main()
{
	int n; 
	int m;
    scanf("%d",&n);
	while(n--)
	{
	   scanf("%d",&m);
	   memset(a,0,sizeof(a));
	   for(int i=0;i<m;i++)
	      {
	      	scanf("%d%d",&a[i].n,&a[i].m);
	      	if(a[i].n>a[i].m)
	      		swap(a[i].n,a[i].m);
	      	if(a[i].n%2==0)
	      	a[i].n--;
	      	if(a[i].m%2==1)
	      	a[i].m++;
	      }
	   sort(a,a+m,cmp);
	   int ans =0;
          //判断哪些可以和别人一起搬房子的,
	   for(int i=0;i<m;i++)
	   {
	   	if(a[i].k==1) continue;
		  int min = a[i].m;
		  int max = a[i].n;
		  int q=0;
	   	  for(int j=i+1;j<m;j++)
	   	  {
                          //如果搬房子之间不冲突,那么就一起搬。
	   	  	 if( (min<a[j].n || max>a[j].m) && a[j].k==0)
	   	  	 {
	   	  	 	min = a[j].m;
	   	  	 	max = a[j].n;
	   	  	 	a[j].k = 1;
	   	  	 	if(q==0)
	   	  	 	{
	   	  	 		  ans+=10;
	   	  	 		  a[i].k=1;
	   	  	 		  q=1;
	   	  	 	}
	   	  	 }
	   	  }
	   }
           //哪些是只能自己搬,不能和别人一起搬房子的。
	    for(int i=0;i<m;i++)
	    {
	    	if(a[i].k==0){
	    		ans+=10;
	    	}
	    }
	   printf("%d\n",ans);
	}
   return 0;
}
***************************************************************************************************
posted @ 2017-07-13 08:25  让你一生残梦  阅读(122)  评论(0编辑  收藏  举报