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
#include <bits/stdc++.h> using namespace std; int t[100009], l[100009], r[100009]; int main(void) { int i, n, m, s, f; while (scanf("%d %d %d %d", &n, &m, &s, &f) == 4) { for (i = 1; i <= m; i++) { scanf("%d %d %d", &t[i], &l[i], &r[i]); } int start = 1; for (i = 1; i <= m; i++, start++) { if (s == f) break; if (start < t[i]) { while (start < t[i]) { start++; if (s < f) { s++; printf("R"); } else { s--; printf("L"); } if (s == f) break; } } if (s == f) break; if (s < l[i] || s > r[i]) { if (s < f) { if ((s + 1) < l[i] || (s + 1) > r[i]) { s++; printf("R"); } else { printf("X"); } } else { if ((s - 1) < l[i] || (s - 1) > r[i]) { s--; printf("L"); } else { printf("X"); } } if (s == f) break; } else { printf("X"); } } while (s < f) { s++; printf("R"); } while (s > f) { s--; printf("L"); } 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.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Arrays; public class B { static int[] u = new int[1010], a = new int[50]; public static void main(String[] args) throws IOException { int n = i(), m = i(), s = i(), f = i(); int dir = s < f ? 1 : -1; char ch = s < f ? 'R' : 'L'; int step = 0; int t = i(), l = i(), r = i(), tCnt = 1; StringBuilder sb = new StringBuilder(); while (s != f) { step++; if (step < t || step > t){ s+= dir; sb.append(ch); } else { if ((s >= l && s <= r )|| (s + dir >= l && s + dir <= r)) { sb.append('X'); }else{ s += dir; sb.append(ch); } if (tCnt < m){ t = i(); l = i(); r = i(); } tCnt++; } } pw.println(sb.toString()); pw.close(); System.exit(0); } // -- DEBUG switch -- static final boolean DBG = false; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StreamTokenizer st = new StreamTokenizer(br); static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); static int i() throws IOException{ st.nextToken(); return (int)st.nval; } static void swap(int[] a, int i, int j){ int t = a[i]; a[i] = a[j]; a[j] = t; } static int parseInt(String s) { return Integer.parseInt(s); } static void prt(int[] a){ prt(a, 0, a.length-1); } static void prt(int[] a, int l, int r){ for (int i=l; i<=r; i++) pw.print(a[i] + " "); pw.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; string ans = ""; cin >> n >> m >> s >> f; int last = 0; for (int i = 0; i < m; i++) { int t, l, r; cin >> t >> l >> r; if (s < f) { for (int j = 0; j < t - last; j++) { if (s == f) { cout << ans << endl; return 0; } if (((s < l || s > r) && (s + 1 < l || s + 1 > r)) || j != (t - last) - 1) { s++; ans += 'R'; } else ans += 'X'; } } else if (s > f) { for (int j = 0; j < t - last; j++) { if (s == f) { cout << ans << endl; return 0; } if (((s < l || s > r) && (s - 1 < l || s - 1 > r)) || j != (t - last) - 1) { s--; ans += 'L'; } else ans += 'X'; } } last = t; } if (s != f) { if (s < f) for (int i = 0; i < f - s; i++) ans += 'R'; else for (int i = 0; i < s - f; i++) ans += 'L'; } 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
import java.io.*; import java.util.*; public class Template implements Runnable { BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException { try { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } catch (Exception e) { String filename = ""; if (filename.isEmpty()) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader(filename + ".in")); out = new PrintWriter(filename + ".out"); } } } String readString() { while (!tok.hasMoreTokens()) { try { tok = new StringTokenizer(in.readLine(), " :"); } catch (Exception e) { return null; } } return tok.nextToken(); } int readInt() { return Integer.parseInt(readString()); } int[] readIntArray(int size) { int[] res = new int[size]; for (int i = 0; i < size; i++) { res[i] = readInt(); } return res; } long readLong() { return Long.parseLong(readString()); } double readDouble() { return Double.parseDouble(readString()); } <T> List<T>[] createGraphList(int size) { List<T>[] list = new List[size]; for (int i = 0; i < size; i++) { list[i] = new ArrayList<>(); } return list; } class Graph { int[][] graph; List<Integer>[] temp; int n; Graph(int n) { this.n = n; graph = new int[n][]; temp = createGraphList(n); } void add(int x, int y) { temp[x].add(y); temp[y].add(x); } void addDir(int x, int y) { temp[x].add(y); } void build() { for (int i = 0; i < n; i++) { graph[i] = new int[temp[i].size()]; for (int j = 0; j < temp[i].size(); j++) graph[i][j] = temp[i].get(j); } } void buildWithUniq() { for (int i = 0; i < n; i++) { HashSet<Integer> uniqSet = new HashSet<>(); uniqSet.addAll(temp[i]); graph[i] = new int[uniqSet.size()]; int index = 0; for (int x : uniqSet) { graph[i][index++] = x; } } } } public static void main(String[] args) { new Template().run(); } long timeBegin, timeEnd; void time() { timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } long memoryTotal, memoryFree; void memory() { memoryFree = Runtime.getRuntime().freeMemory(); System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB"); } public void run() { try { timeBegin = System.currentTimeMillis(); memoryTotal = Runtime.getRuntime().freeMemory(); init(); solve(); out.close(); if (System.getProperty("ONLINE_JUDGE") == null) { time(); memory(); } } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } int next(int cur, int target) { if (target > cur) return cur + 1; return cur - 1; } char nextchar(int cur, int target) { return cur < target ? 'R' : 'L'; } boolean in(int l, int r, int i) { return i >= l && i <= r; } private void solve() throws IOException { int n = readInt(); int m = readInt(); int s = readInt() - 1; int f = readInt() - 1; int[] time = new int[m + 1]; int[] l = new int[m + 1]; int[] r = new int[m + 1]; for (int i = 0; i < m; i++) { time[i] = readInt(); l[i] = readInt() - 1; r[i] = readInt() - 1; } time[m] = 2 * (int) 1e9; int ind = 0; StringBuilder sb = new StringBuilder(); for (int t = 1; ; t++) { boolean ok = true; if (time[ind] == t) { ok = !in(l[ind], r[ind], s) && !in(l[ind], r[ind], next(s, f)); ind++; } if (ok) { sb.append(nextchar(s, f)); s = next(s, f); } else { sb.append('X'); } if (s == f) { out.println(sb.toString()); return; } } } }
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() { cin.sync_with_stdio(0); int n, m, s, f; cin >> n >> m >> s >> f; char pass = (s > f) ? 'L' : 'R'; int t, l, r; int prev = 0; for (int i = 0; i < m; i++) { cin >> t >> l >> r; if (s == f) continue; int frees = t - prev - 1; for (int j = 0; j < frees; j++) { cout << pass; if (s < f) s++; else s--; if (s == f) { break; } } if (s != f) { if (s > f) { if (s - 1 > r || s < l) { s--; cout << pass; } else { cout << 'X'; } } else { if (s > r || s + 1 < l) { s++; cout << pass; } else { cout << 'X'; } } } prev = t; } int rem = f - s; if (rem < 0) rem *= -1; for (int i = 0; i < rem; i++) cout << pass; 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
maxn=100000+200; ans="" def prin(): print ans[1:] opt=[[] for i in range(maxn)] N,M,S,T=map(int, raw_input().split()) if S<T: mv=1 chr='R' else: mv=-1 chr='L' opt[0]=[0,1,N] for i in range(M): opt[i+1]=list(map(int, raw_input().split())); opt[M+1]=[99999999,0,0] for i in range(M+1): t,l,r=opt[i][0],opt[i][1],opt[i][2] if (l<=S and S<=r) or (l<=S+mv and S+mv<=r): ans+='X' else: S+=mv ans+=chr if S==T: prin() exit() j=t+1 while j<opt[i+1][0]: S+=mv ans+=chr if S==T: prin() exit() j+=1
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 i = s, t = 1; string ans = ""; map<int, pair<int, int>> watching; for (int i = 1; i <= m; i++) { int t; cin >> t; int l, r; cin >> l >> r; watching[t] = {l, r}; } while (i != f) { if (watching.find(t) == watching.end()) { if (f > s) { i++; ans += 'R'; } else { i--; ans += 'L'; } } else { int l = watching[t].first, r = watching[t].second; int curr = i, next = (f > s) ? i + 1 : i - 1; if ((l <= curr && r >= curr) || (l <= next && r >= next)) ans += 'X'; else { if (f > s) { i++; ans += 'R'; } else { i--; ans += 'L'; } } } t++; } 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> using namespace std; int main() { int n, m, s, f; cin >> n >> m >> s >> f; int t, l, r; int d = f - s; cin >> t >> l >> r; for (int i = 1;; ++i) { if (i > t) { cin >> t >> l >> r; } if (d > 0) { if (i != t || s > r || s + 1 < l) { s++; cout << "R"; } else { cout << "X"; } } else { if (i != t || s < l || s - 1 > r) { s--; cout << "L"; } else { cout << "X"; } } if (s == f) { 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=list(map(int,input().split())) if s>f: a='L' c=-1 elif s==f: a='X' c=0 else: a='R' c=1 l=[] for i in range(m): l.append(list(map(int,input().split()))) ans='' if l[0][0]!=1: d=l[0][0]-1 while s!=f and d>0: s=s+c ans=ans+a d=d-1 for i in range(m): if s==f: break if i==0: if (s>=l[i][1] and s<=l[i][2]) or (s+c>=l[i][1] and s+c<=l[i][2]): ans=ans+'X' else: ans=ans+a s=s+c else: if l[i][0]-l[i-1][0]==1: if (s>=l[i][1] and s<=l[i][2]) or (s+c>=l[i][1] and s+c<=l[i][2]): ans=ans+'X' else: ans=ans+a s=s+c else: d=l[i][0]-l[i-1][0]-1 while s!=f and d>0: s=s+c ans=ans+a d=d-1 if s==f: break if (s>=l[i][1] and s<=l[i][2]) or (s+c>=l[i][1] and s+c<=l[i][2]): ans=ans+'X' else: ans=ans+a s=s+c if s==f: break if s!=f: while s!=f: ans=ans+a s=s+c 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
n, m, s, f = [int(x) for x in raw_input().strip().split()] ans = [] prevt = 1 pos = s movedir = 'R' if f < s: movedir = 'L' togo = (f - pos if movedir == 'R' else pos - f) done = False for i in range(m): t, l, r = [int(x) for x in raw_input().strip().split()] if done: continue freemoves = t - prevt if togo <= freemoves: ans.append(movedir * togo) togo = 0 done = True continue else: ans.append(movedir * freemoves) pos += (freemoves if movedir == 'R' else -freemoves) togo -= freemoves if movedir == 'R': if (pos < l or pos > r) and (pos + 1 < l or pos + 1 > r): ans.append(movedir) pos += 1 togo -= 1 else: ans.append('X') if movedir == 'L': if (pos < l or pos > r) and (pos - 1 < l or pos - 1 > r): ans.append(movedir) pos -= 1 togo -= 1 else: ans.append('X') if togo == 0: done = True continue prevt = t + 1 if togo != 0: ans.append(movedir * togo) 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
import java.util.*; import java.io.*; public class xs { public static void main (String[] args) throws Exception{ Scanner in = new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); StringBuilder sb=new StringBuilder(); int n=in.nextInt(),m=in.nextInt(),s=in.nextInt(),f=in.nextInt(),last=0; if(s==f){ out.println("X"); out.close(); return; } for(int i=1;i<=m&&s!=f;i++){ int t=in.nextInt(); int l=in.nextInt(); int r=in.nextInt(); while(last+1<t&&s!=f){ if(s<f){ sb.append('R'); s++; } else if(s>f){ sb.append('L'); s--; } last++; } last=t; if(s==f) break; if(s>=l&&s<=r){ sb.append('X'); continue; } if(s<f){ if(s+1>=l&&s+1<=r) sb.append('X'); else{ sb.append('R'); s++; } } else { if(s-1>=l&&s-1<=r) sb.append('X'); else{ sb.append('L'); s--; } } } while(s!=f){ if(s<f){ sb.append('R'); s++; } else { sb.append('L'); s--; } } out.println(sb); out.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 sys n,m,s,f=map(int,sys.stdin.readline().split()) L=[] R=[] T=[] for i in range(m): t,l,r=map(int,sys.stdin.readline().split()) T.append(t) L.append(l) R.append(r) if(f>s): i=s step=1 ind=0 Ans="" while(i!=f): if(ind>=m or T[ind]!=step): Ans+="R" i+=1 else: if((i>=L[ind] and i<=R[ind]) or (i+1>=L[ind] and i+1<=R[ind])): Ans+="X" else: Ans+="R" i+=1 ind+=1 step+=1 else: i=s step=1 ind=0 Ans="" while(i!=f): if(ind>=m or T[ind]!=step): Ans+="L" i-=1 else: if((i>=L[ind] and i<=R[ind]) or (i-1>=L[ind] and i-1<=R[ind])): Ans+="X" else: Ans+="L" i-=1 ind+=1 step+=1 sys.stdout.write(Ans+"\n")
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() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int n, m, s, f; cin >> n >> m >> s >> f; long long int t[m], l[m], r[m]; for (long long i = 0; i < m; i++) { cin >> t[i] >> l[i] >> r[i]; } string str; long long int pos = s, step = 1, j = 0; if (s <= f) { for (int step = 1;; step++) { if (pos == f) break; if (t[j] == step) { if ((l[j] <= pos && r[j] >= pos) || (l[j] <= pos + 1 && r[j] >= pos + 1)) { str.push_back('X'); } else { str.push_back('R'); pos++; } j++; } else { str.push_back('R'); pos++; } } } else { for (int step = 1;; step++) { if (pos == f) break; if (t[j] == step) { if ((l[j] <= pos && r[j] >= pos) || (l[j] <= pos - 1 && r[j] >= pos - 1)) { str.push_back('X'); } else { str.push_back('L'); pos--; } j++; } else { str.push_back('L'); pos--; } } } cout << 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; const int MAXN = 1e5 + 10; int n, m, s, f; map<int, int> l; map<int, int> r; int main() { cin >> n >> m >> s >> f; for (int i = 0; i < m; i++) { int x, y, t; cin >> t >> x >> y; l[t] = x; r[t] = y; } for (int i = 1; s < f; i++) if (s + 1 < l[i] || r[i] < s || !r.count(i)) { cout << 'R'; s++; } else cout << 'X'; for (int i = 1; s > f; i++) { if (s < l[i] || r[i] < s - 1 || !l.count(i)) { cout << 'L'; s--; } else cout << 'X'; } cout << '\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 arr[100009][3]; int main() { int n, m, s, f, i; vector<char> V; scanf("%d%d%d%d", &n, &m, &s, &f); for (i = 1; i <= m; i++) { scanf("%d%d%d", &arr[i][0], &arr[i][1], &arr[i][2]); } int count = 1, ind = 1, curr = s; if (s < f) { while (true) { if (ind <= m) { if (count == arr[ind][0]) { if (curr >= arr[ind][1] && curr <= arr[ind][2]) { V.push_back('X'); } else if (curr + 1 == arr[ind][1]) V.push_back('X'); else { V.push_back('R'); curr++; } } else { V.push_back('R'); curr++; } } else { V.push_back('R'); curr++; } if (curr == f) break; count++; if (arr[ind][0] < count) ind++; } } else { while (true) { if (ind <= m) { if (count == arr[ind][0]) { if (curr >= arr[ind][1] && curr <= arr[ind][2]) { V.push_back('X'); } else if (curr - 1 == arr[ind][2]) V.push_back('X'); else { V.push_back('L'); curr--; } } else { V.push_back('L'); curr--; } } else { V.push_back('L'); curr--; } if (curr == f) break; count++; if (arr[ind][0] < count) ind++; } } for (i = 0; i < V.size(); i++) printf("%c", V[i]); 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 ContestTemplate { public static void main(String[] args) throws IOException { rd = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); st = new StringTokenizer(rd.readLine()); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); s = Integer.parseInt(st.nextToken()); f = Integer.parseInt(st.nextToken()); t = new int[m]; for(int i=0; i<m; i++){ st = new StringTokenizer(rd.readLine()); t[i] = Integer.parseInt(st.nextToken()); L.put(t[i], Integer.parseInt(st.nextToken())); R.put(t[i], Integer.parseInt(st.nextToken())); } int cur = s; int time = 1; while(true){ if(cur==f) break; int next = (f<s? cur-1: cur+1); while (L.containsKey(time) && intersect(L.get(time), R.get(time), Math.min(cur, next), Math.max(cur, next))){ pw.print('X'); time++; } pw.print(next>cur? 'R': 'L'); time++; cur = next; } pw.println(); pw.flush(); } static boolean intersect(int a, int b, int x, int y){ return (x<=a && b<=y) || (a<=x && x<=b) || (a<=y && y<=b); } static int[] t; static HashMap<Integer, Integer> L = new HashMap<Integer, Integer>(), R = new HashMap<Integer, Integer>(); static int n, m, s, f; static BufferedReader rd; static PrintWriter pw; static StringTokenizer st; }
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.BufferedReader; import java.io.InputStreamReader; public class XeniaSpies{ public static void main(String[] argv) throws Exception{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String[] words=br.readLine().trim().split(" "); int n=Integer.parseInt(words[0]), m=Integer.parseInt(words[1]), s=Integer.parseInt(words[2]), f=Integer.parseInt(words[3]); String direction; if(s<f){ direction="R"; }else{ direction="L"; } int t, l, r, prevT=1; for(int i=0; i<m; i++){ words=br.readLine().trim().split(" "); t=Integer.parseInt(words[0]); l=Integer.parseInt(words[1]); r=Integer.parseInt(words[2]); for(int j=prevT; s!=f && j<t; j++){ if(direction.equals("R")){ if(s<n){ System.out.print("R"); s++; } }else{ if(s>1){ System.out.print("L"); s--; } } } if(s==f){ break; }else{ if(direction.equals("R")){ if(s<n && (s<l || s>r)&&(s+1<l || s+1>r)){ System.out.print("R"); s++; }else{ System.out.print("X"); } }else{ if(s>1 && (s<l||s>r) && (s-1<l ||s-1>r)){ System.out.print("L"); s--; }else{ System.out.print("X"); } } } prevT=t+1; } while(s!=f){ if(direction.equals("R")){ System.out.print("R"); s++; }else{ System.out.print("L"); s--; } } 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 n, m, s, f; int main() { cin >> n >> m >> s >> f; int tim = 1; for (int i = 0; i < m; i++) { int t, l, r; scanf("%d%d%d", &t, &l, &r); while (tim < t && s != f) { if (s < f) cout << "R"; else cout << "L"; if (s < f) s++; else s--; tim++; } if (s != f) { if (s < l || s > r) { int tmp = s; if (s < f) tmp++; else tmp--; if (tmp < l || tmp > r) { if (s < f) cout << "R"; else cout << "L"; if (s < f) s++; else s--; } else cout << "X"; } else cout << "X"; } tim++; } while (s != f) { if (s < f) cout << "R"; else cout << "L"; if (s < f) s++; else 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.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.StringTokenizer; public class Xenia2 { static StringBuilder result = new StringBuilder(); static int current; static boolean right; 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 f = Integer.parseInt(st.nextToken()); int s = Integer.parseInt(st.nextToken()); long step = 1; PrintWriter pw=new PrintWriter(System.out); current = f; right = false; if (f < s) right = true; for (int i = 0; i < m; i++) { // System.out.println(result+" not ans"); st = new StringTokenizer(bf.readLine()); long ti = Long.parseLong(st.nextToken()); int li = Integer.parseInt(st.nextToken()); int ri = Integer.parseInt(st.nextToken()); //System.out.println(ti+" "+step+" current is "+current); long l=ti-step; if (right) { if (ti > step) {// now he is not watching any one if ((current + l >= s)) { write('R', s - current); pw.print(result.toString());pw.flush();pw.close(); return; } // else if didnt reach current += l; write('R',l); checkRight(li, ri); step = ti + 1; } else if(!(current + 1 <= ri && current + 1 >= li || current <= ri && current >= li)) { current++; write('R',1);++step; } else{ write('X',1);++step; } } else { if (ti > step) { if (current - (l) <= s) {//XXXRXRXXRR write('L',current-s); pw.print(result.toString());pw.flush();pw.close(); return; } current -=l; write('L',l); checkLeft(li,ri); step = ti + 1; } else{ if(!(current - 1 <= ri && current - 1 >= li || current <= ri && current >= li)) { --current; write('L',1); } else{ write('X',1); } ++step; } } if (current == s) { pw.print(result.toString());pw.flush();pw.close(); return; } } if(right) write('R',s-current); else write('L',current-s); pw.print(result.toString());pw.flush();pw.close(); } public static void write(char x, long l) { for (int i = 0; i < l; i++) { result.append(x); } } public static void checkRight(int li, int ri) { if (!(current + 1 <= ri && current + 1 >= li || current <= ri && current >= li)) { current++; write('R', 1); } else write('X', 1); } public static void checkLeft(int li, int ri) { if (!(current - 1 <= ri && current - 1 >= li || current <= ri && current >= li)) { --current; write('L', 1); } else write('X', 1); } }
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 def start(): fin=sys.stdin n,m,s,f=map(int,fin.readline().split()) res="" cur=s if s<f: i=0 for _ in range(m): t,l,r=map(int,fin.readline().split()) while i+1<t: res+="R" cur+=1 i+=1 if cur==f: print res return if cur>=l and cur<=r or cur+1>=l and cur+1<=r: res+="X" else: res+="R" cur+=1 if cur==f: print res return i+=1 while cur<f: res+="R" cur+=1 else: i=0 for _ in range(m): t,l,r=map(int,fin.readline().split()) while i+1<t: res+="L" cur-=1 i+=1 if cur==f: print res return if cur>=l and cur<=r or cur-1>=l and cur-1<=r: res+="X" else: res+="L" cur-=1 if cur==f: print res return i+=1 while cur>f: res+="L" cur-=1 print res start()
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; struct W { int t, l, r; } w[100010]; int n, m, s, f; int main() { while (cin >> n >> m >> s >> f) { for (int i = (0); i < (m); i++) { cin >> w[i].t >> w[i].l >> w[i].r; } int t = 1, p = 0; int dir = f > s ? 1 : -1; while (s != f) { if (p < m && w[p].t == t && ((w[p].l <= s && s <= w[p].r) || (w[p].l <= s + dir && s + dir <= w[p].r))) { cout << "X"; } else { s += dir; cout << (dir == 1 ? 'R' : 'L'); } if (++t > w[p].t) p++; } 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
"""http://codeforces.com/problemset/problem/342/B""" def solve(s, f, t): res = '' step = 1 if s < f else -1 i = current = 0 while s != f: current += 1 ti, l, r = t[i] if i < len(t) else (-1, -1, -1) if ti == current: i += 1 if l <= s <= r or l <= s + step <= r: res += 'X' continue s += step res += 'R' if step > 0 else 'L' return res if __name__ == '__main__': parse = lambda: list(map(int, input().split())) n, m, s, f = parse() l = [parse() for _ in range(m)] print(solve(s, f, l))
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> struct tn { int t, l, r; }; tn lk[111111]; bool in(int loc, int l, int r) { if (loc >= l && loc <= r) return true; return false; } int absv(int i1) { return i1 < 0 ? -i1 : i1; } int main() { int n, m, s, f; scanf("%d%d%d%d", &n, &m, &s, &f); int now = s; int to; char toc; if (s < f) to = 1, toc = 'R'; else to = -1, toc = 'L'; int lastt = 0; for (int i = 0; i != m; i++) { int t, l, r; scanf("%d%d%d", &t, &l, &r); if (now == f) continue; int mid = t - lastt - 1; int go = absv(f - now); if (mid < go) { for (int j = 0; j != mid; j++) putchar(toc), now += to; } else { for (int j = 0; j != go; j++) putchar(toc), now += to; } if (now == f) continue; if (in(now + to, l, r) || in(now, l, r)) putchar('X'); else putchar(toc), now += to; lastt = t; } if (now != f) { int st = absv(now - f); for (int i = 0; i != st; i++) putchar(toc); } 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; public class B { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.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 d = S < F ? 1 : -1; char c = S < F ? 'R' : 'L'; StringBuilder out = new StringBuilder(); int currTime = 1; for (int i = 0; i < M && S != F; i++) { st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); int left = Integer.parseInt(st.nextToken()); int right = Integer.parseInt(st.nextToken()); while (t > currTime && S != F) { out.append(c); S += d; currTime++; } if (S != F) if ((S >= left && S <= right) || (S + d >= left && S + d <= right)) out.append('X'); else{ S += d; out.append(c); } currTime = t + 1; } while(S != F) { S += d; out.append(c); } System.out.println(out); } }
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; const int M = 110000; int t[M], l[M], r[M]; char ans[2 * M]; int main() { int i; int n, m, s, f; 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]); int k = 0, cr = 0, ds; while (s != f) { if (s < f) ds = 1; else ds = -1; int fl = 1; if (t[cr] - 1 == k) { if ((l[cr] <= s + ds && r[cr] >= s + ds) || (l[cr] <= s && r[cr] >= s)) { fl = 0; ans[k] = 'X'; } cr++; } if (fl) { if (ds < 0) ans[k] = 'L'; else ans[k] = 'R'; s += ds; } k++; } ans[k] = '\0'; printf("%s\n", 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; map<int, int> m; void primeFactors(int n) { while (n % 2 == 0) { m[2]++; n = n / 2; } for (int i = 3; i <= sqrt(n); i = i + 2) { while (n % i == 0) { m[i]++; n = n / i; } } if (n > 2) m[n]++; } long long gcd(long long a, long long b) { if (a % b == 0) return b; else return gcd(b, a % b); } int sum(long long a) { int sum = 0; while (a > 0) { sum = sum + (a % 10); a = a / 10; } return sum; } int count_digit(long long n) { int count = 0; while (n > 0) { if (n % 10 == 9) { count++; n = n / 10; continue; } else { return count; n = n / 10; } } } int binarySearch(int x, int y, long long z, long long v[]) { int low = x; int high = y; int mid = x + (y - x) / 2; while (low <= high) { if (v[mid] == z) return mid; if (v[mid] < z) return binarySearch(mid + 1, high, z, v); if (v[mid] > z) return binarySearch(low, mid - 1, z, v); } return -1; } long long modularExponentiation(long long x, long long n, long long M) { if (n == 0) return 1; else if (n % 2 == 0) return modularExponentiation((x * x) % M, n / 2, M); else return (x * modularExponentiation((x * x) % M, (n - 1) / 2, M)) % M; } long long binaryExponentiation(long long x, long long n) { if (n == 0) return 1; else if (n % 2 == 0) return binaryExponentiation(x * x, n / 2); else return x * binaryExponentiation(x * x, (n - 1) / 2); } int binary(int n) { int c = 0; while (n > 0) { if (n % 2 == 1) { return pow(2, c); } n = n / 2; c++; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tests = 1; while (tests--) { int n, m, l, r; long long t, tprev = 0, s, f; cin >> n >> m >> s >> f; while (m--) { cin >> t >> l >> r; if (t - 1 - tprev > 0) { if (f > s) { for (int i = 0; i < min(f - s, t - 1 - tprev); i++) cout << 'R'; s = s + min(f - s, t - 1 - tprev); } else if (s > f) { for (int i = 0; i < min(s - f, t - 1 - tprev); i++) cout << "L"; s = s - min(s - f, t - 1 - tprev); } } if (f > s) { if ((s < l && s + 1 < l) || s > r) { cout << "R"; s++; } else cout << "X"; } else if (s > f) { if ((s > r && s - 1 > r) || s < l) { s--; cout << 'L'; } else cout << "X"; } tprev = t; } if (s != f) { if (f > s) { for (int i = 0; i < f - s; i++) cout << "R"; } else { for (int i = 0; i < s - f; i++) 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
#include <bits/stdc++.h> using namespace std; struct inter { int l, r, t; }; int n, m, s, f; vector<inter> V; bool pro(inter a, inter b) { return a.t < b.t; } int main() { scanf("%d %d %d %d", &n, &m, &s, &f); for (int i = 0; i < m; i++) { inter p; scanf("%d %d %d", &p.t, &p.l, &p.r); V.push_back(p); } sort(V.begin(), V.end(), pro); int currT = 1; int currI = 0; int smjer = 1; char c = 'R'; if (s > f) { smjer *= -1; c = 'L'; } while (s != f) { while (currI < m && currT > V[currI].t) currI++; if (currI < m && currT == V[currI].t && ((V[currI].l <= s && V[currI].r >= s) || (V[currI].l <= s + smjer && V[currI].r >= s + smjer))) { printf("X"); currI++; currT++; } else { printf("%c", c); s += smjer; currT++; if (s == f) break; } } 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; struct T { int l, r; int t; }; T p[100010]; int main() { long long n, m, s, f; cin >> n >> m >> s >> f; s--; f--; int last = 0; for (int i = 0; i < m; i++) { cin >> p[i].t >> p[i].l >> p[i].r; p[i].l--; p[i].r--; } if (s < f) for (int i = 1; i <= 10000000; i++) { if (s == f) break; if (p[last].t == i) { last++; if (p[last - 1].l - 1 <= s && p[last - 1].r >= s) { printf("X"); continue; } } s++; printf("R"); } else for (int i = 1; i <= 10000000; i++) { if (s == f) break; if (p[last].t == i) { last++; if (p[last - 1].l <= s && p[last - 1].r + 1 >= s) { printf("X"); continue; } } s--; printf("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 spys, steps, start, finish, l, r, t; int counter; string res; map<int, pair<int, int> > step; bool left; while (cin >> spys >> steps >> start >> finish) { res.clear(); step.clear(); counter = 1; for (int i = 0; i < steps; i++) { cin >> t >> l >> r; step[t] = make_pair(l, r); } if (start < finish) left = true; else left = false; while (start != finish) { if (step.count(counter) && (start >= step[counter].first && start <= step[counter].second)) { res += "X"; } else if (step.count(counter)) { if (left) if (start + 1 >= step[counter].first && start + 1 <= step[counter].second) res += "X"; else res += "R", start++; else if (start - 1 >= step[counter].first && start - 1 <= step[counter].second) res += "X"; else res += "L", start--; } else { if (left) res += "R", start++; else res += "L", start--; } counter++; } cout << res << 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; const int inf = 1 << 30; int L[100010 * 2], R[100010 * 2]; int main() { int n, m, x, y; int t, l, r; scanf("%d%d%d%d", &n, &m, &x, &y); for (int i = 0; i < m; i++) { scanf("%d%d%d", &t, &l, &r); if (t <= n * 2) { L[t] = l, R[t] = r; } } for (int i = 1; i <= 2 * n; i++) { if (x < y) { if (x + 1 >= L[i] && x <= R[i]) printf("X"); else printf("R"), x++; } else if (x > y) { if (x >= L[i] && x - 1 <= R[i]) printf("X"); else printf("L"), x--; } else { printf("\n"); break; } } }
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, u, v, prev = 1, move; cin >> n >> m >> u >> v; string output; for (int i = 0; i < m; i++) { int t, l, r; cin >> t >> l >> r; move = t - prev; if (u > v) { move = min(u - v, move); output += string(move, 'L'); u -= move; if (u > v) { if (!(u >= l && u <= r + 1)) { output += string(1, 'L'); u--; } else output += string(1, 'X'); } } else if (u < v) { move = min(v - u, move); output += string(move, 'R'); u += move; if (u < v) { if (!(u >= l - 1 && u <= r)) { output += string(1, 'R'); u++; } else output += string(1, 'X'); } } prev = t + 1; } if (u > v) output += string(u - v, 'L'); else if (u < v) output += string(v - u, 'R'); cout << output << 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 sys a = [int(i) for i in raw_input().split()] (n, m, s, f) = (a[0], a[1], a[2], a[3]) w = [] for i in range(m): w.append([int(i) for i in raw_input().split()]) def isWarching(i, st, cw): if cw >= len(w): return False if w[cw][0] == st: return w[cw][1] <= i <= w[cw][2] cw += 1 if cw >= len(w): return False if w[cw][0] == st: return w[cw][1] <= i <= w[cw][2] return False st = 1 cw = 0 pos = s while pos != f: if s <= f: if not isWarching(pos, st, cw) and not isWarching(pos + 1, st, cw): pos += 1 sys.stdout.write('R') else: sys.stdout.write('X') else: if not isWarching(pos, st, cw) and not isWarching(pos - 1, st, cw): pos -= 1 sys.stdout.write('L') else: sys.stdout.write('X') if cw < len(w) and w[cw][0] == st: cw += 1 st += 1
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; long long power(long long x, long long y, long long p) { int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long PO(long long a, long long n) { long long val = 1; for (long long i = 0; i < n; i++) val *= a; return val; } bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } int s, f, cur; void move() { if (s < f) { cout << 'R'; cur++; } else { cout << 'L'; cur--; } } signed main() { int n, m; cin >> n >> m >> s >> f; int prev = 0; cur = s; for (int i = 0; i < m; i++) { int t, l, r; cin >> t >> l >> r; int buf = t - prev - 1; prev = t; if (cur == f) break; if (cur < f) { int cc = -cur + f; for (int i = 0; i < min(buf, cc); i++) { cout << 'R'; cur++; } if (cur == f) break; if (cur <= r && cur >= l - 1) cout << 'X'; else { cout << 'R'; cur++; } } else { int cc = cur - f; for (int i = 0; i < min(buf, cc); i++) { cout << 'L'; cur--; } if (cur == f) break; if (cur >= l && cur <= r + 1) cout << 'X'; else { cout << 'L'; cur--; } } } for (int i = 1; i <= abs(cur - f); i++) { if (cur < f) 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
#include <bits/stdc++.h> using namespace std; map<int, int> L, R; int main() { int n, m, s, t; while (scanf("%d%d%d%d", &n, &m, &s, &t) == 4) { L.clear(), R.clear(); for (int i = 0; i < (m); i++) { int a, b, x; scanf("%d%d%d", &x, &a, &b); L[x] = a, R[x] = b; } int dx = (s < t ? 1 : -1), Time = 1; while (s != t) { bool ok1 = 0, ok2 = 0; if (!L.count(Time)) ok1 = 1; else if (!(L[Time] <= s && s <= R[Time])) ok1 = 1; if (!L.count(Time)) ok2 = 1; else if (!(L[Time] <= (s + dx) && (s + dx) <= R[Time])) ok2 = 1; if (ok1 && ok2) s += dx, printf("%s", dx == 1 ? "R" : "L"); else printf("X"); Time++; } 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
n, m, s, f = map(int, input().split()) data = {} for _ in range(m): t, l, r = map(int, input().split()) data[t] = (l, r) k = 1 curr = s while curr != f: if k in data: if data[k][0] <= curr <= data[k][1]: print('X', end='') else: if 1 < curr < n: if curr > f: if data[k][0] <= curr - 1 <= data[k][1]: print('X', end='') else: print('L', end='') curr -= 1 else: if data[k][0] <= curr + 1 <= data[k][1]: print('X', end='') else: print('R', end='') curr += 1 elif curr == 1: if data[k][0] <= 2 <= data[k][1]: print('X', end='') else: print('R', end='') curr = 2 elif curr == n: if data[k][0] <= curr-1 <= data[k][1]: print('X', end='') else: print('L', end='') curr -= 1 else: if curr > f: print('L', end='') curr -= 1 else: print('R', end='') curr += 1 k += 1 # Mon Aug 12 2019 14:53:46 GMT+0300 (MSK)
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; long long mod = 1000000007; class Triplet { public: long long x; long long y; long long gcd; }; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } Triplet extendedEuclid(long long a, long long b) { if (b == 0) { Triplet ans; ans.gcd = a; ans.x = 1; ans.y = 0; return ans; } Triplet smallAns = extendedEuclid(b, a % b); Triplet ans; ans.gcd = smallAns.gcd; ans.x = smallAns.y; ans.y = smallAns.x - (a / b) * smallAns.y; return ans; } long long powerm(long long x, long long y, long long p) { long long r = 1; while (y) { if (y & 1) r = (r * x) % p; y = y >> 1; x = (x * x) % p; } return r % p; } long long modulo(long long a, long long m) { Triplet ans = extendedEuclid(a, m); return ans.x; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m, s, f; cin >> n >> m >> s >> f; long long t = 1; if (s < f) { while (s < f) { long long ti, l, r; cin >> ti >> l >> r; while (t != ti) { if (s != f) { cout << "R"; s++; t++; } else { break; } } if (s == f) break; if ((s + 1 < l || s + 1 > r) && (s < l || s > r)) { cout << "R"; t++; s++; } else { cout << "X"; t++; } } } else { while (s > f) { long long ti, l, r; cin >> ti >> l >> r; while (t != ti) { if (s != f) { cout << "L"; s--; t++; } else { break; } } if (s == f) break; if ((s - 1 < l || s - 1 > r) && (s < l || s > r)) { cout << "L"; t++; s--; } else { cout << "X"; t++; } } } 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 l[100005], r[100005], t[100005]; int main() { int n, m, s, f, step = 1; 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]); for (int i = 0; i < m || s != f; i++) { if (t[i] != step++) { if (s < f) { printf("R"); s += 1; } else { printf("L"); s -= 1; } i--; } else if (s < f && (s + 1 < l[i] || s + 1 > r[i]) && (s < l[i] || s > r[i])) { printf("R"); s += 1; } else if (s > f && (s - 1 < l[i] || s - 1 > r[i]) && (s < l[i] || s > r[i])) { printf("L"); s -= 1; } else printf("X"); if (s == f) break; } 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
#include <bits/stdc++.h> using namespace std; int s, f, n, m; int t[100300], x[100300], y[100300]; int main() { scanf("%d %d %d %d", &n, &m, &s, &f); for (int i = 0; i < m; i++) scanf("%d %d %d", &t[i], &x[i], &y[i]); string res = ""; int next = 0; int tiempo = 1; int cur = 0; char ch; while (s != f) { if (s < f) { ch = 'R'; next = s + 1; } else { ch = 'L'; next = s - 1; } if (cur < m && t[cur] == tiempo) { if (next >= x[cur] && next <= y[cur]) { ch = 'X'; next = s; } if (s >= x[cur] && s <= y[cur]) { ch = 'X'; next = s; } cur++; } res += ch; s = next; tiempo++; } cout << res << 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 nei(int now, int s, int f) { if (s < f) return now + 1; else return now - 1; } int main() { ios::sync_with_stdio(0); cin.tie(NULL); int n, m, s, f; cin >> n >> m >> s >> f; int now = s; int t_now = 1; for (int i = 0; i < m; i++) { int t, l, r; cin >> t >> l >> r; while (t > t_now) { if (now == f) { cout << '\n'; exit(0); } else { if (s < f) cout << 'R', now++; else cout << 'L', now--; } t_now++; } if (now == f) { cout << '\n'; exit(0); } if ((now < l || now > r) && (nei(now, s, f) < l || nei(now, s, f) > r)) { if (s < f) cout << 'R', now++; else cout << 'L', now--; } else cout << 'X'; t_now++; } while (now < f) cout << 'R', now++; while (now > f) cout << 'L', now--; cout << '\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 long double pi = 3.14159265359; const long long N = 1e9 + 7; using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n = 0, s, f, e = 1, a, b, c; cin >> a >> b >> s >> f; bool g, r = s < f; while (s != f) { c = (r ? s + 1 : s - 1); g = 1; if (e > n) { cin >> n >> a >> b; } if ((a <= c && c <= b) || (a <= s && s <= b)) g = 0; if (n != e || g) { if (r) { s++; cout << 'R'; } else { s--; cout << 'L'; } } else cout << 'X'; e++; } 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
def seen(i, l, r): return (i >= l and i <= r) n, m, src, dst = map(int, raw_input().split()) steps = [map(int, raw_input().split()) for i in range(m)] curr = 1 idx = 0 dx = 1 ans = [] sdx = "R" if src > dst: dx = -1 sdx = "L" while src != dst: if idx < len(steps) and curr == steps[idx][0]: if seen(src, steps[idx][1], steps[idx][2]) or seen(src + dx, steps[idx][1], steps[idx][2]): ans.append('X') else: ans.append(sdx) src += dx idx += 1 else: ans.append(sdx) src += dx curr += 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; void solve() { int n, m, s, f; cin >> n >> m >> s >> f; int step = 0; int flag = 0; while (m) { if (s == f) return; int t, l, r; if (flag == 0) { cin >> t >> l >> r; m--; } if (s != f) { step++; if (step == t) { flag = 0; if (s < f) { if (s >= l && s <= r) { cout << "X"; } else { if (s + 1 >= l && s + 1 <= r) cout << "X"; else { s++; cout << "R"; } } } else { if (s >= l && s <= r) { cout << "X"; } else { if (s - 1 >= l && s - 1 <= r) cout << "X"; else { s--; cout << "L"; } } } } else { flag = 1; if (s < f) { s++; cout << "R"; } else { s--; cout << "L"; } } } } if (s != f) { if (s < f) { for (int i = 1; i <= f - s; i++) cout << "R"; } else { for (int i = 1; i <= s - f; i++) cout << "L"; } } } int main() { solve(); }
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.math.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { //FileInputStream cao = new FileInputStream("injava.txt"); Reader.init(System.in); StringBuilder ans = new StringBuilder(); int n = Reader.nextInt(); int m = Reader.nextInt(); int s = Reader.nextInt(); int f = Reader.nextInt(); T[] t = new T[m]; for (int i=0; i<m; i++) { t[i] = new T(); t[i].t = Reader.nextInt(); t[i].l = Reader.nextInt(); t[i].r = Reader.nextInt(); } Arrays.sort(t, new cmpT()); int d = 1; if (s > f) d = -1; int now = 1, now1 = 0; while (s != f) { boolean flag = true; while (now1 < m && t[now1].t == now) { if ((t[now1].l <= s + d && t[now1].r >= s + d) || (t[now1].l <= s + 0 && t[now1].r >= s + 0)) flag = false; now1++; } now++; if (flag) { if (d == 1) ans.append("R"); else ans.append("L"); s += d; } else ans.append("X"); } System.out.println(ans); } } class T { int t, l, r; } class cmpT implements Comparator<T> { public int compare (T a, T b) { if (a.t < b.t) return -1; if (a.t == b.t) return 0; return 1; } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static boolean isend() throws IOException { if (tokenizer.hasMoreTokens()) return true; do { String s = reader.readLine(); if (s == null) return true; tokenizer = new StringTokenizer(s); } while (!tokenizer.hasMoreTokens()); return false; } }
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; const int maxn = 100001; int n, m, s, f, t[maxn], l[maxn], r[maxn]; int main() { scanf("%d%d%d%d", &n, &m, &s, &f); for (int i = 1; i <= m; i++) scanf("%d%d%d", &t[i], &l[i], &r[i]); if (s < f) { for (int i = 1, j = 1; i < maxn << 1; i++) { if (s == f) break; if (j <= m && i == t[j]) { if ((l[j] <= s && s <= r[j]) || (l[j] <= s + 1 && s + 1 <= r[j])) printf("X"); else { s++; printf("R"); } j++; } else { s++; printf("R"); } } printf("\n"); } else { for (int i = 1, j = 1; i < maxn << 1; i++) { if (s == f) break; if (j <= m && i == t[j]) { if ((l[j] <= s && s <= r[j]) || (l[j] <= s - 1 && s - 1 <= r[j])) printf("X"); else { s--; printf("L"); } j++; } else { s--; printf("L"); } } 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 implements Runnable { public void solve() throws IOException { int N = nextInt(); int M = nextInt(); int src = nextInt(); int tgt = nextInt(); int time = 0; for(int i = 0; i < M; i++){ int ntime = nextInt(); int l = nextInt(); int r = nextInt(); int timeInBetween = ntime - time - 1; if(timeInBetween > 0){ while(src < tgt && timeInBetween > 0){ src++; System.out.print('R'); timeInBetween--; } while(src > tgt && timeInBetween > 0){ src--; System.out.print('L'); timeInBetween--; } } if(src == tgt){ } else{ if(src < tgt){ if(src >= l && src <= r || src + 1 >= l && src + 1 <= r){ System.out.print("X"); } else{ src++; System.out.print("R"); } } else{ if(src >= l && src <= r || src - 1 >= l && src - 1 <= r) System.out.print('X'); else{ System.out.print('L'); src--; } } } time = ntime; } while(src < tgt ){ src++; System.out.print('R'); } while(src > tgt ){ src--; System.out.print('L'); } } //----------------------------------------------------------- public static void main(String[] args) { new Main().run(); } public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); tok = null; solve(); in.close(); } catch (IOException e) { System.exit(0); } } public String nextToken() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } BufferedReader in; StringTokenizer tok; }
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; inline void boost() { ios_base::sync_with_stdio(); cin.tie(0); cout.tie(0); } const long long MAXN = 1e5 + 123; const long long inf = 1e9 + 123; const long long MOD = 1e9 + 7; const double pi = acos(-1); map<int, pair<int, int> > mm; int main() { boost(); int n, m, s, f; cin >> n >> m >> s >> f; int it = s; for (int i = 1; i <= m; i++) { int t, l, r; cin >> t >> l >> r; mm[t] = make_pair(l, r); } int cntr = 0; while (it != f) { cntr++; pair<int, int> a = mm[cntr]; if ((a.first <= it && it <= a.second) || (it < f && a.first <= it + 1 && a.second >= it + 1) || (it - 1 <= a.second && it - 1 >= a.first && it > f)) { cout << "X"; } else { if (it < f) { it++; cout << "R"; } else { it--; cout << "L"; } } } exit(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; scanf("%d%d%d%d", &n, &m, &s, &f); int i; int x[m][3]; for (i = 0; i < m; i++) { scanf("%d%d%d", &x[i][0], &x[i][1], &x[i][2]); } int step = 1; int add; int current = s; if (s < f) { add = 1; } else { add = -1; } int observing = 0; while (1) { if (current == f) { break; } else { if (x[observing][0] == step) { if ((current + add <= x[observing][2] && current + add >= x[observing][1]) || (current <= x[observing][2] && current >= x[observing][1])) { printf("X"); } else if ((current + add > x[observing][2] || current + add < x[observing][1]) && (current > x[observing][2] || current < x[observing][1])) { if (add == -1) { printf("L"); current--; } else { printf("R"); current++; } } step++; observing++; } else { if (x[observing][0] < step) { if (add == -1) { printf("L"); current--; } else { printf("R"); current++; } step++; } else if (x[observing][0] > step) { if (add == -1) { printf("L"); current--; } else { printf("R"); current++; } step++; } } } } 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; int main(int argc, char* argv[]) { int n = 3, m = 5, s = 1, f = 3; cin >> n >> m >> s >> f; int l, r; char c = 'L'; int d = -1; if (s < f) { c = 'R'; d = 1; } int cur = s, step = 0, km = 0; int next = -1; if (m > 0) { cin >> next >> l >> r; km++; } while (cur - f) { step++; bool flag = false; if (step == next) { if (cur >= l && cur <= r) flag = true; if (cur + d >= l && cur + d <= r) flag = true; if (km < m) { cin >> next >> l >> r; km++; } else next = -1; } if (flag) cout << 'X'; else { cout << c; cur += d; } } 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
R = lambda : map(int , raw_input().split()) n,m,s,f = R() out = "" prev_t = 1 sign = 1 if s < f else -1 dir = "R" if sign == 1 else "L" for i in xrange(m): t, l, r = R() while t > prev_t and s != f: out += dir s += sign prev_t += 1 if s == f: break if l <= s <= r or l <= s + sign <= r: out += "X" else: out += dir s += sign prev_t += 1 while s != f: out += dir s += sign prev_t += 1 print out
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.HashMap; import java.util.Scanner; 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(); StringBuilder res = new StringBuilder(); HashMap<Integer, Integer[]> t = new HashMap<Integer, Integer[]>(); for(int i = 0; i < m; i++) t.put(sc.nextInt(), new Integer[]{sc.nextInt(), sc.nextInt()}); for(int curStep = 1; s != f; curStep++){ if(t.containsKey(curStep)){ int a = t.get(curStep)[0]; int b = t.get(curStep)[1]; if(s < f){ if(!isInRange(s, a, b) && !isInRange(s + 1, a, b)){ s++; res.append("R"); } else res.append("X"); } else if(s > f){ if(!isInRange(s, a, b) && !isInRange(s - 1, a, b)){ s--; res.append("L"); } else res.append("X"); } } else{ if(s < f){ s++; res.append("R"); } else if(s > f){ s--; res.append("L"); } } } System.out.println(res); } static boolean isInRange(int x, int a, int b){ return x >= a && x <= b; } }
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.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Set; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeSet; public class Main { private static BufferedReader in; private static BufferedWriter out; public static void main(String... args) throws IOException { // streams boolean file = false; if (file) in = new BufferedReader(new FileReader("input.txt")); else in = new BufferedReader(new InputStreamReader(System.in)); out = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer tok; // take input tok = new StringTokenizer(in.readLine()); int n = Integer.parseInt(tok.nextToken()); int m = Integer.parseInt(tok.nextToken()); int s = Integer.parseInt(tok.nextToken()); int t = Integer.parseInt(tok.nextToken()); HashMap<Integer, Integer> lefts = new HashMap<>(); HashMap<Integer, Integer> rights = new HashMap<>(); for (int i = 0; i < m; i++) { tok = new StringTokenizer(in.readLine()); int time = Integer.parseInt(tok.nextToken()); int l = Integer.parseInt(tok.nextToken()); int r = Integer.parseInt(tok.nextToken()); lefts.put(time, l); rights.put(time, r); } // get answer StringBuilder ans = new StringBuilder(); int currentTime = 0; while (s != t) { currentTime ++; // check source and next are not watched if (lefts.containsKey(currentTime)) { int left = lefts.get(currentTime); int right = rights.get(currentTime); if (s >= left && s <= right) { ans.append("X"); continue; } if ((t > s) && s+1 >= left && s+1 <= right) { ans.append("X"); continue; } if ((t < s) && s-1 >= left && s-1 <= right) { ans.append("X"); continue; } } // passs it if (t > s) { ans.append("R"); s++; } else { ans.append("L"); s--; } } System.out.println(ans.toString()); out.flush(); } }
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; const int N = 1010; int n, m, s, f; int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> s >> f; vector<pair<int, pair<int, int> > > v(m + 1); for (long long int i = 0; i < int(m); ++i) { cin >> v[i].first >> v[i].second.first >> v[i].second.second; } v[m].first = 1 << 29; int last = 0; int direction = s < f ? 1 : -1; int round = 0; char directionC = direction == 1 ? 'R' : 'L'; vector<char> ans; for (int i = 0; i <= m; ++i) { int cn = v[i].first - last - 1; last = v[i].first; int kk = min(abs(s - f), cn); for (int i = 0; i < kk; ++i) { ans.push_back(directionC); s += direction; } if (s == f) break; if ((s < v[i].second.first || s > v[i].second.second) && (s + direction < v[i].second.first || s + direction > v[i].second.second)) { ans.push_back(directionC); s += direction; if (s == f) break; } else { ans.push_back('X'); } } for (int i = 0; i < ans.size(); ++i) { cout << (char)ans[i]; } 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 l[100009], r[100009], t[100009]; int main() { int n, m, s, f; int i, j; while (scanf("%d%d%d%d", &n, &m, &s, &f) != -1) { t[0] = 0; for (i = 1; i <= m; i++) { scanf("%d%d%d", &t[i], &l[i], &r[i]); } int pos = s; if (s < f) { int time = 1, d = 1; for (i = s; i < f; time++) { if (time == t[d] && (i <= r[d] && i + 1 >= l[d])) { d++; printf("X"); } else printf("R"), i++; if (time == t[d]) d++; } printf("\n"); } else { int time = 1, d = 1; for (i = s; i > f; time++) { if (time == t[d] && (i - 1 <= r[d] && i >= l[d])) { printf("X"); } else printf("L"), i--; if (time == t[d]) d++; } 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
n,m,s,f=map(int,input().split()) t=dict(); for i in range(m): t1,l1,r1=map(int,input().split()) t[t1]=(l1,r1); pos=s;i=1; while(1): if(pos==f): break if i in t: if t[i][0] <= pos<=t[i][1]: print('X',end='') i+=1 continue elif(f-pos>0 and t[i][0]<= pos+1<=t[i][1]): print('X',end='') i+=1 continue elif(f-pos<0 and t[i][0] <= pos-1<=t[i][1]): print('X',end='') i+=1 continue if(f-pos>0): print('R',end='') pos+=1 elif(pos-f>0): print('L',end='') pos-=1 i+=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
/* Aman Agarwal algo.java */ import java.util.*; import java.io.*; public class B342 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 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()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader sc = new FastReader(); static PrintWriter out = new PrintWriter(System.out); static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static int[][] nai2(int n,int m)throws IOException{int a[][] = new int[n][m];for(int i=0;i<n;i++)for(int j=0;j<m;j++)a[i][j] = ni();return a;} static int[] nai(int N,int start)throws IOException{int[]A=new int[N+start];for(int i=start;i!=(N+start);i++){A[i]=ni();}return A;} static Integer[] naI(int N,int start)throws IOException{Integer[]A=new Integer[N+start];for(int i=start;i!=(N+start);i++){A[i]=ni();}return A;} static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;} static void print(int arr[]){for(int i=0;i<arr.length;i++)out.print(arr[i]+" ");out.println();} static void print(long arr[]){for(int i=0;i<arr.length;i++)out.print(arr[i]+" ");out.println();} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} // return the number of set bits. static boolean isPrime(int number){if(number==1) return false;if (number == 2 || number == 3){return true;}if (number % 2 == 0) {return false;}int sqrt = (int) Math.sqrt(number) + 1;for(int i = 3; i < sqrt; i += 2){if (number % i == 0){return false;}}return true;} static boolean isPrime(long number){if(number==1) return false;if (number == 2 || number == 3){return true;}if (number % 2 == 0) {return false;}long sqrt = (long) Math.sqrt(number) + 1;for(int i = 3; i < sqrt; i += 2){if (number % i == 0){return false;}}return true;} static int power(int n,int p){if(p==0)return 1;int a = power(n,p/2);a = a*a;int b = p & 1;if(b!=0){a = n*a;}return a;} static long power(long n,long p){if(p==0)return 1;long a = power(n,p/2);a = a*a;long b = p & 1;if(b!=0){a = n*a;}return a;} static void reverse(int[] a) {int b;for (int i = 0, j = a.length - 1; i < j; i++, j--) {b = a[i];a[i] = a[j];a[j] = b;}} static void reverse(long[] a) {long b;for (int i = 0, j = a.length - 1; i < j; i++, j--) {b = a[i];a[i] = a[j];a[j] = b;}} static void swap(int a[],int i,int j){a[i] = a[i] ^ a[j];a[j] = a[j] ^ a[i];a[i] = a[i] ^ a[j];} static void swap(long a[],int i,int j){a[i] = a[i] ^ a[j];a[j] = a[j] ^ a[i];a[i] = a[i] ^ a[j];} static int count(int n){int c=0;while(n>0){c++;n = n/10;}return c;} static int[] prefix_sum(int a[],int n){int s[] = new int[n];s[0] = a[0];for(int i=1;i<n;i++){s[i] = a[i]+s[i-1];}return s;} static long[] prefix_sum_int(int a[],int n){long s[] = new long[n];s[0] = (long)a[0];for(int i=1;i<n;i++){s[i] = ((long)a[i])+s[i-1];}return s;} static long[] prefix_sum_Integer(Integer a[],int n){long s[] = new long[n];s[0] = (long)a[0];for(int i=1;i<n;i++){s[i] = ((long)a[i])+s[i-1];}return s;} static long[] prefix_sum_long(long a[],int n){long s[] = new long[n];s[0] = a[0];for(int i=1;i<n;i++){s[i] = a[i]+s[i-1];}return s;} static boolean isPerfectSquare(double x){double sr = Math.sqrt(x);return ((sr - Math.floor(sr)) == 0);} static ArrayList<Integer> sieve(int n) {int k=0; boolean prime[] = new boolean[n+1];ArrayList<Integer> p_arr = new ArrayList<>();for(int i=0;i<n;i++) prime[i] = true;for(int p = 2; p*p <=n; p++){ k=p;if(prime[p] == true) { p_arr.add(p);for(int i = p*2; i <= n; i += p) prime[i] = false; } }for(int i = k+1;i<=n;i++){if(prime[i]==true)p_arr.add(i);}return p_arr;} static boolean[] seive_check(int n) {boolean prime[] = new boolean[n+1];for(int i=0;i<n;i++) prime[i] = true;for(int p = 2; p*p <=n; p++){ if(prime[p] == true) { for(int i = p*2; i <= n; i += p) prime[i] = false; } }prime[1]=false;return prime;} static int get_bits(int n){int p=0;while(n>0){p++;n = n>>1;}return p;} static int get_bits(long n){int p=0;while(n>0){p++;n = n>>1;}return p;} static int get_2_power(int n){if((n & (n-1))==0)return get_bits(n)-1;return -1;} static int get_2_power(long n){if((n & (n-1))==0)return get_bits(n)-1;return -1;} static void close(){out.flush();} /*-------------------------Main Code Starts(algo.java)----------------------------------*/ public static void main(String[] args) throws IOException { int test = 1; //t = sc.nextInt(); while(test-- > 0) { int n = ni(); int m = ni(); int s = ni(); int f = ni(); StringBuilder sb = new StringBuilder(""); int q=0; for(int i=1;i<=m;i++) { int t = ni(); int l = ni(); int r = ni(); if((t-q)>0) { int d = t-q-1; while(s!=f && d>0) { if(s<f) { s++; sb.append("R"); } else if(s>f) { s--; sb.append("L"); } d--; } } if(s<f) { if(s<l && s+1<l || s>r && s+1>r) { s++; sb.append("R"); } else sb.append("X"); } else if(s>f) { if(s<l && s-1<l || s>r && s-1>r) { s--; sb.append("L"); } else sb.append("X"); } q=t; } while(s!=f) { if(s<f) { s++; sb.append("R"); } else { s--; sb.append("L"); } } out.println(sb); } 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.util.Scanner; public class XeniaAndSpies { @SuppressWarnings("unused") public static void main(String[] args) { Scanner sc = new Scanner(System.in); int spies = sc.nextInt(); int steps = sc.nextInt(); int start = sc.nextInt(); int finish = sc.nextInt(); int mat[][] = new int[3][100001]; int currentStep = 0, i; for(i = 0; i < steps; i++) { mat[0][i] = sc.nextInt(); mat[1][i] = sc.nextInt(); mat[2][i] = sc.nextInt(); } i = 0; while(start != finish) { currentStep++; if(start < finish) { if(mat[0][i] == currentStep) { if(mat[1][i] > start+1 || mat[2][i] < start) { start++; System.out.print("R"); } else { System.out.print("X"); } i++; } else { start++; System.out.print("R"); } } else { // finish < start if(mat[0][i] == currentStep) { if(mat[1][i] > start || mat[2][i] < start-1) { start--; System.out.print("L"); } else { System.out.print("X"); } i++; } else { start--; System.out.print("L"); } } } } }
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; const int INF = 1901486712; const int MOD = 1000000007; const double PI = acos(-1); const double EPS = 1E-9; bool between(int x, int l, int r) { return (l <= x && x <= r); } string tostring(int x) { char dum[20]; sprintf(dum, "%d", x); string ret(dum); return ret; } int n, m, awal, akhir, t, l, r; void anjing() { int cur = 1; for (int i = 0; i < m; i++) { scanf("%d %d %d", &t, &l, &r); if (awal == akhir) continue; while (cur < t && awal != akhir) { awal++; cur++; printf("R"); } if (cur == t && awal != akhir) { if (between(awal, l, r) || between(awal + 1, l, r)) printf("X"); else printf("R"), awal++; cur++; } } while (awal != akhir) printf("R"), awal++; puts(""); } void babi() { int cur = 1; for (int i = 0; i < m; i++) { scanf("%d %d %d", &t, &l, &r); if (awal == akhir) continue; while (cur < t && awal != akhir) { awal--; cur++; printf("L"); } if (cur == t && awal != akhir) { if (between(awal, l, r) || between(awal - 1, l, r)) printf("X"); else printf("L"), awal--; cur++; } } while (awal != akhir) printf("L"), awal--; puts(""); } int main() { scanf("%d %d %d %d", &n, &m, &awal, &akhir); if (awal < akhir) anjing(); else babi(); 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
//B. Xenia and Spies //time limit per test2 seconds //memory limit per test256 megabytes //inputstandard input //outputstandard output //Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. //Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. //But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. //You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). //Input //The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. //Output //Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". //As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. //If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. //Sample test(s) //input //3 5 1 3 //1 1 2 //2 2 3 //3 3 3 //4 1 1 //10 1 3 //output //XXRR import java.util.*; import java.io.*; import java.awt.Point; import static java.lang.Math.*; public class Solution { public static int numspies; public static int numsteps; public static int start; public static int end; public static int currentspy; //spy who has the note public static ArrayList<Character> steps; public static void main(String[] args) throws Exception { Solution temp = new Solution(); } public Solution() throws Exception { steps = new ArrayList<Character>(); readFile(); for (int i = 0; i < steps.size(); i++) { System.out.print(steps.get(i)); } System.out.println(); } private static void readFile() throws Exception { //Scanner is used for parsing tokens from the contents of the stream Scanner in = new Scanner(System.in); //BufferedReader just reads the stream and does not do any special parsing. //BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String line = in.nextLine(); String[] ints = line.split(" "); numspies = Integer.parseInt(ints[0]); numsteps = Integer.parseInt(ints[1]); start = Integer.parseInt(ints[2]); end = Integer.parseInt(ints[3]); currentspy = start; int counter = 0; //it seems like we never want to pass backwards/away from the direction of spy destination f. We always //want to pass towards it. So in a way it's greedy - we always pass to the very next spy for each new turn //as long as Xenia isn't watching int priorstep = 0; //iterate through each turn/step while (counter < numsteps) { counter++; line = in.nextLine(); String[] data = line.split(" "); int currentstep = Integer.parseInt(data[0]); int rangestart; int rangeend; //if Xenia didn't take a step off e.g. went from step 1 to step 2 if (currentstep - priorstep == 1) { rangestart = Integer.parseInt(data[1]); rangeend = Integer.parseInt(data[2]); } //Xenia took a step off/didn't examine any spies whatsoever for a step/steps: examination range is //nonexistent e.g. went from step 1 to step 3 else { rangestart = 0; rangeend = 0; } //we now have the start and the end of the range of spies being watched, so we iterate through the //number of turns from the line before to the line just read int iteration = 0; while (iteration < currentstep - priorstep) { //if we've iterate through the steps that Xenia took off and have finally reached the step she //started examining again, apply the range if (iteration == currentstep - priorstep - 1) { rangestart = Integer.parseInt(data[1]); rangeend = Integer.parseInt(data[2]); } //if the note is on the end of the list of spies if (currentspy == 1 || currentspy == numspies) { //we want to pass note to the right, only one way to go since note is on left end if (currentspy < end) { //if the current spy and its right neighbor are not being examined, pass to the right if ((currentspy < rangestart || currentspy > rangeend) && (currentspy + 1 < rangestart || currentspy + 1 > rangeend)) { currentspy++; steps.add('R'); } else { steps.add('X'); } } //we want to pass the note to the left else if (currentspy > end) { //if the current spy and its left neighbor are not being examined, pass to the left if ((currentspy < rangestart || currentspy > rangeend) && (currentspy - 1 < rangestart || currentspy - 1 > rangeend)) { currentspy--; steps.add('L'); } else { steps.add('X'); } } //note reached destination else { return; } } //spy who has note has two neighbors else { //we want to pass note to the right if (currentspy < end) { //if the current spy and its right neighbor are not being examined, pass to the right if ((currentspy < rangestart || currentspy > rangeend) && (currentspy + 1 < rangestart || currentspy + 1 > rangeend)) { currentspy++; steps.add('R'); } else { steps.add('X'); } } //we want to pass the note to the left else if (currentspy > end) { //if the current spy and its left neighbor are not being examined, pass to the left if ((currentspy < rangestart || currentspy > rangeend) && (currentspy - 1 < rangestart || currentspy - 1 > rangeend)) { currentspy--; steps.add('L'); } else { steps.add('X'); } } //note reached destination else { return; } } iteration++; } //we finished processing a line of input, so the prior step is bumped up to current step before current //step moves to the next one priorstep = currentstep; } //we haven't yet returned from this function, so the destination spy has not yet been reached but Xenia has //finished all examination steps while (currentspy < end) { currentspy++; steps.add('R'); } while (currentspy > end) { currentspy--; steps.add('L'); } } }
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, l, r, mi; int curr, prev = 1; scanf("%d %d %d %d", &n, &m, &s, &f), curr = s; for (int i = 1; i <= m && curr != f; i++) { scanf("%d %d %d", &mi, &l, &r); for (int j = 0; j < mi - prev && curr != f; j++) if (s < f) curr++, putchar('R'); else curr--, putchar('L'); if (curr == f) break; prev = mi + 1; if (s < f) if ((l <= curr && r >= curr) || (l <= curr + 1 && r >= curr + 1)) putchar('X'); else curr++, putchar('R'); else { if ((l <= curr && r >= curr) || (l <= curr - 1 && r >= curr - 1)) putchar('X'); else curr--, putchar('L'); } } for (int j = 0; curr != f; j++) if (s < f) curr++, putchar('R'); else curr--, putchar('L'); while (scanf("%d", &mi) != EOF) { } 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 Node { int times, l, r; } node[100005]; int main() { int n, m, s, f; int flag = 0; scanf("%d %d %d %d", &n, &m, &s, &f); int now = 0; for (int i = 0; i < m; i++) { scanf("%d %d %d", &node[i].times, &node[i].l, &node[i].r); } if (s <= f) flag = 1; int t = s; int c = 0; if (flag == 1) while (t != f) { now++; if (c >= m) { t++; printf("R"); } else if (now == node[c].times) { if (t >= node[c].l - 1 && t <= node[c].r) printf("X"); else { printf("R"); t++; } c++; } else if (now < node[c].times) { printf("R"); t++; } } else { while (t != f) { now++; if (c >= m) { t--; printf("L"); } else if (now == node[c].times) { if (t >= node[c].l && t <= node[c].r + 1) printf("X"); else { printf("L"); t--; } c++; } else if (now < node[c].times) { printf("L"); t--; } } } 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; long long mode = pow(10, 9) + 7; bool cmp(pair<long double, int> x, pair<long double, int> y) { return x.first < y.first; } const long long maxn = 2 * 1e5 + 5; int main() { ios::sync_with_stdio(0); cin.tie(0); long long n, m, s, f; cin >> n >> m >> s >> f; map<long long, pair<long long, long long>> mp; for (int i = 0; i < m; i++) { long long a, b, c; cin >> a >> b >> c; mp[a] = make_pair(b, c); } if (s < f) { long long step = 1; string ans = ""; while (s != f) { if (mp.find(step) == mp.end()) { ans += "R"; s++; step++; } else { auto p = mp[step]; if (s >= p.first && s <= p.second) ans += "X"; else if (s + 1 >= p.first && s + 1 <= p.second) ans += "X"; else ans += "R", s++; step++; } } cout << ans << "\n"; } else { long long step = 1; string ans = ""; while (s != f) { if (mp.find(step) == mp.end()) { ans += "L"; s--; step++; } else { auto p = mp[step]; if (s >= p.first && s <= p.second) ans += "X"; else if (s - 1 >= p.first && s - 1 <= p.second) ans += "X"; else ans += "L", s--; step++; } } 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
import java.util.*; import java.io.*; public class BX { /* */ public static BR in; public static LR lin; public static PrintWriter out; public static PrintWriter fout; static { try { in = new BR(); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out ) ) ); fout = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output.txt") ) ) ); } catch (Exception e){ } } public static boolean bg = true; public static void main(String[] args) throws Exception { s1(); out.close(); } static int[] left, right, time; private static void s1() throws Exception { in.ni(); int m = in.ni(); int s = in.ni() - 1; int f = in.ni() - 1; time = new int[m]; left = new int[m]; right = new int[m]; for (int i = 0 ;i < m; i++){ int k1 = in.ni(); int k2 = in.ni() - 1; int k3 = in.ni() - 1; time[i] = k1; left[i] = k2; right[i] = k3; } int ptr = s; int dir = f - s; if (dir >= 0){ dir = 1; } else { dir = -1; } int[] seg = {s,f}; Arrays.sort(seg); StringBuilder fin = new StringBuilder(); int time = 0; for (;;){ time++; if (ptr == f){ break; } if (!in(ptr,time) && !in(ptr+dir,time) ){ if (dir == 1){ fin.append("R"); } else { fin.append("L"); } ptr += dir; } else { fin.append("X"); } } pn(fin); } public static boolean in(int k1, int t1){ int id = Arrays.binarySearch(time, t1); if (id < 0) return false; if (k1 <= right[id] && k1 >= left[id]){ return true; } return false; } private static void pn(Object... o1) { for (int i = 0; i < o1.length; i++) { if (i != 0) out.print(" "); out.print(o1[i]); } out.println(); } private static class LR { } private static class BR { BufferedReader k1 = null; StringTokenizer k2 = null; public BR() { k1 = new BufferedReader(new InputStreamReader(System.in)); } public String nx() throws Exception { for (;;) { if (k2 == null || !k2.hasMoreTokens()) { String temp = k1.readLine(); if (temp == null) return null; k2 = new StringTokenizer(temp); } else break; } return k2.nextToken(); } public int ni() throws Exception { return Integer.parseInt(nx()); } } }
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.BufferedReader; import java.io.OutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; 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(); } } 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 time[][] = new int[m][]; for (int i = 0; i < m;i++) { time[i] = in.parseInt1D(3); } int ti = 0; int tt = 1; int a = s < f ? 1 : -1 ; String r = s < f ? "R" : "L"; while (s != f) { if(ti >= time.length) { s += a; out.print(r); } else { if(tt == time[ti][0] && ((s >= time[ti][1] && s <= time[ti][2]) || ((s + a) >= time[ti][1] && (s + a) <= time[ti][2])) ) { out.print("X"); } else { out.print(r); s += a; } if(tt == time[ti][0]) { ti++; } } tt++; } out.println(); } } class InputReader { private BufferedReader br; private StringTokenizer st; public InputReader(InputStream in) { br=new BufferedReader(new InputStreamReader(in)); try { st=new StringTokenizer(br.readLine()); } catch (IOException ignored) { } } public void readLine() { try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { return; } } public int nextInt(){ return Integer.parseInt(st.nextToken()); } /** * Parse 1D array from current StringTokenizer */ public int[] parseInt1D(int n){ readLine(); int r[]=new int[n]; for(int i=0;i<n;i++){ r[i]=nextInt(); } return 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
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import static java.lang.Math.*; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Trung Pham */ public class B { public static void main(String[] args) { Scanner in = new Scanner(); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int m = in.nextInt(); int s = in.nextInt(); int f = in.nextInt(); Node[] steps = new Node[m]; for (int i = 0; i < m; i++) { int t = in.nextInt(); int l = in.nextInt(); int r = in.nextInt(); steps[i] = new Node(t, l, r); } StringBuilder result = new StringBuilder(); int step = 1; int start = s; int unit = s < f ? 1 : -1; int node = 0; while (start != f) { if (node < steps.length && steps[node].t == step) { int next = start + unit; if (steps[node].l <= start && steps[node].r >= start) { result.append("X"); } else if (steps[node].l <= next && steps[node].r >= next) { result.append("X"); } else { start += unit; result.append(unit > 0 ? "R" : "L"); } node++; } else { start += unit; result.append(unit > 0 ? "R" : "L"); } step++; } out.println(result.toString()); out.close(); } static class Node { int t, l, r; public Node(int t, int l, int r) { this.t = t; this.l = l; this.r = r; } } static double area(Point a, Point b, Point c) { double i = distance(a, b); double j = distance(b, c); double k = distance(a, c); double s = (i + j + k) / 2; double result = s * (s - j) * (s - i) * (s - k); return Math.sqrt(result); } static class Point { double x, y; Point(double x, double y) { this.x = x; this.y = y; } Point(int x, int y) { this.x = x; this.y = y; } @Override public String toString() { return "Point: " + x + " " + y; } } static double distance(Point a, Point b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } static Point intersect(Point a, Point b, Point c) { double D = cross(a, b); if (D != 0) { return new Point(cross(c, b) / D, cross(a, c) / D); } return null; } static Point convert(Point a, double angle) { double x = a.x * cos(angle) - a.y * sin(angle); double y = a.x * sin(angle) + a.y * cos(angle); return new Point(x, y); } static Point minus(Point a, Point b) { return new Point(a.x - b.x, a.y - b.y); } static Point add(Point a, Point b) { return new Point(a.x + b.x, a.y + b.y); } static double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { //System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
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() { long long n, m, s, f, t, l, r, i, j = 1; string st = ""; cin >> n >> m >> s >> f; for (i = 1; i <= m; i++) { cin >> t >> l >> r; if (s == f) { } else if (s < f) { while (j <= t) { if (s == f) { break; } if (j == t && ((s >= l && s <= r) || (s + 1 >= l && s + 1 <= r))) { st += 'X'; } else { st += 'R'; s++; } j++; } } else { while (j <= t) { if (s == f) { break; } if (j == t && ((s >= l && s <= r) || (s - 1 >= l && s - 1 <= r))) { st += 'X'; } else { st += 'L'; s--; } j++; } } } if (s < f) { for (i = 1; i <= (f - s); i++) { st += 'R'; } } if (s > f) { for (i = 1; i <= (s - f); i++) { st += 'L'; } } cout << st; 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 { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pr=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); //System.out.println("enter"); int n,m,f,s,t,l,r,count=0,i,temp=1; StringTokenizer input=new StringTokenizer(br.readLine()); n=Integer.parseInt(input.nextToken()); m=Integer.parseInt(input.nextToken()); f=Integer.parseInt(input.nextToken()); s=Integer.parseInt(input.nextToken()); count=f; for(i=0;i<m;i++) { StringTokenizer inp=new StringTokenizer(br.readLine()); t=Integer.parseInt(inp.nextToken()); l=Integer.parseInt(inp.nextToken()); r=Integer.parseInt(inp.nextToken()); if(f<=s) { while(((t-temp)>0)&&count<s) { pr.print("R"); temp+=1; count+=1; } if(l<=count&&count<=r&&count<s) { pr.print("X"); temp+=1; } else if(((count+1)<l||count>r)&&count<s) { pr.print("R"); count+=1; temp+=1; } else if(((count+1)==l)&&count<s) { pr.print("X"); temp+=1; } } else if(f>s) { while(((t-temp)>0)&&count>s) { pr.print("L"); temp+=1; count-=1; } if(l<=count&&count<=r&&count>s) { pr.print("X"); temp+=1; } else if(((count-1)>r||count<l)&&(count>s)) { pr.print("L"); count-=1; temp+=1; } else if(((count-1)==r)&&count>s) { pr.print("X"); temp+=1; } } } if(count<s) { for(i=count;i<s;i++) pr.print("R"); } else if(count>s) { for(i=count;i>s;i--) pr.print("L"); } pr.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> using namespace std; const int M = 100005; int n, m, s, f; struct interval { int l, r, ti; void read() { scanf("%d %d %d", &ti, &l, &r); } bool judge(int x) { if (x >= l && x <= r) return false; else return true; } } q[M]; void solve() { int dir = s < f ? 1 : -1; char ch = s < f ? 'R' : 'L'; int ti = 0; int X = 0; while (s != f) { ++ti; if (ti == q[X].ti) { if (q[X].judge(s) && q[X].judge(s + dir)) putchar(ch), s += dir; else putchar('X'); X++; } else { putchar(ch); s += dir; } } } int main() { scanf("%d %d %d %d", &n, &m, &s, &f); for (int i = 0; i < m; ++i) { q[i].read(); } 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> #pragma comment(linker, "/STACK:16777216") using namespace std; int main() { int n, m, s, f; cin >> n >> m >> s >> f; long t[100001]; int l[100001], r[100001]; for (int i = 0; i < m; i++) { cin >> t[i] >> l[i] >> r[i]; } t[m] = 1000000001; int pos = 1; int tps = s; int tm = 0; while (tps != f) { for (int i = pos; i < t[tm]; i++) { if (tps == f) return 0; if (f > s) { cout << "R"; tps++; } else { cout << "L"; tps--; } } if (tps == f) return 0; int np = (f - s) / abs(f - s); if (((tps >= l[tm]) && (tps <= r[tm])) || ((tps + np >= l[tm]) && (tps + np <= r[tm]))) cout << "X"; else { if (f > s) { cout << "R"; tps++; } else { cout << "L"; tps--; } } pos = t[tm] + 1; tm++; } 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
/** * Created with IntelliJ IDEA. * User: den * Date: 9/7/13 * Time: 11:35 AM * To change this template use File | Settings | File Templates. */ import java.io.*; import java.util.StringTokenizer; public class TaskB extends Thread { private void solve() throws IOException { int n = _int(); int m = _int(); int s = _int(); int f = _int(); int[] a = new int[n]; int[][] steps = new int[m][3]; int delta = s < f ? 1 : -1; for (int i = 0; i < m; i++){ steps[i][0] = _int(); steps[i][1] = _int(); steps[i][2] = _int(); } int now = 1; int watch = 0; while (now <= steps[m-1][0] + n){ if (watch < m && now == steps[watch][0]){ if ((steps[watch][1] <= s && steps[watch][2] >= s )||(steps[watch][1] <= s + delta && steps[watch][2] >= s + delta)){ out.print("X"); }else{ s += delta; out.print(delta>0 ? "R" : "L"); if (s == f) return; } watch++; }else{ s += delta; out.print(delta>0 ? "R" : "L"); if (s == f) return; } now++; } } public void run() { try { solve(); } catch (Exception e) { System.out.println("System exiting...."); e.printStackTrace(); System.exit(888); } finally { out.flush(); out.close(); } } public static void main(String[] args) throws FileNotFoundException { new TaskB().run(); } public TaskB() throws FileNotFoundException { //in = new BufferedReader(new FileReader("A-large.in")); //out = new PrintWriter(new File("A-large.out")); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); setPriority(Thread.MAX_PRIORITY); } private BufferedReader in; private PrintWriter out; private StringTokenizer st; private int _int() throws IOException { return Integer.parseInt(nextToken()); } private double _double() throws IOException { return Double.parseDouble(nextToken()); } private long _long() throws IOException { return Long.parseLong(nextToken()); } private char[] _chars() throws IOException { return nextToken().toCharArray(); } private String nextToken() throws IOException { if (st == null || !st.hasMoreElements()) st = new StringTokenizer(in.readLine(), " \t\r\n"); return st.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
import java.util.*; public class XeniaAndSpies { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int s = in.nextInt() - 1; int f = in.nextInt() - 1; int dir = s < f ? 1 : -1; StringBuilder sb = new StringBuilder(); int prevT = 0; int remaining = Math.abs(s - f); for (int i = 0; i < m; ++i) { int t = in.nextInt(); int l = in.nextInt() - 1; int r = in.nextInt() - 1; if (remaining > 0) { int timePassed = t - prevT - 1; if (timePassed > 0) { // check if time passed is not enough to reach destination int distWalked = Math.min(timePassed, Math.abs(s - f)); sb.append(String.format(String.format("%%%ds", distWalked), " ").replace(" ", dir == 1 ? "R" : "L")); s += dir * distWalked; remaining -= distWalked; if (s == f) continue; } // now process current watch if ((s >= l && s <= r) || (s + dir >= l && s + dir <= r)) { sb.append("X"); } else { sb.append(dir == 1 ? "R" : "L"); s += dir; --remaining; } } prevT = t; } if (remaining > 0) { sb.append(String.format(String.format("%%%ds", remaining), " ").replace(" ", dir == 1 ? "R" : "L")); } System.out.println(sb); in.close(); System.exit(0); } }
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 XeniaAndSpies { public static InputReader in; public static PrintWriter out; public static final int MOD = (int) (1e9 + 7); public static void main(String[] args) { in = new InputReader(System.in); out = new PrintWriter(System.out); int n = in.nextInt(), m = in.nextInt(), s = in.nextInt(), f = in.nextInt(); int MAX = 100005; HashMap<Integer, Integer> start = new HashMap<Integer, Integer>(); HashMap<Integer, Integer> end = new HashMap<Integer, Integer>(); for (int i = 0; i < m; i++) { int ti = in.nextInt(); start.put(ti, in.nextInt()); end.put(ti, in.nextInt()); } int curr = s; if(s < f) { for (int i = 1; i < 2*MAX && curr != f; i++) { if(!start.containsKey(i) || (isFree(curr, start.get(i), end.get(i)) && isFree(curr + 1, start.get(i), end.get(i)))) { curr++; out.print("R"); } else { out.print("X"); } } } else { for (int i = 1; i < 2*MAX && curr != f; i++) { if(!start.containsKey(i) || (isFree(curr, start.get(i), end.get(i)) && isFree(curr - 1, start.get(i), end.get(i)))) { curr--; out.print("L"); } else { out.print("X"); } } } out.close(); } public static boolean isFree(int curr, int left, int right) { return left == 0 || right == 0 || curr < left || curr > right; } static class Node implements Comparable<Node> { int next; int dist; public Node(int u, int v) { this.next = u; this.dist = v; } public void print() { out.println(next + " " + dist + " "); } public int compareTo(Node that) { return Integer.compare(this.next, that.next); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } 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
#include <bits/stdc++.h> using namespace std; const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; int XX[] = {-1, -1, -1, 0, 0, 1, 1, 1}; int YY[] = {-1, 0, 1, -1, 1, -1, 0, 1}; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); long long int n, i, j, t, m, k, l, x, s, f, last = 0, r; cin >> n >> m >> s >> f; string str; while (m--) { cin >> t >> l >> r; long long int count = t - last - 1; while (count != 0) { if (s < f) { char ch = 'R'; str.push_back(ch); s++; } else if (s > f) { char ch = 'L'; str.push_back(ch); s--; } else break; count--; } if (s == f) { cout << str; return 0; } last = t; if (s < f) { if (s >= l && s <= r || (s + 1) >= l && (s + 1) <= r) { char ch = 'X'; str.push_back(ch); continue; } else { char ch = 'R'; s++; str.push_back(ch); continue; } } if (s > f) { if (s >= l && s <= r || (s - 1) >= l && (s - 1) <= r) { char ch = 'X'; str.push_back(ch); continue; } else { char ch = 'L'; s--; str.push_back(ch); continue; } } } while (s < f) { char ch = 'R'; s++; str.push_back(ch); } while (s > f) { char ch = 'L'; s--; str.push_back(ch); } 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
#include <bits/stdc++.h> using namespace std; int main() { int msg, dest, m, n, i, li, ri, ti, step = 1; cin >> n >> m >> msg >> dest; for (i = 1; i <= m; i++) { cin >> ti >> li >> ri; while (step != ti) { if (msg == dest) goto end; if (msg < dest) cout << 'R', msg++; else cout << 'L', msg--; step++; } if (msg == dest) goto end; if (msg < dest) if (!(msg >= li && msg <= ri) && !(msg + 1 >= li && msg + 1 <= ri)) cout << 'R', msg++; else cout << 'X'; else if (!(msg >= li && msg <= ri) && !(msg - 1 >= li && msg - 1 <= ri)) cout << 'L', msg--; else cout << 'X'; step++; } while (msg != dest) { if (msg < dest) cout << 'R', msg++; else cout << 'L', msg--; } end:; }
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); cout.tie(NULL); long long n, m, st, f, i, j; long long a, b, c; set<long long> s; map<long long, pair<long long, long long> > mp; cin >> n >> m >> st >> f; for (i = 0; i < m; i++) { cin >> a; cin >> b; cin >> c; mp[a].first = b; mp[a].second = c; s.insert(a); } long long cur = st; i = 1; if (st < f) { while (cur < f) { if (s.find(i) == s.end()) { cout << "R"; cur = cur + 1; } else { if ((cur >= mp[i].first && cur <= mp[i].second) || (cur + 1 >= mp[i].first && cur + 1 <= mp[i].second)) { cout << "X"; } else { cout << "R"; cur = cur + 1; } } i++; } } else { while (cur > f) { if (s.find(i) == s.end()) { cout << "L"; cur = cur - 1; } else { if ((cur >= mp[i].first && cur <= mp[i].second) || (cur - 1 >= mp[i].first && cur - 1 <= mp[i].second)) { cout << "X"; } else { cout << "L"; cur = cur - 1; } } i++; } } }
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.*; /** * * @author Do Quoc bao */ public class ProblemB { public static void main(String[] args) throws java.lang.Exception { in.init(System.in); StringBuilder kq=new StringBuilder(); int n=in.nextInt(),m=in.nextInt(),s=in.nextInt(),f=in.nextInt(),i,time=0; char d=' ';if (s<f) d='R';else d='L'; TreeMap<Integer,pair> map=new TreeMap<>(); for (i=0;i<m;++i) { int t=in.nextInt(),x=in.nextInt(),y=in.nextInt(); map.put(t, new pair(x,y)); } while (s!=f) { ++time;int pass;if (d=='L') pass=s-1;else pass=s+1; if (map.containsKey(time)) { pair p=map.get(time); if (p.inSight(pass)|p.inSight(s)) {kq.append("X");continue;} } kq.append(d);s=pass; } System.out.println(kq.toString()); } } class pair implements Comparable { int x,y; public pair(int x1,int y1) { x=x1;y=y1; } boolean inSight(int k) { if (k>=this.x&k<=this.y) return true;return false; } @Override public int compareTo(Object t) { pair o=(pair)t; if (this.x==o.x) return Integer.compare(this.y, o.y); else return Integer.compare(this.x, o.x); } } /**/ class in { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( 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
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s, f, _f, i, j, t, L, R, pre; string ans = ""; cin >> n >> m >> s >> f; for (pre = i = 0; i < m; i++) { cin >> t >> L >> R; if (s == f) continue; for (j = 1; j < t - pre; j++) { if (f > s) { ans += 'R'; ++s; } else if (f < s) { ans += 'L'; --s; } else break; } if (s == f) continue; if (s >= L and s <= R) ans += 'X'; else if (f > s) { _f = s + 1; if (_f >= L and _f <= R) ans += 'X'; else { ans += 'R'; ++s; } } else if (f < s) { _f = s - 1; if (_f >= L and _f <= R) ans += 'X'; else { ans += 'L'; --s; } } pre = t; } while (s != f) { if (s < f) { ans += 'R'; ++s; } else { ans += 'L'; --s; } } 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; void solve() { int n, m, s, f; cin >> n >> m >> s >> f; map<int, pair<int, int> > mp; for (int i = 0; i < m; i++) { int t, l, r; cin >> t >> l >> r; mp[t] = {l, r}; }; string ans; if (s < f) { for (int step = 1;; step++) { if (s == f) { break; } if (!mp.count(step)) { ; s++; ans += "R"; } else { auto p = mp[step]; if (p.first <= s && s <= p.second) { ans += "X"; } else if (p.first <= s + 1 && s + 1 <= p.second) { ans += "X"; } else { ; ans += 'R'; s++; } } } } else if (s > f) { ; for (int step = 1;; step++) { if (s == f) { break; } if (!mp.count(step)) { s--; ans += "L"; } else { auto p = mp[step]; if (p.first <= s && s <= p.second) { ans += "X"; } else if (p.first <= s - 1 && s - 1 <= p.second) { ans += "X"; } else { ans += 'L'; s--; } } } } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(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> #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse4") using namespace std; inline long long in() { long long x; scanf("%lld", &x); return x; } int32_t main() { long long n = in(); long long m = in(); long long s = in(); long long f = in(); long long cur = s; long long to = 0; for (long long i = 1; i <= m; i++) { long long t = in(); long long l = in(); long long r = in(); if (t - to > 1) { if (cur < f) { long long d = min(t - to - 1, f - cur); cur += d; for (long long i = 1; i <= d; i++) cout << "R"; } else if (cur > f) { long long d = min(t - to - 1, cur - f); cur -= d; for (long long i = 1; i <= d; i++) cout << "L"; } } if (cur < f) { long long tar = cur + 1; if ((tar >= l and tar <= r) or (cur >= l and cur <= r)) cout << "X"; else { cout << "R"; cur++; } } else if (cur > f) { long long tar = cur - 1; if ((tar >= l and tar <= r) or (cur >= l and cur <= r)) cout << "X"; else { cout << "L"; cur--; } } to = t; } if (cur != f) { if (cur < f) cout << string(f - cur, 'R'); else cout << string(cur - f, '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
#include <bits/stdc++.h> using namespace std; int t[100005], l[100005], r[100005]; int main() { int n, m, s, f; cin >> n >> m >> s >> f; int c = s; int last = 0; for (int i = 0; i < m; i++) cin >> t[i] >> l[i] >> r[i]; string out = ""; int ct = 1; int k = 0; while (s != f) { char ch; int tt; if (s < f) { ch = 'R'; tt = 1; } else { ch = 'L'; tt = -1; } if (ct == t[k]) { if ((s >= l[k] && s <= r[k]) || (s + tt >= l[k] && s + tt <= r[k])) out += 'X'; else { out += ch; s += tt; } k++; } else { out += ch; s += tt; } ct++; } 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
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s, f, t0 = 0; scanf("%d%d%d%d", &n, &m, &s, &f); string step; for (int t, l, r, i = 0; i < m; ++i, t0 = t) { scanf("%d%d%d", &t, &l, &r); int dt = t - t0; if (s < f) { int d = f - s; int dx = min(d, dt - 1); for (int j = 0; j < dx; ++j) step += 'R'; s += dx; if (s == f) break; if (s + 1 < l || s > r) { s++; step += 'R'; if (s == f) break; } else { step += 'X'; } } else { int d = s - f; int dx = min(d, dt - 1); for (int j = 0; j < dx; ++j) step += 'L'; s -= dx; if (s == f) break; if (s < l || s - 1 > r) { s--; step += 'L'; if (s == f) break; } else { step += 'X'; } } } if (s < f) { int d = f - s; for (int j = 0; j < d; ++j) step += 'R'; } else { int d = s - f; for (int j = 0; j < d; ++j) step += 'L'; } puts(step.c_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
#include <bits/stdc++.h> using namespace std; int main() { map<int, pair<int, int> > mp; string move = "LRX"; int n, m, s, f, temp, counter = 1; cin >> n >> m >> s >> f; for (int i = 0; i < m; i++) { cin >> temp; cin >> mp[temp].first; cin >> mp[temp].second; } temp = 1; if (s > f) { temp = 0; counter = -1; } for (int i = 1; s != f; i++) { if (mp[i].first && mp[i].second) { if ((s < mp[i].first || s > mp[i].second) && (s + counter < mp[i].first || s + counter > mp[i].second)) cout << move[temp]; else { cout << move[2]; s -= counter; } } else cout << move[temp]; s += counter; } }
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 t[100001], L[100001], R[100001]; char dev[300001]; char ff(int dir) { if (dir == 1) return 'R'; return 'L'; } bool noesta(int x, int a, int b) { if (x < a || x > b) return 1; return 0; } int main() { int n, m, s, f; while (cin >> n >> m >> s >> f) { for (int i = 0; i < m; i++) scanf("%d%d%d", &t[i], &L[i], &R[i]); int cont = 0; int dir = -1; if (s < f) dir = 1; int tiempo = 1; int pos = 0; while (s != f) { if (tiempo != t[pos]) { s += dir; dev[cont++] = ff(dir); } else { int izq = L[pos]; int der = R[pos]; if (noesta(s, izq, der) && noesta(s + dir, izq, der)) { s += dir; dev[cont++] = ff(dir); } else { dev[cont++] = 'X'; } pos++; } tiempo++; } for (int i = 0; i < cont; i++) printf("%c", dev[i]); 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.OutputStreamWriter; import java.io.BufferedWriter; import java.util.Comparator; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.util.List; import java.io.IOException; import java.util.InputMismatchException; import java.util.ArrayList; import java.util.NoSuchElementException; import java.math.BigInteger; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Nguyen Trung Hieu - [email protected] */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, InputReader in, OutputWriter out) { int count = in.readInt(); int timeCount = in.readInt(); int start = in.readInt() - 1; int end = in.readInt() - 1; int[] time = new int[timeCount]; int[] from = new int[timeCount]; int[] to = new int[timeCount]; IOUtils.readIntArrays(in, time, from, to); MiscUtils.decreaseByOne(time, from, to); for (int i = 0; i < timeCount; i++) { for (int j = (i == 0 ? 0 : time[i - 1] + 1); j < time[i] && start != end; j++) { if (start < end) { start++; out.print('R'); } else { start--; out.print('L'); } } if (start != end && (start <= to[i] && start >= from[i]) || (end < start && start - 1 <= to[i] && start - 1 >= from[i]) || (end > start && start + 1 >= from[i] && start + 1 <= to[i])) out.print('X'); else if (end < start) { out.print('L'); start--; } else if (end > start) { out.print('R'); start++; } } if (start < end) for (int i = start; i < end; i++) out.print('R'); else for (int i = start; i > end; i--) out.print('L'); // int idx = start; // // int curTime = 1; // int i = 0; // while (end != idx) { // if (curTime == time[i] && ((idx <= to[i] && idx >= from[i]) || (end < idx && idx - 1 <= to[i] && idx - 1 >= from[i]) || (end > idx && idx + 1 >= from[i] && idx + 1 <= to[i]))) { // out.print('X'); // i++; // } // else if (end < idx) { // out.print('L'); // idx--; // } // else if (end > idx) { // out.print('R'); // idx++; // } // curTime++; // } } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(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 int readInt() { 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') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void close() { writer.close(); } } class IOUtils { public static void readIntArrays(InputReader in, int[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) arrays[j][i] = in.readInt(); } } } class MiscUtils { public static void decreaseByOne(int[]...arrays) { for (int[] array : arrays) { for (int i = 0; i < array.length; i++) array[i]--; } } }
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.*; import java.math.*; import static java.lang.Math.*; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.Double.parseDouble; import static java.lang.String.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); //(new FileReader("input.in")); StringBuilder out = new StringBuilder(); StringTokenizer tk; Reader.init(System.in); //PrintWriter pw = new PrintWriter("output.out", "UTF-8"); tk = new StringTokenizer(in.readLine()); int n = parseInt(tk.nextToken()),m = parseInt(tk.nextToken()),a = parseInt(tk.nextToken()),b = parseInt(tk.nextToken()); boolean flag = true; if(a > b) flag = false; int t,l,r; Map<Integer,pair> map = new HashMap<Integer,pair>(); while(m-- > 0) { tk = new StringTokenizer(in.readLine()); t = parseInt(tk.nextToken()); l = parseInt(tk.nextToken()); r = parseInt(tk.nextToken()); map.put(t, new pair(l,r)); } int cur = a; int s = 1; while((flag && cur<b) || (!flag && cur>b)) { if(!map.containsKey(s) || (cur+(flag ? 1 : 0)<map.get(s).l || cur-(flag ? 0 : 1)>map.get(s).r)) { out.append(flag ? "R" : "L"); cur += flag ? 1 : -1; } else out.append("X"); s++; } System.out.println(out); } static class pair { int l,r; public pair(int x,int y) { this.l = x; this.r = y; } } } class Tools { public static boolean[] seive(int n) { boolean [] isPrime = new boolean[n+1]; Arrays.fill(isPrime, true); for(int i=2; i*i<=n; i++) if(isPrime[i]) for(int j=i; i*j<=n; j++) isPrime[i*j] = false; isPrime[1] = false; return isPrime; } public static boolean next_permutation(int[] p, int len) { int a = len - 2; while (a >= 0 && p[a] >= p[a + 1]) { a--; } if (a == -1) { return false; } int b = len - 1; while (p[b] <= p[a]) { b--; } p[a] += p[b]; p[b] = p[a] - p[b]; p[a] -= p[b]; for (int i = a + 1, j = len - 1; i < j; i++, j--) { p[i] += p[j]; p[j] = p[i] - p[j]; p[i] -= p[j]; } return true; } public static int lower_bound(Comparable[] arr, Comparable key) { int len = arr.length; int lo = 0; int hi = len-1; int mid = (lo + hi)/2; while (true) { int cmp = arr[mid].compareTo(key); if (cmp == 0 || cmp > 0) { hi = mid-1; if (hi < lo) return mid; } else { lo = mid+1; if (hi < lo) return mid<len-1?mid+1:-1; } mid = (lo + hi)/2; } } public static int upper_bound(Comparable[] arr, Comparable key) { int len = arr.length; int lo = 0; int hi = len-1; int mid = (lo + hi)/2; while (true) { int cmp = arr[mid].compareTo(key); if (cmp == 0 || cmp < 0) { lo = mid+1; if (hi < lo) return mid<len-1?mid+1:-1; } else { hi = mid-1; if (hi < lo) return mid; } mid = (lo + hi)/2; } } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) throws UnsupportedEncodingException { reader = new BufferedReader( new InputStreamReader(input, "UTF-8")); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static String nextLine() throws IOException { return reader.readLine(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(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
#include <bits/stdc++.h> using namespace std; int main() { std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long f = 0, j, q = 1, i, n; while (q--) { long long y, k = 1, x, M, s; cin >> n >> M >> s >> f; unordered_map<int, pair<int, int> > m; for (i = 0; i < M; i++) { long long X, Y, Z; cin >> X >> Y >> Z; m.insert(make_pair(X, make_pair(Y, Z))); } if (s < f) { while (s != f) { if (m.count(k)) { if ((s >= m[k].first && s <= m[k].second) || (s + 1 >= m[k].first && s + 1 <= m[k].second)) cout << "X"; else { cout << "R"; s++; } } else { cout << "R"; s++; } k++; } } else { while (s != f) { if (m.count(k)) { if ((s >= m[k].first && s <= m[k].second) || (s - 1 >= m[k].first && s - 1 <= m[k].second)) cout << "X"; else { cout << "L"; s--; } } else { cout << "L"; s--; } k++; } } 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
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s, f; cin >> n >> m >> s >> f; char move; int inc; if (s < f) move = 'R', inc = 1; else move = 'L', inc = -1; int cur = s, i = 1, cnt = 0; while (cur != f) { int t, l, r; if (cnt < m) { cin >> t >> l >> r; cnt++; } while (i < t) { cur += inc; cout << move; if (cur == f) return 0; i++; } if ((i == t) and ((l <= cur and cur <= r) or (l <= cur + inc and cur + inc <= r))) cout << "X"; else { cout << move; cur += inc; } i++; } 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 main() { int n, m, s, f, t, l, r, cur, inc, i, temp; char ch; scanf("%d %d %d %d", &n, &m, &s, &f); cur = s; if (s < f) { inc = 1; ch = 'R'; } else { inc = -1; ch = 'L'; } temp = 0; for (i = 0; i < m; i++) { scanf("%d %d %d", &t, &l, &r); while (++temp < t) { cur += inc; putchar(ch); if (cur == f) return 0; } if ((cur + inc >= l && cur <= r && s < f) || (cur >= l && cur + inc <= r && s > f)) printf("X"); else { cur += inc; putchar(ch); } if (cur == f) return 0; temp = t; } while (cur != f) { cur += inc; putchar(ch); } 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()) ans, p = '', 1 if s < f: for i in range(m): t, l, r = map(int, input().split()) if t > p: if t - p < f - s: ans += 'R' * (t - p) s += t - p else: ans += 'R' * (f - s) s = f break p = t + 1 if s + 1 < l or s > r: ans += 'R' s += 1 if s == f: break else: ans += 'X' else: for i in range(m): t, l, r = map(int, input().split()) if t > p: if t - p < s - f: ans += 'L' * (t - p) s -= t - p else: ans += 'L' * (s - f) s = f break p = t + 1 if s < l or s - 1 > r: ans += 'L' s -= 1 if s == f: break else: ans += 'X' print(ans + ('R' * (f - s) if f > s else 'L' * (s - f)))
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.*; 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; 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 first solution is to put another if statement and break outside of for loop. if(currentplacement == f) break; i++; } //************** if(currentplacement == f) break; //************** 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
import java.util.StringTokenizer; import java.io.*; public class XeniaAndSpies { public static void main(String[]args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tt = new StringTokenizer(br.readLine()); int n = Integer.parseInt(tt.nextToken()); int m = Integer.parseInt(tt.nextToken()); int s = Integer.parseInt(tt.nextToken()); int f = Integer.parseInt(tt.nextToken()); int j=1,d=1; boolean right=false; boolean safe; if(f>s) right=true; while(d<=m&&s!=f) { safe=true; tt = new StringTokenizer(br.readLine()); int t = Integer.parseInt(tt.nextToken()); int l = Integer.parseInt(tt.nextToken()); int r = Integer.parseInt(tt.nextToken()); while(j<=t&&s!=f) { if(j<t) safe=true; else if(right) { if(s+1>=l&&s+1<=r||s>=l&&s<=r) safe=false; } else { if(s-1>=l&&s-1<=r||s>=l&&s<=r) safe=false; } if(safe) { if(right) { s++; System.out.print("R"); } else { System.out.print("L"); s--; } } else System.out.print("X"); j++; } d++; } while(s!=f) { if(right) { s++; System.out.print("R"); } else { System.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, idx_spy, step = 1, idx_xen = 0; vector<int> tim(100001), lef(100001), righ(100001); string ans; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> s >> f; for (int i = 0; i < m; i++) cin >> tim[i] >> lef[i] >> righ[i]; tim[m] = lef[m] = righ[m] = 0; idx_spy = s; while (true) { if (idx_spy == f) { cout << ans << endl; break; } if (step != tim[idx_xen]) { if (f > s) { ans += 'R'; idx_spy++; } else { ans += 'L'; idx_spy--; } } else { if (f > s) { if ((idx_spy >= lef[idx_xen] && idx_spy <= righ[idx_xen]) || (idx_spy + 1 >= lef[idx_xen] && idx_spy + 1 <= righ[idx_xen])) ans += 'X'; else { ans += 'R'; idx_spy++; } } else { if ((idx_spy >= lef[idx_xen] && idx_spy <= righ[idx_xen]) || (idx_spy - 1 >= lef[idx_xen] && idx_spy - 1 <= righ[idx_xen])) ans += 'X'; else { ans += 'L'; idx_spy--; } } idx_xen++; } step++; } 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.*; public class cf342B{ 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(); char toApp = 'L'; int toAdd = -1; StringBuilder ans = new StringBuilder(); if(s < f) { toApp = 'R'; toAdd = 1; } int t=1; int[][] checker = new int[m][3]; for(int i=0; i<m; i++) { checker[i][0] = sc.nextInt(); checker[i][1] = sc.nextInt(); checker[i][2] = sc.nextInt(); } int checker_index =0, ct = 1; while(s!=f) { if(checker_index < m && checker[checker_index][0] == ct && ((s>= checker[checker_index][1] && s <= checker[checker_index][2]) || (s+toAdd>= checker[checker_index][1] && s+toAdd <= checker[checker_index][2])) ) { checker_index++; ans.append('X'); } else { ans.append(toApp); s += toAdd; if(checker_index <m) if(checker[checker_index][0] == ct) checker_index++; } ct++; //System.out.println(s); } System.out.println(ans.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
import java.util.*; import java.io.*; public class XeniaSpies324B { public static class MyFasterScanner { BufferedReader br; StringTokenizer st; public MyFasterScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } 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()); } String nextLine(){ String str=""; try { str = br.readLine().toString(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { // Set up scanner Scanner sc = new Scanner(System.in); // System.out.println("Enter n"); // Total number of spies int n = sc.nextInt(); // System.out.println("Enter m"); // Number of times Xenia watches the spies int m = sc.nextInt(); // System.out.println("Enter s"); // Starting note position int s = sc.nextInt(); // System.out.println("Enter f"); // Finishing note position int f = sc.nextInt(); StringBuilder sb = new StringBuilder(); int loc = s; char dir; int curtime = 1; if (f > s) { dir = 'r'; } else { dir = 'l'; } for (int i=0; i<m; i++) { // System.out.println("Input next time to watch them"); int nexttime = sc.nextInt(); // System.out.println("Enter left spy"); int leftspy = sc.nextInt(); // System.out.println("Enter right spy"); int rightspy = sc.nextInt(); // Move when not watching at all for (int t=curtime; t<nexttime; t++) { if (dir == 'r') { sb.append("R"); loc++; if (loc == f) { System.out.println(sb); return; } } if (dir == 'l') { sb.append("L"); loc--; if (loc == f) { System.out.println(sb); return; } } } // Figure out what to do when watching if (dir == 'r') // Either move right or stay put { if ((loc>=leftspy && loc<=rightspy) || (loc+1>=leftspy && loc+1<=rightspy)) { sb.append("X"); } else { sb.append("R"); loc++; if (loc == f) { System.out.println(sb); return; } } } else // dir == 'l' { if ((loc>=leftspy && loc<=rightspy) || (loc-1>=leftspy && loc-1<=rightspy)) { sb.append("X"); } else { sb.append("L"); loc--; if (loc == f) { System.out.println(sb); return; } } } curtime = nexttime + 1; } // Now must move the rest of the way there, if it hasn't already made it if (dir == 'r') { while (loc < f) { sb.append("R"); loc++; } System.out.println(sb); return; } else // dir == 'l' { while (loc > f) { sb.append("L"); loc--; } System.out.println(sb); return; } } }
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, l, r, T = 1, t, shift; char C; char res[1111111]; int resl; void read_input() { scanf("%d%d%d%d", &n, &m, &s, &f); } void write_output() { printf("%s\n", res); } void solve() { if (s < f) { C = 'R'; shift = 1; } else { C = 'L'; shift = -1; } for (int i = 0; i < m; i++) { scanf("%d%d%d", &t, &l, &r); for (int j = 0; j < t - T; j++) { res[resl++] = C; s += shift; if (s == f) return; } if ((l <= s && s <= r) || (l <= s + shift && s + shift <= r)) res[resl++] = 'X'; else { res[resl++] = C; s += shift; if (s == f) return; } T = t + 1; } while (s != f) { s += shift; res[resl++] = C; } } int main() { read_input(); solve(); write_output(); 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 i, j, k, l, x, y, z, m, n, ans, dir, s, f, current, p, q; pair<int, int> a; pair<int, pair<int, int> > spies[300000]; int main() { scanf("%d %d %d %d", &n, &m, &s, &f); for (i = 0; i < m; i++) { scanf("%d %d %d", &x, &y, &z); a = make_pair(y, z); spies[i] = make_pair(x, a); } if (s > f) dir = -1; else dir = 1; current = s; i = 0; j = 1; while (current != f) { a = spies[i].second; x = spies[i].first; if (x == j) { i++; y = a.first; z = a.second; p = current; q = current + dir; if ((p >= y && p <= z) || (q >= y && q <= z)) { printf("X"); } else { current = current + dir; if (dir == 1) printf("R"); else printf("L"); } } else { current = current + dir; if (dir == 1) printf("R"); else printf("L"); } j++; } 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
def geto(a, b): return max(0, min(a[1], b[1]) - max(a[0], b[0])+1) n,m,s,f = map(int, input().split()) di = {} for _ in range(m): t, l, r = map(int, input().split()) di[t] = [l, r] t = 1 ans = [] while s != f: if f > s: inte = [s, s+1] if t in di and geto(inte, di[t]): ans += ['X'] else: ans += ['R'] s += 1 else: inte = [s-1, s] if t in di and geto(inte, di[t]): ans += ['X'] else: ans += ['L'] s -= 1 t += 1 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> const int N = 1e5 + 10; using namespace std; map<int, pair<int, int> > mp; int main() { int n, m, s, f; scanf("%d%d%d%d", &n, &m, &s, &f); while (m--) { int t, x, y; scanf("%d%d%d", &t, &x, &y); mp[t] = make_pair(x, y); } char c; int mv; if (s < f) c = 'R', mv = 1; else c = 'L', mv = -1; int cur = s; for (int t = 1;; ++t) { if (cur == f) break; int next = cur + mv; if ((cur >= mp[t].first && cur <= mp[t].second) || (next >= mp[t].first && next <= mp[t].second)) putchar('X'); else { cur += mv; putchar(c); } } 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; bool sortbysec(const pair<long long int, long long int> &a, const pair<long long int, long long int> &b) { return (a.second < b.second); } pair<long long int, long long int> call(long long int n) { long long int i = 0; long long int k = n; while (n % 2 == 0) { n /= 2; i++; } return {1 << i, k}; } void solve() { long long int n, m, s, f; cin >> n >> m >> s >> f; vector<long long int> ti(m), li(m), ri(m); for (long long int i = 0; i < m; i++) cin >> ti[i] >> li[i] >> ri[i]; long long int mv = f > s ? 1 : -1; char k = f > s ? 'R' : 'L'; long long int t = 1; for (long long int i = 0; i < m; i++) { if (s == f) break; while (t < ti[i] && s != f) { s += mv; cout << k; t++; } if (s == f) break; else if ((s <= ri[i] && s >= li[i]) || (s + mv <= ri[i] && +mv + s >= li[i])) { cout << 'X'; t++; } else { cout << k; t++; s += mv; } } while (s != f) { s += mv; cout << k; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); ; ; long long int t = 1; while (t--) { solve(); } }
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 N = 100005; int n, m, s, f, t[N], l[N], r[N]; int main() { 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); } t[m] = -1; int timer = 0, pos = 0; string ans; while (s != f) { char ch; if (s > f) { ch = 'L'; } else { ch = 'R'; } ++timer; if (t[pos] == timer) { int p1 = s, p2 = ch == 'L' ? s - 1 : s + 1; if (p1 >= l[pos] && p1 <= r[pos] || p2 >= l[pos] && p2 <= r[pos]) { ch = 'X'; } ++pos; } ans += ch; if (ch == 'L') { --s; } else if (ch == 'R') { ++s; } } printf("%s\n", ans.c_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() { int t, l, r, n, m, s, f; cin >> n >> m >> s >> f; int cnt = 1; for (int i = 0; i < m; i++) { cin >> t >> l >> r; while (t > cnt && s != f) { cnt++; if (s < f) s++, cout << "R"; else s--, cout << "L"; } if (s == f) break; if (t == cnt) { if (s > f) { if (s >= l && s - 1 <= r) cout << "X", cnt++; else cout << "L", cnt++, s--; } else if (s < f) { if (s + 1 >= l && s <= r) cout << "X", cnt++; else cout << "R", cnt++, s++; } } } if (s > f) { while (s != f) { cout << "L"; s--; } } else if (s < f) { while (s != f) cout << "R", s++; } return 0; }
CPP