Description
The 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.
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.
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 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 3 + N -rd
line, the remaining test cases are listed in the same manner as above.
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 3 + N -rd
line, the remaining test cases are 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
10 20 30
Source
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
const int MAXN = 210;
int n;
struct node{
int s, e;
bool operator <(const node a)const
{
return e < a.e;
}
}g[MAXN];
int main()
{
//freopen("d:cin.in","r",stdin);
int t, i, j;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%d%d",&g[i].s,&g[i].e);
if(g[i].s>g[i].e)
{
swap(g[i].s, g[i].e);
}
if(g[i].s%2==0)
{
g[i].s/=2;
}else{
g[i].s=(g[i].s+1)/2;
}
if(g[i].e%2==0)
{
g[i].e/=2;
}else{
g[i].e=(g[i].e+1)/2;
}
}
sort(g,g+n);
//直接查找重复最大的走廊
/*int ans = 0, sumroom[MAXN]={0};
for(i=0; i<n; i++)
{
for(j=g[i].s; j<=g[i].e; j++)sumroom[j]++;
}
for(j=1; j<=200; j++)
{
ans = max(ans,sumroom[j]);
}*/
//要保证是加入在最大房间的后面,一直在错
int ans = 0, endroom[MAXN];
for(i=0; i<n; i++)
{
int select = -1;
for(j=ans-1; j>=0; j--)
{
if(endroom[j] < g[i].s)
{
if(select==-1||endroom[select] < endroom[j])
{
select = j;
}
}
}
if(select==-1)
{
endroom[ans++] = g[i].e;
}else{
endroom[select] = g[i].e;
}
}
printf("%d\n",ans*10);
}
return 0;
}
用扫描算法做,把每次移动看做是线段,我们找某一时刻过线段最多就是答案
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
const int MAXN = 210;
int n;
struct node{
bool type; //true为左端点事件,false右端点事件
int s;
bool operator < (const node a)const
{
if(s==a.s)
{
//先判断左端点事件
return type;
}
return s<a.s;
}
}event[MAXN*2];
int top;
int main()
{
//freopen("d:cin.in","r",stdin);
int t, i, j, a, b;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
top = 0;
for(i=0; i<n; i++)
{
scanf("%d%d",&a,&b);
if(a>b)
{
swap(a,b);
}
if(a%2==0)
{
a/=2;
}else{
a=(a+1)/2;
}
if(b%2==0)
{
b/=2;
}else{
b=(b+1)/2;
}
event[top].type = true;
event[top++].s = a;
event[top].type = false;
event[top++].s = b;
}
sort(event,event+top);
int ans = 0, ct = 0;
for(i=0; i<top; i++)
{
if(event[i].type)
{
ct++;
ans = max(ans, ct);
}else{
ct--;
}
}
printf("%d\n",ans*10);
}
return 0;
}