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.
3 4 10 20 30 40 50 60 70 80 2 1 3 2 200 3 10 100 20 80 30 50
10 20 30
数学模型:
数轴上一些区间的集合,最少要将这一个集合划分分成多少个集合,才能保证被划分的集合中没有两个区间有重叠。
解决:
如果将每一个区间看成一个元素,则两个区间A、B有以下三种关系:区间A位于区间B左边;区间A位于区间B右边;区间A与区间B重叠。显然,如果区间A位于区间B左边,区间B位于区间C左边,则区间A位于区间C 左边(传递),区间之间的“位于左边”是一个偏序关系。原问题是一个划分问题,集合划分问题中有一个著名的Diworth引理:
定理:
一个集合的链最大长度为k,那么它的反链划分个数至少为k,并且,可以取到k。
对偶定理(Diworth对偶):
一个集合的反链最大长度为k,那么它的连划分个数至少为k,并且,可以取到k。
原题目的转化:
显然,我们需要所集合划分成一些链才能保证没有两个区间重叠,根据定理,划分链的最小个数是反链集元素数目的最大个数,而两个区间互为反链,当且仅当这两个区间重叠,如果两个区间不重叠,则这两个区间就构成链。如果一个区间的集合是反链集,则要求这个集合中任何两个元素(区间)都不可比(即任意两个区间都重叠),现在问题转化得更简单了:找一个原集合的子集,子集内的任意两个区间均有重叠。
原问题的解:
事实上,如果一个集合中任意两个区间有公共点(重叠),则存在一个区间被这个集合中任意一个区间包含(即这个集合的所有区间的交不为空),现在问题进一步转化为,找一个点,使得它被最多的区间包含,这个就简单了,一个一个地试区间的断点,总有一个满足条件。
另外一个问题:
上面提到, 如果一个集合中任意两个区间有公共点(重叠),则存在一个区间被这个集合中任意一个区间包含(即这个集合的所有区间的交不为空),事实上,这个问题与海莱凸包定理极为类似,海莱定理表明,平时凸集中,如果任何3个有公共点,则这集合中所有凸包有一个公共点;另外,还可以延伸到三维,即空间中凸体集,若任意4个都有公共点,则这整个凸休集有一个公共点。
原问题的代码:
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using std::max;
using std::swap;
typedef struct Record{
public:
int iStart;
int iEnd;
}Record;
Record r[1000];
int n;
void Handle();
int Count(int x);
int main(){
int icase;
scanf("%d", &icase);
while(icase--){
Handle();
}
}
void Handle(){
scanf("%d",&n);
for(int i = 0; i < n; ++i){
scanf("%d %d", &r[i].iStart, &r[i].iEnd);
r[i].iStart = (r[i].iStart + 1) >> 1;
r[i].iEnd = (r[i].iEnd + 1) >> 1;
if(r[i].iStart > r[i].iEnd){
swap(r[i].iStart , r[i].iEnd);
}
}
int cc = 0;
for(int i = 0; i < n; ++i){
cc = max(cc, Count(r[i].iStart));
cc = max(cc, Count(r[i].iEnd));
}
cc = (cc << 3) + (cc << 1);
printf("%d\n", cc);
}
int Count(int x){
int cc = 0;
for(int i = 0; i < n; ++i){
if(r[i].iStart <= x && r[i].iEnd >= x){
++cc;
}
}
return cc;
}