Description
Xenia the vigorous detective faced n(n ≥ 2) foreign spies lined up in a row. We’ll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy’s number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, …, ri(1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can’t do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies’ cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You’ve got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f(1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri(1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < … < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies’ actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal “L”. If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal “R”. If the spy must keep the note at the i-th step, the i-th character must equal “X”.
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Sample Input
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR
题意:
两个间谍要做不得了的勾当,把见不得人的东西从数轴上的s位置传递到f位置,但碍于人多眼杂,在t时间内的l~r领域内是禁止活动的地带,只能就地等候直到风头过了,求行动指南(L向左R向右X原地潜伏)。
分析:
只要把题意理清就可以有一定思路了:
首先,间谍只能向一个方向移动或不动,否则就很不划算了。。
再者,不在有禁止活动规定的时间内行动,便可以大胆向着目的地前进。。
所以,行动指南由两个部分组成:
第一次禁止活动的时刻到最后一次禁止活动的时刻的这段时间内需要判断是否可以继续活动,
其余时间妹妹你大胆地向前走啊~这段时间又可以分为第一次之前与最后一次之后。
于是在进行充分剪枝后,向着一个方向暴力即可。
要注意的是,如果需要开数组,请在main函数之外来开,一把辛酸泪。。。
代码:
代码1是当时直接鼓捣出来的,时间空间效率都要差些;
代码2就相对高精尖一些了。
/*代码1*/
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
lint t[100005];
lint r[100005];
lint l[100005];
int main(){
lint n,m,s,f;
memset(t,0,sizeof(t));
memset(r,0,sizeof(r));
memset(l,0,sizeof(l));
cin >> n >> m >> s >> f;
// cout << n << m << s << f << endl;
for( lint i = 1 ; i <= m ; i++ ){
cin >> t[i] >> l[i] >> r[i];
}
lint step = 1;
lint i = 1;
while(s != f){
if(step < t[i]){
if(s < f){
cout << 'R';
step++;
s++;
}
else{
cout << 'L';
step++;
s--;
}
}
else{
if(s < f){
if(l[i] > s + 1 || r[i] < s){
cout << 'R';
step++;
s++;
}
else{
cout << 'X';
step++;
}
}
else{
if(l[i] > s || r[i] < s - 1){
cout << 'L';
step++;
s--;
}
else{
cout << 'X';
step++;
}
}
i++;
}
}
return 0;
}
/*代码2*/
#include<cstdio>
#include<algorithm>
#include<sstream>
#include<string>
#include<string.h>
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int n,m,s,f,dir,t,l,r,step=1;
cin>>n>>m>>s>>f;
char c;
if(s<f){c='R';dir=1;}
else {c='L';dir=-1;}
while(scanf("%d%d%d",&t,&l,&r)==3)
{
while(step!=t)
{
step++;
printf("%c",c);
s+=dir;
if(s==f)exit(0);
}
if(step==t)
{
if((r>=s+dir&&s+dir>=l)||(r>=s&&s>=l)){step++;printf("X");}
else{step++;s+=dir;printf("%c",c);}
}
if(s==f)exit(0);
}
while(s!=f)
{
printf("%c",c);
s+=dir;
}
}