题意:
400个房间如图所示,然后给你 n <script type="math/tex" id="MathJax-Element-78">n</script> 个起始房间和终点房间,要通过中间的走廊把起始房间的桌子移到终点房间。每移动一次会消耗10min时间,当移动产生重叠的时候,需要两次。求最少的时间。
思路:
是不是很像给你n个区间,然后让你分类,使得每一类所有区间不重叠,然后求最少的类,时间就是|类|*10.
那么先排序一下,然后拿一个数组存当前所有类的包含的最远的右端点,so对于每个区间,二分找一个比这个区间Left小的,且离得最近的右端点,然后把这个区间塞到这个类里就好了。
PS: 区间这里要转个弯 :1 2属于同一位置,2 3属于同一位置,4 5属于同一位置,….so?
code:
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<math.h>
#include<algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long LL;
//#pragma comment(linker, "/STACK:102400000,102400000")
const int N=2e2+10;
int n;
struct asd{
int Left,Right;
}seg[N];
bool cmp(asd x,asd y){
if(x.Left == y.Left) return x.Right<y.Right;
return x.Left<y.Left;
}
int cnt;
int R[N];
void solve(int i,int Left,int Right){
int w = seg[i].Left;
while(Left<Right){
int Mid=Left+(Right-Left+1)/2;
if(w>R[Mid])
Left=Mid;
else
Right=Mid-1;
}
if(R[Left]<w)
R[Left]=seg[i].Right;
else
R[++cnt]=seg[i].Right;
sort(R+1,R+cnt+1);
}
int main(){
int T,u,v;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d%d",&u,&v);
u=(u+1)/2;
v=(v+1)/2;
seg[i].Left=min(u,v);
seg[i].Right=max(u,v);
}
sort(seg,seg+n,cmp);
cnt=1;
R[1]=seg[0].Right;
for(int i=1;i<n;i++)
solve(i,1,cnt);
printf("%d\n",10*cnt);
}
return 0;
}