Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import com.sun.corba.se.spi.orb.ParserImplBase; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; /** * Created with IntelliJ IDEA. * User: AUtemuratov * Date: 07.04.14 * Time: 15:43 * To change this template use File | Settings | File Templates. */ public class Main { static FastReader fr = new FastReader(new BufferedReader(new InputStreamReader(System.in))); static PrintWriter pw = new PrintWriter(System.out); static int n,m,cnt,s,f,min = Integer.MAX_VALUE; static int a[] = new int[111111]; static StringBuilder ans = new StringBuilder(); public static void main(String[] args) throws Exception { n = fr.nextInt(); m = fr.nextInt(); s = fr.nextInt(); f = fr.nextInt(); int cur = s; int dir; if (f-s>0) dir = 1; else dir = -1; int l = -1; int r = -1; for (int i=1,h=1; i<=m; i++,h++) { int t = fr.nextInt(); if (t!=h) { l = -1; r = -1; for (int j=t; j<h; j++) { if (cur==f) break; if ((cur+dir<l && cur<l) || (cur+dir>r && cur>r)) { if (dir==1) { ans.append('R'); cur++; } else { ans.append('L'); cur--; } } else { ans.append('X'); } } } else { l = fr.nextInt(); r = fr.nextInt(); } if (cur==f) continue; if ((cur+dir<l && cur<l) || (cur+dir>r && cur>r)) { if (dir==1) { ans.append('R'); cur++; } else { ans.append('L'); cur--; } } else { ans.append('X'); } } for (int i=1; i<=Math.abs(f-cur); i++) { if (dir==1) ans.append('R'); else ans.append('L'); } pw.println(ans); pw.close(); } } class Pair { int l,r; public Pair(int pos, int dif) { this.l = pos; this.r = dif; } } class FastReader { BufferedReader bf; StringTokenizer tk = null; String DELIM = " "; public FastReader(BufferedReader bf) { this.bf = bf; } public String nextToken() throws Exception { if(tk==null || !tk.hasMoreTokens()) { tk = new StringTokenizer(bf.readLine(),DELIM); } return tk.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public int nextLong() throws Exception { return Integer.parseInt(nextToken()); } public int nextDouble() throws Exception { return Integer.parseInt(nextToken()); } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; long long tpp = 9e18; int tp = 1e9; long long n, m, s, f, t, d, l, r, prev = 0; string Q; char ch; vector<int> a; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> s >> f; if (s < f) { ch = 'R'; d = 1; } else { ch = 'L'; d = -1; } int x = s, T = 1; for (int i = 1; i <= m; i++) { cin >> t >> l >> r; ; while (T < t) { if (x == f) break; T++; x += d; cout << ch; } if (x == f) break; if ((x < l or x > r) and (x + d < l or x + d > r)) { x += d; cout << ch; } else cout << "X"; T++; } while (x != f) { x += d; cout << ch; } cout << endl; return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
n, m, s, f = map(int, input().split()) if s < f: d = 1 c = 'R' else: d = -1 c = 'L' res = '' i, j = 1, s t, l, r = map(int, input().split()) k = 1 while j != f: if i > t and k < m: t, l, r = map(int, input().split()) k += 1 if i == t and (l <= j <= r or l <= j + d <= r): res += 'X' else: res += c j += d i += 1 print(res)
PYTHON3
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int s, f; string ans; void move() { if (s == f) return; if (s < f) { s++; ans += "R"; } else { s--; ans += "L"; } } int main() { int n, m, i, t, a, b, prev; cin >> n >> m >> s >> f; for (prev = i = 1; i <= m; i++) { cin >> t >> a >> b; if (s == f) continue; while (prev < t) { prev++; move(); if (s == f) break; } prev = t + 1; if (s == f) continue; if (s < a or s > b) { if (s < f and (s + 1 < a or s + 1 > b)) move(); else if (s > f and (s - 1 < a or s - 1 > b)) move(); else ans += "X"; } else ans += "X"; } while (s != f) move(); cout << ans; return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; const long long mod = 1000000007; const double pi = 3.1415926536; int main() { cin.tie(0); std::ios::sync_with_stdio(false); int n, m, s, f; cin >> n >> m >> s >> f; int step[m]; vector<pair<int, int> > rl; string ans = ""; int d = 1; char c = 'R'; if (f < s) { d = -1, c = 'L'; } for (int i = 0; i < m; i++) { cin >> step[i]; int x, y; cin >> x >> y; rl.push_back(make_pair(x, y)); } int b = 0; for (int i = 1; s != f; i++) { int x = 0, y = 0; if (i == step[b]) { x = rl[b].first, y = rl[b++].second; } if ((s >= x && s <= y) || (s + d >= x && s + d <= y)) ans += 'X'; else ans += c, s += d; } cout << ans; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s, f; cin >> n >> m >> s >> f; vector<vector<int> > a; int t, l, r; vector<int> q; for (int i = 0; i < m; ++i) { cin >> t >> l >> r; q.push_back(t); q.push_back(l); q.push_back(r); a.push_back(q); q.clear(); } sort(a.begin(), a.end()); int k = 0; t = 1; while (s != f) { if (s < f) { if (k < a.size()) { if (a[k][0] == t && ((s >= a[k][1] && s <= a[k][2]) || (s + 1 >= a[k][1] && s + 1 <= a[k][2]))) { cout << "X"; } else { s++; cout << "R"; } if (a[k][0] == t) { k++; } } else { cout << "R"; s++; } } else { if (k < a.size()) { if (a[k][0] == t && (s >= a[k][1] && s <= a[k][2] || s - 1 >= a[k][1] && s - 1 <= a[k][2])) { cout << "X"; } else { s--; cout << "L"; } if (a[k][0] == t) k++; } else { s--; cout << "L"; } } t++; } }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
n, m, s, f = map(int, input().split()) if s < f: step = "R" step_i = 1 else: step = "L" step_i = -1 ans = [] tp = 0 for i in range(m): t, l, r = map(int, input().split()) k = min(t - tp - 1, abs(f - s)) for j in range(k): ans.append(step) s += step_i * k if s == f: print("".join(ans)) break if not l <= s + step_i <= r and not l <= s <= r: s += step_i ans.append(step) else: ans.append("X") tp = t else: if s != f: ans.extend([step] * abs(f - s)) print("".join(ans))
PYTHON3
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 10; struct node { int t; int x; int y; } tt[maxn]; int main() { int n, m, s, f; while (~scanf("%d%d%d%d", &n, &m, &s, &f)) { memset(tt, 0, sizeof tt); for (int i = 0; i < m; i++) { scanf("%d%d%d", &tt[i].t, &tt[i].x, &tt[i].y); } int cnt = 0; int time = 1; int peo = s; int jj; if (f > s) jj = 1; else jj = -1; while (peo != f) { if (tt[cnt].t != time) { peo += jj; if (jj == 1) { printf("R"); } else printf("L"); } else if ((peo < tt[cnt].x || peo > tt[cnt].y) && ((peo + jj) < tt[cnt].x || (peo + jj) > tt[cnt].y)) { peo += jj; if (jj == 1) { printf("R"); } else printf("L"); cnt++; } else { printf("X"); cnt++; } time++; } printf("\n"); } return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.*; import java.util.*; public class Main { private static boolean inRange(int x, int l, int r) { return x >= l && x <= r; } private static void solve(InputReader in, OutputWriter out) { int n = in.nextInt(); int m = in.nextInt(); int s = in.nextInt(); int f = in.nextInt(); int[][] ar = new int[m][3]; for (int i = 0; i < m; i++) { ar[i][0] = in.nextInt(); ar[i][1] = in.nextInt(); ar[i][2] = in.nextInt(); } char dir = (s < f) ? 'R' : 'L'; int val = (s < f) ? 1 : -1; int stage = 0, pointer = 0; while (s != f) { stage++; int next = s + val; if (pointer < m && ar[pointer][0] == stage) { int l = ar[pointer][1], r = ar[pointer][2]; if (inRange(s, l, r) || inRange(next, l, r)) { out.print('X'); } else { out.print(dir); s = next; } pointer++; } else { out.print(dir); s = next; } } } private static void shuffleArray(int[] array) { int index; Random random = new Random(); for (int i = array.length - 1; i > 0; i--) { index = random.nextInt(i + 1); if (index != i) { array[index] ^= array[i]; array[i] ^= array[index]; array[index] ^= array[i]; } } } public static void main(String[] args) { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); solve(in, out); in.close(); out.close(); } private static class InputReader { private BufferedReader br; private StringTokenizer st; InputReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); st = null; } String nextLine() { String line = null; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return line; } String next() { while (st == null || !st.hasMoreTokens()) { String line = nextLine(); if (line == null) return null; st = new StringTokenizer(line); } return st.nextToken(); } byte nextByte() { return Byte.parseByte(next()); } short nextShort() { return Short.parseShort(next()); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } private static class OutputWriter { BufferedWriter bw; OutputWriter(OutputStream os) { bw = new BufferedWriter(new OutputStreamWriter(os)); } void print(int i) { print(Integer.toString(i)); } void println(int i) { println(Integer.toString(i)); } void print(long l) { print(Long.toString(l)); } void println(long l) { println(Long.toString(l)); } void print(double d) { print(Double.toString(d)); } void println(double d) { println(Double.toString(d)); } void print(boolean b) { print(Boolean.toString(b)); } void println(boolean b) { println(Boolean.toString(b)); } void print(char c) { try { bw.write(c); } catch (IOException e) { e.printStackTrace(); } } void println(char c) { println(Character.toString(c)); } void print(String s) { try { bw.write(s); } catch (IOException e) { e.printStackTrace(); } } void println(String s) { print(s); print('\n'); } void close() { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import math import sys def main(): n, m, s, f = map(int, raw_input().split()) t = 1 xenias = [] for i in range(m): next_t, l, r = map(int, raw_input().split()) xenias.append((next_t, l, r)) index = 0 current_spy = s if f > s: d = 'R' next_spy = s + 1 else: d = 'L' next_spy = s - 1 out = '' while current_spy != f: if index < len(xenias): next_t, l, r = xenias[index] else: next_t = -1 if next_t == t: if current_spy >= l and current_spy <= r or next_spy >= l and next_spy <= r: out += 'X' else: out += d if d == 'R': current_spy, next_spy = next_spy, next_spy + 1 else: current_spy, next_spy = next_spy, next_spy - 1 index += 1 else: out += d if d == 'R': current_spy, next_spy = next_spy, next_spy + 1 else: current_spy, next_spy = next_spy, next_spy - 1 t += 1 print out if __name__ == '__main__': main()
PYTHON
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int n, m, s, f; int main() { cin >> n >> m >> s >> f; int t = 0, l, r; int perv = -1; for (int i = (0), _end = (m); i < _end; ++i) { perv = t; cin >> t >> l >> r; for (int j = (0), _end = (abs(t - perv) - 1); j < _end; ++j) { if (s < f) putchar('R'), ++s; else if (s > f) putchar('L'), --s; else { cout << endl; return 0; } } if (s == f) { cout << endl; return 0; } if (s < f) { if (s + 1 < l || s > r) putchar('R'), ++s; else putchar('X'); } else if (s > f) { if (s < l || s - 1 > r) putchar('L'), --s; else putchar('X'); } else { cout << endl; return 0; } } while (s != f) if (s < f) putchar('R'), ++s; else putchar('L'), --s; cout << endl; return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
n,m,s,f = map (int, input().split()) watch = {} for i in range (m): t,l,r = map (int, input().split()) watch[t] = (l,r) cur = s move,symbol = (1,'R') if s < f else (-1,'L') step = 1 while cur != f: if step in watch: l,r = watch[step] if (l <= cur <= r or l <= (cur+move) <= r): print ('X',end='') else: cur += move print (symbol,end='') else: cur += move print (symbol,end='') step += 1
PYTHON3
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class XeniaAndSpies { private static final char PASS_TO_LEFT = 'L'; private static final char PASS_TO_RIGHT = 'R'; private static final char KEEP_IT = 'X'; private static int numSpies; public static void main(String... args) { Scanner in = new Scanner(System.in); numSpies = in.nextInt(); int numWatches = in.nextInt(); int spyA = in.nextInt(); int spyB = in.nextInt(); int timeInstant = 1; StringBuilder output = new StringBuilder(); while (spyA != spyB) { if (numWatches == 0) { int spyAdjustment = passNoteOn(output, spyA, spyB); spyA += spyAdjustment; } else { int instantOfWatch = in.nextInt(); int leftMostWatchedSpy = in.nextInt(); int rightMostWatchedSpy = in.nextInt(); if (timeInstant < instantOfWatch) { for (int i = 0; i < instantOfWatch - timeInstant; i++) { int spyAdjustment = passNoteOn(output, spyA, spyB); spyA += spyAdjustment; if (spyA == spyB) { break; } } timeInstant = instantOfWatch; } if (spyA != spyB) { int neighbourSpy = getNeighbour(spyA, spyB); if (neighbourBeingWatched(spyA, neighbourSpy, leftMostWatchedSpy, rightMostWatchedSpy)) { output.append(KEEP_IT); } else { int spyAdjustment = passNoteOn(output, spyA, spyB); spyA += spyAdjustment; } } numWatches--; } timeInstant++; } System.out.println(output.toString()); } private static int passNoteOn(StringBuilder output, int spyA, int spyB) { if (spyA < spyB) { output.append(PASS_TO_RIGHT); return +1; } else { output.append(PASS_TO_LEFT); return -1; } } private static int getNeighbour(int spyA, int spyB) { if(spyA < spyB) { return spyA + 1; } return spyA - 1; } private static boolean neighbourBeingWatched(int spyA, int neighbourSpy, int leftMostWatchedSpy, int rightMostWatchedSpy) { return (leftMostWatchedSpy <= spyA && spyA <= rightMostWatchedSpy) || (leftMostWatchedSpy <= neighbourSpy && neighbourSpy <= rightMostWatchedSpy); } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.*; import java.util.StringTokenizer; /** * @author kperikov */ public class B implements Runnable { PrintWriter out; BufferedReader br; StringTokenizer st; public static void main(String[] args) throws IOException { new Thread(new B()).start(); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public void run() { try { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new BufferedOutputStream(System.out)); solve(); out.close(); } catch (IOException e) { throw new IllegalStateException(e); } } public void solve() throws IOException { int n = nextInt(); int m = nextInt(); int s = nextInt(); int f = nextInt(); int t[] = new int[m]; int[] l = new int[m]; int[] r = new int[m]; for (int i = 0; i < m; ++i) { t[i] = nextInt(); l[i] = nextInt(); r[i] = nextInt(); } StringBuilder sb = new StringBuilder(); char dir; int dx; if (s < f) { dir = 'R'; dx = 1; } else { dir = 'L'; dx = -1; } int curr = s; int currT = 0; for (int i = 1; i <= n + m; ++i) { if (curr == f) { out.println(sb.toString()); return; } if (currT < m && t[currT] == i) { if ((curr >= l[currT] && curr <= r[currT]) || (curr + dx >= l[currT] && curr + dx <= r[currT])) { sb.append("X"); } else { sb.append(dir); curr += dx; } ++currT; } else { sb.append(dir); curr += dx; } } } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import sys from itertools import * from math import * def solve(): n,m,s,f = map(int, input().split()) s-=1 f-=1 d = {} for _ in range(m): t,l,r = map(int, input().split()) d[t-1] = (l - 1, r - 1) # d = {(t - 1) : (l - 1, r - 1) for t,l,r in map(int, input().split()) for _ in range(m)} step = 0 res = list() while s != f: wantnext = s + 1 if s < f else s - 1 canmove = True if step in d: l, r = d[step] if (s >= l and s <= r) or (wantnext >= l and wantnext <= r): canmove = False if canmove: res.append('R' if wantnext > s else 'L') s = wantnext else: res.append('X') step += 1 print(''.join(map(str, res))) #change to string at end to see if faster if sys.hexversion == 50594544 : sys.stdin = open("test.txt") solve()
PYTHON3
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int dx8[8] = {-1, 1, 0, 0, 1, 1, -1, -1}; int dy8[8] = {0, 0, 1, -1, 1, -1, 1, -1}; void file() {} void fast() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); } int check(long long x) { long long sum = 0; long long x1 = x; while (x1 != 0) { sum += x1 % 10; x1 /= 10; } return sum; } int main() { int n, m, s, f, si, nxt; cin >> n >> m >> s >> f; vector<int> t(m), l(m), r(m); for (int i = 0; i < m; i++) cin >> t[i] >> l[i] >> r[i]; if (f - s > 0) si = 1; else si = -1; for (int i = 1, j = 0; s != f; i++) { nxt = s + si; if (j < m && t[j] == i) { if ((s >= l[j] && s <= r[j]) || (nxt >= l[j] && nxt <= r[j])) cout << 'X'; else { s = nxt; if (si > 0) cout << 'R'; else cout << 'L'; } j++; } else { s = nxt; if (si > 0) cout << 'R'; else cout << 'L'; } } }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
def prog(): inp = raw_input().split() n = int(inp[0]) m = int(inp[1]) s = int(inp[2]) f = int(inp[3]) start = s ans = "" curr = 1 if(s>f): di = "L" step = -1 else: di = "R" step = 1 for ind in xrange(1,m+1): if(start == f): raw_input() continue an = True inp = raw_input().split() t = int(inp[0]) l = int(inp[1]) r = int(inp[2]) if(t > curr): z = min((t-curr),abs(f-start)) ans += di*z start += step*z if(start == f): continue curr = t+1 a = start b = start + step xy = True if( l<=a and a<=r): xy = False elif( l<=b and b<=r): xy = False if(xy==True): start += step ans += di else: ans += "X" ans += di*(abs(f-start)) print ans prog()
PYTHON
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; map<int, pair<int, int> > T; int main() { int N, M, S, F; scanf("%d%d%d%d", &N, &M, &S, &F); for (int i = 0; i < M; i++) { int A, B, C; cin >> A >> B >> C; T[A].first = B; T[A].second = C; } int step = 1; int cur = S; while (true) { if (cur == F) break; int nxt = F > S ? cur + 1 : cur - 1; if ((cur >= T[step].first && T[step].second >= cur) || (nxt >= T[step].first && T[step].second >= nxt)) { printf("%c", 'X'); nxt = cur; } else printf("%c", F > S ? 'R' : 'L'); step++; cur = nxt; } }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
n, m, s, f = [int(x) for x in input().split()] now = 1 d = 1 if f > s else -1 step = 'R' if d == 1 else 'L' ret = "" for _ in range(m): t, l, r = [int(x) for x in input().split()] if t > now: flag = False while t > now: s += d ret += step now += 1 if s == f: flag = True break if flag: break if l <= s <= r or l <= s+d <= r: ret += 'X' else: ret += step s += d if s == f: break now += 1 while s != f: s += d ret += step print(ret)
PYTHON3
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
n, m, s, f = map(int, input().split()) l = [] for i in range(m): t, c,r = map(int, input().split()) l.append([t, c, r]) if s > f: st = "" t = 1 i = 0 while(i<m): if t == l[i][0]: if (s<l[i][1] or s>l[i][2]) and (s-1<l[i][1] or s-1>l[i][2]): s-=1 st+="L" else: st+="X" i+=1 if s == f: break else: st+='L' s-=1 if s == f: break t+=1 while(s>f): s-=1 st+='L' print(st) if s<f: st = "" t = 1 i = 0 while(i<m): if t == l[i][0]: if (s<l[i][1] or s>l[i][2]) and (s+1<l[i][1] or s+1>l[i][2]): s+=1 st+="R" else: st+="X" i+=1 if s == f: break else: st+='R' s+=1 if s == f: break t+=1 while(s<f): s+=1 st+='R' print(st)
PYTHON3
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.Scanner; public class C_199_D2_B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int s = sc.nextInt(); int f = sc.nextInt(); int[][] a = new int[m][3]; for (int i = 0; i < m; i++) { for (int j = 0; j < 3; j++) { a[i][j] = sc.nextInt(); } } int pos; String lr; if (s > f) { pos = -1; lr = "L"; } else { pos = 1; lr = "R"; } int count = 0; int i = 0; while( s != f) { i++; if (count < m && a[count][0] == i){ if ((a[count][1] > s || a[count][2] < s) && (a[count][1] > s + pos || a[count][2] < s + pos) ){ System.out.print(lr); s += pos; } else { System.out.print("X"); } count++; } else { System.out.print(lr); s += pos; } } sc.close(); } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> int main() { int n, m, s, f; scanf("%d %d %d %d", &n, &m, &s, &f); int ct = 1; while (m--) { int t, l, r; scanf("%d %d %d", &t, &l, &r); while (ct < t && s != f) { if (s < f) { printf("R"); s++; } else { printf("L"); s--; } ct++; } ct = t + 1; if (s < f) { if (r < s || l > s + 1) { printf("R"); s++; } else { printf("X"); } } else if (s > f) { if (r < s - 1 || l > s) { printf("L"); s--; } else { printf("X"); } } else { break; } } while (s < f) { printf("R"); s++; } while (s > f) { printf("L"); s--; } return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; vector<long long int> prm(1000010, 1); long long int P = 1e9 + 7; long long int M = 998244353; char adjlist[1001][1001]; bool visited[1000001] = {false}; long double pi = 3.14159265358979323846264338327950; map<int, int> visit; map<int, set<int>> g; int cc = 0; vector<pair<int, int>> moves = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int lps[1000000]; vector<pair<int, int>> final_set; bool comp(const pair<long long int, long long int> &a, const pair<long long int, long long int> &b) { if (a.first == b.first) return a.second > b.second; return a.first < b.first; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, m, s, f; cin >> n >> m >> s >> f; bool flag = true; long long int curr = s; if (s > f) flag = false; long long int i, j; long long int t, x, y; bool mflag = false; long long int back = 0; string str = ""; for (i = 0; i < m; i++) { if (i == 0) back = 0; else back = t; cin >> t >> x >> y; if (curr == f) continue; if (t - 1 != back) { while (back + 1 < t) { if (flag == false) { str.push_back('L'); curr--; } else { str.push_back('R'); curr++; } if (curr == f) break; back++; } } if (curr == f) continue; if (flag == false) { if (curr < x || curr - 1 > y) { str.push_back('L'); curr--; continue; } else { str.push_back('X'); continue; } } else { if (curr > y || curr + 1 < x) { str.push_back('R'); curr++; continue; } else { str.push_back('X'); continue; } } } if (curr != f) { if (flag == false) { for (i = curr; i > f; i--) str.push_back('L'); } else { for (i = curr; i < f; i++) str.push_back('R'); } } cout << str << "\n"; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.InputStreamReader; import java.io.IOException; import java.util.HashMap; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author nasko */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int s = in.nextInt(); int f = in.nextInt(); HashMap<Integer,Integer> leftInterval = new HashMap<Integer,Integer>(); HashMap<Integer,Integer> rightInterval = new HashMap<Integer,Integer>(); for(int i = 0; i < m; ++i) { int time = in.nextInt(); int left = in.nextInt(); int right = in.nextInt(); leftInterval.put(time,left); rightInterval.put(time,right); } StringBuilder sb = new StringBuilder(""); int current = s; int time = 1; while(true) { if(current == f) { if(!leftInterval.containsKey(time)) break; else { if(current < leftInterval.get(time) || current > rightInterval.get(time)) { break; } else sb.append("X"); } } else { if (!leftInterval.containsKey(time)) { if (current < f) { current++; sb.append("R"); } else if (current > f) { --current; sb.append("L"); } if(current == f) break; } else { if (current < f) { if (current + 1 < leftInterval.get(time)) { current++; sb.append("R"); } else if (current > rightInterval.get(time)) { ++current; sb.append("R"); } else sb.append("X"); } else { if(current > rightInterval.get(time) && current - 1 > rightInterval.get(time)) { --current; sb.append("L"); } else if(current < leftInterval.get(time)) { --current; sb.append("L"); } else sb.append("X"); } if(current == f) break; } } ++time; } out.println(sb); } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.*; import java.util.*; public class XeniaAndSpies { public static void main(String[] args) throws IOException { //Locale.setDefault (Locale.US); Reader in = new Reader(); StringBuilder out = new StringBuilder(); int n, m, s, f, t, l , r, curr, mv; char cr; //for (int i = 0; i < 8; i++) { n = in.nextInt(); m = in.nextInt(); s = in.nextInt(); f = in.nextInt(); curr=s; if(s < f){ mv = 1; cr = 'R'; } else{ mv = -1; cr = 'L'; } for (int j = 0, c = 1; j < m; j++, c++) { t = in.nextInt(); l = in.nextInt(); r = in.nextInt(); while( t > c && curr != f){ out.append(cr); curr+=mv; c++; } if(curr==f)continue; if( (curr+mv < l || curr+mv > r) && (curr < l || curr > r)){ out.append(cr); curr+=mv; } else out.append("X"); } while(curr != f) { curr+=mv; out.append(cr); } System.out.println(out); out = new StringBuilder(); } } static class Reader { BufferedReader br; StringTokenizer st; Reader() { // To read from the standard input br = new BufferedReader(new InputStreamReader(System.in)); } Reader(int i) throws IOException { // To read from a file br = new BufferedReader(new FileReader("Sample Input.txt")); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; struct c_istream { c_istream operator>>(int &n) { scanf("%i", &n); return *this; } c_istream operator>>(char &c) { scanf("%c", &c); return *this; } c_istream operator>>(double &d) { scanf("%lf", &d); return *this; } }; struct c_ostream { c_ostream operator<<(int n) { printf("%d", n); return *this; } c_ostream operator<<(char c) { printf("%c", c); return *this; } c_ostream operator<<(double d) { printf("%lf", d); return *this; } c_ostream operator<<(const char *s) { printf("%s", s); return *this; } }; int nums[100000]; int main() { int n, m, s, f; cin >> n >> m >> s >> f; bool right = false; if (f > s) right = true; int t = 1; int ct, cl, cr; while (m > 0) { cin >> ct >> cl >> cr; m--; while (t < ct) { if (s == f) return 0; if (right) { cout << "R"; s++; } else { cout << "L"; s--; } t++; } if (s == f) return 0; if (right && (s + 1 > cr || s + 1 < cl) && (s > cr || s < cl)) { cout << "R"; s++; } else if (!right && (s - 1 > cr || s - 1 < cl) && (s > cr || s < cl)) { cout << "L"; s--; } else cout << "X"; t++; } while (s != f) { if (right) { cout << "R"; s++; } else { cout << "L"; s--; } } return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.Scanner; public class XeniaAndSpies { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int spiesCount = scanner.nextInt(); int watchMovesCount = scanner.nextInt(); int fromSpy = scanner.nextInt() - 1; int toSpy = scanner.nextInt() - 1; scanner.nextLine(); Triple[] watchMoves = new Triple[watchMovesCount]; for (int i = 0; i < watchMovesCount; i++) { watchMoves[i] = new Triple(scanner.nextInt() - 1, scanner.nextInt() - 1, scanner.nextInt() - 1); scanner.nextLine(); } StringBuilder resultBuilder = new StringBuilder(); solve(resultBuilder, fromSpy < toSpy, fromSpy, toSpy, watchMoves, 0, 0); System.out.println(resultBuilder.toString()); } private static void solve(StringBuilder result, boolean leftToRight, int from, int to, Triple[] watchMoves, int watchIndex, int moveIndex) { if (from != to) { int neighbourSpy = leftToRight ? from + 1 : from - 1; char moveChar = leftToRight ? 'R' : 'L'; if (watchIndex >= watchMoves.length) { result.append(moveChar); solve(result, leftToRight, neighbourSpy, to, watchMoves, watchIndex, moveIndex); } else { Triple watch = watchMoves[watchIndex]; if (watch.index == moveIndex) { if ((watch.from <= from && from <= watch.to) || (watch.from <= neighbourSpy && neighbourSpy <= watch.to)) { result.append('X'); solve(result, leftToRight, from, to, watchMoves, watchIndex + 1, moveIndex + 1); } else { result.append(moveChar); solve(result, leftToRight, neighbourSpy, to, watchMoves, watchIndex + 1, moveIndex + 1); } } else { result.append(moveChar); solve(result, leftToRight, neighbourSpy, to, watchMoves, watchIndex, moveIndex + 1); } } } } private static class Triple { final int index; final int from; final int to; Triple(int index, int from, int to) { this.index = index; this.from = from; this.to = to; } } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.awt.image.BufferedImage; import java.io.*; import java.util.*; import java.util.concurrent.ExecutionException; public class Main { public void Run() { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int s = in.nextInt(); int f = in.nextInt(); ArrayList<Integer> l = new ArrayList<Integer>(), r = new ArrayList<Integer>(), t = new ArrayList<Integer>(); for (int i = 0; i < m; i++) { int T = in.nextInt(), L = in.nextInt(), R = in.nextInt(); t.add(T); l.add(L); r.add(R); } int cur = s, j = 0; ArrayList<Character> ans = new ArrayList<Character>(); for (int i = 1; i < 1000000; i++) { int ncur = cur + (f - s) / Math.abs(f - s); if (cur == f) break; if (j < m && i == t.get(j)) { if (l.get(j) <= cur && r.get(j) >= cur || l.get(j) <= ncur && r.get(j) >= ncur ) { j++; ans.add('X'); continue; } j++; } if (s < f) ans.add('R'); else ans.add('L'); cur = ncur; } for (int i = 0; i < ans.size(); i++) System.out.print(ans.get(i)); System.out.println(); } public static void main(String[] args) { new Main().Run(); } } class Scanner { BufferedReader in; StringTokenizer tok; public Scanner(InputStream in) { this.in = new BufferedReader(new InputStreamReader(in)); tok = new StringTokenizer(""); } private String tryReadNextLine() { try { return in.readLine(); } catch (Exception e) { throw new InputMismatchException(); } } public String nextToken() { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(next()); } return tok.nextToken(); } public String next() { String newLine = tryReadNextLine(); if (newLine == null) throw new InputMismatchException(); return newLine; } public int nextInt() { return Integer.parseInt(nextToken()); } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; template <typename T> inline T sqr(T n) { return (n * n); } int main() { int n, m, s, f; cin >> n >> m >> s >> f; int add = 1; char c = 'R'; if (s > f) { add = -1; c = 'L'; } int t, l, r; int tim = 1; string res; bool found = false; for (int i = 0; i < m && !found; i++) { cin >> t >> l >> r; while (tim < t && !found) { res += c; s += add; tim++; if (s == f) found = true; } if (!found) { if (((s >= l && s <= r) || (s + add >= l && s + add <= r))) res += 'X'; else { res += c; s += add; if (s == f) found = true; } } tim++; } if (!found) { while (s != f) { s += add; res += c; } } cout << res; return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long INF = 1e18; const int N = 121234; const int mod = 100000007; const int M = 5241234; int n, m, s, f; struct Node { int t, l, r; } xx[N]; int ans[M]; int main() { while (scanf("%d%d%d%d", &n, &m, &s, &f) != EOF) { int i, j; for (i = 0; i < m; ++i) { scanf("%d%d%d", &xx[i].t, &xx[i].l, &xx[i].r); } int tot = 0; int cur = 0; int pre = 0; if (s < f) { xx[m].t = 1000000001, xx[m].l = xx[m].r = f + 1; for (i = 0; i <= m; ++i) { cur = xx[i].t - pre - 1; for (j = 0; j < cur; ++j) { ans[tot++] = 1; s++; if (s == f) break; } if (s == f) break; if ((s < xx[i].l || s > xx[i].r) && (s + 1 < xx[i].l || s + 1 > xx[i].r)) { ans[tot++] = 1; s++; } else ans[tot++] = 0; if (s == f) break; pre = xx[i].t; } } else { xx[m].t = 1000000001, xx[m].l = xx[m].r = f - 1; for (i = 0; i <= m; ++i) { cur = xx[i].t - pre - 1; for (j = 0; j < cur; ++j) { ans[tot++] = -1; s--; if (s == f) break; } if (s == f) break; if ((s < xx[i].l || s > xx[i].r) && (s - 1 < xx[i].l || s - 1 > xx[i].r)) { ans[tot++] = -1; s--; } else ans[tot++] = 0; if (s == f) break; pre = xx[i].t; } } for (i = 0; i < tot; ++i) { if (ans[i] == 1) putchar('R'); else if (ans[i] == 0) putchar('X'); else putchar('L'); } puts(""); } }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; struct triple { int t, l, r; }; void move(int& s, int& f, string& out) { if (s < f) s++, out += "R"; else s--, out += "L"; } int next(int s, int f) { if (s < f) return s + 1; else return s - 1; } int main() { int n, m, s, f; cin >> n >> m >> s >> f; vector<triple> v(m); for (int i = 0; i < m; i++) cin >> v[i].t >> v[i].l >> v[i].r; string out = ""; int cur_t = 1, cur_pos = 0; while (s != f && cur_pos < m) { if (cur_t < v[cur_pos].t) move(s, f, out); else if (cur_t == v[cur_pos].t) { if ((v[cur_pos].l <= s && s <= v[cur_pos].r) || (v[cur_pos].l <= next(s, f) && next(s, f) <= v[cur_pos].r)) out += "X"; else move(s, f, out); cur_pos++; } cur_t++; } if (cur_pos == m) while (s != f) move(s, f, out); cout << out << endl; return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int s = in.nextInt(); int f = in.nextInt(); int[] t = new int[m]; int[] l = new int[m]; int[] r = new int[m]; for (int i = 0; i < m; i++) { t[i] = in.nextInt(); l[i] = in.nextInt(); r[i] = in.nextInt(); } int step = (f - s) / Math.abs(f - s); int cur = s; int ind = 0; int tVal = 0; int tInd = 0; while (cur != f) { tVal++; while (tInd < m && t[tInd] < tVal) tInd++; if (tInd < m && t[tInd] == tVal && ((l[tInd] <= cur && cur <= r[tInd]) || (l[tInd] <= cur + step && cur + step <= r[tInd]))) { out.print('X'); } else { if (step == 1) { out.print('R'); } else { out.print('L'); } cur += step; } } } } static class InputReader { private BufferedReader reader; private StringTokenizer stt; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { return null; } } public String next() { while (stt == null || !stt.hasMoreTokens()) { stt = new StringTokenizer(nextLine()); } return stt.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.*; import java.io.*; public class CF1 implements Runnable { static FastReader sc; static PrintWriter out; static int mod = 1000000007, inf = (int) 1e9, minf = -(int) 1e9; static long infL = (long) 1e18, minfL = -(long) 1e18; public static void main(String[] args) { new Thread(null, new CF1(), "coderrohan14", 1 << 26).start(); } @Override public void run() { try { ioSetup(); } catch (IOException e) { return; } } public static void ioSetup() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { File f1 = new File("input.txt"); File f2 = new File("output.txt"); Reader r = new FileReader(f1); sc = new FastReader(r); out = new PrintWriter(f2); double prev = System.currentTimeMillis(); solve(); out.println("\n\nExecuted in : " + ((System.currentTimeMillis() - prev) / 1e3) + " sec"); } else { sc = new FastReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); } out.flush(); out.close(); } static void solve() { // int t = sc.nextInt(); // StringBuilder ans = new StringBuilder(""); // while (t-- > 0) { // } // out.println(ans); int n = sc.nextInt(), m = sc.nextInt(), l = sc.nextInt(), r = sc.nextInt(); int[] t = new int[m], left = new int[m], right = new int[m]; for (int i = 0; i < m; i++) { t[i] = sc.nextInt(); left[i] = sc.nextInt(); right[i] = sc.nextInt(); } int time = 0, ct = 1; StringBuilder ans = new StringBuilder(); if (l <= r) { while (l < r && time < m) { if (t[time] == ct && ((l >= left[time] && l <= right[time]) || (l + 1 >= left[time] && l + 1 <= right[time]))) { // out.println(l + " " + time); ans.append('X'); time++; ct++; continue; } // out.println("HERE " + l + " " + time); ans.append('R'); l++; ct++; while (time < m && t[time] < ct) time++; } if (l < r) { for (int x = l; x < r; x++) { ans.append('R'); } } } else { while (l > r && time < m) { if (t[time] == ct && ((l >= left[time] && l <= right[time]) || (l - 1 >= left[time] && l - 1 <= right[time]))) { ans.append('X'); time++; ct++; continue; } // out.println("HERE " + l + " " + time); ans.append('L'); l--; ct++; while (time < m && t[time] < ct) time++; } if (l > r) { for (int x = l; x > r; x--) { ans.append('L'); } } } out.println(ans.toString()); } /****************************************************************************************************************************************************************************************/ static long modInverse(long a, int mod) { long g = gcd(a, mod); if (g != 1) return -1; else { return modPower(a, mod - 2L, mod); } } static long modPower(long x, long y, int mod) { long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } static int gcd(int a, int b) { int tmp = 0; while (b != 0) { tmp = b; b = a % b; a = tmp; } return a; } static long gcd(long a, long b) { long tmp = 0; while (b != 0) { tmp = b; b = a % b; a = tmp; } return a; } static boolean isPrime(long n) { if (n == 2 || n == 3) return true; if (n % 2 == 0) return false; for (long i = 3; i * i <= n; i += 2) { if (n % i == 0) return false; } return n != 1; } static boolean isPerfectSquare(long x) { long sqrt = (long) Math.sqrt(x); return (sqrt * sqrt) == x; } static int digitsCount(long x) { return (int) Math.floor(Math.log10(x)) + 1; } static boolean isPowerTwo(long n) { return (n & n - 1) == 0; } static void sieve(boolean[] prime, int n) { // Sieve Of Eratosthenes for (int i = 1; i <= n; i++) { prime[i] = true; } for (int i = 2; i * i <= n; i++) { if (prime[i]) { for (int j = 2; i * j <= n; j++) { prime[i * j] = false; } } } } static long nCr(long n, long r) { // Combinations if (n < r) return 0; if (r > n - r) { // because nCr(n, r) == nCr(n, n - r) r = n - r; } long ans = 1L; for (long i = 0; i < r; i++) { ans *= (n - i); ans /= (i + 1); } return ans; } static int floor(int[] a, int v) { int l = 0, h = a.length - 1; while (l < h) { int mid = (l + h) / 2; if (a[mid] == v) return mid; if (v < a[mid]) h = mid; else { if (mid + 1 < h && a[mid + 1] < v) l = mid + 1; else return mid; } } return a[l] <= v ? l : -1; } static int floor(long[] a, long v) { int l = 0, h = a.length - 1; while (l < h) { int mid = (l + h) / 2; if (a[mid] == v) return mid; if (v < a[mid]) h = mid; else { if (mid + 1 < h && a[mid + 1] < v) l = mid + 1; else return mid; } } return a[l] <= v ? l : -1; } static int ceil(int[] a, int v) { int l = 0, h = a.length - 1; while (l < h) { int mid = (l + h) / 2; if (a[mid] == v) return mid; if (a[mid] < v) l = mid + 1; else h = mid; } return a[h] >= v ? h : -1; } static int ceil(long[] a, long v) { int l = 0, h = a.length - 1; while (l < h) { int mid = (l + h) / 2; if (a[mid] == v) return mid; if (a[mid] < v) l = mid + 1; else h = mid; } return a[h] >= v ? h : -1; } static long catalan(int n) { // n-th Catalan Number long c = nCr(2 * n, n); return c / (n + 1); } static class Pair implements Comparable<Pair> { // Pair Class long x; long y; public Pair(long x, long y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof Pair)) { return false; } Pair pair = (Pair) o; if (x != pair.x) { return false; } if (y != pair.y) { return false; } return true; } @Override public int hashCode() { long result = x; result = 31 * result + y; return (int) result; } @Override public int compareTo(Pair o) { return (int) (this.x - o.x); } } static class Trip { // Triplet Class long x; long y; long z; Trip(long x, long y, long z) { this.x = x; this.y = y; this.z = z; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof Trip)) { return false; } Trip trip = (Trip) o; if (x != trip.x) { return false; } if (y != trip.y) { return false; } if (z != trip.z) { return false; } return true; } @Override public int hashCode() { long result = 62 * x + 31 * y + z; return (int) result; } } /**************************************************************************************************************************************************************************************/ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(Reader r) { br = new BufferedReader(r); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArrayI(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } return arr; } long[] readArrayL(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); } return arr; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } boolean hasNext() { if (st != null && st.hasMoreTokens()) { return true; } String tmp; try { br.mark(1000); tmp = br.readLine(); if (tmp == null) { return false; } br.reset(); } catch (IOException e) { return false; } return true; } } /* * ASCII Range--->(A-Z)--->[65,90]<<::>>(a-z)--->[97,122] */ /******************************************************************************************************************/ static void printArray(int[] arr) { out.print("["); for (int i = 0; i < arr.length; i++) { if (i < arr.length - 1) out.print(arr[i] + ","); else out.print(arr[i]); } out.print("]"); out.println(); } static void printArray(long[] arr) { out.print("["); for (int i = 0; i < arr.length; i++) { if (i < arr.length - 1) out.print(arr[i] + ","); else out.print(arr[i]); } out.print("]"); out.println(); } static void printArray(double[] arr) { out.print("["); for (int i = 0; i < arr.length; i++) { if (i < arr.length - 1) out.print(arr[i] + ","); else out.print(arr[i]); } out.print("]"); out.println(); } /**********************************************************************************************************************/ }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s, f; cin >> n >> m >> s >> f; int k = 0; int t[m], l[m], r[m]; for (int i = 0; i < m; i++) { cin >> t[i] >> l[i] >> r[i]; } int pos = s; if (s < f) { for (int time = 1; time <= 2 * n + 2 * m; time++) { if (k < m && time == t[k]) { if (pos > r[k] || pos < l[k] - 1) { cout << "R"; pos++; if (pos == f) { cout << endl; return 0; } } else { cout << "X"; } k++; } else { cout << "R"; pos++; if (pos == f) { cout << endl; return 0; } } } } if (s > f) { for (int time = 1; time <= 2 * n + 2 * m; time++) { if (k < m && time == t[k]) { if (pos > r[k] + 1 || pos < l[k]) { cout << "L"; pos--; if (pos == f) { cout << endl; return 0; } } else { cout << "X"; } k++; } else { cout << "L"; pos--; if (pos == f) { cout << endl; return 0; } } } } return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int espies; int vigies; int origen; int desti; int t; int l; int r; int tmax = 1000000000; int pas; cin >> espies >> vigies >> origen >> desti; cin >> t >> l >> r; for (int i = 1; i <= tmax; i++) { if (origen == desti) { break; } else if (t == i) { if (origen < desti) { if ((origen >= l and origen <= r) or (origen + 1 >= l and origen + 1 <= r)) cout << "X"; else { if (origen < espies) { cout << "R"; origen++; } else cout << "X"; } } else { if ((origen >= l and origen <= r) or (origen - 1 >= l and origen - 1 <= r)) cout << "X"; else { if (origen > 1) { cout << "L"; origen--; } else cout << "X"; } } cin >> t >> l >> r; } else { if (origen < desti) { if (origen < espies) { cout << "R"; origen++; } else cout << "X"; } else { if (origen > 1) { cout << "L"; origen--; } else cout << "X"; } } } }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
R = lambda: map(int, input().split()) n, m, s, f = R() if s < f: d = 1 c = 'R' else: d = -1 c = 'L' res = "" i = 1 j = s t, l, r = R() k = 1 while j != f: if i > t and k < m: t, l, r = R() k += 1 if i == t and (l <= j <= r or l <= j + d <= r): res += 'X' else: res += c j += d i += 1 print(res)
PYTHON3
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
""" Template written to be used by Python Programmers. Use at your own risk!!!! Owned by enraged(rating - 5 star at CodeChef and Specialist at Codeforces). """ import sys from functools import lru_cache, cmp_to_key from heapq import merge, heapify, heappop, heappush from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log from collections import defaultdict as dd, deque, Counter as c from itertools import combinations as comb, permutations as perm from bisect import bisect_left as bl, bisect_right as br, bisect # sys.setrecursionlimit(2*pow(10, 6)) # sys.stdin = open("input.txt", "r") # sys.stdout = open("output.txt", "w") mod = pow(10, 9) + 7 mod2 = 998244353 def data(): return sys.stdin.readline().strip() def out(var): sys.stdout.write(str(var)) def outln(var): sys.stdout.write(str(var)+"\n") def l(): return list(sp()) def sl(): return list(ssp()) def sp(): return map(int, data().split()) def ssp(): return map(str, data().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)] n, m, s, f = sp() answer = [] last = 0 for _ in range(m): ti, li, ri = sp() diff = ti-last-1 if f < s: while f < s and diff: s -= 1 diff -= 1 answer.append('L') else: while f > s and diff: s += 1 answer.append('R') diff -= 1 if f < s: if li <= s <= ri or li <= s-1 <= ri: answer.append('X') else: answer.append('L') s -= 1 elif f > s: if li <= s <= ri or li <= s+1 <= ri: answer.append('X') else: answer.append('R') s += 1 else: break last = ti if f > s: for i in range(f-s): answer.append('R') elif f < s: for i in range(s-f): answer.append('L') outln(''.join(answer))
PYTHON3
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; map<int, int> l, r; int main() { string ans = ""; int n, m, s, f, j, a, b; cin >> n >> m >> s >> f; for (int i = 1; i <= m; i++) { cin >> j >> a >> b; l[j] = a; r[j] = b; } for (int i = 1;; i++) { if (s == f) break; if (s < f) { if (l[i] == 0 && r[i] == 0) cout << "R", s++; else if ((s >= l[i] && s <= r[i]) || (s + 1 >= l[i] && s + 1 <= r[i])) cout << "X"; else cout << "R", s++; } else { if (l[i] == 0 && r[i] == 0) cout << "L", s--; else if ((s >= l[i] && s <= r[i]) || (s - 1 >= l[i] && s - 1 <= r[i])) cout << "X"; else cout << "L", s--; } } return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> int num[8]; int main() { int n, m, s, t, c, a, b, pre = 0; scanf("%d%d%d%d", &n, &m, &s, &t); while (m--) { scanf("%d%d%d", &c, &a, &b); if (s < t) { while (pre < c - 1 && s != t) { putchar('R'); s++; pre++; } if (s == t) break; pre++; if ((a <= s + 1 && s + 1 <= b) || (a <= s && s <= b)) putchar('X'); else { putchar('R'); s++; } } else if (s > t) { while (pre < c - 1 && s != t) { putchar('L'); s--; pre++; } if (s == t) break; pre++; if ((a <= s - 1 && s - 1 <= b) || (a <= s && s <= b)) putchar('X'); else { putchar('L'); s--; } } else break; } while (s < t) { putchar('R'); s++; } while (s > t) { putchar('L'); s--; } putchar('\n'); return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.BufferedReader; import java.io.InputStreamReader; public class XeniaSpies2 { public static int n; public static int m; public static int s; public static int f; public static void start(int[][] numbers) { StringBuilder result = new StringBuilder(); int counter = 0; for (int i = 1; i <= m; i++) { counter++; int t = numbers[i-1][0]; int first = numbers[i-1][1]; int last = numbers[i-1][2]; if (t != counter) { for (int j = counter; j < t; j++) { if (s < f) { // the receiver is on the right result.append("R"); s = s + 1; } else { // the receiver is on the left result.append("L"); s = s - 1; } if (s == f) { // finish System.out.println(result); return; } counter++; } } if (t == counter) { if (s < f) { // the receiver is on the right if ((s >= first && s <= last) || (s + 1 >= first && s + 1 <= last)) { // s is being watched or the next one to s is being // watched result.append("X"); } else { result.append("R"); s = s + 1; if (s == f) { // finish System.out.println(result); return; } } } else { // the receiver is on the left if ((s >= first && s <= last) || (s - 1 >= first && s - 1 <= last)) { // s is being watched or the next one to s is being // watched result.append("X"); } else { result.append("L"); s = s - 1; if (s == f) { // finish System.out.println(result); return; } } } } } if (s!=f){ if (s < f){ // the receiver is on the right while (s<f){ result.append("R"); s=s+1; } System.out.println(result); return; } else { // the receiver is on the left while (s>f){ result.append("L"); s=s-1; } System.out.println(result); return; } } } public static void main(String[] args) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); try { String l = in.readLine(); String[] array = l.split(" "); n = Integer.parseInt(array[0]); m = Integer.parseInt(array[1]); s = Integer.parseInt(array[2]); f = Integer.parseInt(array[3]); int[][] numbers = new int[m][3]; for (int i = 0; i < m; i++) { l = in.readLine(); array = l.split(" "); int t = Integer.parseInt(array[0]); int first = Integer.parseInt(array[1]); int last = Integer.parseInt(array[2]); numbers[i][0] = t; numbers[i][1] = first; numbers[i][2] = last; } start(numbers); } catch (Exception e) { } } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.HashSet; import java.util.*; import java.util.Scanner; import java.util.Set; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.text.DecimalFormat; import java.lang.Math; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int s = sc.nextInt(); int f = sc.nextInt(); Map<Integer, int[]> A = new HashMap<>(); for(int i = 1; i <= m; i++){ int t = sc.nextInt(); int[] temp = new int[2]; temp[0] = sc.nextInt(); temp[1] = sc.nextInt(); A.put(t, temp); } StringBuffer ans = new StringBuffer(""); int p = 1; while(s!=f){ int borders[] = A.get(p); if(borders!=null){ if(f>s && ((s>= borders[0] && s<= borders[1] )||(s+1 >= borders[0] && s+1 <= borders[1]))){ ans.append("X"); } else if(f>s){ ans.append("R"); s = s+1; } else if(f<s && ((s>= borders[0] && s<= borders[1] )||(s-1 >= borders[0] && s-1 <= borders[1]))){ ans.append("X"); } else if(f<s){ ans.append("L"); s = s-1; } } else{ if(f>s){ ans.append("R"); s += 1; } else{ ans.append("L"); s -= 1; } } p++; } System.out.println(ans); } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.Scanner; public class B { public static void main(String arg[]) { Scanner reader = new Scanner(System.in); int n = reader.nextInt(); int m = reader.nextInt(); int s = reader.nextInt(); int f = reader.nextInt(); int a[][] = new int[m][3]; for (int i = 0; i < m; i++) { a[i][0] = reader.nextInt(); a[i][1] = reader.nextInt(); a[i][2] = reader.nextInt(); } int cm = 0, steps = 0; int i; String ans = "L"; int add = -1; int l1 = 0, l2 = -1; if (s < f) { ans = "R"; add = 1; l1 = 1; l2 = 0; } for (i = s; i != f; ) { steps++; while (cm < m && a[cm][0] < steps) cm++; if (cm >= m || a[cm][0] != steps) { System.out.print(ans); i = i + add; continue; } else if (i + l1 >= a[cm][1] && i + l2 <= a[cm][2]) { System.out.print("X"); cm++; } else { System.out.print(ans); i = i + add; continue; } } System.out.println(""); } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
n, m, s, f = map(int, raw_input().split()) a = [] ans = "" t = 1 d = 1 if s < f else -1 for q in xrange(m): a = map(int, raw_input().split()) while a[0] > t and s != f: ans += "R" if d == 1 else "L" s += d t += 1 if s == f: break if a[1] <= s <= a[2] or a[1] <= s + d <= a[2]: ans += "X" else: ans += "R" if d == 1 else "L" s += d t += 1 if s == f: break while s != f: ans += "R" if d == 1 else "L" s += d t += 1 print ans
PYTHON
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import static java.lang.System.out; public class CF199B { private int s,f; private StringBuilder res = new StringBuilder(""); public void init(int s, int f) { this.s=s; this.f=f; } private int tm = 0; private void move(int cnt) { while(cnt > 0 && s != f) { cnt--; if (s<f) { res.append("R"); s++; } else { res.append("L"); s--; } } } private void nomove() { res.append("X"); } public boolean step(int tm, int l, int r) { this.tm++; move(tm-this.tm); this.tm = tm; if (s == f) return true; if ((s < f && (s+1 < l || r < s)) || (f < s && (s < l || r+1 < s))) move(1); else nomove(); return s == f; } public void run() { int m,s,f; FastReaderB fr = new FastReaderB(); fr.nextInt(); m = fr.nextInt(); s = fr.nextInt(); f = fr.nextInt(); init(s,f); for (int i=0; i<m; i++) { int t = fr.nextInt(),l = fr.nextInt(),r = fr.nextInt(); if (step(t,l,r)) { out.println(res.toString()); return; } } step(1000*1000*1000+1,-1,-1); out.println(res.toString()); } public static void main(String[] args) { // while(true) (new CF199B()).run(); } } class FastReaderB { private StringTokenizer st; private BufferedReader br; public FastReaderB() { br = new BufferedReader(new InputStreamReader(System.in)); } private String nextToken() { while(st == null || (!st.hasMoreTokens())) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public String next() { return nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int mod = 1000000007; int powmod(int a, int b, int p) { int res = 1; while (b) if (b & 1) res = int(res * 1ll * a % p), --b; else a = int(a * 1ll * a % p), b >>= 1; return res; } int generator(int p) { vector<int> fact; int phi = p - 1, n = phi; for (int i = 2; i * i <= n; ++i) if (n % i == 0) { fact.push_back(i); while (n % i == 0) n /= i; } if (n > 1) fact.push_back(n); for (int res = 2; res <= p; ++res) { bool ok = true; for (size_t i = 0; i < fact.size() && ok; ++i) ok &= powmod(res, phi / fact[i], p) != 1; if (ok) return res; } return -1; } int binpow(int a, int n) { if (n == 0) return 1; if (n % 2 == 1) return binpow(a, n - 1) * a; else { int b = binpow(a, n / 2); return b * b; } } bool is_prime(int p) { if (p == 1) return false; for (int i = 2; i * i <= p; i++) if (p % i == 0) return false; return true; } int main() { int n, m, s, f; cin >> n >> m >> s >> f; int z = s; bool ri; if (s < f) ri = true; else ri = false; vector<int> l(m); vector<int> r(m); vector<int> t(m); for (int i = 0; i < m; i++) scanf("%d%d%d", &t[i], &l[i], &r[i]); int k = 1, ind = 0; if (ri) { while (z != f) { if (ind < m && k == t[ind] && (l[ind] - 1 <= z && z <= r[ind])) { printf("X"); } else { printf("R"); z++; } if (ind < m && k == t[ind]) ind++; k++; } } else { while (z != f) { if (ind < m && k == t[ind] && (l[ind] <= z && z <= r[ind] + 1)) { printf("X"); } else { printf("L"); z--; } if (ind < m && k == t[ind]) ind++; k++; } } printf("\n"); }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; public class CF199B { private static class Pair { int l; int r; public Pair(final int le, final int ri) { l = le; r = ri; } } public static void main(final String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] parts = br.readLine().split("\\s+"); int n = Integer.parseInt(parts[0]); int m = Integer.parseInt(parts[1]); int s = Integer.parseInt(parts[2]); int f = Integer.parseInt(parts[3]); int dir = f - s > 0 ? 1 : -1; Map<Integer, Pair> map = new HashMap<Integer, Pair>(); while (m-- > 0) { parts = br.readLine().split("\\s+"); int t = Integer.parseInt(parts[0]); int l = Integer.parseInt(parts[1]); int r = Integer.parseInt(parts[2]); map.put(t, new Pair(l, r)); } StringBuilder sb = new StringBuilder(); StringBuilder sb1 = new StringBuilder(); int step = 0; while (s != f) { step++; Pair p = map.get(step); if (p != null) { if (inRange(s, p.l, p.r) || inRange(s + dir, p.l, p.r)) { sb.append("X"); } else if (dir < 0) { sb.append("L"); s += dir; } else { sb.append("R"); s += dir; } } else { if (dir < 0) { sb.append("L"); s += dir; } else { sb.append("R"); s += dir; } } } System.out.println(sb.toString()); } private static boolean inRange(final int mark, final int l, final int r) { return mark >= l && mark <= r; } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
def checkKey(dict, key): if key in dict: return True return False # def helper(s): # l=len(s) # if (l==1): # l=[] # l.append(s) # return l # ch=s[0] # recresult=helper(s[1:]) # myresult=[] # myresult.append(ch) # for st in recresult: # myresult.append(st) # ts=ch+st # myresult.append(ts) # return myresult # mod=1000000000+7 # def helper(s,n,open,close,i): # if(i==2*n): # for i in s: # print(i,end='') # print() # return # if(open<n): # s[i]='(' # helper(s,n,open+1,close,i+1) # if(close<open): # s[i]=')' # helper(s,n,open,close+1,i+1) # def helper(arr,i,n): # if(i==n-1): # recresult=[arr[i]] # return recresult # digit=arr[i] # recresult=helper(arr,i+1,n) # myresult=[] # for i in recresult: # myresult.append(i) # myresult.append(i+digit); # myresult.append(digit) # return myresult # import copy # n=int(input()) # arr=list(map(int,input().split())) # ans=[] # def helper(arr,i,n): # if(i==n-1): # # for a in arr: # # print(a,end=" ") # # print() # l=copy.deepcopy(arr) # ans.append(l) # return # for j in range(i,n): # if(i!=j): # if(arr[i]==arr[j]): # continue # else: # arr[i],arr[j]=arr[j],arr[i] # helper(arr,i+1,n) # arr[j],arr[i]=arr[i],arr[j] # else: # arr[i],arr[j]=arr[j],arr[i] # helper(arr,i+1,n) # def helper(sol,n,m): # for i in range(n+1): # for j in range(m+1): # print(sol[i][j],end=" ") # print() # def rat_in_a_maze(maze,sol,i,j,n,m): # if(i==n and j==m): # sol[i][j]=1 # helper(sol,n,m) # exit() # if(i>n or j>m): # return False # if(maze[i][j]=='X'): # return False # maze[i][j]='X' # sol[i][j]=1 # if(rat_in_a_maze(maze,sol,i,j+1,n,m)): # return True # elif(rat_in_a_maze(maze,sol,i+1,j,n,m)): # return True # sol[i][j]=0 # return False n,m,s,f=map(int,input().split()) p=s d=-1 c='L' if s<f: d=1 c='R' t=1 ts={} ans="" for _ in range(m): x,y,z=map(int,input().split()) ts[x]=(y,z) while(p!=f): if t in ts: (l,r)=ts[t] if l<=p<=r or l<=p+d<=r: ans+='X' else: p+=d ans+=c else: p+=d ans+=c t+=1 print(ans)
PYTHON3
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> char pos[] = "XLR"; int inc, c; int abs(int a) { return (a < 0) ? (0 - a) : a; } int main() { int n, m, s, f; scanf("%d %d %d %d", &n, &m, &s, &f); if (s < f) { inc = 1; c = 2; } else { inc = -1; c = 1; } int to = 1, t, l, r, a; for (int i = 0; i < m; i++) { scanf("%d %d %d", &t, &l, &r); if (t > to) { while (abs(f - s) && (t > to)) { to++; s += inc; putchar(pos[c]); } } if ((((s > (r + 1) || s < l) && c == 1) || ((s < (l - 1) || s > r) && c == 2)) && abs(s - f)) { s += inc; putchar(pos[c]); to++; } else if (abs(s - f)) { putchar(pos[0]); to++; } } while (abs(f - s)) { s += inc; putchar(pos[c]); } putchar('\n'); return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> const double pi = 3.141592653589793238462; const double eps = 1e-10; using namespace std; const int maxn = 100010; int t[maxn], l[maxn], r[maxn]; int main() { int i, j, k, n, m, tc, s, f; ios_base::sync_with_stdio(false); cin >> n >> m >> s >> f; for (i = 0; i < m; i++) cin >> t[i] >> l[i] >> r[i]; k = 1; i = 0; while (s != f) { if (s < f && (t[i] != k || (t[i] == k && (r[i] < s || l[i] > s + 1)))) { cout << "R"; s++; } else if (s > f && (t[i] != k || (t[i] == k && (r[i] < s - 1 || l[i] > s)))) { cout << "L"; s--; } else cout << "X"; if (t[i] == k) i++; k++; } cout << endl; return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> int mark[100005]; struct node { int x, y; int id; } a[100005]; int main() { int n, m, s, f; while (scanf("%d%d%d%d", &n, &m, &s, &f) != EOF) { memset(mark, 0, sizeof(mark)); int i, k, x, y; for (i = 1; i <= m; i++) scanf("%d%d%d", &a[i].id, &a[i].x, &a[i].y); a[i].id = 99999999; mark[s] = 1; int cnt, tt; cnt = tt = 1; while (mark[f] != 1) { if (s < f) { if (cnt != a[tt].id) { mark[s + 1] = 1; s = s + 1; printf("R"); } else { if ((s < a[tt].x || s > a[tt].y) && (s + 1 < a[tt].x || s + 1 > a[tt].y)) { mark[s + 1] = 1; s = s + 1; printf("R"); } else printf("X"); tt++; } cnt++; } else { if (cnt != a[tt].id) { mark[s - 1] = 1; s = s - 1; printf("L"); } else { if ((s < a[tt].x || s > a[tt].y) && (s - 1 < a[tt].x || s - 1 > a[tt].y)) { mark[s - 1] = 1; s = s - 1; printf("L"); } else printf("X"); tt++; } cnt++; } } printf("\n"); } return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.*; import java.io.*; public class Main { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static StringBuilder out = new StringBuilder(); public static void main(String[] args){ int[] inp = nextIntArray(); int n, m, s, f; n = inp[0]; m = inp[1]; s = inp[2]; f = inp[3]; int[] t, l, r; t = new int[m]; l = new int[m]; r = new int[m]; for (int i = 0; i < m; i++){ inp = nextIntArray(); t[i] = inp[0]; l[i] = inp[1]; r[i] = inp[2]; } int i = 0; // index for t, l, r int direction = f < s ? -1 : 1; // direction to which the note should be passed for (int step = 1; s != f; step ++){ if (i < m && step == t[i]){ if ((l[i] <= s && s <= r[i]) || (l[i] <= s + direction && s + direction <= r[i])){ out.append("X"); i++; continue; } i ++; } // able to pass note if (f < s){ out.append("L"); s --; } else { out.append("R"); s ++; } } System.out.println(out); } // the followings are methods to take care of inputs. static int nextInt(){ return Integer.parseInt(nextLine()); } static long nextLong(){ return Long.parseLong(nextLine()); } static int[] nextIntArray(){ String[] inp = nextLine().split("\\s+"); int[] ary = new int[inp.length]; for (int i = 0; i < ary.length; i++){ ary[i] = Integer.parseInt(inp[i]); } return ary; } static int[] nextIntArrayFrom1(){ String[] inp = nextLine().split("\\s+"); int[] ary = new int[inp.length + 1]; for (int i = 0; i < inp.length; i++){ ary[i+1] = Integer.parseInt(inp[i]); } return ary; } static long[] nextLongArray(){ String[] inp = nextLine().split("\\s+"); long[] ary = new long[inp.length]; for (int i = 0; i < inp.length; i++){ ary[i] = Long.parseLong(inp[i]); } return ary; } static long[] nextLongArrayFrom1(){ String[] inp = nextLine().split("\\s+"); long[] ary = new long[inp.length + 1]; for (int i = 0; i < inp.length; i++){ ary[i+1] = Long.parseLong(inp[i]); } return ary; } static String nextLine(){ try { return reader.readLine().trim(); } catch (Exception e){} return null; } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.*; import java.io.*; import java.lang.*; import java.math.*; public class B { public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(bf.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int s = Integer.parseInt(st.nextToken()); int f = Integer.parseInt(st.nextToken()); int currentplacement = s; StringBuilder ans = new StringBuilder(); StringTokenizer str; myforloop: for(int i=1; i<=m; i++) { if(currentplacement == f) break; str = new StringTokenizer(bf.readLine()); int a = Integer.parseInt(str.nextToken()); int b = Integer.parseInt(str.nextToken()); int c = Integer.parseInt(str.nextToken()); while(i < a) { if(s < f) { ans.append('R'); currentplacement++; } if(s > f) { ans.append('L'); currentplacement--; } // Note that this break statement only breaks while loop. // The second solution is to break the for loop. if(currentplacement == f) break myforloop; i++; } if(s < f && (b > currentplacement || c < currentplacement) && (b > currentplacement+1 || c < currentplacement+1) ) { ans.append('R'); currentplacement++; } else if(s > f && (b > currentplacement || c < currentplacement) && (b > currentplacement-1 || c < currentplacement-1) ) { ans.append('L'); currentplacement--; } else { if(currentplacement == f) break; ans.append('X'); } } if(currentplacement != f) { if(s < f) { while(currentplacement != f) { ans.append('R'); currentplacement++; } } if(s > f) { while(currentplacement != f) { ans.append('L'); currentplacement--; } } } System.out.println(ans); } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> int n, m, s, f, v; int qry[100002][3]; int main() { scanf("%d%d%d%d", &n, &m, &s, &f); if (s < f) { v = 1; } else { v = -1; } for (int i = 0; i < m; i++) { scanf("%d%d%d", &qry[i][0], &qry[i][1], &qry[i][2]); } int nxt = 0; int tm = 1; while (s != f) { if (nxt < m) { if (qry[nxt][0] == tm) { int flg = 1; if (qry[nxt][1] <= s && qry[nxt][2] >= s) flg = 0; if (qry[nxt][1] <= s + v && qry[nxt][2] >= s + v) flg = 0; if (flg) { s += v; if (v > 0) { printf("R"); } else { printf("L"); } } else { printf("X"); } nxt++; } else { s += v; if (v > 0) { printf("R"); } else { printf("L"); } } } else { s += v; if (v > 0) { printf("R"); } else { printf("L"); } } tm++; } printf("\n"); return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; template <typename S, typename T> ostream& operator<<(ostream& out, pair<S, T> const& p) { out << '(' << p.first << ", " << p.second << ')'; return out; } template <typename T> ostream& operator<<(ostream& out, vector<T> const& v) { long long l = v.size(); for (long long i = 0; i < l - 1; i++) out << v[i] << ' '; if (l > 0) out << v[l - 1]; return out; } template <typename T> void trace(const char* name, T&& arg1) { cout << name << " : " << arg1 << endl; } template <typename T, typename... Args> void trace(const char* names, T&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; trace(comma + 1, args...); } int n, m, s, f, cur_hol, pass, to_pass, cp; char curr, temp; long long t, x, y, arr[100010][3]; ; int main() { cin >> n >> m >> s >> f; cur_hol = s; if ((f - s) > 0) curr = 'R', pass = 1; else curr = 'L', pass = -1; to_pass = cur_hol + pass; for (int i = 0; i < m; i++) { for (int j = 0; j < 3; j++) { cin >> arr[i][j]; } } cp = 0; for (int i = 1;; i++) { if (arr[cp][0] == i && ((arr[cp][1] <= cur_hol and arr[cp][2] >= cur_hol) || (arr[cp][1] <= to_pass and arr[cp][2] >= to_pass))) { cout << 'X'; cp++; } else if (arr[cp][0] == i) { cp++; cout << curr; cur_hol = to_pass; to_pass = cur_hol + pass; } else { cout << curr; cur_hol = to_pass; to_pass = cur_hol + pass; } if (cur_hol == f) { break; } } return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int n, m, s, f; map<int, pair<int, int> > a; bool ok(int i, int x) { pair<int, int> u = a[i]; if (s < f) return x < u.first - 1 || x > u.second; else return x > u.second + 1 || x < u.first; } int main() { scanf("%d%d%d%d", &n, &m, &s, &f); for (int i = 1; i <= m; ++i) { int x, y, z; scanf("%d%d%d", &x, &y, &z); a[x] = make_pair(y, z); } for (int i = 1; s != f; ++i) { if (!a.count(i) || ok(i, s)) { if (s < f) ++s, printf("R"); else --s, printf("L"); ; } else printf("X"); } puts(""); return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; struct xx { int s; int l; int r; }; xx a[100005]; int xp, kt, n, m; int check1(int xp, int step) { if (xp >= a[step].l && xp <= a[step].r) return 0; if (xp + 1 >= a[step].l && xp + 1 <= a[step].r) return 0; return 1; } int check2(int xp, int step) { if (xp >= a[step].l && xp <= a[step].r) return 0; if (xp - 1 >= a[step].l && xp - 1 <= a[step].r) return 0; return 1; } void solve() { int i, x, y, z, step, j; for (i = 1; i <= m; i++) { scanf("%d %d %d", &x, &y, &z); a[i].s = x; a[i].l = y; a[i].r = z; } step = 0; if (xp < kt) { for (i = 1; i <= m; i++) { { for (j = a[i - 1].s; j < a[i].s - 1; j++) { printf("R"), xp++; if (xp == kt) { printf("\n"); return; } } } if (check1(xp, i) == 1) { printf("R"), xp++; if (xp == kt) { printf("\n"); return; } } else printf("X"); } } else { for (i = 1; i <= m; i++) { { for (j = a[i - 1].s; j < a[i].s - 1; j++) { printf("L"), xp--; if (xp == kt) { printf("\n"); return; } } } if (check2(xp, i) == 1) { printf("L"), xp--; if (xp == kt) { printf("\n"); return; } } else printf("X"); } } if (xp < kt) for (i = xp; i < kt; i++) printf("R"); else for (i = xp; i > kt; i--) printf("L"); printf("\n"); } int main() { while (scanf("%d %d %d %d", &n, &m, &xp, &kt) > 0) solve(); return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long i, j, k, l, m, n, s, f; cin >> n >> m >> s >> f; long long curr = s, dest = f; vector<vector<long long> > req; for (i = 0; i < m; i++) { long long a, b, c; cin >> a >> b >> c; req.push_back({a, b, c}); } long long i1 = 0; string ans; for (i = 1; i <= 3e5 && curr != dest; i++) { long long lft = 0, rt = 0; if (i1 < (long long)req.size() && req[i1][0] == i) { lft = req[i1][1], rt = req[i1][2]; i1++; } if (curr > rt || curr < lft) { if (curr < dest) { if (curr + 1 > rt || curr + 1 < lft) ans.push_back('R'), curr++; else ans.push_back('X'); } if (curr > dest) { if (curr - 1 > rt || curr - 1 < lft) ans.push_back('L'), curr--; else ans.push_back('X'); } } else ans.push_back('X'); } cout << ans << "\n"; return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } long long pw(long long b, long long e, long long m) { long long r = 1; b = b % m; while (e > 0) { if (e & 1) r = (r * b) % m; e >>= 1; b = (b * b) % m; } return r; } int n, m, d, f; int arr[100100]; vector<pair<int, pair<int, int> > > watch; int main() { int s; cin >> n >> m >> s >> f; if (s > f) d = -1; else d = 1; int temp; for (int i = 0; i < m; i++) { pair<int, pair<int, int> > a; scanf("%d%d%d", &a.first, &a.second.first, &a.second.second); watch.push_back(a); } int time, idx; time = 1; idx = 0; while (s != f) { if (idx >= watch.size() || watch[idx].first != time || (watch[idx].second.first > s && watch[idx].second.first > s + d) || (watch[idx].second.second < s && watch[idx].second.second < s + d)) { s += d; printf("%c", d == -1 ? 'L' : 'R'); } else printf("X"); if (watch[idx].first == time) idx++; time++; } cout << endl; return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
n, m, s, f = map(int, raw_input().split(" ")) curr = s ct = 1 ans = "" for i in range(m): t, l, r = map(int, raw_input().split(" ")) while ct < t and curr != f: if s < f: curr += 1 ans += "R" else: curr -= 1 ans += "L" ct += 1 if curr == f: break else: if l <= curr and curr <= r: ans += "X" elif s < f and (curr+1 < l or curr+1 > r): curr += 1 ans += "R" elif s > f and (curr-1 > r or curr-1 < l): curr -= 1 ans += "L" else: ans += "X" ct += 1 while curr < f: ans += "R" curr += 1 while curr > f: ans += "L" curr -= 1 print ans
PYTHON
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.*; import java.math.*; public class Main { static void append(StringBuilder builder, char c, int count) { for (int i = 0; i < count; i++) { builder.append(c); } } public static void main(String[] args) throws Exception { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int m = scan.nextInt(); int s = scan.nextInt(); int f = scan.nextInt(); int[] time = new int[m]; int[] left = new int[m]; int[] right = new int[m]; for (int i = 0; i < m; i++) { time[i] = scan.nextInt(); left[i] = scan.nextInt(); right[i] = scan.nextInt(); } StringBuilder builder = new StringBuilder(); int pos = s; int t = 1; if (s < f) { for (int i = 0; i < m; i++) { int remain = time[i] - t; if (pos + remain >= f) { append(builder, 'R', f - pos); pos = f; break; } else { append(builder, 'R', remain); pos += remain; } if (pos + 1 < left[i] || pos > right[i]) { pos++; append(builder, 'R', 1); } else { append(builder, 'X', 1); } t = time[i] + 1; } append(builder, 'R', f - pos); System.out.println(builder.toString()); } else { for (int i = 0; i < m; i++) { int remain = time[i] - t; if (pos - remain <= f) { append(builder, 'L', pos - f); pos = f; break; } else { append(builder, 'L', remain); pos -= remain; } if (pos < left[i] || pos - 1 > right[i]) { pos--; append(builder, 'L', 1); } else { append(builder, 'X', 1); } t = time[i] + 1; } append(builder, 'L', pos - f); System.out.println(builder.toString()); } } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#------------------------template--------------------------# import os import sys from math import * from collections import * # from fractions import * # from heapq import* from bisect import * from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ALPHA='abcdefghijklmnopqrstuvwxyz/' M=998244353 EPS=1e-6 def Ceil(a,b): return a//b+int(a%b>0) def value():return tuple(map(int,input().split())) def array():return [int(i) for i in input().split()] def Int():return int(input()) def Str():return input() def arrayS():return [i for i in input().split()] #-------------------------code---------------------------# # vsInput() n,m,s,d=value() move=1 if(d<s):move=0 last=0 cur=s ans=[] given=[] for i in range(m):given.append(value()) # print(given) for t,l,r in given: if(t-1!=last): have=t-last-1 need=abs(d-cur) if(move): ans.extend(['R' for i in range(min(need,have))]) cur+=min(need,have) else: ans.extend(['L' for i in range(min(need,have))]) cur-=min(need,have) if(cur==d): break if(move): if(cur+1<l or cur>r): ans.append('R') cur+=1 else: ans.append('X') else: if(cur<l or cur-1>r): ans.append('L') cur-=1 else: ans.append('X') last=t if(cur!=d): need=abs(d-cur) have=inf if(move): ans.extend(['R' for i in range(min(need,have))]) else: ans.extend(['L' for i in range(min(need,have))]) print(*ans,sep="")
PYTHON3
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
// practice with rainboy import java.io.*; import java.util.*; public class CF342B extends PrintWriter { CF342B() { super(System.out, true); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF342B o = new CF342B(); o.main(); o.flush(); } void main() { int n = sc.nextInt(); int m = sc.nextInt(); int s = sc.nextInt() - 1; int f = sc.nextInt() - 1; int[] tt = new int[m]; int[] ll = new int[m]; int[] rr = new int[m]; for (int h = 0; h < m; h++) { tt[h] = sc.nextInt() - 1; ll[h] = sc.nextInt() - 1; rr[h] = sc.nextInt() - 1; } char[] cc = new char[n + m]; int k = 0; for (int h = 0, t = 0, i = s; i != f; h++, t++) { while ((h == m || t < tt[h]) && i != f) { if (i < f) { cc[k++] = 'R'; i++; } else { cc[k++] = 'L'; i--; } t++; } if (i == f) break; int j = i < f ? i + 1 : i - 1; if (ll[h] <= i && i <= rr[h] || ll[h] <= j && j <= rr[h]) cc[k++] = 'X'; else { cc[k++] = i < f ? 'R' : 'L'; i = j; } } println(new String(cc, 0, k)); } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; long long n, m, s, f, z, cnt = 1; map<int, int> l, r; string ans; int main() { cin >> n >> m >> s >> f; for (int i = 1; i <= m; i++) { cin >> z; cin >> l[z] >> r[z]; } while (s != f) { if (s > f) { if (l[cnt] > s || r[cnt] < s - 1) { s--; ans += 'L'; } else ans += 'X'; } else { if (l[cnt] > s + 1 || r[cnt] < s) { s++; ans += 'R'; } else ans += 'X'; } cnt++; } cout << ans; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.*; import java.io.PrintWriter; import java.util.*; import java.io.InputStream; import java.io.DataInputStream; public class Main { //static{ System.out.println("hello");} static InputStream inputStream = System.in; static OutputStream outputStream = System.out; static FastReader scn = new FastReader(inputStream); static PrintWriter out = new PrintWriter(outputStream); public static void main(String[] args) throws IOException { // HashMap<Integer,ArrayList<Integer>> hm=new HashMap<Integer,ArrayList<Integer>>(); int n=scn.ni(); int m=scn.ni(); int t[]=new int[1000010]; int s=scn.ni();int l[]=new int[1000010]; int f=scn.ni();int r[]=new int[1000010]; for(int i=1;i<=m ;i++) { t[i]=scn.ni(); l[i]=scn.ni(); r[i]=scn.ni(); } int curn=s; int loop=1,step=1; if (s<=f){ while(true) { if (curn>=f) break; if (step==t[loop]) { if (l[loop]<=curn && curn<=r[loop] ) { out.print('X'); } else if (curn==l[loop]-1) {out.print('X'); } else {out.print('R'); curn++;} loop++; } else {curn++; out.print('R');} step++; } } else { while(true) { if (curn<=f) break; if (step==t[loop]) { if (l[loop]<=curn && curn<=r[loop] ) { out.print('X'); } else if (curn==r[loop]+1) {out.print('X'); } else {out.print('L'); curn--;} loop++; } else {curn--; out.print('L'); } step++; } } out.close(); } } class num implements Comparator<num> { public int ff; public int ss; num() { } num(int x,int y) { this.ff = x; this.ss =y; } public int getff() { return ff; } public int getss() { return ss; } public boolean equals(Object n1) {num n=(num)n1; if (this.ff==n.ff && this.ss==n.ss) return true; else return false; } public int compare(num n1,num n2) { if (n1.ff!=n2.ff) return n1.ff-n2.ff; else return n2.ss-n1.ss; } } class FastReader { public InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException (); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read (buf); } catch (IOException e) { throw new InputMismatchException (); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public char nc() { int c = read (); while (isSpaceChar (c)) c = read (); return (char) c; } public int ni() { int c = read (); while (isSpaceChar (c)) c = read (); int sgn = 1; if (c == '-') { sgn = -1; c = read (); } int res = 0; do { if (c < '0' || c > '9') { } res *= 10; res += c - '0'; c = read (); } while (!isSpaceChar (c)); return res * sgn; } public long nl() { int c = read (); while (isSpaceChar (c)) c = read (); int sgn = 1; if (c == '-') { sgn = -1; c = read (); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException (); } res *= 10; res += c - '0'; c = read (); } while (!isSpaceChar (c)); return res * sgn; } public String ns() { int c = read (); while (isSpaceChar (c)) c = read (); StringBuilder res = new StringBuilder (); do { res.appendCodePoint (c); c = read (); } while (!isSpaceChar (c)); return res.toString (); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar (c); } return isWhitespace (c); } public static boolean isWhitespace(int c) { return c==' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return ns (); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import sys from itertools import * from math import * def solve(): n,m,s,f = map(int, input().split()) s-=1 f-=1 d = {} for _ in range(m): t,l,r = map(int, input().split()) d[t-1] = (l - 1, r - 1) # d = {(t - 1) : (l - 1, r - 1) for t,l,r in map(int, input().split()) for _ in range(m)} step = 0 res = "" while s != f: wantnext = s + 1 if s < f else s - 1 canmove = True if step in d: l, r = d[step] if (s >= l and s <= r) or (wantnext >= l and wantnext <= r): canmove = False if canmove: res += 'R' if wantnext > s else 'L' s = wantnext else: res += 'X' step += 1 print(res) # print(''.join(map(str, res))) #change to string at end to see if faster if sys.hexversion == 50594544 : sys.stdin = open("test.txt") solve()
PYTHON3
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; const int N = 100100; int t[N], l[N], r[N]; char str[N << 2]; int main() { int n, m, s, f, i; scanf("%d%d%d%d", &n, &m, &s, &f); for (i = 0; i < m; i++) scanf("%d%d%d", &t[i], &l[i], &r[i]); t[m] = 2000000000; int step = (f > s) ? 1 : -1, now = s, time = 1, len = 0, x = 0; while (now != f) { while (t[x] < time) x++; if (t[x] == time && ((l[x] <= now && r[x] >= now) || (l[x] <= now + step && r[x] >= now + step))) str[len++] = 'X'; else { str[len++] = (f > s) ? 'R' : 'L'; now += step; } time++; } str[len] = '\0'; printf("%s\n", str); return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, s, f, l = 1; cin >> n >> m >> s >> f; string q; for (int i = 0; i < m; i++) { int t, a, b; cin >> t >> a >> b; if (s != f) { for (int j = l; j < t; j++) { if (f > s) { s++; q.push_back('R'); } else if (f < s) { s--; q.push_back('L'); } else break; } if (s != f) { if (f > s) { if (!((s + 1) >= a && (s + 1) <= b)) { if (!(s >= a && s <= b)) { s++; q.push_back('R'); } else q.push_back('X'); } else q.push_back('X'); } else if (f < s) { if (!((s - 1) >= a && (s - 1) <= b)) { if (!(s >= a && s <= b)) { s--; q.push_back('L'); } else q.push_back('X'); } else q.push_back('X'); } } } l = t + 1; } if (s != f) { int y = abs(s - f); for (int i = 0; i < y; i++) { if (f > s) { s++; q.push_back('R'); } else if (f < s) { s--; q.push_back('L'); } } } cout << q << endl; return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { vector<char> v; int n, m, s, f, t, a, b, k, cnt = 1; char c, x = 'X'; cin >> n >> m >> s >> f; if (s < f) { k = 1; c = 'R'; } else { k = 0; c = 'L'; } for (int i = 1; i <= m; i++) { cin >> t >> a >> b; if (s != f) { for (int j = cnt; j < t; j++) { if (s == f) break; v.push_back(c); (k == 1) ? s++ : s--; } cnt = t + 1; if (k == 1) { if (s + 1 < a | s > b && s != f) { v.push_back(c); s++; } else if (s != f) v.push_back(x); } else if (k == 0) { if (s<a | s - 1> b && s != f) { v.push_back(c); s--; } else if (s != f) v.push_back(x); } } } int g = abs(s - f); if (g) { for (int h = 0; h < g; h++) v.push_back(c); } if (v.size()) { for (int i = 0; i < v.size(); i++) cout << v[i]; } cout << endl; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
n,m,s,f=map(int,input().split()) t={} step=1 ans='' if s<f:sig='R' else :sig='L' for i in range(m): t0,l0,r0=map(int,input().split()) t[t0]=[l0,r0] for i in range(1,n+m+1): if s<f: u=s+1 else:u=s-1 if i in t: if (t[i][0]<=s<=t[i][1])or(t[i][0]<=u<=t[i][1]): ans+='X' else: ans+=sig s=u else : ans+=sig s=u if s==f:break print(ans)
PYTHON3
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> int main() { int n, m, s, f, i, cnt = 0, x, y, tt = 0, now; scanf("%d%d%d%d", &n, &m, &s, &f); if (s < f) { now = s; for (i = 1;; i++) { if (i > tt && cnt < m) { scanf("%d%d%d", &tt, &x, &y); cnt++; } if (i == tt) { if ((x <= now && now <= y) || (x <= now + 1 && now + 1 <= y)) printf("X"); else { printf("R"); now++; } } else { printf("R"); now++; } if (now == f) break; } } else { now = s; for (i = 1;; i++) { if (i > tt && cnt < m) { scanf("%d%d%d", &tt, &x, &y); cnt++; } if (i == tt) { if ((x <= now && now <= y) || (x <= now - 1 && now - 1 <= y)) printf("X"); else { printf("L"); now--; } } else { printf("L"); now--; } if (now == f) break; } } return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
n, m, s, f = map(int ,raw_input().split()) data = [] for i in xrange(0,m): data.append(map(int ,raw_input().split())) now = s if s<f: d = 'R' di = 1 else: d = 'L' di = -1 t = 1 ans = [] data.append((10**9+1,1,n)) for i in data: while t<i[0]: ans.append(d) t += 1 now += di if now == f: break if now == f: break if (now>=i[1] and now<=i[2]) or (now+di>=i[1] and now+di<=i[2]): ans.append('X') else: ans.append(d) now += di if now == f: break t += 1 print ''.join(ans)
PYTHON
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s, f; cin >> n >> m >> s >> f; int t, l, r; string ans; int steps = 1; for (int i = 1; i <= m; i++) { cin >> t >> l >> r; while (steps != t) { if (s == f) break; if (s < f) { ans += 'R'; s += 1; } else { ans += 'L'; s -= 1; } steps++; } if (s < f) { if (s + 1 >= l && s + 1 <= r || s >= l && s <= r) { ans += 'X'; } else { s += 1; ans += 'R'; } } else if (s > f) { if (s - 1 >= l && s - 1 <= r || s >= l & s <= r) { ans += 'X'; } else { s -= 1; ans += 'L'; } } else break; steps++; } while (s != f) { if (s < f) { ans += 'R'; s += 1; } else { ans += 'L'; s -= 1; } } cout << ans << endl; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> int main(void) { int n, m, s, f, c, t = 0, l, r; scanf("%d %d %d %d", &n, &m, &s, &f); int d = s < f ? 1 : -1; for (c = 1; s != f; c++) { if (c > t && m) scanf("%d %d %d", &t, &l, &r), m--; if (c != t || (s < l || s > r) && (s + d < l || s + d > r)) s += d, putchar(d == 1 ? 'R' : 'L'); else putchar('X'); } }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.*; import java.util.*; /** * Created by Желтяков on 17.03.2017. */ public class app { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in/*new FileReader(new File("badsubs.in"))*/); int n = scanner.nextInt(); int m = scanner.nextInt(); int s = scanner.nextInt(); int f = scanner.nextInt(); int[][] a = new int[m][3]; StringBuffer sb=new StringBuffer(); int pos = s; for (int i = 0; i <m ; i++) { a[i][0] = scanner.nextInt(); a[i][1] = scanner.nextInt(); a[i][2] = scanner.nextInt(); } int i = 0; int t = 0; while (pos != f) { t++; boolean flag = false; if (i < m) { while (t == a[i][0]) { if (f > s) { if ((pos+1 >= a[i][1]&& pos <= a[i][2])) { flag = true; } } else { if ((pos >= a[i][1] && pos-1 <= a[i][2])) { flag = true; } } i++; if (i == m) { break; } } } if (flag) { sb.append("X"); } else { if (f > s) { sb.append("R"); pos++; } else { sb.append("L"); pos--; } } } System.out.println(sb.toString()); } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, m, s, f; cin >> n >> m >> s >> f; map<int, pair<int, int> > ma; map<int, pair<int, int> >::iterator it; long long a, b, c; string ans = ""; for (long long int i = 0; i < m; i++) { cin >> a >> b >> c; ma[a] = make_pair(b, c); } if (s < f) { long long i = 1; while (s != f) { it = ma.find(i); if (it == ma.end()) { ans += "R"; s++; } else { if ((s < it->second.first || s > it->second.second) && (s + 1 < it->second.first || s + 1 > it->second.second)) { ans += "R"; s++; } else ans += "X"; } i++; } } else { long long i = 1; while (s != f) { it = ma.find(i); if (it == ma.end()) { ans += "L"; s--; } else { if ((s < it->second.first || s > it->second.second) && (s - 1 < it->second.first || s - 1 > it->second.second)) { ans += "L"; ; s--; } else ans += "X"; } i++; } } cout << ans; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
n,m,s,f = map(int,raw_input().strip().split()) w = dict() for _ in range(m): t,l,r = map(int,raw_input().strip().split()) w[t] = l,r if s>f: dirc = "L" d = -1 else: dirc = "R" d = 1 p = s ans = [] t=1 while p!=f: if t in w: rs,re = w[t] if (p >= rs and p<= re) or (p+d >= rs and p+d <=re): ans.append("X") else: ans.append(dirc) p+=d else: ans.append(dirc) p+=d t+=1 print "".join(ans)
PYTHON
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { long n, m, s, f; cin >> n >> m >> s >> f; map<long long, pair<long, long> > mp; for (long i = 1; i <= m; i++) { long long t; long l, r; cin >> t >> l >> r; mp[t] = make_pair(l, r); } if (s < f) { string s1 = ""; long t = 1; while (s < f) { if ((s >= mp[t].first && s <= mp[t].second) || (s + 1 >= mp[t].first && s + 1 <= mp[t].second)) { s1 += 'X'; t++; } else { s1 += 'R'; s++; t++; } } cout << s1; return 0; } if (s > f) { string s1 = ""; long t = 1; while (s > f) { if ((s >= mp[t].first && s <= mp[t].second) || (s - 1 >= mp[t].first && s - 1 <= mp[t].second)) { s1 += 'X'; t++; } else { s1 += 'L'; s--; t++; } } cout << s1; return 0; } }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { string u, str = ""; int n, m, s, f, a, p = 0; cin >> n >> m >> s >> f; if (s < f) { u = "R"; a = 1; } else { u = "L"; a = -1; } for (int i = 0; i < m; i++) { int t, l, r; cin >> t >> l >> r; while (t - p > 0) if (s != f) { if (((s < l || r < s) && (s + a < l || r < s + a)) || p + 1 < t) { str += u; s += a; } else str += "X"; p++; } else p = t; } if (s != f) for (int i = 0; i < a * (f - s); i++) str += u; cout << str; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
input_string = raw_input() input_list = input_string.split(" ") n = int(input_list[0]) m = int(input_list[1]) s = int(input_list[2]) f = int(input_list[3]) if s < f: dir = 1 ch = 'R' else: dir = -1 ch = 'L' pos = s moves = [] t = [] l = [] r = [] step = 1 ind = 0 for i in range(m): input_string = raw_input() input_list = input_string.split(" ") x = int(input_list[0]) y = int(input_list[1]) z = int(input_list[2]) t.append(x) l.append(y) r.append(z) while pos != f: if ind < m and t[ind] == step: if (l[ind] <= pos and pos <= r[ind]) or (l[ind] <= (pos+dir) and (pos+dir) <= r[ind]): moves.append('X') else: pos += dir moves.append(ch) ind += 1 else: moves.append(ch) pos += dir step += 1 print "".join(moves)
PYTHON
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
//------------------------------------------------------------------------------------------------------- import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Reader; import java.io.UnsupportedEncodingException; import java.text.DecimalFormat; import java.util.StringTokenizer; public class B { private final static long START_TIME=System.currentTimeMillis(); private final static boolean LOG_ENABLED=true; private final static boolean ONLINE_JUDGE = LOG_ENABLED && (System.getProperty("ONLINE_JUDGE") != null); private final static String SYSTEM_ENCODING="utf-8"; private static class Logger{ private final PrintWriter logWriter=Util.newPrintWriter(System.err,true); private final DecimalFormat df=new DecimalFormat("0.000"); private void message(String type, String message, Object ... params){ if(ONLINE_JUDGE){ return; } logWriter.printf("["+type+"] "+df.format((System.currentTimeMillis()-START_TIME)/1000.0)+": "+message+"\r\n", params); } public void debug(String message, Object ... params){ message("DEBUG", message, params); } } private final static class Util{ public static PrintWriter newPrintWriter(OutputStream out, boolean autoFlush){ try { return new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(out), SYSTEM_ENCODING),autoFlush); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } } } public final static class FastScanner{ private BufferedReader reader; private StringTokenizer currentTokenizer; public FastScanner(Reader reader) { if(reader instanceof BufferedReader){ this.reader=(BufferedReader) reader; }else{ this.reader=new BufferedReader(reader); } } public String next(){ if(currentTokenizer==null || !currentTokenizer.hasMoreTokens()){ try { currentTokenizer=new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new IllegalStateException(e); } } return currentTokenizer.nextToken(); } public int nextInt(){ return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble(){ return Double.parseDouble(next()); } } private final static Logger log=new Logger(); private final BufferedReader reader; private final FastScanner in; private final PrintWriter out=Util.newPrintWriter(System.out,false); public B(BufferedReader reader){ this.reader=reader; in=new FastScanner(this.reader); } public static void main(String[] args) throws IOException { log.debug("Started"); try{ new B(new BufferedReader(new InputStreamReader(System.in, SYSTEM_ENCODING))).run(); }finally{ log.debug("Stopped"); } } void run(){ solve(); out.flush(); } private void solve(){ try{ int n = in.nextInt(); int m = in.nextInt(); int s = in.nextInt(); int f = in.nextInt(); boolean right=s<f; int prevT=1; for(int i=0;i<m;i++){ int currT=in.nextInt(); int l=in.nextInt(); int r=in.nextInt(); while(currT>prevT){ if(right){ s++; out.print("R"); }else{ s--; out.print("L"); } if(s==f){ return; } prevT++; } prevT++; int next = right?s+1:s-1; if(inRange(s,l,r)||inRange(next, l, r)){ out.print("X"); continue; } if(right){ s++; out.print("R"); }else{ s--; out.print("L"); } if(s==f){ return; } } while(s!=f){ if(right){ s++; out.print("R"); }else{ s--; out.print("L"); } } }finally{ out.println(); } } private boolean inRange(int s, int l, int r) { return (s>=l)&&(s<=r); } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> int main() { int n, m, s, f, t = 0, l, r, now, pass; char pr; scanf("%d %d %d %d", &n, &m, &s, &f); now = s; if (f - s > 0) { pr = 'R'; pass = 1; } else { pr = 'L'; pass = -1; } for (int i = 1; 1; i++) { if (i > t) { scanf(" %d %d %d", &t, &l, &r); m--; } if (i == t) { if ((l <= now && now <= r) || (l <= now + pass && now + pass <= r)) { printf("X"); } else { printf("%c", pr); now += pass; } } else { printf("%c", pr); now += pass; } if (now == f) { printf("\n"); break; } } while (m-- > 0) scanf(" %d %d %d", &t, &l, &r); return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class XeniaAndSpies { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(bf.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int s = Integer.parseInt(st.nextToken()); int f = Integer.parseInt(st.nextToken()); int now = 1; int j = 1; int[] t = new int[100000+10]; int[] l = new int[100000+10]; int[] r = new int[100000+10]; for (int i = 1; i <= m; ++i){ st = new StringTokenizer(bf.readLine()); t[i] = Integer.parseInt(st.nextToken()); l[i] = Integer.parseInt(st.nextToken()); r[i] = Integer.parseInt(st.nextToken()); } StringBuilder sb = new StringBuilder(""); while(s!=f){ int next = (s < f) ? s + 1 : s - 1; if (now != t[j]){ sb.append(s < f ? 'R' : 'L'); s = next; }else{ if ((s >= l[j] && s <= r[j]) || (next >= l[j] && next <= r[j])) sb.append('X'); else{ sb.append(s < f ? 'R' : 'L'); s = next; } j++; } now++; } System.out.println(sb); } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int T[100000 + 5], L[100000 + 5], R[100000 + 5]; int main() { int n, m, s, f; scanf("%d%d%d%d", &n, &m, &s, &f); for (int i = 0; i < m; ++i) { scanf("%d%d%d", &T[i], &L[i], &R[i]); } if (s < f) { int p = 0, cur = s; for (int i = 1;; ++i) { if (p < m && T[p] == i) { if ((cur >= L[p] && cur <= R[p]) || (cur + 1 >= L[p] && cur + 1 <= R[p])) { printf("X"); } else { printf("R"), cur++; } p++; } else { printf("R"), cur++; } if (cur == f) { break; } } } else { int p = 0, cur = s; for (int i = 1;; ++i) { if (p < m && T[p] == i) { if ((cur >= L[p] && cur <= R[p]) || (cur - 1 >= L[p] && cur - 1 <= R[p])) { printf("X"); } else { printf("L"), cur--; } p++; } else { printf("L"), cur--; } if (cur == f) { break; } } } puts(""); }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; const long long inf = 1000000000; double Time() { return double(clock()) / double(CLOCKS_PER_SEC); } const int N = 1000001; int n, s, f, m; int main() { cin >> n >> m >> f >> s; int j = 1; for (int i = (int)1; i <= (int)m; i++) { int t, l, r; cin >> t >> l >> r; if (f == s) break; if (t == j) { if (f < s) { if (f >= l && f <= r || f + 1 >= l && f + 1 <= r) cout << "X"; else cout << "R", f++; } else { if (f >= l && f <= r || f - 1 >= l && f - 1 <= r) cout << "X"; else cout << "L", f--; } } else { while (j < t) { if (f == s) break; if (f < s) cout << "R", f++; else cout << "L", f--; j++; } if (f == s) break; if (f < s) { if (f >= l && f <= r || f + 1 >= l && f + 1 <= r) cout << "X"; else cout << "R", f++; } else { if (f >= l && f <= r || f - 1 >= l && f - 1 <= r) cout << "X"; else cout << "L", f--; } } j = t + 1; } while (s != f) { if (f == s) break; if (f < s) cout << "R", f++; else cout << "L", f--; } return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; struct watch { long long l, r; }; int main() { int n, m, f, s, z; cin >> n >> m >> f >> s; map<long long, watch> mapa; watch tmp; for (int i = 0; i < m; i++) { cin >> z; cin >> tmp.l; cin >> tmp.r; mapa[z] = tmp; } if (f < s) { int pos = f; for (int t = 1; pos < s; t++) { if ((pos >= mapa[t].l && pos <= mapa[t].r) || (pos + 1 >= mapa[t].l && pos + 1 <= mapa[t].r)) { cout << "X"; } else { cout << "R"; pos++; } } } else { int pos = f; for (int t = 1; pos != s; t++) { if ((pos >= mapa[t].l && pos <= mapa[t].r) || (pos - 1 >= mapa[t].l && pos - 1 <= mapa[t].r)) { cout << "X"; } else { cout << "L"; pos--; } } } cout << endl; return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, m, s, f, t, l, r, dir, prvT = 0; cin >> n >> m >> s >> f; if (f > s) dir = 1; else dir = -1; while (m--) { cin >> t >> l >> r; long long int moves = t - prvT - 1; for (; moves > 0 && s != f; moves--) { s += dir; if (dir == 1) cout << 'R'; else cout << 'L'; } if (s == f) continue; if ((s < l || s > r) && ((s + dir) < l || (s + dir) > r)) { s += dir; if (dir == 1) cout << 'R'; else cout << 'L'; } else cout << 'X'; prvT = t; } while (s != f) { s += dir; if (dir == 1) cout << 'R'; else cout << 'L'; } return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s, f; int t, l, r, nw, count; while (scanf("%d %d %d %d", &n, &m, &s, &f) == 4) { string str; count = 1; map<int, pair<int, int> > v; for (int i = 1; i <= m; i++) { scanf("%d", &t); scanf("%d %d", &v[t].first, &v[t].second); } while (s != f) { if (s < f) { if ((s >= v[count].first && s <= v[count].second) || (s + 1 >= v[count].first && s + 1 <= v[count].second)) str.push_back('X'); else str.push_back('R'), s++; } else if (s > f) { if ((s >= v[count].first && s <= v[count].second) || (s - 1 >= v[count].first && s - 1 <= v[count].second)) str.push_back('X'); else str.push_back('L'), s--; } count++; } cout << str << endl; } return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; void fastInOut() { ios_base::sync_with_stdio(0); cin.tie(NULL), cout.tie(NULL); } int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); } int lcm(int a, int b) { return ((a * b) / gcd(a, b)); } long long pw(long long b, long long p) { if (!p) return 1; long long sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; } int sd(long long x) { return x < 10 ? x : x % 10 + sd(x / 10); } int n, m; string str1, str2; char C; int arr2[10]; int main() { int s, f, t, l, r; cin >> n >> m >> s >> f; map<int, pair<int, int> > gemy; for (long long i = 0; i < m; i++) { cin >> t >> l >> r; gemy[t] = {l, r}; } int cnt = 0; while (s != f) { cnt++; if (gemy.count(cnt) != 0) { if (s < gemy[cnt].first || s > gemy[cnt].second) { if (s > f && (s - 1 < gemy[cnt].first || s - 1 > gemy[cnt].second)) { cout << "L"; s--; } else if (s < f && (s + 1 < gemy[cnt].first || s + 1 > gemy[cnt].second)) { s++; cout << "R"; } else cout << "X"; } else cout << "X"; } else if (s > f) { s--; cout << "L"; } else if (s < f) { s++; cout << "R"; } } }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; long long M = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(0); cerr.tie(0); int n, m, s, f, l, r, a[100005], b[100005], t[100005]; cin >> n >> m >> s >> f; for (int i = 0; i < m; i++) { cin >> t[i] >> a[i] >> b[i]; } int dir = 1; if (s > f) dir = -1; int t2 = 1, j = 0; while (s != f) { int mv = 0; if (j < m && t2 == t[j]) { if ((a[j] <= s && b[j] >= s) || (a[j] <= (s + dir) && b[j] >= (s + dir))) mv = 1; j++; } if (mv == 0) { s += dir; if (dir == 1) cout << "R"; else cout << "L"; } else { cout << "X"; } t2++; } return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.*; import java.io.*; /** * * @author alanl */ public class Solve { /** * @param args the command line arguments */ static BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static void main(String[] args) throws IOException { int n = readInt(), m = readInt(), s = readInt(), f = readInt(); ArrayList<edge>adj = new ArrayList(); adj.add(new edge(0, 0, 0)); for(int i = 0; i<m; i++){ int t = readInt(), l = readInt(), r = readInt(); adj.add(new edge(t, l, r)); } adj.add(new edge((int)(1e9+1), 0, 0)); int cur = s; boolean flag = false; for(int i = 1; i<adj.size(); i++){ edge val = adj.get(i); int diff = adj.get(i).t-adj.get(i-1).t; if(diff>1){ for(int j = 0; j<diff-1; j++){ if(cur>f){ print("L"); cur--; } else{ print("R"); cur++; } if(cur==f){ flag = true; break; } } } if(cur==f || flag)break; if(cur>f){ if(cur>=val.s && cur<=val.e || cur-1>=val.s && cur-1<=val.e) print("X"); else{ print("L"); cur--; } } else{ if(cur>=val.s && cur<=val.e || cur+1>=val.s && cur+1<=val.e) print("X"); else{ print("R"); cur++; } } if(cur==f || flag)break; } println(); } static class edge{ int t, s, e; edge(int t0, int s0, int e0){ t = t0; s = s0; e = e0; } } static String next () throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(input.readLine().trim()); return st.nextToken(); } static long readLong () throws IOException { return Long.parseLong(next()); } static int readInt () throws IOException { return Integer.parseInt(next()); } static double readDouble () throws IOException { return Double.parseDouble(next()); } static char readChar () throws IOException { return next().charAt(0); } static String readLine () throws IOException { return input.readLine().trim(); } static void print(Object b) { System.out.print(b); } static void println(Object b) { System.out.println(b); } static void println() { System.out.println(); } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int a[100010][3]; bool inside(int x, int a, int b) { return a <= x && x <= b; } int main(int argc, char** argv) { ios_base::sync_with_stdio(false); int n, m, s, f; cin >> n >> m >> s >> f; for (int i = 0; i < m; ++i) { cin >> a[i][0] >> a[i][1] >> a[i][2]; } string result = ""; int cur_t = 1; char direction = s < f ? 'R' : 'L'; int d = s < f ? 1 : -1; int i = 0; for (int cur_t = 1; s != f; ++cur_t) { int t = a[i][0]; int l = a[i][1]; int r = a[i][2]; if (cur_t == t) { if (inside(s, l, r) || inside(s + d, l, r)) { result += "X"; } else { result += direction; s += d; } ++i; } else { result += direction; s += d; } } cout << result << "\n"; return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int n, m, s, e; map<int, pair<int, int> > col; char ans[200100]; inline bool in(int k, pair<int, int> avg) { return (avg.first <= k && k <= avg.second); } int main() { scanf("%d%d%d%d", &n, &m, &s, &e); col.clear(); int a, b, c; for (int i = 0; i < m; i++) { scanf("%d%d%d", &a, &b, &c); col[a] = make_pair(b, c); } int val = 0; char ch; if (s < e) { val = 1; ch = 'R'; } else { val = -1; ch = 'L'; } map<int, pair<int, int> >::iterator ite; int state = s; for (int i = 1; i < 200100; i++) { if (state == e) { ans[i] = 0; break; } pair<int, int> avg; if (col.end() != (ite = col.find(i))) avg = ite->second; else avg = make_pair(-1, -1); if (!in(state, avg) && !in(state + val, avg)) { ans[i] = ch; state += val; } else ans[i] = 'X'; } printf("%s", ans + 1); return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
n,m,s,f = map(int,input().split()) l = [] for i in range(m): k = list(map(int,input().split())) l.append(k) count = 1 ans = '' i = 0 while i<m: if count < l[i][0]: if f>s: ans+='R'*min(l[i][0] - count,f-s) s+=min(l[i][0] - count,f-s) count = l[i][0] if f<s: ans+='L'*min(l[i][0] - count,s-f) s-=min(l[i][0] - count,s-f) # print(min(l[i][0] - count,s-f)) count = l[i][0] else: if l[i][1]<=s<=l[i][2]: ans+='X' else: if f>s: if l[i][1]<=s+1<=l[i][2]: ans+='X' else: ans+='R' s+=1 if f<s: if l[i][1]<=s-1<=l[i][2]: ans+='X' else: ans+='L' s-=1 i+=1 count+=1 # print(s) if s == f: break if s!=f: if s<f: ans+='R'*(f-s) else: ans+='L'*(s-f) print(ans)
PYTHON3
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { ios::sync_with_stdio(0); cin.tie(NULL); long long n, m, s, f; string moves = ""; cin >> n >> m >> s >> f; queue<pair<int, pair<int, int> > > vec; for (int i = 0; i < m; i++) { int t, l, r; cin >> t >> l >> r; vec.push(pair<int, pair<int, int> >(t, pair<int, int>(l, r))); } int actual = 1; while (s != f) { if (vec.front().first == actual) { pair<int, int> aux = vec.front().second; vec.pop(); if (s < f) { if ((s >= aux.first && s <= aux.second) || (s + 1 >= aux.first && s + 1 <= aux.second)) { moves += "X"; } else { moves += "R"; s++; } } else { if ((s >= aux.first && s <= aux.second) || (s - 1 >= aux.first && s - 1 <= aux.second)) { moves += "X"; } else { moves += "L"; s--; } } } else { if (s < f) { moves += "R"; s++; } else { moves += "L"; s--; } } actual++; } cout << moves; return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.Scanner; public class Main { private static Scanner sc = new Scanner(System.in); private static int m, s, f; private static int ti, li, ri; private static int time = 1, step = 0, next; private static int move; private static char act; private static StringBuilder action = new StringBuilder(); public static void main(String[] args) { String[] info = sc.nextLine().split(" "); m = Integer.parseInt(info[1]); s = Integer.parseInt(info[2]); f = Integer.parseInt(info[3]); move = s < f ? 1 : -1; act = s < f ? 'R' : 'L'; readStep(); while (s != f) { if (time == ti) { next = s + move; if ((s >= li && s <= ri) || (next >= li && next <= ri)) keep(); else pass(); readStep(); } else { next = s + move; pass(); } } sc.close(); System.out.println(action.toString()); } private static void readStep() { if (step < m) { String[] info = sc.nextLine().split(" "); ti = Integer.parseInt(info[0]); li = Integer.parseInt(info[1]); ri = Integer.parseInt(info[2]); step++; } } private static void keep() { action.append('X'); time++; } private static void pass() { action.append(act); s = next; time++; } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.*; import java.util.*; public class XeniaAndSpies { public static void main(String args[]) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int m = scn.nextInt(); int s = scn.nextInt(); int f = scn.nextInt(); int i,j,time = 1; int arr[][] = new int[m][3]; StringBuilder sb = new StringBuilder(); for(i = 0;i < m; ++i) { for(j = 0;j < 3; ++j) { arr[i][j] = scn.nextInt(); } } i = 0;j = 0; PrintWriter pw = new PrintWriter(System.out); if(s < f) { while(s < f && i < m) { if(time == arr[i][0]) { if((s >= arr[i][1] && s <= arr[i][2]) || ((s + 1) >= arr[i][1] && (s + 1) <= arr[i][2])) { sb.append('X'); } else { sb.append('R'); s++; } i++; } else { sb.append('R'); s++; } time++; } while(s != f) { sb.append('R'); s++; } } else if(s > f) { while(s > f && i < m) { if(time == arr[i][0]) { if((s >= arr[i][1] && s <= arr[i][2]) || ((s - 1) >= arr[i][1] && (s - 1) <= arr[i][2])) { sb.append('X'); } else { sb.append('L'); s--; } i++; } else { sb.append('L'); s--; } time++; } while(s != f) { sb.append('L'); s--; } } pw.println(sb); pw.close(); scn.close(); } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.awt.Point; import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; public class _Solution implements Runnable{ final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException{ if (ONLINE_JUDGE){ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); }else{ in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException{ while(!tok.hasMoreTokens()){ try{ tok = new StringTokenizer(in.readLine()); }catch (Exception e){ return null; } } return tok.nextToken(); } int readInt() throws IOException{ return Integer.parseInt(readString()); } long readLong() throws IOException{ return Long.parseLong(readString()); } double readDouble() throws IOException{ return Double.parseDouble(readString()); } public static void main(String[] args){ new Thread(null, new _Solution(), "", 256 * (1L << 20)).start(); } long timeBegin, timeEnd; void time(){ timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } public void run(){ try{ timeBegin = System.currentTimeMillis(); init(); solve(); out.close(); time(); }catch (Exception e){ e.printStackTrace(System.err); System.exit(-1); } } void solve() throws IOException{ int n = readInt(); int m = readInt(); int s = readInt(); int f = readInt(); int tt = 0; for (int i = 0; i < m; i++) { int t = readInt(); int l = readInt(); int r = readInt(); tt++; if (tt < t){ int q = t - tt; for (int j =0; j < q; j++) { if (s == f) return; if (s < f) { out.print("R"); s++; }else{ out.print("L"); s--; } } tt = t; } if (tt == t) { if (s == f) return; if (s < f) { if (s < l-1 || s > r) { out.print("R"); s++; }else { out.print("X"); } } if (s > f) if (s > r+1 || s < l) { out.print("L"); s--; }else { out.print("X"); } } } while (s != f) { if (s == f) return; if (s < f) { out.print("R"); s++; }else{ out.print("L"); s--; } } } }
JAVA
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int n, m, s, f, t[100000], l[100000], r[100000], t1; int main() { cin >> n >> m >> s >> f; for (int i = 0; i < m; ++i) cin >> t[i] >> l[i] >> r[i]; for (int i = 1, j = 0; s != f; ++i) { if (i == t[j]) { if (s < f) { if (s > r[j] || s + 1 < l[j]) putchar('R'), ++s; else putchar('X'); } else { if (s < l[j] || s - 1 > r[j]) putchar('L'), --s; else putchar('X'); } ++j; } else { if (s < f) putchar('R'), ++s; else putchar('L'), --s; } } puts(""); return 0; }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int const OO = (1 << 28); int cnt[5003]; vector<pair<int, pair<int, int> > > X; void solve(int inc, char dir, int S, int F) { int idx = 0; int t = 0; while (++t && S != F) { if (idx >= X.size() || t != X[idx].first) { S += inc; cout << dir; } else { if ((S < X[idx].second.first || S > X[idx].second.second) && (S + inc < X[idx].second.first || S + inc > X[idx].second.second)) { S += inc; cout << dir; } else cout << "X"; idx++; } } cout << endl; } int main() { int N, M, S, F; cin >> N >> M >> S >> F; for (int i = 0; i < M; ++i) { int t, l, r; cin >> t >> l >> r; X.push_back(make_pair(t, make_pair(l, r))); } if (S < F) { solve(1, 'R', S, F); } else { solve(-1, 'L', S, F); } }
CPP
342_B. Xenia and Spies
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. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.math.BigDecimal; import java.math.BigInteger; import java.nio.ByteBuffer; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Stack; //: God does not play dice with the world :) public class Main{ public static void main(String[] args) throws IOException { // String IN = "C:\\Users\\ugochukwu.okeke\\Desktop\\in.file"; // String OUT = "C:\\Users\\ugochukwu.okeke\\Desktop\\out.file"; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); String[] in = br.readLine().split(" "); int n = Integer.parseInt(in[0]),m = Integer.parseInt(in[1]),s = Integer.parseInt(in[2]),f = Integer.parseInt(in[3]); StringBuilder sb = new StringBuilder(); int g = 1; for(int i=0;i<m;i++){ in=br.readLine().split(" "); int L = Integer.parseInt(in[1]); int R = Integer.parseInt(in[2]); int k = Integer.parseInt(in[0]); if(s==f) break; //System.out.println("L R "+L +" "+R); int d = k-g; if(s < f) { while(d>0 && s<f) { d--; sb.append("R"); s++; } if(s==f) break; if((L<=s&&s<=R) || (L<=s+1&&s+1<=R)){ sb.append("X"); } else{ s++; sb.append("R"); } } else{ // System.out.println(k+" "+g); // System.out.println(i+" "+d); while(d>0 && s > f) { d--; sb.append("L"); s--; } if(s==f) break; if((L<=s&&s<=R) || (L<=s-1&&s-1<=R)){ sb.append("X"); } else{ s--; sb.append("L"); } } g = k+1; //System.out.print("s is "+s+" "); } while(s<f) { s++; sb.append("R"); } while(s>f) { s--; sb.append("L"); } bw.append(sb.toString()+"\n"); bw.close(); } }
JAVA