Moving Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26540 Accepted Submission(s): 8789
Problem 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.
![](https://i-blog.csdnimg.cn/blog_migrate/0f88d1381a43a8b882b6669ab58c043d.gif)
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.
![](https://i-blog.csdnimg.cn/blog_migrate/647dc38195ec5284c681a0197b345470.gif)
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. 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 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 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
这道题目简单的方法便是区间覆盖的方法:
首先便是走廊附属问题1号房间和2号房间都对应位置1号的走廊,3号房间和四号房间对应2号走廊,如此,便是有200个这样的位置,然后我们通过观察就可以发现两边的规律,走廊号 = (房间号 + 1)/ 2(用位运算便是 >> 1).
接着就是区间覆盖方面的问题了,大家首先看完题目后,就能在心里确定一个最核心的东西,便是时间的多少,取决于相互覆盖接触的区间的多少,应该他们无法同时进行移动,如此便可以运用区间覆盖的思想,对于整体的200个走廊区间,用一个数组X[MAXN]去标记我这个走廊在我所有的移动区间中被覆盖了多少次,由于这些覆盖了同一个走廊的移动是不能够同时发生的,所以我们选取其中最大的一个数便是不能同时发生的区间移动的次数,答案便是如此。
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define pb push_back
#define mp make_pair
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
typedef long long LL;
typedef pair<int, int > PII;
typedef unsigned long long uLL;
template<typename T>
void print(T* p, T* q, string Gap = " ") {
int d = p < q ? 1 : -1;
while(p != q) {
cout << *p;
p += d;
if(p != q) cout << Gap;
}
cout << endl;
}
template<typename T>
void print(const T &a, string bes = "") {
int len = bes.length();
if(len >= 2)cout << bes[0] << a << bes[1] << endl;
else cout << a << endl;
}
const int INF = 0x3f3f3f3f;
const int MAXM = 2e5;
const int MAXN = 200 + 5;
int T, N;
int X[MAXN];
int main(){
scanf("%d", &T);
while(T --){
scanf("%d", &N);
int a, b, res = 0;
memset(X, 0, sizeof(X));
for(int i = 0;i < N;i ++){
scanf("%d%d", &a, &b);
if(a > b) swap(a, b);
a = (a + 1) >> 1;
b = (b + 1) >> 1;
for(int i = a;i <= b;i ++){
X[i] ++;
res = max(res, X[i]);
}
}
printf("%d\n", res * 10);
}
return 0;
}
接着便是贪心方法了。
对于有着工作任务方面选择的题型,大家或许都知道对工作结束点进行排序,然后选取每次最多的,如此想当然的便认为对了,但是提交上去,便会发现撞了鬼了,竟然没有过,WA了,如此便要静下心来好好的分析,虽然我们平时做的题型,对于当前任务选取最多数量,可以这一种对工作结束点进行排序然后选取的,只能够对于第一次起作用,在接下来的选取中,每一次的选择都不再是最优的选择,因为选择的可能根本不是最前沿的,导致我们本来的目的是余留更多的时间和空间,反而导致我们在这个上面翻跟头,得到的结果却不是最优的,然而,将他们的起始点进行排序,不断从最前沿开始选择,他的最本质的便是我在上面讲的区间覆盖问题,通过对他们的起始点进行排序,其实就是另一个版本的区间覆盖。
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define pb push_back
#define mp make_pair
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
typedef long long LL;
typedef pair<int, int > PII;
typedef unsigned long long uLL;
template<typename T>
void print(T* p, T* q, string Gap = " ") {
int d = p < q ? 1 : -1;
while(p != q) {
cout << *p;
p += d;
if(p != q) cout << Gap;
}
cout << endl;
}
template<typename T>
void print(const T &a, string bes = "") {
int len = bes.length();
if(len >= 2)cout << bes[0] << a << bes[1] << endl;
else cout << a << endl;
}
const int INF = 0x3f3f3f3f;
const int MAXM = 2e5;
const int MAXN = 200 + 5;
int T, N;
pair<int, int >Q[MAXN];
bool Ires[MAXN];
int main(){
scanf("%d", &T);
while(T --){
memset(Ires, false, sizeof(Ires));
scanf("%d", &N);
for(int i = 0;i < N;i ++){
scanf("%d%d", &Q[i].first, &Q[i].second);
if(Q[i].first > Q[i].second) swap(Q[i].first, Q[i].second);
Q[i].second = (Q[i].second + 1) >> 1;
Q[i].first = (Q[i].first + 1) >> 1;
}
sort(Q, Q + N);
int r = 0;
while(true){
int res = 0;
int cur = 0;
for(int i = 0;i < N;i ++){
res += Ires[i];
}
if(res == N) break;
for(int i = 0;i < N;i ++){
if(cur < Q[i].first && !Ires[i]){
cur = Q[i].second;
Ires[i] = true;
}
}
r += 10;
}
printf("%d\n", r);
}
return 0;
}