Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.util.Scanner; public class popo { public static void main(String[] args) { InputReader in=new InputReader(System.in); PrintWriter out=new PrintWriter(System.out); int n=in.nextInt(); int m=in.nextInt(); int s=in.nextInt(); int f=in.nextInt(); int count=0; if(f>s) { while (m > 0) { m--; int t = in.nextInt(); int l = in.nextInt(); int r = in.nextInt(); for (int i = 0; ; i++) { if(count==t-1) { if ((s >= l && s <= r) || (s + 1 == l)) { out.print('X'); } else { out.print('R'); s++; //out.println(s); if (s == f) { //out.print(s); break; } } count++; break; }else{ out.print('R'); s++; if (s == f) { //out.print(s); break; } } count++; } if(s==f) { break; } } if(m==0 && s!=f){ while(s!=f){ s++; out.print('R'); } } } else { while (m > 0) { m--; int t = in.nextInt(); int l = in.nextInt(); int r = in.nextInt(); for (int i = 0; ; i++) { if(count==t-1) { if ((s >= l && s <= r) || (s - 1 == r)) { out.print('X'); } else { out.print('L'); s--; if (s == f) { break; } } count++; break; }else { out.print("L"); s--; // out.println(s); if (s == f) { //out.print(s); break; } } count++; // System.out.println(count); } if (s == f) { break; } } if(m==0 && s!=f){ while(s!=f){ s--; out.print('L'); } } } out.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private InputReader.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; map<int, pair<int, int> > Map; int n, m, i, s, f, t, l, r; bool Check(int a) { return ((l <= a) && (a <= r)); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> s >> f; for (i = 1; i <= m; i++) { cin >> t >> l >> r; Map[t] = make_pair(l, r); } t = 0; while (true) { t++; l = Map[t].first; r = Map[t].second; if (s < f) { if ((!Check(s)) && (!Check(s + 1))) { cout << "R"; s++; } else cout << "X"; } else if (s > f) { if ((!Check(s - 1)) && (!Check(s))) { cout << "L"; s--; } else cout << "X"; } else break; } 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
import java.util.*; public class b199 { public static void main(String ar[]) { Scanner obj=new Scanner(System.in); int n=obj.nextInt(); int m=obj.nextInt(); int s=obj.nextInt(); int f=obj.nextInt(); long a[][]=new long[m][3]; for(int i=0;i<m;i++) { a[i][0]=obj.nextLong(); a[i][1]=obj.nextLong(); a[i][2]=obj.nextLong(); } int i=1,j=0; if(s==f) System.out.print("X"); else { while(s!=f) { if(j>=m||i!=a[j][0]) { if(s<f) { s+=1; System.out.print("R"); } else { s-=1; System.out.print("L"); } } else { if(s>=a[j][1]&&s<=a[j][2]) System.out.print("X"); else if(s<f && (s+1)==a[j][1]) System.out.print("X"); else if(s>f && (s-1)==a[j][2]) System.out.print("X"); else { if(s<f) { s+=1; System.out.print("R"); } else { s-=1; System.out.print("L"); } } j+=1; } i+=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
#include <bits/stdc++.h> using namespace std; const double PI = 3.14159265359; int n, m, s, f; int main() { cin >> n >> m >> s >> f; for (int i = 0, j = 1; i < m; ++i) { int t, l, r; scanf("%d %d %d", &t, &l, &r); for (; j != t; ++j) { if (f < s) --s, putchar('L'); else if (f > s) ++s, putchar('R'); else return 0; } if (s == f) return 0; if (s >= l && s <= r) putchar('X'); else if (f < s) { if (s - 1 >= l && s - 1 <= r) putchar('X'); else --s, putchar('L'); } else { if (s + 1 >= l && s + 1 <= r) putchar('X'); else ++s, putchar('R'); } ++j; } while (s != f) if (s < f) ++s, putchar('R'); else --s, putchar('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
R = lambda: map(int, input().split()) n, m, s, f = R() if s < f: d = 1 c = 'R' else: d = -1 c = 'L' res = "" i = 1 j = s t, l, r = R() k = 1 while j != f: if i > t and k < m: t, l, r = R() k += 1 if i == t and (l <= j <= r or l <= j + d <= r): res += 'X' else: res += c j += d i += 1 print(res) # Made By Mostafa_Khaled
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.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { int n, m, s, f; Scanner in = new Scanner(System.in); n = in.nextInt(); m = in.nextInt(); s = in.nextInt(); f = in.nextInt(); Map<Integer, int[]> map = new HashMap<>(); for (int i = 0; i < m; i++) { int x = in.nextInt(); int y = in.nextInt(); int z = in.nextInt(); map.put(x, new int[] { y, z }); } StringBuilder sb = new StringBuilder(); int st = 0; if (s < f) { while (s < f) { st++; if (map.containsKey(st)) { if ((map.get(st)[0] <= s && map.get(st)[1] >= s) || (map.get(st)[0] <= s + 1 && map.get(st)[1] >= s + 1)) { sb.append("X"); continue; } } sb.append("R"); s++; } } else { while (s > f) { st++; if (map.containsKey(st)) { if ((map.get(st)[0] <= s && map.get(st)[1] >= s) || (map.get(st)[0] <= s - 1 && map.get(st)[1] >= s - 1)) { sb.append("X"); continue; } } sb.append("L"); s--; } } System.out.println(sb.toString()); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; const long double pi = acos(-1.0); vector<string> given; vector<vector<int>> given1; int mx1 = -1, mx2 = -1, mx3 = -1; int bits(int n) { int ans = 0; while (n) { ans += n % 2; n /= 2; } return ans; } long long bs(long long a[], long long low, long long high, long long val) { long long mid = (low + high) / 2; if (low > high) return -1; if (a[mid] > val) bs(a, low, mid - 1, val); else if (a[mid] < val) bs(a, mid + 1, high, val); else return mid; } int calc(int n) { if (n == 1) return 9; int ans = 9 * 4 * (pow(2, n) - 2) + 9 * (pow(2, n - 1) - 1) + 9; return ans; } void DJ() { int n, m, s, f, i, j; cin >> n >> m >> s >> f; map<int, pair<int, int>> steps; for (i = 1; i < m + 1; ++i) { int a, l, r; cin >> a >> l >> r; steps[a].first = l; steps[a].second = r; } if (s > f) { int k = 1; while (s != f) { if (steps.find(k) == steps.end()) { cout << 'L'; s--; k++; continue; } if (s >= steps[k].first && s <= steps[k].second || s - 1 >= steps[k].first && s - 1 <= steps[k].second) { cout << 'X'; } else { cout << 'L'; s--; } k++; } } else { int k = 1; while (s != f) { if (steps.find(k) == steps.end()) { cout << 'R'; s++; k++; continue; } if (s >= steps[k].first && s <= steps[k].second || s + 1 >= steps[k].first && s + 1 <= steps[k].second) { cout << 'X'; } else { cout << 'R'; s++; } k++; } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1, i; while (t--) DJ(); return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.Scanner; public class XeniaandSpies { public static void main(String asd[])throws Exception { Scanner in=new Scanner(System.in); int n=in.nextInt(); int m=in.nextInt(); int s=in.nextInt(); int f=in.nextInt(); StringBuilder ss=new StringBuilder();int q=0; for(int i=1;i<=m;i++) { int t=in.nextInt(); int l=in.nextInt(); int r=in.nextInt(); if(t-q>1) { int d=t-q-1; while(s!=f && d>0){ if(s<f) { s++; ss.append("R"); } else if(s>f) { s--; ss.append("L"); } d-=1; } } if(s<f) { if(s<l && s+1<l || s>r && s+1>r) { s+=1; ss.append("R"); } else ss.append("X"); } else if(s>f) { if(s<l && s-1<l || s>r && s-1>r) { s-=1; ss.append("L"); } else ss.append("X"); } q=t; } while(s!=f){ if(s<f) { s++; ss.append("R"); } else if(s>f) { s--; ss.append("L"); } } System.out.println(ss); } }
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 Max = 1e5 + 100; int n, m, s, f, l, r, st = 0, dir; map<int, pair<int, int> > watch; string ans = ""; bool isBlocked(int idx, int pos) { if (watch.find(idx) != watch.end()) { l = watch[idx].first; r = watch[idx].second; if (pos >= l && pos <= r) return true; return false; } return false; } void init() { int idx; cin >> n >> m >> s >> f; for (int i = int(1); i <= int(m); i++) { cin >> idx >> l >> r; watch[idx] = make_pair(l, r); st = max(st, idx); } if (f > s) dir = 1; else dir = -1; } void solve() { int m = s; for (int i = int(1); i <= int(st); i++) { if (m == f) return; if (isBlocked(i, m)) ans.push_back('X'); else if (isBlocked(i, m + dir)) ans.push_back('X'); else if (dir == 1) { ans.push_back('R'); m += 1; } else { ans.push_back('L'); m -= 1; } } while (m != f) { if (dir == 1) { ans.push_back('R'); m += 1; } else { ans.push_back('L'); m -= 1; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); init(); solve(); cout << ans << 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; long long gcd(long long a, long long b); long long cross_prod(long long ax, long long ay, long long bx, long long by, long long cx, long long cy); long long sum_digit(long long A); long long get_mask(long long x); long long get_nr(string &S, int pos, int lg); bool cmp_pair_ii1(pair<int, int> A, pair<int, int> B); bool cmp_i2(int A, int B); string next_word(string &S, int &pos); long long rise(long long x, long long p); double dist(int a, int b, int x, int y); class SegmentTree { public: vector<int> V; void init(int dim); int query(int start, int end, int pos, int node); void build(int start, int end, int node); }; int N, M, S, F; int main() { int x, y, t, dif, i; char ch; scanf("%d %d %d %d", &N, &M, &S, &F); if (S < F) { ch = 'R'; } else { ch = 'L'; } dif = -1; if (F > S) dif = 1; scanf("%d %d %d", &t, &x, &y); i = 0; while (S != F) { ++i; if (i == t) { if ((x <= S && S <= y) || (x <= S + dif && S + dif <= y)) { printf("X"); } else { S += dif; printf("%c", ch); } scanf("%d %d %d", &t, &x, &y); } else { do { S += dif; printf("%c", ch); ++i; } while (i < t && S != F); --i; } } return 0; } double dist(int a, int b, int x, int y) { double t1, t2; t1 = a - x; t1 *= t1; t2 = b - y; t2 *= t2; return sqrt(t1 + t2); } bool cmp_pair_ii1(pair<int, int> A, pair<int, int> B) { return A.first < B.first; } bool cmp_i2(int A, int B) { return A > B; } long long gcd(long long a, long long b) { return (b ? gcd(b, a % b) : a); } long long cross_prod(long long ax, long long ay, long long bx, long long by, long long cx, long long cy) { return (bx - ax) * (cy - ay) - (by - ay) * (cx - ax); } long long sum_digit(long long A) { long long sum = 0; do { sum += A % 10; A /= 10; } while (A); return sum; } long long get_mask(long long x) { long long mask = 0, c; while (x) { c = x % 10; x /= 10; if (c == 4 || c == 7) { mask = mask * 10 + c; } } return mask; } void SegmentTree::init(int dim) { V.resize((dim + 1) << 2); } void SegmentTree::build(int start, int end, int node) { V[node] = end - start + 1; if (start == end) { return; } int mid; mid = (start + end) >> 1; build(start, mid, (node << 1)); build(mid + 1, end, ((node << 1) | 1)); } int SegmentTree::query(int start, int end, int pos, int node) { --V[node]; if (start == end) return end; int L, R; L = node << 1; R = L | 1; int mid; mid = (start + end) >> 1; if (pos <= V[L]) { return query(start, mid, pos, L); } else { return query(mid + 1, end, pos - V[L], R); } } long long get_nr(string &S, int pos, int lg) { long long nr = 0; while (lg) { nr = nr * 10 + (S[pos] - '0'); ++pos; --lg; } return nr; } string next_word(string &S, int &pos) { int pos_l, pos_sp; if (S[S.length() - 1] != ' ') { S += ' '; } pos_l = S.find_first_not_of(" ", pos); if (pos_l == -1) { pos = -1; return ""; } pos_sp = S.find_first_of(" ", pos_l); pos = pos_sp; return S.substr(pos_l, pos_sp - pos_l); } long long rise(long long x, long long p) { if (p == 1) { return x; } long long res = rise(x, p / 2); res %= 1000000007; if (p & 1) { return (((res * res) % 1000000007) * x) % 1000000007; } else { return ((res * res) % 1000000007); } }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.Scanner; public class XeniaAndSpies { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(), s = in.nextInt(), f = in .nextInt(); char move = (s < f) ? 'R' : 'L'; int start = s, it = 0; StringBuilder ans = new StringBuilder(); for (int i = 0; i < m; i++) { if (start == f) break; int t = in.nextInt(), j = 0; long l = in.nextLong(), r = in.nextLong(); int next = ( (move == 'R') ? start + 1 : start - 1 ); boolean found = false; if (move == 'R') { for (j = it + 1; j < t; j++, start++, next++) { if (start == f) { //System.out.print(ans); return; } //ans.append("R"); System.out.print("R"); } } else { for (j = it + 1; j < t; j++, start--, next--) { if (start == f) { //System.out.print(ans); return; } //ans.append("L"); System.out.print("L"); } } if (start == f) break; if ((next < l && start < l) || (start > r && next > r)) found = true; if (!found) //ans.append("X"); System.out.print("X"); else { if (move == 'R') { //ans.append("R"); System.out.print("R"); start++; } else { //ans.append("L"); System.out.print("L"); start--; } } it = j; } int j = f - start; if (move == 'R') { for (int x = 0; x < j; x++) //ans.append("R"); System.out.print("R"); } else { j *= -1; for (int x = 0; x < j; x++) //ans.append("L"); System.out.print("L"); } //System.out.print(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
/** * Created with IntelliJ IDEA. * User: zangetsu * Date: 9/1/13 */ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.lang.reflect.Array; import java.util.*; public class Main_A { /** * @param args * @throws IOException */ static InputReader scan = new InputReader(System.in); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws IOException { int n = scan.nextInt(), m = scan.nextInt(), s = scan.nextInt(), f = scan.nextInt(); int[][] move = new int[m][3]; for (int ii=0; ii<m; ii++) { move[ii][0] = scan.nextInt(); move[ii][1] = scan.nextInt(); move[ii][2] = scan.nextInt(); } int pos = s; int idx = 0; int turn = 1; StringBuilder ans = new StringBuilder(); char cmove, cstop = 'X'; int inc; if (s < f) { cmove = 'R'; inc = 1; } else { cmove = 'L'; inc = -1; } while (pos != f) { if (idx < m) { if (move[idx][0] != turn) { ans.append(cmove); pos += inc; } else { if ((move[idx][1] <= pos && pos <= move[idx][2]) || (move[idx][1] <= pos + inc && pos + inc <= move[idx][2])) { ans.append(cstop); } else { ans.append(cmove); pos += inc; } idx ++; } } else { ans.append(cmove); pos += inc; } turn++; } pw.println(ans); pw.close(); } } class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } class ArrayUtils { private static int[] tempInt = new int[0]; public static int[] sort(int[] array, IntComparator comparator) { return sort(array, 0, array.length, comparator); } public static int[] sort(int[] array, int from, int to, IntComparator comparator) { ensureCapacityInt(to - from); System.arraycopy(array, from, tempInt, 0, to - from); sortImpl(array, from, to, tempInt, 0, to - from, comparator); return array; } private static void ensureCapacityInt(int size) { if (tempInt.length >= size) return; size = Math.max(size, tempInt.length << 1); tempInt = new int[size]; } private static void sortImpl(int[] array, int from, int to, int[] temp, int fromTemp, int toTemp, IntComparator comparator) { if (to - from <= 1) return; int middle = (to - from) >> 1; int tempMiddle = fromTemp + middle; sortImpl(temp, fromTemp, tempMiddle, array, from, from + middle, comparator); sortImpl(temp, tempMiddle, toTemp, array, from + middle, to, comparator); int index = from; int index1 = fromTemp; int index2 = tempMiddle; while (index1 < tempMiddle && index2 < toTemp) { if (comparator.compare(temp[index1], temp[index2]) <= 0) array[index++] = temp[index1++]; else array[index++] = temp[index2++]; } if (index1 != tempMiddle) System.arraycopy(temp, index1, array, index, tempMiddle - index1); if (index2 != toTemp) System.arraycopy(temp, index2, array, index, toTemp - index2); } } interface IntComparator { public static final IntComparator DEFAULT = new IntComparator() { public int compare(int first, int second) { if (first < second) return -1; if (first > second) return 1; return 0; } }; public int compare(int first, int second); }
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; bool check(int x, int l, int r) { if (x >= l && x <= r) return true; return false; } const int maxn = 1e5 + 10; int n, m, s, f, d, pos; map<int, int> mp; pair<int, int> p[maxn]; string ans; int main() { cin >> n >> m >> s >> f; for (int i = 1; i <= m; ++i) { int t; cin >> t >> p[i].first >> p[i].second; mp[t] = i; } int t = 1; pos = s; if (f > s) d = 1; else d = -1; while (pos != f) { int k = mp[t]; if (k && (check(pos, p[k].first, p[k].second) || check(pos + d, p[k].first, p[k].second))) ans += '0'; else { ans += '1'; pos += d; } ++t; } for (int i = 0; i < ans.size(); ++i) if (ans[i] - '0') if (d > 0) cout << 'R'; else cout << 'L'; 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
n, m, s, f = map(int, input().split()) c, t1 = 1, 0 ans = "" t = 1 while s != f and t1 < m: t, l, r = map(int, input().split()) # print(ans) while t != c: if s < f: ans += "R" s += 1 else: ans += "L" s -= 1 if s == f: break c += 1 if s < f: if s >= l-1 and s <= r: ans += "X" else: ans += "R" s += 1 elif s > f: if s <= r+1 and s >= l: ans += "X" else: ans += "L" s -= 1 c += 1 t1 += 1 if s < f: ans += "R"*(f-s) elif s > f: ans += "L"*(s-f) print(ans)
PYTHON3
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
''' ||Sri:|| __| ______________|_________________________ | | ___| | | ___| | | |___ /\ /\ | |___ | | |___| | | |___| / \/ \ | ___| | | |_/ |___| |___ I am a beginner. Feel free to send tutorials/help to [email protected]. Codeforces, SPOJ handle: desikachariar. Hackerrank, Codechef handle: Prof_Calculus. Advices/Help are welcome. You will definitely get a thanks in return. Comments to be avoided. College: International Institute of Information Technology, Bangalore. ''' import math st="" d = {} if __name__ == '__main__': n,m,start,dest= [int(pp) for pp in raw_input().split(' ')] for i in range(m): a,b,c = [int(pp) for pp in raw_input().split(' ')] d[a] = [b,c] step = 1 while start != dest: to_go = start-1 c = 'L' if dest > start: to_go = start+1 c = 'R' if step in d and ((start >= d[step][0] and start <= d[step][1]) or (to_go >= d[step][0] and to_go <= d[step][1])): st+='X' else: st+=c start = to_go step+=1 print st
PYTHON
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class con199_B { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); StringTokenizer tok = new StringTokenizer(line); int n = ti(tok.nextToken()); int m = ti(tok.nextToken()); int[] t = new int[m + 1]; int[] l = new int[m + 1]; int[] r = new int[m + 1]; t[m] = Integer.MAX_VALUE; l[m] = 0; r[m] = 0; int s = ti(tok.nextToken()); int f = ti(tok.nextToken()); for ( int i = 0; i < m; ++i ) { line = br.readLine(); tok = new StringTokenizer(line); t[i] = ti(tok.nextToken()); l[i] = ti(tok.nextToken()); r[i] = ti(tok.nextToken()); } System.out.println(solve(n, m, s, f, t, l, r)); } private static String solve(int n, int m, int s, int f, int[] t, int[] l, int[] r) { StringBuilder steps = new StringBuilder(); int nws = 0; // next watched step index int as = 1; // actual step index int na = nextAgent(s, f); while (s != f) { if (t[nws] > as) { // not watched steps.append( s < na ? 'R' : 'L'); s = na; na = nextAgent(s, f); ++as; continue; } assert t[nws] == as; if (isBetween(s, l[nws], r[nws]) || isBetween(na, l[nws], r[nws])) { steps.append('X'); } else { steps.append( s < na ? 'R' : 'L'); s = na; na = nextAgent(s, f); } ++nws; ++as; } return steps.toString(); } private static boolean isBetween(int x, int f, int t) { return f <= x && x <= t; } private static int nextAgent(int s, int f) { if (s < f) return s + 1; if (s > f) return s - 1; return s; } private static int ti(String s) { return Integer.parseInt(s); } private static long tl(String s) { return Long.parseLong(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; typedef struct { int t, l, r; } item; int n, m, s, f; item a[100005]; int Right() { int cur = s; for (int j = 1; j < a[1].t; j++) { ++cur; cout << "R"; if (cur == f) return 0; } for (int i = 1; i <= m; i++) { if (a[i].l <= cur && cur <= a[i].r) { cout << "X"; } else { if (a[i].l <= cur + 1 && cur + 1 <= a[i].r) { cout << "X"; } else if (cur + 1 < a[i].l || cur + 1 > a[i].r) { ++cur; cout << "R"; } } if (cur == f) { return 0; } for (int j = a[i].t + 1; j < a[i + 1].t; j++) { cout << "R"; ++cur; if (cur == f) return 0; } } for (int j = a[m].t + 1;; j++) { cout << "R"; ++cur; if (cur == f) return 0; } } int Left() { int cur = s; for (int j = 1; j < a[1].t; j++) { --cur; cout << "L"; if (cur == f) return 0; } for (int i = 1; i <= m; i++) { if (a[i].l <= cur && cur <= a[i].r) { cout << "X"; } else { if (a[i].l <= cur - 1 && cur - 1 <= a[i].r) { cout << "X"; } else if (cur - 1 < a[i].l || cur - 1 > a[i].r) { --cur; cout << "L"; } } if (cur == f) { return 0; } for (int j = a[i].t + 1; j < a[i + 1].t; j++) { cout << "L"; --cur; if (cur == f) return 0; } } for (int j = a[m].t + 1;; j++) { cout << "L"; --cur; if (cur == f) return 0; } } int main() { ios ::sync_with_stdio(false); cin >> n >> m >> s >> f; for (int i = int(1); i <= int(m); ++i) { cin >> a[i].t >> a[i].l >> a[i].r; } if (s < f) Right(); else Left(); 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:300000000") const long double eps = 1e-20; const long double pi = acos(-1.0); const long long inf = 1000 * 1000 * 1000 * 1000 * 1000 * 1000; const long long base = 1000 * 1000 * 1000 + 7; using namespace std; map<int, pair<int, int> > m; int n, q, s, f, t, l, r, dif, ans; set<int> o; bool in(pair<int, int> a, int b) { return (a.first <= b && b <= a.second); } int main() { ios_base ::sync_with_stdio(false); cin >> n >> q >> s >> f; for (int i = 0; i < (q); i++) { cin >> t >> l >> r; o.insert(t); m[t] = make_pair(l, r); } ans = 1; dif = abs(s - f); while (dif) { int now = s, next; if (now < f) next = now + 1; else next = now - 1; if (o.find(ans) == o.end()) { if (s > f) { cout << "L"; s--; } else { cout << "R"; s++; } ans++; dif--; continue; } pair<int, int> tmp = m[ans]; if (in(tmp, now) || in(tmp, next)) { cout << "X"; ans++; } else { if (s > f) { cout << "L"; s--; } else { cout << "R"; s++; } ans++; dif--; continue; } } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Map; import java.util.Scanner; import java.util.HashMap; /** * 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; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); CF_342B solver = new CF_342B(); solver.solve(1, in, out); out.close(); } static class CF_342B { public void solve(int testNumber, Scanner input, PrintWriter out) { ShortScanner in = new ShortScanner(input); int n = in.i(), m = in.i(), s = in.i(), f = in.i(); Map<Integer, Pair> map = new HashMap<>(); for (int i = 0; i < m; ++i) map.put(in.i(), new Pair(in.i(), in.i())); int step = 1, cur = s; while (true) { if (cur == f) break; boolean ok = true; if (map.containsKey(step)) { Pair p = map.get(step); if (p.a <= cur && cur <= p.b) ok = false; if (f > s && p.a <= cur + 1 && cur + 1 <= p.b) ok = false; if (f < s && p.a <= cur - 1 && cur - 1 <= p.b) ok = false; } if (ok) { if (f > s) { out.print('R'); cur++; } else { out.print('L'); cur--; } } else out.print('X'); step++; } out.println(); } class ShortScanner { Scanner in; ShortScanner(Scanner in) { this.in = in; } int i() { return in.nextInt(); } } class Pair { int a; int b; public Pair(int a, int b) { this.a = a; this.b = b; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; if (a != pair.a) return false; return b == pair.b; } public int hashCode() { int result = a; result = 31 * result + b; return result; } } } }
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 t[1 << 17], L[1 << 17], R[1 << 17]; 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]); int cur = 0; string res = ""; for (int T = 1;; ++T) { if (s == f) break; int to = s < f ? s + 1 : s - 1; if (t[cur] == T) { if (L[cur] <= s && s <= R[cur] || L[cur] <= to && to <= R[cur]) res += 'X'; else { res += (s < f ? 'R' : 'L'); s = to; } cur++; } else { res += (s < f ? 'R' : 'L'); s = to; } } printf("%s\n", res.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
n, m, s, f = map(int, input().split()) q = dict() for i in range(m): t,l,r = map(int, input().split()) q[t] = (l, r) k=1;pos=s; while pos!=f: if k in q: if q[k][0] <= pos <= q[k][1]: print('X', end='') else: if 1 < pos < n: if pos > f: if q[k][0] <= pos-1 <= q[k][1]: print('X',end='') else: print('L',end='') pos-=1 else: if q[k][0]<=pos+1<=q[k][1]: print('X',end='') else: print('R',end='') pos+=1 elif pos==1: if q[k][0]<=2<=q[k][1]: print('X',end='') else: print('R',end='') pos=2 elif pos==n: if q[k][0]<=pos-1<=q[k][1]: print('X',end='') else: print('L',end='') pos-=1 else: if pos>f: print('L', end='') pos-=1 else: print('R', end='') pos+=1 k+=1
PYTHON3
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class XeniaAndSpies { static IR in = new IR(System.in); static PrintWriter out = new PrintWriter(System.out); static int N,s,f,M; static int l,r; static int diff = 0; public static void move(){ if(diff>0) { out.printf("R"); s++; diff--;} else { out.printf("L"); s--; diff++;} } public static void main(String[]args)throws Throwable{ N = in.j(); M = in.j(); s = in.j(); f = in.j(); diff = f-s;; int curr =0; for(int i=0;i<M;i++,curr++){ int step = in.j(); l = in.j(); r = in.j(); step--; while(step!=curr){ if(s==f){ out.close(); return; } move(); curr++; } if(s==f){ out.close(); return; } if(s>=l&&s<=r) out.printf("X"); else { if(diff>0) { int cand = s+1; if(cand>=l&&cand<=r) out.printf("X"); else{ out.printf("R"); s++; diff--;} } else if(diff<0) { int cand = s-1; if(cand>=l&&cand<=r) out.printf("X"); else{ out.printf("L"); s--; diff++;} } } } if(s!=f){ if(diff>0){ while(s!=f){ out.printf("R"); f--; } } else{ while(s!=f){ out.printf("L"); f++; } } } out.close(); } static class IR { public BufferedReader reader; public StringTokenizer tokenizer; public IR(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int j() { return Integer.parseInt(next()); } public double d() { return Double.parseDouble(next()); } public long ll(){ 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
n, m, s, f = map(int, raw_input().split()) def overlap(r1, r2, s1, s2): x1, x2 = min(r1, r2), max(r1, r2) y1, y2 = min(s1, s2), max(s1, s2) return y2 >= x1 and x2 >= y1 if s > f: step = (-1, 'L') else: step = (1, 'R') steps = range(0, n + 1) result = '' last_timestamp = 1 for i in range(m): timestamp, l, r = map(int, raw_input().split()) timestamp_difference = timestamp - last_timestamp if timestamp_difference > 1: number_of_steps = min(abs(s - f), timestamp_difference) result += step[1] * number_of_steps s += step[0] * number_of_steps last_timestamp = timestamp if s == f: continue overlapping = overlap(s, s + step[0], l, r) if overlapping: result += 'X' * (timestamp - i) else: result += step[1] s += step[0] if s != f: result += step[1] * abs(s - f) print result
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 static java.lang.Math.*; import static java.lang.System.currentTimeMillis; import static java.lang.System.exit; import static java.lang.System.arraycopy; import static java.util.Arrays.sort; import static java.util.Arrays.binarySearch; import static java.util.Arrays.fill; import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { new Main().run(); } BufferedReader in; PrintWriter out; StringTokenizer st = new StringTokenizer(""); private void run() throws IOException { // in = new BufferedReader(new FileReader("input.txt")); // out = new PrintWriter("output.txt"); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); in.close(); out.close(); } final int MAXN = 200011; final byte right = 1; final byte pass = 2; final byte left = 3; byte[] steps = new byte[MAXN]; int n,m,s,f,step; private void solve() throws IOException { n = nextInt(); m = nextInt(); s = nextInt(); f = nextInt(); step = 0; boolean success = false; int prevT = 1; for(int i = 0; i < m; i++){ int t,l,r; t = nextInt(); l = nextInt(); r = nextInt(); if(!success){ for(;prevT <= t; prevT++){ if(success) break; if(s < f){ if(s < l-1 || s > r || prevT != t){ steps[step++] = right; s++; } else{ steps[step++] = pass; } } else if(s > f) { if(s > r+1 || s < l || prevT != t){ steps[step++] = left; s--; } else { steps[step++] = pass; } } else { success = true; } } } } if(s == f) success = true; if(!success){ if(s < f){ while(s < f){ steps[step++] = right; s++; } } else { while (s > f){ steps[step++] = left; s--; } } } for(int i = 0; i < step; i++){ if(steps[i] == right) out.print("R"); if(steps[i] == left) out.print("L"); if(steps[i] == pass) out.print("X"); } } String nextToken() throws IOException { while (!st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } boolean EOF() throws IOException { while (!st.hasMoreTokens()) { String s = in.readLine(); if (s == null) return true; st = new StringTokenizer(s); } 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; int main() { int i, j, n, m, s, f, l, r, t, c = 0, d; char g; cin >> n >> m >> s >> f; if (s < f) { d = 1; g = 'R'; } else if (s > f) d = -1, g = 'L'; for (i = 1; i <= m; i++) { if (s == f || s > n || s < 1 || f > n || f < 1) break; c++; cin >> t >> l >> r; while (c < t && s != f) { s += d; putchar(g); c++; } if (s != f) if ((s < l || r < s) && (s + d < l || r < s + d)) s += d, putchar(g); else putchar('X'); if (s == f) break; } while (s != f) { s += d; putchar(g); } 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> using namespace std; map<int, int> mp; pair<int, int> a[(int)1e5 + 100]; int n, m, s, g; int main() { scanf("%d%d%d%d", &n, &m, &s, &g); int dx = (s > g) ? -1 : 1; memset(a, -1, sizeof(a)); for (int i = 1; i <= int(m); i++) { int l, r, t; scanf("%d%d%d", &t, &l, &r); mp[t] = i; a[i] = pair<int, int>(l, r); } int T = 1; int p = s; while (true) { int t = mp[T]; int l = a[t].first, r = a[t].second; if ((l <= p && p <= r) || (l <= p + dx && p + dx <= r)) putchar('X'); else { if (dx == 1) putchar('R'); else putchar('L'); p += dx; if (p == g) break; } T++; } }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
def can(cur, d, l, r): dst = cur+d return not ((cur>=l and cur<=r) or (dst>=l and dst<=r)) n, m, s, f = map(int, raw_input().split()) src = [map(int, raw_input().split()) for i in range(m)] for i in range(len(src)): src[i][1]-=1 src[i][2]-=1 srcmapa = {} for i in src: srcmapa[i[0]] = [i[1], i[2]] s-=1 f-=1 res = "" cur = s i=0 j=1 src={} while True: if cur==f: break if cur<f: d = 1 if cur>f: d=-1 if not (j in srcmapa): cur += d if d==1: res+="R" else: res+="L" else: l = srcmapa[j][0] r = srcmapa[j][1] if can(cur, d, l, r): cur += d if d==1: res += "R" else : res += "L" else: res += "X" j+=1 print res
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
r=lambda:map(int,raw_input().split()) n,m,s,f=r() T=0 c=['R','L'][f<s] p='' o=0 for i in range(m): #print s,f,p t,L,R=r() if T < t-1: v=min(t-1-T, abs(f-s)) s+=v*(-1)**(f-s<0) p+=v*c #print "t:",v,s if f == s: break #print "sp:", L - (f-s>0), R + (f-s<0) if s >= L - (f-s>0) and s <= R + (f-s<0): p+='X' else: p+=c s+=(-1)**(f-s<0) #print "c:",(-1)**(f-s<0) if f == s: break T=t if f!=s: p+=abs(f-s)*c print p
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> #pragma comment(linker, "/STACK:96777216") using namespace std; int main() { int N, M, s, f; cin >> N >> M >> s >> f; int cur = s; int time = 1; int T[100000], L[100000], R[100000], p = 0; for (int i = 0; i < M; i++) cin >> T[i] >> L[i] >> R[i]; while (cur != f) { if (time == T[p]) { if ((L[p] <= cur && R[p] >= cur) || (cur > f && R[p] == cur - 1) || (cur < f && L[p] == cur + 1)) { cout << "X"; p++; } else { if (cur > f) { cout << "L"; cur--; } else { cout << "R"; cur++; } p++; } } else { if (cur > f) { cout << "L"; cur--; } else { cout << "R"; cur++; } } time++; } 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.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.util.InputMismatchException; import java.util.Arrays; import java.util.Vector; public class XAS { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReaderXAS in = new InputReaderXAS(inputStream); OutputWriterXAS out = new OutputWriterXAS(outputStream); int n=in.readInt();int m=in.readInt();int s=in.readInt();int f=in.readInt(); int[][] spy=new int[m][3]; int i,inc; int step=1; char push; Vector V=new Vector(); for(i=0;i<m;i++) { spy[i][0]=in.readInt();spy[i][1]=in.readInt();spy[i][2]=in.readInt(); } if(s<f) { inc=1; push='R'; } else { inc=-1; push='L'; } int comp=spy[0][0]; int ind=0; while(s!=f) { if(step==comp) { if(spy[ind][1]<=s && s<=spy[ind][2] || spy[ind][1]<=s+inc && s+inc<=spy[ind][2] ) { V.add('X'); } else { s+=inc; V.add(push); } ind++; if(ind<m) comp=spy[ind][0]; else comp=-1; } else { s+=inc; V.add(push); } step++; } for(i=0;i<V.size();i++) out.print(V.get(i)); out.printLine(); out.close(); } } class InputReaderXAS { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReaderXAS(InputStream stream) { this.stream = stream; } private 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 long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriterXAS { private final PrintWriter writer; public OutputWriterXAS(OutputStream outputStream) { writer = new PrintWriter(outputStream); } public OutputWriterXAS(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 printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.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
//package round199; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class B { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(), m = ni(), s = ni()-1, f = ni()-1; int[][] q = new int[m][]; for(int i = 0;i < m;i++){ q[i] = new int[]{ni(), ni()-1, ni()-1}; } int p = 0; for(int i = 1;s != f;i++){ if(p < m && q[p][0] == i){ int nex = s < f ? s+1 : s-1; if( (q[p][1] <= s && s <= q[p][2]) || (q[p][1] <= nex && nex <= q[p][2])){ out.print("X"); }else{ if(f > s){ out.print("R"); s++; }else{ out.print("L"); s--; } } p++; }else{ if(f > s){ out.print("R"); s++; }else{ out.print("L"); s--; } } } out.println(); } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new B().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
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> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; string ans; map<int, int> ml, mr; int main() { int i, j, k, l; int n, m, s, f; while (scanf("%d%d%d%d", &n, &m, &s, &f) != EOF) { ml.clear(); mr.clear(); ans.clear(); for (i = 1; i <= m; i++) { scanf("%d%d%d", &j, &k, &l); ml[j] = k; mr[j] = l; } int mark = 0; while (s != f) { mark++; int next_; if (s < f) next_ = s + 1; else next_ = s - 1; if ((s >= ml[mark] && s <= mr[mark]) || (next_ >= ml[mark] && next_ <= mr[mark]) && ml[mark] + mr[mark]) { ans += 'X'; continue; } if (s < f) { s++; ans += 'R'; } else { s--; ans += 'L'; } } cout << ans << 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, flag = 0, flagi = 0; cin >> n >> m >> s >> f; if (s > f) flag = 1; for (int i = 1; i <= m; i++) { int a, b, c; cin >> a >> b >> c; for (int j = i; j < a; j++, i++) { if (s == f) { break; flagi = 1; } if (flag == 1) { cout << "L"; s = s - 1; } else { cout << "R"; s = s + 1; } } if (s == f || flagi == 1) break; if (flag == 1) { if ((s > c || s < b) && (s - 1 > c || s - 1 < b)) { s = s - 1; cout << "L"; } else cout << "X"; } else { if ((s > c || s < b) && (s + 1 > c || s + 1 < b)) { s = s + 1; cout << "R"; } else cout << "X"; } flagi = 0; } while (s != f) { if (flag == 1) { cout << "L"; s--; } else { cout << "R"; s++; } } cout << endl; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.HashMap; import java.util.InputMismatchException; import java.io.IOException; 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); Spies solver = new Spies(); solver.solve(1, in, out); out.close(); } static class Spies { 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(); long max = Integer.MIN_VALUE; HashMap<Integer, Step> map = new HashMap<>(); for (int i = 0; i < m; i++) { int t = in.nextInt(); map.put(t, new Step(in.nextInt(), in.nextInt())); } int start = 1; StringBuilder sb = new StringBuilder(""); while (true) { if (map.containsKey(start)) { if ((!notIn(s, map.get(start).l, map.get(start).r)) && (!notIn(getNext(s, f), map.get(start).l, map.get(start).r))) { if (s < f) { sb.append("R"); } else { sb.append("L"); } s = getNext(s, f); } else { sb.append("X"); } } else { if (s < f) { sb.append("R"); } else { sb.append("L"); } s = getNext(s, f); } if (s == f) { out.println(sb.toString()); break; } start++; } } private int getNext(int s, int f) { if (s > f) { return s - 1; } else { return s + 1; } } private boolean notIn(int num, int l, int r) { if (num >= l && num <= r) { return true; } else return false; } class Step { int l; int r; public Step(int l, int r) { this.l = l; this.r = r; } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.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 nextInt() { 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 isWhitespace(c); } public static boolean isWhitespace(int 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; int a[100010][3]; bool inside(int x, int a, int b) { return a <= x && x <= b; } void move_f(int& s, int l, int r, int n, string& result) { if (s + 1 <= n && !inside(s + 1, l, r)) { result += "R"; ++s; } else { result += "X"; } } void move_b(int& s, int l, int r, int n, string& result) { if (s - 1 >= 1 && !inside(s - 1, l, r)) { result += "L"; --s; } else { result += "X"; } } int main(int argc, char** argv) { ios_base::sync_with_stdio(false); int n, m, s, f; cin >> n >> m >> s >> f; for (int i = 0; i < m; ++i) { cin >> a[i][0] >> a[i][1] >> a[i][2]; } string result = ""; int cur_t = 1; for (int i = 0; i < m && s != f; ++i) { int t = a[i][0]; int l = a[i][1]; int r = a[i][2]; if (cur_t == t) { if (inside(s, l, r)) { result += "X"; } else { if (s < f) { move_f(s, l, r, n, result); } else { move_b(s, l, r, n, result); } } ++cur_t; } else { int diff = t - cur_t + (i == 0 ? 0 : 1); int dist = s - f; int sz = min(abs(dist), diff); if (dist < 0) { result += string(sz, 'R'); s += sz; } else { result += string(sz, 'L'); s -= sz; } cur_t = t + (i == 0 ? 0 : 1); if (i == 0) { --i; } } } if (s != f) { if (s < f) { result += string(f - s, 'R'); } else { result += string(s - f, 'L'); } } cout << result << "\n"; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; bool find(int l, int m, int r) { if (m < l) return 1; if (m > r) return 1; return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int m, n, s, f, i, ct = 1, t, l, r, cp; cin >> n >> m >> s >> f; cp = s; for (i = 0; i < m; i++) { cin >> t >> l >> r; if (cp == f) continue; while (ct < t && cp != f) { if (cp > f) { cout << "L"; cp--; } else if (cp < f) { cout << "R"; cp++; } else cout << "X"; ct++; } while (ct == t && cp != f) { if (cp > f && find(l, cp, r) && find(l, cp - 1, r)) { cout << "L"; cp--; } else if (cp < f && find(l, cp, r) && find(l, cp + 1, r)) { cout << "R"; cp++; } else cout << "X"; ct++; } } while (cp != f) { if (cp > f) { cout << "L"; cp--; } else if (cp < f) { cout << "R"; cp++; } else cout << "X"; ct++; } }
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 = 1000010; const int INF = 0x3f3f3f3f; const int MOD = 100000, STA = 8000010; const long long LNF = 1LL << 60; const double EPS = 1e-8; const double OO = 1e15; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, 1, 0, -1}; const int day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline int sign(double x) { return (x > EPS) - (x < -EPS); } template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template <class T> inline T lcm(T a, T b, T d) { return a / d * b; } template <class T> inline T Min(T a, T b) { return a < b ? a : b; } template <class T> inline T Max(T a, T b) { return a > b ? a : b; } template <class T> inline T Min(T a, T b, T c) { return min(min(a, b), c); } template <class T> inline T Max(T a, T b, T c) { return max(max(a, b), c); } template <class T> inline T Min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); } template <class T> inline T Max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); } int a[N]; struct pa { int l, r; } nod[N]; int main() { int i, n, m, s, f, b; scanf("%d%d%d%d", &n, &m, &s, &f); bool flag = true; if (s < f) b = 1; else b = -1; for (i = 0; i < N; i++) { nod[i].l = -1; nod[i].r = -1; } for (i = 0; i < m; i++) { int t, l, r; scanf("%d%d%d", &t, &l, &r); if (t >= N) continue; nod[t].l = l; nod[t].r = r; } for (i = 1;; i++) { if (s == f) break; if (nod[i].l == -1 && nod[i].r == -1) { if (s + b >= 1 && s + b <= n) { printf("%s", b == 1 ? "R" : "L"); b == 1 ? s++ : s--; } } else { if (s >= nod[i].l && s <= nod[i].r) { printf("X"); } else if (s + b >= 1 && s + b <= n) { if (s + b >= nod[i].l && s + b <= nod[i].r) { printf("X"); } else { printf("%s", b == 1 ? "R" : "L"); b == 1 ? s++ : 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.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException{ Reader.init(System.in); PrintWriter out = new PrintWriter(System.out); TaskB solver = new TaskB(); solver.solve(out); out.close(); } } class TaskA{ int[] result; ArrayList<track> list; void solve(PrintWriter out) throws IOException{ result = new int[8]; list = new ArrayList<track>(); int n = Reader.nextInt(); for(int i=0; i<n; i++) result[Reader.nextInt()] ++; cut(1,3,6); cut(1,2,6); cut(1,2,4); for(int x : result) if(x != 0) { out.println("-1"); return ; } print(out); } void cut(int a,int b,int c){//use a ,cut b&c int min = (result[a] < result[b]) ? result[a] : result[b]; min = (min < result[c]) ? min : result[c]; if(min == 0) return; result[a] -= min; result[b] -= min; result[c] -= min; list.add(new track(a,b,c,min)); } class track{ int a,b,c,min; track(int a,int b,int c,int min){ this.a = a; this.b = b; this.c = c; this.min = min; } } void print(PrintWriter out){ for(track t : list) for(int i=0; i<t.min; i++) out.printf("%d %d %d\n",t.a, t.b, t.c); } } class TaskB{ List<Seg> list; void solve(PrintWriter out) throws IOException{ int n = Reader.nextInt(); int m = Reader.nextInt(); int s = Reader.nextInt(); int f = Reader.nextInt(); list = new ArrayList<Seg>(); for(int i=0; i<m; i++) list.add(new Seg(Reader.nextInt(), Reader.nextInt(), Reader.nextInt())); int currTime = 0; int nextStep = s; int currPtr = 0; int step = Integer.signum(f-s); while(nextStep != f){ nextStep += step; currTime += 1; //out.printf("-step= %d time= %d\n", nextStep, currTime);// boolean canMove = true; if(currPtr < list.size()){ Seg nextSeg = list.get(currPtr); if(nextSeg.time == currTime){ canMove = nextSeg.canMove(nextStep-step) && nextSeg.canMove(nextStep);//songchu & jieshou currPtr ++; } } if(canMove){ if(step>0) out.print('R'); else out.print('L'); } else{ nextStep -= step; out.print('X'); } } } class Seg{ int time,l,r; Seg(int t,int l,int r){ this.time = t; this.l = l; this.r = r; } boolean canMove(int p){//true = can return !(p>=l && p<=r); } } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ 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 long nextLong() throws IOException { return Long.parseLong(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; scanf("%d%d%d%d", &n, &m, &s, &f); if (s < f) { int e = s, st = 1; string ss; while (m--) { int t, l, r; scanf("%d%d%d", &t, &l, &r); if (st < t) for (int i = st; i < t; i++) if (e < f) { ss += "R"; e++; } st = t; st++; if (e >= l - 1 && e <= r && e < f) ss += "X"; else if (e < f) { ss += "R"; e++; } } if (e < f) for (int i = e; i < f; i++) ss += "R"; cout << ss << endl; } else { int e = s, st = 1; string ss; while (m--) { int t, l, r; scanf("%d%d%d", &t, &l, &r); if (st < t) for (int i = st; i < t; i++) if (e > f) { ss += "L"; e--; } st = t; st++; if (e >= l && e <= r + 1 && e > f) ss += "X"; else if (e > f) { ss += "L"; e--; } } if (e > f) for (int i = e; i > f; i--) ss += "L"; cout << ss << 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; cin >> n >> m >> s >> f; int a[m][3]; for (int i = 0; i < m; i++) { cin >> a[i][0] >> a[i][1] >> a[i][2]; } int k = 0; for (int i = 0;; i++) { if (s == f) { break; } if (f > s) { if (a[k][0] == i + 1) { if (((a[k][1] <= s + 1) && (a[k][2] >= s + 1)) || ((a[k][1] <= s) && (a[k][2] >= s))) { cout << "X"; k++; continue; } k++; } s++; cout << "R"; } else { if (a[k][0] == i + 1) { if (((a[k][1] <= s - 1) && (a[k][2] >= s - 1)) || ((a[k][1] <= s) && (a[k][2] >= s))) { cout << "X"; k++; continue; } k++; } s--; cout << "L"; } } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int t[maxn], l[maxn], r[maxn]; int main() { int n, m, from, to, dir; while (scanf("%d%d%d%d", &n, &m, &from, &to) != EOF) { for (int i = 0; i < m; i++) { scanf("%d%d%d", &t[i], &l[i], &r[i]); } dir = to > from ? 1 : -1; char a = to > from ? 'R' : 'L'; int now = from, nc = 1, p = 0; while (now != to && p < m) { while (nc < t[p] && now != to) { nc++; now += dir; printf("%c", a); } if (nc == t[p] && now != to) { if (now >= l[p] && now <= r[p] || (now + dir >= l[p] && now + dir <= r[p])) { printf("X"); p++; nc++; } else { nc++; now += dir; printf("%c", a); p++; } } } for (; now != to; now += dir) printf("%c", a); 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 x = 1, y = 0, z = 0, a, b, c, d, e, f, g; cin >> a >> b >> c >> d; while (c != d) { scanf("%d%d%d", &e, &f, &g); while (x != e) { if (c == d) { return 0; } if (c < d) { printf("R"); c++; } else { printf("L"); c--; } x++; } if (c == d) { return 0; } if (c < d) { if ((c + 1 >= f && c + 1 <= g) || (c >= f && c <= g)) { printf("X"); } else { printf("R"); c++; } } else { if ((c - 1 >= f && c - 1 <= g) || (c >= f && c <= g)) { printf("X"); } else { printf("L"); c--; } } x++; } }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s, f; cin >> n >> m >> s >> f; int t = 1, x = s; int dir = (x < f) ? 1 : -1; while (m-- > 0) { int t_in, l, r; cin >> t_in >> l >> r; for (; t <= t_in; t++) { if (t != t_in || !(l <= x && x <= r) && !(l <= x + dir && x + dir <= r)) { cout << (dir == -1 ? "L" : "R"); x += dir; } else cout << "X"; if (x == f) return 0; } } for (; x != f; x += dir) cout << (dir == -1 ? "L" : "R"); return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.Scanner; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author KHALED */ public class XeniaandSpies { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int s = sc.nextInt(); int f = sc.nextInt(); int[][] a = new int[m][3]; for (int i = 0; i < m; i++) { for (int j = 0; j < 3; j++) { a[i][j] = sc.nextInt(); } } int pos; String lr; if (s > f) { pos = -1; lr = "L"; } else { pos = 1; lr = "R"; } int count = 0; int i = 0; while( s != f) { i++; if (count < m && a[count][0] == i){ if ((a[count][1] > s || a[count][2] < s) && (a[count][1] > s + pos || a[count][2] < s + pos) ){ System.out.print(lr); s += pos; } else { System.out.print("X"); } count++; } else { System.out.print(lr); s += pos; } } sc.close(); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s, f; scanf("%d%d%d%d", &n, &m, &s, &f); if (s < f) { int cur = s; int pret = 0; while (m--) { int t, l, r; scanf("%d%d%d", &t, &l, &r); if (cur >= f) continue; while (pret + 1 < t) { cur++; pret++; putchar('R'); if (cur >= f) break; } if (cur >= f) continue; pret = t; if ((l <= cur && cur <= r) || (l <= cur + 1 && cur + 1 <= r)) putchar('X'); else { cur++; putchar('R'); } } while (cur < f) { putchar('R'); cur++; } } else { int cur = s; int pret = 0; while (m--) { int t, l, r; scanf("%d%d%d", &t, &l, &r); if (cur <= f) continue; while (pret + 1 < t) { cur--; pret++; putchar('L'); if (cur <= f) break; } if (cur <= f) continue; pret = t; if ((l <= cur && cur <= r) || (l <= cur - 1 && cur - 1 <= r)) putchar('X'); else { cur--; putchar('L'); } } while (cur > f) { putchar('L'); cur--; } } 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 i, j, n, t; int m; scanf("%d", &n); scanf("%d", &m); int s, e; scanf("%d", &s); scanf("%d", &e); map<int, pair<int, int> > a; int p, q; for (int i = (int)1; i < (int)m + 1; ++i) { scanf("%d", &t); scanf("%d", &p); scanf("%d", &q); a[t] = make_pair(p, q); } t = 1; int d; if (s > e) d = -1; else d = 1; int c; c = s; p = c + d; char ch; if (d == 1) ch = 'R'; else ch = 'L'; while (1) { if (a.find(t) != a.end()) { if (!(c >= a[t].first && c <= a[t].second) && !(p >= a[t].first && p <= a[t].second)) { printf("%c", ch); c = p; p = c + d; if (c == e) break; } else printf("X"); } else { printf("%c", ch); c = p; p = c + d; if (c == e) break; } t++; } 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 T[100000]; int L[100000]; int R[100000]; bool notLie(int a, int l, int r) { return (!(a >= l && a <= r)); } int main() { int n, m, s, f; scanf("%d %d %d %d", &n, &m, &s, &f); for (int i = 0; i < m; i++) scanf("%d %d %d", &T[i], &L[i], &R[i]); int curTime = 1; int curPos = s; int ch = (f > s) ? 'R' : 'L'; int inc = (f > s) ? 1 : -1; for (int i = 0; i < m;) { if (T[i] > curTime) { printf("%c", ch); curPos += inc; curTime++; if (curPos == f) break; } else { if (notLie(curPos, L[i], R[i]) && notLie(curPos + inc, L[i], R[i])) { printf("%c", ch); curPos += inc; curTime++; i++; if (curPos == f) break; } else { printf("%c", 'X'); curTime++; i++; } } } while (curPos != f) { printf("%c", ch); curPos += inc; } 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() { ios::sync_with_stdio(false); string ans = ""; long long time, n, m, s, f; cin >> n >> m >> s >> f; vector<int> t(m), l(m), r(m); for (long long i = 0; i < m; i++) { cin >> t[i] >> l[i] >> r[i]; } if (s < f) { time = 1; vector<int>::iterator it; while (1) { it = lower_bound((t).begin(), (t).end(), time); if (it == t.end()) { ans += string(f - s, 'R'); cout << ans; return 0; } if (*it > time) { if (f - s <= *it - time) { ans += string(f - s, 'R'); cout << ans; return 0; } else { s += *it - time; ans += string(*it - time, 'R'); time = *it; } } if ((s >= l[it - t.begin()] && s <= r[it - t.begin()]) || (s + 1 <= r[it - t.begin()] && s + 1 >= l[it - t.begin()])) { ans += "X"; } else { ans += "R"; s++; if (f == s) { cout << ans; return 0; } } time++; } } else { time = 1; vector<int>::iterator it; while (1) { it = lower_bound((t).begin(), (t).end(), time); if (it == t.end()) { ans += string(s - f, 'L'); cout << ans; return 0; } if (*it > time) { if (s - f <= *it - time) { ans += string(s - f, 'L'); cout << ans; return 0; } else { s -= *it - time; ans += string(*it - time, 'L'); time = *it; } } if ((s >= l[it - t.begin()] && s <= r[it - t.begin()]) || (s - 1 <= r[it - t.begin()] && s - 1 >= l[it - t.begin()])) { ans += "X"; } else { ans += "L"; s--; if (f == s) { cout << ans; return 0; } } time++; } } 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 long long INF = 1ll << 32; int n, m, s, e; string ans = ""; bool ch(int l, int r, int idx, bool b) { bool re = 1; if (l <= idx && idx <= r) re = 0; if (b) { if (l <= idx + 1 && idx + 1 <= r) re = 0; } else if (l <= idx - 1 && idx - 1 <= r) re = 0; return re; } int main() { scanf("%d %d %d %d", &n, &m, &s, &e); map<int, pair<int, int>> mp; for (int i = 0; i < m; i++) { int a; scanf("%d", &a); scanf("%d %d", &mp[a].first, &mp[a].second); } int j = 1; if (s < e) { for (int i = s; i < e; i++) { if (mp[j].first) { if (ch(mp[j].first, mp[j].second, i, 1)) ans += "R"; else { ans += "X"; i--; } j++; } else { while (!mp[j].first && i < e) { ans += "R"; i++; j++; } i--; } } } else { for (int i = s; i > e; i--) { if (mp[j].first) { if (ch(mp[j].first, mp[j].second, i, 0)) ans += "L"; else { ans += "X"; i++; } j++; } else { while (!mp[j].first && i > e) { ans += "L"; i--; j++; } i++; } } } cout << ans << 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 n, m, s, e, top; bool dis; struct query { int t, l, r; } q[100005]; int main() { int i, j; scanf("%d%d%d%d", &n, &m, &s, &e); for (i = 0; i < m; i++) { scanf("%d%d%d", &q[i].t, &q[i].l, &q[i].r); } for (i = 1;; i++) { if (q[top].t == i && q[top].l <= s && q[top].r >= s) { printf("X"); } else { if (s > e) { if (q[top].l <= s - 1 && q[top].r >= s - 1 && q[top].t == i) printf("X"); else { printf("L"); s--; } } else { if (q[top].l <= s + 1 && q[top].r >= s + 1 && q[top].t == i) printf("X"); else { printf("R"); s++; } } } if (q[top].t == i) top++; if (s == e) 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
import sys input = sys.stdin.readline def print(val): sys.stdout.write(str(val) + '\n') def prog(): n,m,s,f = map(int,input().split()) curr = s step = 1 steps = {} output = [] for i in range(m): t,l,r = map(int,input().split()) steps[t] = [l,r] if f > s: while curr != f: if step not in steps: output.append('R') step += 1 curr += 1 elif curr + 1 < steps[step][0] or curr > steps[step][1]: output.append('R') step += 1 curr += 1 else: output.append('X') step += 1 else: while curr != f: if step not in steps: output.append('L') step += 1 curr -= 1 elif curr - 1 > steps[step][1] or curr < steps[step][0]: output.append('L') step += 1 curr -= 1 else: output.append('X') step += 1 print(''.join(output)) prog()
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
while True: try: def soln(n, m, s, t, stp): stp.sort() flg = True if s > t: flg = False i = 1 j = 0 ans = [] while True: fund = False while j < m: if stp[j][0] == i: j += 1 fund = True break if fund and not flg: a= max(stp[j-1][1], stp[j-1][2]) b = min(stp[j-1][1], stp[j-1][2]) if ( s <= a) and ( s >=b): ans.append('X') elif s-1 <= a and s-1 >=b: ans.append('X') else: ans.append('L') s -= 1 elif fund and flg: a= min(stp[j-1][1], stp[j-1][2]) b = max(stp[j-1][1], stp[j-1][2]) if ( s >=a) and ( s <=b): ans.append('X') elif ( s+1 >=a) and ( s+1 <=b): ans.append('X') else: ans.append('R') s += 1 elif flg: ans.append("R") s += 1 else: ans.append("L") s -= 1 if s == t: break i += 1 print("".join(ans)) def read(): n, m, s, t = map(int,input().split()) stp = [] for i in range(m): a, b, c = map(int, input().split()) stp.append([a,b,c]) soln(n, m, s, t, stp) if __name__ == "__main__": read() except EOFError: break
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.*; public class Codeforces{ public static void main(String[] args) { FastScanner sc =new FastScanner(); PrintWriter out=new PrintWriter(System.out); int n=sc.nextInt(),m=sc.nextInt(),start=sc.nextInt(),end=sc.nextInt(); Map<Integer,Pair> map=new HashMap<>(); for(int i=0;i<m;i++){ map.put(sc.nextInt(),new Pair(sc.nextInt(),sc.nextInt())); } StringBuilder str =new StringBuilder(); if(start<=end){ int i=1; while(start<end){ if(map.containsKey(i)){ Pair temp=map.get(i); if((temp.a<=start && temp.b>=start) || (temp.a<=start+1 && temp.b>=start+1)){ str.append('X'); } else{ start++; str.append('R'); } } else{ start++; str.append('R'); } i++; } } else{ int i=1; while(start>end){ if(map.containsKey(i)){ Pair temp=map.get(i); if((temp.a<=start && temp.b>=start) || (temp.a<=start-1 && temp.b>=start-1)){ str.append('X'); } else{ start--; str.append('L'); } } else{ start--; str.append('L'); } i++; } } out.println(str); out.close(); } } class Pair{ int a=0,b=0; Pair(int a ,int b){ this.a=a; this.b=b; } } class FastScanner{ BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st =new StringTokenizer(""); String next(){ if(!st.hasMoreTokens()){ try{ st=new StringTokenizer(br.readLine()); } catch(Exception e){ } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.*; import java.io.*; import java.math.BigInteger; import java.nio.charset.Charset; public class olymp { FastScanner in; PrintWriter out; public void solve() throws IOException { int n = in.nextInt(); int m = in.nextInt(); int s = in.nextInt(); int f = in.nextInt(); ArrayList<Integer[]> watch = new ArrayList<Integer[]>(); for (int i = 0; i < m; i++) { watch.add(new Integer[3]); watch.get(i)[0] = in.nextInt(); watch.get(i)[1] = in.nextInt(); watch.get(i)[2] = in.nextInt(); } int k = 0; int it = 0; if (s < f) { while (s < f) { k++; if (it < watch.size()) { if (k == watch.get(it)[0]) { if (s < watch.get(it)[1] - 1 || s > watch.get(it)[2]) { out.print("R"); s++; } else { out.print("X"); } it++; } else { out.print("R"); s++; } } else { out.print("R"); s++; } } } else { while (s > f) { k++; if (it < watch.size()) { if (k == watch.get(it)[0]) { if (s < watch.get(it)[1] || s > watch.get(it)[2] + 1) { out.print("L"); s--; } else { out.print("X"); } it++; } else { out.print("L"); s--; } } else { out.print("L"); s--; } } } } public void run() { try { in = new FastScanner(System.in); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream f) throws FileNotFoundException { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } } public static void main(String[] arg) { new olymp().run(); } }
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 = 0x3f3f3f3f; const int maxN = 1e5 + 10; int n, m, s, f; int t[maxN], l[maxN], r[maxN]; bool inside(int l, int m, int r) { return l <= m && m <= r; } void slv() { int d = f > s ? 1 : -1; m++; t[m] = inf, l[m] = inf, r[m] = -inf; int tm = 1; int p = 1; while (true) { if (s == f) break; if (tm != t[p]) s += d, printf("%c", d > 0 ? 'R' : 'L'); else { if (inside(l[p], s, r[p]) || inside(l[p], s + d, r[p])) printf("X"); else printf("%c", d > 0 ? 'R' : 'L'), s += d; p++; } tm++; } } int main() { while (~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]); slv(); } 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, i; cin >> n >> m >> s >> f; long long t; map<long long, pair<int, int>> h; pair<int, int> R; for (i = 0; i < m; i++) { cin >> t >> R.first >> R.second; h[t] = R; } i = 1; if (s < f) while (s != f) { if (h.find(i) == h.end()) { cout << 'R'; s++; } else { if ((h[i].first <= s && s <= h[i].second) || (h[i].first <= s + 1 && s + 1 <= h[i].second)) { cout << 'X'; } else { cout << 'R'; s++; } } i++; } else { while (s != f) { if (h.find(i) == h.end()) { cout << 'L'; s--; } else { if ((h[i].first <= s && s <= h[i].second) || (h[i].first <= s - 1 && s - 1 <= h[i].second)) { cout << 'X'; } else { cout << 'L'; s--; } } 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.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; import java.util.Vector; public class Main { /** * @param args * @throws IOException */ class Parser { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Parser(InputStream in) { din = new DataInputStream(in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String nextString(int maxSize) { byte[] ch = new byte[maxSize]; int point = 0; try { byte c = read(); while (c == ' ' || c == '\n' || c=='\r') c = read(); while (c != ' ' && c != '\n' && c!='\r') { ch[point++] = c; c = read(); } } catch (Exception e) {} return new String(ch, 0,point); } public int nextInt() { int ret = 0; boolean neg; try { byte c = read(); while (c <= ' ') c = read(); neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; } catch (Exception e) {} return ret; } public long nextLong() { long ret = 0; boolean neg; try { byte c = read(); while (c <= ' ') c = read(); neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; } catch (Exception e) {} return ret; } private void fillBuffer() { try { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); } catch (Exception e) {} if (bytesRead == -1) buffer[ 0] = -1; } private byte read() { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); Parser pr=new Main().new Parser(System.in); int n,m,s,f; n=pr.nextInt(); m=pr.nextInt(); s=pr.nextInt(); f=pr.nextInt(); int t,l,r; int p=1; String res=new String(""); boolean done=false; for(int i=0; i<m; i++) { t=pr.nextInt(); l=pr.nextInt(); r=pr.nextInt(); if(!done) { while(t!=p) { p++; if(s<f) { s++; System.out.print('R'); }else if(s>f) { s--; System.out.print('L'); } if(s==f) { done=true; break; } } if(!done) if(s<f&&(s>r||s<l-1)) { s++; System.out.print('R'); }else if(s>f&&(s>r+1||s<l)) { s--; System.out.print('L'); }else if(s!=f) System.out.print('X'); } p++; } if(s!=f) { if(s>f) for(int i=f; i<s; i++) System.out.print('L'); else for(int i=s; i<f; i++) System.out.print('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.*; import java.util.*; public class Solution{ public static void main(String []args){ //System.out.println("Hello World"); Solution ob=new Solution(); ob.go(); } void go() { Scanner sc=new Scanner(System.in); int step=1,t,l,r; int n = sc.nextInt(); int m = sc.nextInt(); int s = sc.nextInt(); int f = sc.nextInt(); int cur=s; if (cur==f) return; for (int i=0;i<m;i++) { t=sc.nextInt(); l=sc.nextInt(); r=sc.nextInt(); //step++; while (step!=t) { if (cur==f) return; if (cur<f) {pr("R");cur++;} else {pr("L");cur--;} step++; } if (cur==f) return; if (cur<f && (cur>r || cur+1<l)) {pr("R");cur++;} else if (cur>f && (cur<l || cur-1>r)) {pr("L");cur--;} else pr("X"); step++; if (cur==f) return; } while (true) { if (cur==f) return; if (cur<f) {pr("R");cur++;} else {pr("L");cur--;} step++; } } void pr(String s) { System.out.print(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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashMap; import java.util.StringTokenizer; /** * Created with IntelliJ IDEA. * User: Evgeniy * Date: 9/11/13 * Time: 4:22 PM */ public class Main { StringTokenizer tokenizer = new StringTokenizer(""); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) { new Main().solve(); } void solve() { int n = nextInt(); int m = nextInt(); int s = nextInt(); int f = nextInt(); HashMap<Integer, Integer> le = new HashMap<Integer,Integer>(); HashMap<Integer, Integer> ri = new HashMap<Integer,Integer>(); for (int i = 0; i < m; ++i) { int step = nextInt(); le.put(step, nextInt()); ri.put(step, nextInt()); } int cur = s; int st = 1; while (cur != f) { int l = le.containsKey(st) ? le.get(st) : 0; int r = ri.containsKey(st) ? ri.get(st) : 0; if (l <= cur && cur <= r) { System.out.print('X'); } else { int dir = f > cur ? 1 : -1; if (l <= cur + dir && cur + dir <= r) { System.out.print('X'); } else { System.out.print(dir == 1 ? 'R' : 'L'); cur += dir; } } ++st; } } String nextLine() { try { return reader.readLine(); } catch (IOException e) { return ""; } } String nextToken() { if (!tokenizer.hasMoreElements()) { tokenizer = new StringTokenizer(nextLine()); } return tokenizer.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, s, f; vector<long long> t; vector<long long> left; vector<long long> right; cin >> n >> m >> s >> f; for (int i = 0; i < m; i++) { long long ti, li, ri; cin >> ti >> li >> ri; t.push_back(ti); left.push_back(li); right.push_back(ri); } int ind = 0; int timeval = 1; while (s != f) { if (ind < t.size() && t[ind] == timeval) { if (s >= left[ind] && s <= right[ind]) cout << "X"; else if (s > f) { if (s - 1 >= left[ind] && s - 1 <= right[ind]) cout << "X"; else { cout << "L"; s--; } } else { if (s + 1 >= left[ind] && s + 1 <= right[ind]) cout << "X"; else { cout << "R"; s++; } } ind++; } else { if (s > f) { cout << "L"; s--; } else { cout << "R"; s++; } } timeval++; } }
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.*; import java.*; import java.math.BigInteger; public class zad { private static BufferedReader in; private static StringTokenizer tok; private static PrintWriter out; private static String readToken() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } private static int readInt() throws IOException { return Integer.parseInt(readToken()); } private static double readDouble() throws IOException { return Double.parseDouble(readToken()); } private static long readLong() throws IOException { return Long.parseLong(readToken()); } static int partition (int[] array, int start, int end) { int marker = start; for ( int i = start; i <= end; i++ ) { if ( array[i] <= array[end] ) { int temp = array[marker]; array[marker] = array[i]; array[i] = temp; marker += 1; } } return marker - 1; } static void quicksort (int[] array, int start, int end) { int pivot; if ( start >= end ) { return; } pivot = partition (array, start, end); quicksort (array, start, pivot-1); quicksort (array, pivot+1, end); } public static void Solve() throws IOException{ int n=readInt(); int m = readInt(); int s = readInt(); int f = readInt(); char x ='m'; int p = 0; if(s>f) { p=-1; x='L'; } else{ p=1; x='R'; } String st= ""; int en =0; int time =0; boolean e = false; for(int i =0;i<m;i++){ time++; int t=readInt(); int l= readInt(); int r = readInt(); if(e) continue; for(;time<t;time++){ s+=p; out.print(x); if(s==f){ en= time; e = true; return; } } time =t; if (e) continue; int need=s+p; if (need>=l && need<=r || s>=l && s <=r){ //st+='X'; out.print("X"); }else{ out.print(x); s=need; if(s==f){ en= time; e = true; return; } } } while(s!=f){ out.print(x); s+=p; } //out.println(st); } public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); Solve(); in.close(); 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
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n, m, s, f; cin >> n >> m >> s >> f; s--; f--; map<int, pair<int, int> > x; for (int i = 0; i < m; i++) { int t, l, r; cin >> t >> l >> r; x[t] = {l - 1, r - 1}; } string ans = ""; for (int i = 1; i <= m + n + 1; i++) { if (x.count(i)) { int l = x[i].first; int r = x[i].second; if (s > f) { if ((s > r || s < l) && (s - 1 > r || s - 1 < l)) { s--; ans += 'L'; } else ans += 'X'; } else { if ((s > r || s < l) && (s + 1 > r || s + 1 < l)) { s++; ans += 'R'; } else ans += 'X'; } } else { if (s < f) { s++; ans += 'R'; } if (s > f) { s--; ans += 'L'; } } if (s == f) break; } cout << ans << endl; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
n, m, s, f = map(int, raw_input().split()); now = 1; ret = ''; for i in range(m): t, l, r = map(int, raw_input().split()); if (s < f): while (s < f and now < t): s += 1; now += 1; ret += 'R'; if (s == f): continue; else: if (s >= l - 1 and s <= r): ret += 'X'; else: ret += 'R'; s += 1; now += 1; else: while (s > f and now < t): s -= 1; now += 1; ret += 'L'; if (s == f): continue; else: if (s >= l and s <= r + 1): ret += 'X'; else: ret += 'L'; s -= 1; now += 1; while (s < f): s += 1; ret += 'R'; while (s > f): s -= 1; ret += 'L'; print ret;
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 adel { int pos; int end; int dir; int cc; char car; void assign(int a, int b) { if (a < b) pos = a, end = b, dir = 1, car = 'R'; else pos = a, end = b, dir = -1, car = 'L'; cc = abs(a - b); } bool qua(int a, int b) { if (!cc) exit(0); if ((a > pos || pos > b) && (a > pos + dir || pos + dir > b)) { pos += dir; cc--; printf("%c", car); return true; } printf("%c", 'X'); return false; } bool comblate() { return !cc; } int size() { return cc; } int poos() { return pos; } }; int main() { int n, m, a, b; scanf("%d%d%d%d", &n, &m, &a, &b); adel ad; ad.assign(a, b); for (int x, l, r, lastx = 0, i = 0; i < m; i++) { scanf("%d%d%d", &x, &l, &r); if (x - lastx > 1) for (; lastx + 1 < x; lastx++) ad.qua(-1, -1); ad.qua(l, r); lastx = x; } while (!ad.comblate()) { ad.qua(-1, -1); } }
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; using ll = long long; using vi = vector<int>; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, s, f; cin >> n >> m >> s >> f; int ct = 1; while (m--) { int t, l, r; cin >> t >> l >> r; while (ct < t && s != f) { if (s < f) cout << "R", s++; else cout << "L", s--; ct++; } if (s != f) { if (s + (s < f) >= l && s - (s > f) <= r) cout << "X"; else { if (s < f) cout << "R", s++; else cout << "L", s--; } } ct++; } while (s != f) { if (s < f) cout << "R", s++; else cout << "L", s--; ct++; } }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main{ public static void main(String args[]) { MyScanner scanner = new MyScanner(); int n = scanner.nextInt(), m = scanner.nextInt(), s = scanner.nextInt(), f = scanner.nextInt(), t, l ,r, pre = 1, temp; StringBuilder str = new StringBuilder(""); for( int i = 0; i < m && s !=f ; i++ ) { t = scanner.nextInt(); l = scanner.nextInt(); r = scanner.nextInt(); int diff = t - pre; if( s < f ) { temp = Math.min( diff, f - s); s += temp; for( int j = 0; j < temp ; j++ ) str.append("R"); if( s != f && !(s>= l && s<=r) && !( s+1 >= l && s+1<=r) ) { s++; str.append("R"); } else if( s != f){ str.append("X"); } } else if( f < s ) { temp = Math.min( diff, s - f); s -= temp; for( int j = 0; j < temp ; j++ ) str.append("L"); if( s != f && !(s>= l && s<=r) && !( s-1 >= l && s-1<=r) ) { s--; str.append("L"); } else if( s!= f ){ str.append("X"); } } pre = t + 1; } if( s!=f ) { while ( s != f ) { if( s < f ){ str.append("R"); s++; } else{ str.append("L"); s--; } } } System.out.println(str); } } class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public int mod(long x) { // TODO Auto-generated method stub return (int) x % 1000000007; } public int mod(int x) { return x % 1000000007; } boolean hasNext() { if (st.hasMoreElements()) return true; try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.hasMoreTokens(); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public long nextLong() { 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
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Scanner; public class Main { static int tPos; public static int mover(int s, int f, int mat[][], int t){ if((mat[tPos][1]<=s)&&(mat[tPos][2]>=s)&&(mat[tPos][0]==t)){ tPos++; return 0;} else{ if(mat[tPos][0]!=t){ if(s<f) return (1); else return(-1); } else { if((s<f)&&(mat[tPos][1]!=s)&&(mat[tPos][2]!=s+1)&&(mat[tPos][2]!=s)&&(mat[tPos][1]!=s+1)){ tPos++; return 1; } else { if((s>f)&&(mat[tPos][1]!=s)&&(mat[tPos][2]!=s-1)&&(mat[tPos][2]!=s)&&(mat[tPos][1]!=s-1)){ tPos++; return -1; } else { tPos++; return 0; } } } } } public static void main(String[] args) { int n,m,s,f; BufferedReader br; Scanner sc=new Scanner(br=new BufferedReader(new InputStreamReader(System.in))); n=sc.nextInt(); m=sc.nextInt(); s=sc.nextInt(); f=sc.nextInt(); int t=1; tPos=1; //ArrayList<Vec> mat=new ArrayList<Vec>(); int mat[][]=new int[m +2][3]; for(int i=1;i<m + 1;i++){ mat[i][0]=sc.nextInt(); mat[i][1]=sc.nextInt(); mat[i][2]=sc.nextInt(); } while(s!=f){ int res = mover(s,f,mat,t); if(res==1){ System.out.print('R'); s++; } else{ if(res==-1){ System.out.print('L'); s--;} else { System.out.print('X'); } } t++; } } //anda esto loco! }
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.lang.reflect.Array; import java.util.*; import java.math.*; import java.net.*; public class Task{ public static void main(String[] args) throws IOException{ new Task().run(); } StreamTokenizer in; Scanner ins; PrintWriter out; int nextInt() throws IOException{ in.nextToken(); return (int)in.nval; } long nextLong() throws IOException{ in.nextToken(); return (long)in.nval; } double nextDouble() throws IOException{ in.nextToken(); return in.nval; } char nextChar() throws IOException{ in.nextToken(); return (char)in.ttype; } String nextString() throws IOException{ in.nextToken(); return in.sval; } long gcdLight(long a, long b){ a = Math.abs(a); b = Math.abs(b); while(a != 0 && b != 0){ if(a > b) a %= b; else b %= a; } return a + b; } Task.ForGCD gcd(int a,int b){ Task.ForGCD tmp = new Task.ForGCD(); if(a == 0){ tmp.x = 0; tmp.y = 1; tmp.d = b; }else{ Task.ForGCD tmp1 = gcd(b%a, a); tmp.d = tmp1.d; tmp.y = tmp1.x; tmp.x = tmp1.y - tmp1.x*(b/a); } return tmp; } void run() throws IOException{ in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); ins = new Scanner(System.in); out = new PrintWriter(System.out); try{ if(System.getProperty("xDx")!=null){ in = new StreamTokenizer(new BufferedReader(new FileReader("input.txt"))); ins = new Scanner(new FileReader("input.txt")); out = new PrintWriter(new FileWriter("output.txt")); } }catch(Exception e){ } int n = nextInt(), m = nextInt(), s = nextInt(), f = nextInt(); See[] rounds = new See[m]; for(int i = 0; i < m; i++){ rounds[i] = new See(nextInt(), nextInt(), nextInt()); } int step = 1; char ch = 'R'; int cur = s; if(f < s){ ch = 'L'; step = -1; } int curR = 0; int curT = 1; while(cur != f){ if(curR == m || curT < rounds[curR].t){ out.print(ch); cur += step; }else{ See tmpR = rounds[curR++]; int newPos = cur + step; if(tmpR.l <= cur && cur <= tmpR.r || tmpR.l <= newPos && newPos <= tmpR.r){ out.print("X"); }else{ out.print(ch); cur += step; } } curT++; } out.close(); } class See{ public int t, l, r; public See(int t, int l, int r){ this.t = t; this.l = l; this.r = r; } } class ForGCD{ int x,y,d; } class Boxes implements Comparable{ public long k,a; public Boxes(long k, long a){ this.k = k; this.a = a; } public int compareTo(Object obj){ Task.Boxes b = (Task.Boxes) obj; if(k < b.k) return -1; else if(k == b.k) return 0; else return 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 java.io.*; import java.util.*; public class spies { 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
import java.awt.Point; 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.util.ArrayList; import java.util.StringTokenizer; public class B { static StringTokenizer st; static BufferedReader br; static PrintWriter pw; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n = nextInt(); int m = nextInt(); int s = nextInt(); int f = nextInt(); ArrayList<Point>[]beasy = new ArrayList[2*n+1]; for (int i = 1; i <= 2*n; i++) { beasy[i] = new ArrayList<Point>(); } for (int i = 1; i <= m; i++) { int t = nextInt(); int L = nextInt(); int R = nextInt(); if (t <= 2*n) beasy[t].add(new Point(L, R)); } for (int time = 1; time <= 2*n; time++) { boolean can = true; if (s==f) break; if (f > s) { for (Point p : beasy[time]) { if (s >= p.x && s <= p.y || s+1 >= p.x && s+1 <= p.y) { can = false; break; } } if (can) { s++; pw.print('R'); } else pw.print('X'); } else { for (Point p : beasy[time]) { if (s >= p.x && s <= p.y || s-1 >= p.x && s-1 <= p.y) { can = false; break; } } if (can) { s--; pw.print('L'); } else pw.print('X'); } } pw.close(); } private static int nextInt() throws IOException { return Integer.parseInt(next()); } private static long nextLong() throws IOException { return Long.parseLong(next()); } private static double nextDouble() throws IOException { return Double.parseDouble(next()); } private static String next() throws IOException { while (st==null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); 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.*; import java.io.*; import java.math.*; public class B { void solve() { int n = sc.nextInt(); int m = sc.nextInt(); int s = sc.nextInt(); int f = sc.nextInt(); int T[] = new int[m]; int L[] = new int[m]; int R[] = new int[m]; for (int i = 0; i < m; i++) { T[i] = sc.nextInt(); L[i] = sc.nextInt(); R[i] = sc.nextInt(); } int j = 0; int d = s < f ? 1 : -1; for (int i = 1; s != f; i++) { if (T[j] < i && j < m - 1) j++; if (T[j] == i) { // out.println(i +" " + j +" " + ( (L[j] <= s && s <= R[j]) || (L[j] <= s + d && s + d <= R[j]) ) ); } if (!(T[j] == i && ( (L[j] <= s && s <= R[j]) || (L[j] <= s + d && s + d <= R[j]) ) ) ) { if (s < f) { out.print("R"); } else { out.print("L"); } s += d; } else { out.print("X"); } } } FastScanner sc; PrintWriter out; public static void main(String[] args) { (new B()).run(); } void run() { sc = new FastScanner(); out = new PrintWriter(System.out); solve(); out.flush(); } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } String nextStr() { return 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.*; import java.io.*; /* * author:yanghui */ public class B { class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public InputReader() throws FileNotFoundException { reader = new BufferedReader(new FileReader("d:/input.txt")); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } public class node{ int left , right; public node(int left ,int right){ this.left = left; this.right = right; } } public void run() { InputReader reader = new InputReader(System.in); int n = reader.nextInt(); int m = reader.nextInt(); int s = reader.nextInt(); int f = reader.nextInt(); TreeMap<Integer,node> map = new TreeMap<Integer,node>(); for(int i = 1 ; i <= m ;i ++){ int t = reader.nextInt(); int left = reader.nextInt(); int right = reader.nextInt(); map.put(t, new node(left,right)); } if(s > f){ int cnt = 1; for(int i = s ; i > f ; cnt++){ if(!map.containsKey(cnt)){ System.out.print("L"); i --; } else{ node tmp = map.get(cnt); if(i>= tmp.left && i <= tmp.right || (i-1>=tmp.left && i-1<=tmp.right)) System.out.print("X"); else{ i--; System.out.print("L"); } } } }else{ int cnt = 1; for(int i = s ; i < f ; cnt++){ if(!map.containsKey(cnt)){ System.out.print("R"); i ++; } else{ node tmp = map.get(cnt); if(i>= tmp.left && i <= tmp.right || (i+1>=tmp.left && i+1<=tmp.right)) System.out.print("X"); else{ i++; System.out.print("R"); } } } } System.out.println(); } public static void main(String args[]) { new B().run(); } }
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 l[100005], r[100005], t[100005]; int main() { int n, m, s, f; scanf("%d%d%d%d", &n, &m, &s, &f); for (int i = 0; i < m; ++i) { scanf("%d%d%d", t + i, l + i, r + i); } int ind = 0, pos = s, j, dir, c; if (f > s) { dir = 1; c = 1; } else { dir = -1; c = 0; } for (j = 1; pos != f; ++j) { if (t[ind] != j || l[ind] > pos + c || r[ind] < pos + c - 1) { if (dir == 1) printf("R"); else printf("L"); pos += dir; } else { printf("X"); } if (t[ind] == j) ++ind; } 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 io() { cin.tie(0); ios_base::sync_with_stdio(false); } bool deg = false; const int N = 1200011; char a[N]; int main() { int i, j, s, f, n, m, l, r, t, i2 = 0, st = 0; scanf("%d", &n), scanf("%d", &m), scanf("%d", &s), scanf("%d", &f); for (i = 0; i < m; i++) { scanf("%d", &t), scanf("%d", &l), scanf("%d", &r); if (s == f) continue; int ct = 1; if (s < f) while (s != f) { if (ct >= t - st) break; a[i2++] = 'R'; s++; ct++; if (s == f) break; } else if (s > f) while (s != f) { if (ct >= t - st) break; a[i2++] = 'L'; s--; ct++; if (s == f) break; } if (s < f) { if (s == n) continue; if ((s >= l && s <= r) || (s + 1 >= l && s + 1 <= r)) a[i2++] = 'X'; else a[i2++] = 'R', s++; } else if (s > f) { if (s == 1) continue; if ((s >= l && s <= r) || (s - 1 >= l && s - 1 <= r)) a[i2++] = 'X'; else a[i2++] = 'L', s--; } st = t; } if (s < f) while (s != f) { a[i2++] = 'R'; s++; if (s == f) break; } else if (s > f) while (s != f) { a[i2++] = 'L'; s--; if (s == f) break; } a[i2] = '\0'; printf("%s\n", a); 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 = 100005; int t[maxn], l[maxn], r[maxn]; bool judge(int x, int y, int z) { if (z < x) return true; if (z > y) return true; return false; } bool judge2(int x, int y, int z) { if (judge(x, y, z) && judge(x, y, z + 1)) return true; return false; } bool judge3(int x, int y, int z) { if (judge(x, y, z) && judge(x, y, z - 1)) return true; return false; } int main() { int n, m, s, f; while (~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]); int tot = 1; for (int i = 1; s != f; i++) { int t1, t2; t1 = t2 = 0; if (t[tot] == i) { t1 = l[tot]; t2 = r[tot]; tot++; } if (f > s) { if (judge2(t1, t2, s)) putchar('R'), s++; else putchar('X'); } else { if (judge3(t1, t2, s)) putchar('L'), s--; else putchar('X'); } } puts(""); } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int n, m, s, f; map<int, int> l, r; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> s >> f; for (int i = 1; i <= m; i++) { int t, x, y; cin >> t >> x >> y; l[t] = x; r[t] = y; } int i = 1; while (i) { if (s == f) break; if (s < f) { if (s + 1 >= l[i] && s <= r[i]) cout << "X"; else { cout << "R"; s++; } } else { if (s >= l[i] && s - 1 <= r[i]) cout << "X"; else { cout << "L"; s--; } } 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> using namespace std; int main() { int nspy; int nstep; int s, f; cin >> nspy >> nstep >> s >> f; int step[nstep + 2][4]; step[nstep + 2][1] = 0; for (int i = 1; i <= nstep; i++) { cin >> step[i][1] >> step[i][2] >> step[i][3]; } char c; int mode; if (s < f) mode = 1; if (s > f) mode = 2; int current = s; int round = 1; int current_step = 1; while (current != f) { if (round != step[current_step][1]) { if (mode == 1) { cout << "R"; current++; } if (mode == 2) { cout << "L"; current--; } } else { if (mode == 1) { if (((current >= step[current_step][2]) && (current <= step[current_step][3])) || ((current + 1 >= step[current_step][2]) && (current + 1 <= step[current_step][3]))) cout << "X"; else { cout << "R"; current++; } } if (mode == 2) { if (((current >= step[current_step][2]) && (current <= step[current_step][3])) || ((current - 1 >= step[current_step][2]) && (current - 1 <= step[current_step][3]))) cout << "X"; else { cout << "L"; current--; } } current_step++; } round++; } }
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 = new int[m], *l = new int[m], *r = new int[m]; for (int i = 0; i < m; i++) cin >> t[i] >> l[i] >> r[i]; char move = 'R'; if (s > f) move = 'L'; string ans = ""; int time = 1, pos = s; for (int i = 0; i < m; i++) { if (t[i] > time) { if (t[i] - time <= abs(pos - f)) { for (int j = 0; j < t[i] - time; j++) ans.push_back(move); if (s > f) pos -= t[i] - time; else pos += t[i] - time; } else { for (int j = 0; j < abs(pos - f); j++) ans.push_back(move); if (s > f) pos -= abs(pos - f); else pos += abs(pos - f); } time = t[i]; } if (pos == f) break; if (s > f) if (pos <= r[i] + 1 && pos >= l[i]) { ans.push_back('X'); } else { ans.push_back(move); pos--; } else { if (pos <= r[i] && pos >= l[i] - 1) { ans.push_back('X'); } else { ans.push_back(move); pos++; } } time++; if (pos == f) break; } if (pos != f) for (int i = 0; i < abs(pos - f); i++) ans.push_back(move); 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; void io_in_data() { freopen("data.in", "r", stdin); freopen("data1.out", "w", stdout); } const int MAXN = 100007; struct Node { int t, l, r; int judge(int x) { return x >= l && x <= r; } } w[MAXN]; int main() { int n, m, s, f; while (~scanf("%d%d%d%d", &n, &m, &s, &f)) { int nxt, mov, i, j; for (i = 0; i < m; i++) scanf("%d%d%d", &w[i].t, &w[i].l, &w[i].r); char mv[] = "LRX"; i = j = 0; while (s != f) { i++; if (s < f) nxt = s + 1, mov = 1; else nxt = s - 1, mov = 0; if (w[j].t == i && (w[j].judge(s) || w[j].judge(nxt))) mov = 2; else s = nxt; if (w[j].t == i) j++; putchar(mv[mov]); } 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; template <typename T> string tostr(T a) { stringstream ss; ss << a; return ss.str(); } int main() { int n, m, f, s; cin >> n >> m >> f >> s; f--; s--; int x = f; int t = 0; for (int i = 1; i <= m; i++) { if (x == s) break; t++; int l, r, ti; cin >> ti >> l >> r; while (t < ti) { if (s == x) { cout << endl; return 0; } if (s < x) { cout << "L"; x--; } if (s > x) { cout << "R"; x++; } t++; } if (x == s) break; l--; r--; if (x >= l && x <= r) { cout << "X"; continue; } if (x > s) { if (x - 1 >= l && x - 1 <= r) cout << "X"; else { cout << "L"; x--; if (x == s) break; } continue; } if (x < s) { if (x + 1 >= l && x + 1 <= r) cout << "X"; else { cout << "R"; x++; if (x == s) break; } continue; } } while (x != s) { if (x > s) { cout << "L"; x--; } if (x < s) { cout << "R"; x++; } } 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
/* ~/BAU/ACM-ICPC/Teams/Rampage/MMZA ~/sudo apt-get verdict Accepted practice... */ //package javaapplication1; //import java.util.Scanner; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; public class Main { static final int Mod=(int)1e9+7; static final int N=(int)2e5+5; public static void main(String[] args) throws IOException { // Reader.init(System.in); //Scanner in=new Scanner(new FileInputStream("input.txt")); FastReader in=new FastReader(); StringBuilder sb=new StringBuilder(); //in=new FileReader("input.txt"); //PrintWriter out=new PrintWriter(new FileOutputStream("output.txt")); PrintWriter out=new PrintWriter(System.out); Main ob=new Main(); 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(); } static int[] reverse(int [] arr){ ArrayList<Integer> temp=new ArrayList(); for(int i=0;i<arr.length;i++) temp.add(arr[i]); Collections.reverse(temp); for(int i=0;i<temp.size();i++) arr[i]=temp.get(i); return arr; } static String reverse(String s){ String temp=""; for(int i=s.length()-1;i>=0;i--) temp+=s.charAt(i); return temp; } static String sort(String inputString) { ArrayList<Character> arr=new ArrayList(); for(int i=0;i<inputString.length();i++) arr.add(inputString.charAt(i)); Collections.sort(arr); StringBuilder sb=new StringBuilder(); for(int i=0;i<arr.size();i++) sb.append(arr.get(i)); return sb.toString(); } 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; } static int lower_bound(ArrayList<Integer> arr,int key) { int len = arr.size(); int lo = 0; int hi = len-1; int mid = (lo + hi)/2; while (true) { int cmp = (arr.get(mid)==key?1:0); 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; } } static int upper_bound(ArrayList<Integer> arr, int key) { int len = arr.size(); int lo = 0; int hi = len-1; int mid = (lo + hi)/2; while (true) { int cmp = (arr.get(mid)==key?1:0); 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; } } static int gcd(int a,int b){ return b==0?a:gcd(b,a%b); } static double dot(Point p,Point q){ return (p.x*q.x+p.y*q.y); } static double cross(Point p,Point q){ return (p.x*q.y-p.y*q.x); } static Point computeLineIntersection(Point a,Point b,Point c,Point d){ b=b.sub(a);d=c.sub(d);c=c.sub(a); return a.add(b.mulCon(cross(c,d)).devideCon(cross(b,d))); } 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; } } } 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 void init(String url) throws FileNotFoundException { reader = new BufferedReader(new FileReader(url)); 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()); } } class Point{ double x,y; Point(double _x,double _y){ x=_x; y=_y; } Point add(final Point p){ return new Point(x+p.x,y+p.y); } Point sub(final Point p){ return new Point(x-p.x,y-p.y); } Point mulCon(final double c){ return new Point(c*x,c*y); } Point devideCon(final double c){ return new Point(x/c,y/c); } } class pair implements Comparator<pair>{ int first; int second; //int third; pair(){} pair(int a,int s){ first = a; second = s; } /* pair(pair a,pair b){ first = a; second = b; }*/ @Override public int compare(pair a,pair b){ return (a.first-b.first); } }
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> struct node { int id, l, r; } a[100005]; int main() { int n, m, s, f; while (scanf("%d %d %d %d", &n, &m, &s, &f) != EOF) { memset(a, 0, sizeof a); for (int i = 0; i < m; i++) scanf("%d %d %d", &a[i].id, &a[i].l, &a[i].r); int t = 1, note = s; for (int i = 0; i < m; i++) { if (note == f) break; if (a[i].id != t) { if (s < f) { printf("R"); note++; } else if (s >= f) { printf("L"); note--; } i--; } else if (s < f && (a[i].l > note + 1 || a[i].r < note)) { printf("R"); note++; } else if (s >= f && (a[i].l > note || a[i].r < note - 1)) { printf("L"); note--; } else printf("X"); t++; } while (note != f) { if (s < f) { printf("R"); note++; } else if (s >= f) { printf("L"); note--; } } puts(""); } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s, f, l[100005], r[100005], t[100005], i, j = 1; cin >> n >> m >> s >> f; t[m + 1] = 1000000000; for (i = 1; i <= m; i++) cin >> t[i] >> l[i] >> r[i]; i = 1; while (1) { if (i <= m + 1 && j > t[i]) i++; if (s == f) break; if (s >= l[i] && s <= r[i] && j == t[i]) cout << "X"; else if (s < f) { if (s + 1 >= l[i] && s + 1 <= r[i] && j == t[i]) cout << "X"; else cout << "R", s++; } else { if (s - 1 >= l[i] && s - 1 <= r[i] && j == t[i]) cout << "X"; else cout << "L", s--; } 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
import java.util.*; import java.io.*; import java.awt.Point; import java.math.BigDecimal; import java.math.BigInteger; import static java.lang.Math.*; // Solution is at the bottom of code public class B implements Runnable{ final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; OutputWriter out; StringTokenizer tok = new StringTokenizer(""); public static void main(String[] args){ new Thread(null, new B(), "", 128 * (1L << 20)).start(); } ///////////////////////////////////////////////////////////////////// void init() throws FileNotFoundException{ Locale.setDefault(Locale.US); if (ONLINE_JUDGE){ in = new BufferedReader(new InputStreamReader(System.in)); out = new OutputWriter(System.out); }else{ in = new BufferedReader(new FileReader("input.txt")); out = new OutputWriter("output.txt"); } } //////////////////////////////////////////////////////////////// long timeBegin, timeEnd; void time(){ timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } void debug(Object... objects){ if (ONLINE_JUDGE){ for (Object o: objects){ System.err.println(o.toString()); } } } ///////////////////////////////////////////////////////////////////// public void run(){ try{ timeBegin = System.currentTimeMillis(); Locale.setDefault(Locale.US); init(); solve(); out.close(); time(); }catch (Exception e){ e.printStackTrace(System.err); System.exit(-1); } } ///////////////////////////////////////////////////////////////////// String delim = " "; String readString() throws IOException{ while(!tok.hasMoreTokens()){ try{ tok = new StringTokenizer(in.readLine()); }catch (Exception e){ return null; } } return tok.nextToken(delim); } String readLine() throws IOException{ return in.readLine(); } ///////////////////////////////////////////////////////////////// final char NOT_A_SYMBOL = '\0'; char readChar() throws IOException{ int intValue = in.read(); if (intValue == -1){ return NOT_A_SYMBOL; } return (char) intValue; } char[] readCharArray() throws IOException{ return readLine().toCharArray(); } ///////////////////////////////////////////////////////////////// int readInt() throws IOException { return Integer.parseInt(readString()); } int[] readIntArray(int size) throws IOException { int[] array = new int[size]; for (int index = 0; index < size; ++index){ array[index] = readInt(); } return array; } int[] readIntArrayWithDecrease(int size) throws IOException { int[] array = readIntArray(size); for (int i = 0; i < size; ++i) { array[i]--; } return array; } /////////////////////////////////////////////////////////////////// long readLong() throws IOException{ return Long.parseLong(readString()); } long[] readLongArray(int size) throws IOException{ long[] array = new long[size]; for (int index = 0; index < size; ++index){ array[index] = readLong(); } return array; } //////////////////////////////////////////////////////////////////// double readDouble() throws IOException{ return Double.parseDouble(readString()); } double[] readDoubleArray(int size) throws IOException{ double[] array = new double[size]; for (int index = 0; index < size; ++index){ array[index] = readDouble(); } return array; } //////////////////////////////////////////////////////////////////// BigInteger readBigInteger() throws IOException { return new BigInteger(readString()); } BigDecimal readBigDecimal() throws IOException { return new BigDecimal(readString()); } ///////////////////////////////////////////////////////////////////// Point readPoint() throws IOException{ int x = readInt(); int y = readInt(); return new Point(x, y); } Point[] readPointArray(int size) throws IOException{ Point[] array = new Point[size]; for (int index = 0; index < size; ++index){ array[index] = readPoint(); } return array; } ///////////////////////////////////////////////////////////////////// List<Integer>[] readGraph(int vertexNumber, int edgeNumber) throws IOException{ @SuppressWarnings("unchecked") List<Integer>[] graph = new List[vertexNumber]; for (int index = 0; index < vertexNumber; ++index){ graph[index] = new ArrayList<Integer>(); } while (edgeNumber-- > 0){ int from = readInt() - 1; int to = readInt() - 1; graph[from].add(to); graph[to].add(from); } return graph; } ///////////////////////////////////////////////////////////////////// static class IntIndexPair { static Comparator<IntIndexPair> increaseComparator = new Comparator<B.IntIndexPair>() { @Override public int compare(IntIndexPair indexPair1, IntIndexPair indexPair2) { int value1 = indexPair1.value; int value2 = indexPair2.value; if (value1 != value2) return value1 - value2; int index1 = indexPair1.index; int index2 = indexPair2.index; return index1 - index2; } }; static Comparator<IntIndexPair> decreaseComparator = new Comparator<B.IntIndexPair>() { @Override public int compare(IntIndexPair indexPair1, IntIndexPair indexPair2) { int value1 = indexPair1.value; int value2 = indexPair2.value; if (value1 != value2) return -(value1 - value2); int index1 = indexPair1.index; int index2 = indexPair2.index; return index1 - index2; } }; int value, index; public IntIndexPair(int value, int index) { super(); this.value = value; this.index = index; } public int getRealIndex() { return index + 1; } } IntIndexPair[] readIntIndexArray(int size) throws IOException { IntIndexPair[] array = new IntIndexPair[size]; for (int index = 0; index < size; ++index) { array[index] = new IntIndexPair(readInt(), index); } return array; } ///////////////////////////////////////////////////////////////////// static class OutputWriter extends PrintWriter{ final int DEFAULT_PRECISION = 12; int precision; String format, formatWithSpace; { precision = DEFAULT_PRECISION; format = createFormat(precision); formatWithSpace = format + " "; } public OutputWriter(OutputStream out) { super(out); } public OutputWriter(String fileName) throws FileNotFoundException { super(fileName); } public int getPrecision() { return precision; } public void setPrecision(int precision) { precision = max(0, precision); this.precision = precision; format = createFormat(precision); formatWithSpace = format + " "; } private String createFormat(int precision){ return "%." + precision + "f"; } @Override public void print(double d){ printf(format, d); } public void printWithSpace(double d){ printf(formatWithSpace, d); } public void printAll(double...d){ for (int i = 0; i < d.length - 1; ++i){ printWithSpace(d[i]); } print(d[d.length - 1]); } @Override public void println(double d){ printlnAll(d); } public void printlnAll(double... d){ printAll(d); println(); } } ///////////////////////////////////////////////////////////////////// int[][] steps = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; int[][] steps8 = { {-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {1, 1}, {1, -1}, {-1, 1} }; boolean check(int index, int lim){ return (0 <= index && index < lim); } ///////////////////////////////////////////////////////////////////// boolean checkBit(int mask, int bitNumber){ return (mask & (1 << bitNumber)) != 0; } ///////////////////////////////////////////////////////////////////// void solve() throws IOException { int n = readInt(); int m = readInt(); int s = readInt(); int f = readInt(); int spyDelta = (s < f? 1: -1); char spyAction = (s < f? 'R': 'L'); int curSpy = s; int curStage = 1; while (m-- > 0 && curSpy != f) { int t = readInt(); int l = readInt(); int r = readInt(); while (curStage < t && curSpy != f) { curSpy += spyDelta; out.print(spyAction); curStage++; } if (curSpy == f) { return; } int nextSpy = curSpy + spyDelta; if (l <= curSpy && curSpy <= r || l <= nextSpy && nextSpy <= r) { out.print('X'); } else { curSpy = nextSpy; out.print(spyAction); } curStage = t + 1; } while (curSpy != f) { curSpy += spyDelta; out.print(spyAction); } } }
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 inrange(x, l, r): return x >= l and x <= r n,m,s,f = [int(x) for x in sys.stdin.readline().strip().split()] s = s-1 f = f-1 moves = [] for x in xrange(m): if s == f: break t,l,r = [int(z) for z in sys.stdin.readline().strip().split()] t = t-1 l = l-1 r = r-1 while len(moves) < t: if s == f: break if s < f: moves.append('R') s = s+1 if s > f: moves.append('L') s = s-1 if s == f: break if s < f: if not(inrange(s,l,r) or inrange(s+1,l,r)): moves.append('R') s = s+1 else: moves.append('X') else: if not(inrange(s,l,r) or inrange(s-1,l,r)): moves.append('L') s = s-1 else: moves.append('X') while s < f: moves.append('R') s = s+1 while s > f: moves.append('L') s = s-1 print ''.join(moves)
PYTHON
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.*; import java.util.*; import static java.lang.Math.*; public class Solution { private IO io; private int ioMode = -1; private String problemName = ""; private final String mjArgument = "master_j"; public static void main(String programArguments[]) throws IOException{ if(programArguments != null && programArguments.length > 0) new Solution().run(programArguments[0]); else new Solution().run(null); } private void run(String programArgument) throws IOException { // _______________________________________________ _________ // | Input Mode | Output Mode | mode | comment | // |------------------|---------------------|----- |---------| // | input.txt | System.out | 0 | mj | // | System.in | System.out | 1 | T / CF | // |<problemName>.in | <problemName>.out | 2 | | // | input.txt | output.txt | 3 | C | // |__________________|_____________________|______|_________| long nanoTime = 0; if(programArgument != null && programArgument.equals(mjArgument)) // mj ioMode = 0; else if(System.getProperty("ONLINE_JUDGE") != null) // T / CF ioMode = 1; else ioMode = 2; switch(ioMode){ case -1: try{ throw new Exception("<ioMode> init failure") ; }catch (Exception e){ e.printStackTrace(); } return; case 0: break; case 1: break; case 2: if(problemName.length() == 0){ try{ throw new Exception("<problemName> init failure"); }catch (Exception e){ e.printStackTrace(); } return; } case 3: break; } io = new IO(ioMode, problemName); if(ioMode == 0){ System.out.println("File output : \n<start>"); System.out.flush(); nanoTime = System.nanoTime(); } solve(); io.flush(); if(ioMode == 0){ System.out.println("</start>"); long t = System.nanoTime() - nanoTime; int d3 = 1000000000, d2 = 1000000, d1 = 1000; if(t>=d3) System.out.println(t/d3 + "." + t%d3 + " seconds"); else if(t>=d2) System.out.println(t/d2 + "." + t%d2 + " millis"); else if(t>=d1) System.out.println(t/d1 + "." + t%d1 + " micros"); else System.out.println(t + " nanos"); System.out.flush(); } } class Pair { int n1, n2; Pair(int n1, int n2){ this.n1 = n1; this.n2 = n2; } @Override public boolean equals(Object o){ if(this == o){ return true; } if(o == null || getClass() != o.getClass()){ return false; } Pair pair = (Pair)o; if(n1 != pair.n1){ return false; } if(n2 != pair.n2){ return false; } return true; } @Override public int hashCode(){ int result = n1; result = 31*result + n2; return result; } } private void solve() throws IOException { int n = io.nI(), m = io.nI(), s = io.nI(), f = io.nI(), pos = s, delta; char c; if(s <= f){ c = 'R'; delta = 1; }else{ c = 'L'; delta = -1; } Map<Integer, Pair> map = new HashMap<Integer, Pair>(); for(int i = 0; i < m; i++) map.put(io.nI(), new Pair(io.nI(), io.nI())); for(int i = 1; pos != f; i++){ Pair p = map.get(i); int l, r; if(p != null){ l = p.n1; r = p.n2; }else{ l = -10; r = -10; } if(l <= pos && pos <= r || l <= pos+delta && pos+delta <= r) io.w('X'); else{ pos += delta; io.w(c); } } io.wln(); }//2.2250738585072012e-308 /** * Input-output class * @author master_j * @version 0.2.5 */ @SuppressWarnings("unused") private class IO{ private boolean alwaysFlush; StreamTokenizer in; PrintWriter out; BufferedReader br; Reader reader; Writer writer; public IO(int ioMode, String problemName) throws IOException{ Locale.setDefault(Locale.US); // _______________________________________________ _________ // | Input Mode | Output Mode | mode | comment | // |------------------|---------------------|----- |---------| // | input.txt | System.out | 0 | mj | // | System.in | System.out | 1 | T / CF | // |<problemName>.in | <problemName>.out | 2 | | // | input.txt | output.txt | 3 | C | // |__________________|_____________________|______|_________| switch(ioMode){ case 0: reader = new FileReader("input.txt"); writer = new OutputStreamWriter(System.out); break; case 1: reader = new InputStreamReader(System.in); writer = new OutputStreamWriter(System.out); break; case 2: reader = new FileReader(problemName + ".in"); writer = new FileWriter(problemName + ".out"); break; case 3: reader = new FileReader("input.txt"); writer = new FileWriter("output.txt"); break; } br = new BufferedReader(reader); in = new StreamTokenizer(br); out = new PrintWriter(writer); alwaysFlush = false; } public void setAlwaysFlush(boolean arg){alwaysFlush = arg;} public void wln(){out.println(); if(alwaysFlush)flush();} public void wln(int arg){out.println(arg); if(alwaysFlush)flush();} public void wln(long arg){out.println(arg); if(alwaysFlush)flush();} public void wln(double arg){out.println(arg); if(alwaysFlush)flush();} public void wln(String arg){out.println(arg); if(alwaysFlush)flush();} public void wln(boolean arg){out.println(arg); if(alwaysFlush)flush();} public void wln(char arg){out.println(arg); if(alwaysFlush)flush();} public void wln(float arg){out.println(arg); if(alwaysFlush)flush();} public void wln(Object arg){out.println(arg); if(alwaysFlush)flush();} public void w(int arg){out.print(arg); if(alwaysFlush)flush();} public void w(long arg){out.print(arg); if(alwaysFlush)flush();} public void w(double arg){out.print(arg); if(alwaysFlush)flush();} public void w(String arg){out.print(arg); if(alwaysFlush)flush();} public void w(boolean arg){out.print(arg); if(alwaysFlush)flush();} public void w(char arg){out.print(arg); if(alwaysFlush)flush();} public void w(float arg){out.print(arg); if(alwaysFlush)flush();} public void w(Object arg){out.print(arg); if(alwaysFlush)flush();} public void wf(String format, Object...args){out.printf(format, args); if(alwaysFlush)flush();} public void flush(){out.flush();} public int nI() throws IOException {in.nextToken(); return(int)in.nval;} public long nL() throws IOException {in.nextToken(); return(long)in.nval;} public String nS() throws IOException {in.nextToken(); return in.sval;} public double nD() throws IOException {in.nextToken(); return in.nval;} public float nF() throws IOException {in.nextToken(); return (float)in.nval;} public char nC() throws IOException {return (char)br.read();} public void wc(char...arg){for(char c : arg){in.ordinaryChar(c);in.wordChars(c, c);}} public void wc(String arg){wc(arg.toCharArray());} public void wc(char arg0, char arg1){in.ordinaryChars(arg0, arg1); in.wordChars(arg0, arg1);} public boolean eof(){return in.ttype == StreamTokenizer.TT_EOF;} public boolean eol(){return in.ttype == StreamTokenizer.TT_EOL;} } }
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.IOException; import java.io.InputStream; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; public class Main { static int maxn = (int)(1e5 + 5); static int[] t = new int[maxn]; static int[] l = new int[maxn]; static int[] r = new int[maxn]; public static void main(String[] args) { InputStream inputStream = System.in; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int n = in.nextInt(); int m = in.nextInt(); int s = in.nextInt(); int f = in.nextInt(); for(int i = 1; i <= m; ++i) { t[i] = in.nextInt(); l[i] = in.nextInt(); r[i] = in.nextInt(); } int pos = s, p = 0; for(int i = 1; pos != f; ++i) { while(p <= m && t[p] < i) { ++p; } if(pos < f) { if(p > m || t[p] != i || (t[p] == i && (l[p] > pos + 1 || r[p] < pos))) { out.print('R'); ++pos; } else { out.print('X'); } } else { if(p > m || t[p] != i || (t[p] == i && (l[p] > pos || r[p] < pos - 1))) { out.print('L'); --pos; } else { out.print('X'); } } } out.flush(); out.close(); // Scanner in = new Scanner(System.in); // in.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while(tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { 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> int N, M, S, F, T, C; char chr[2] = {'R', 'L'}; int main(void) { int Ti, Li, Ri, i, j, tp; scanf("%d%d%d%d", &N, &M, &S, &F); tp = S > F; C = S; T = 1; for (i = 0; i < M; ++i) { scanf("%d%d%d", &Ti, &Li, &Ri); if (Ti > T) { int st = (Ti - T > (F - C < 0 ? -(F - C) : F - C) ? (F - C < 0 ? -(F - C) : F - C) : Ti - T); for (j = 0; j < st; ++j) putchar(chr[tp]); if (!tp) C += st; else C -= st; T += st; } if (!tp && C < F) { if (C + 1 >= Li && C <= Ri) { putchar('X'); T++; } } else if (tp && C > F) { if (C >= Li && C - 1 <= Ri) { putchar('X'); T++; } } } int st = (F - C < 0 ? -(F - C) : F - C); for (j = 0; j < st; ++j) putchar(chr[tp]); C += st; T += st; putchar('\n'); return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.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 keyword outside of while 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.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.io.*; import java.math.*; import java.text.*; import java.util.*; /* br = new BufferedReader(new FileReader("input.txt")); pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); */ public class Main { private static BufferedReader br; private static StringTokenizer st; private static PrintWriter pw; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); //int qq = 1; int qq = Integer.MAX_VALUE; //int qq = readInt(); for(int casenum = 1; casenum <= qq; casenum++) { int n = readInt(); int m = readInt(); int s = readInt(); int f = readInt(); int[] time = new int[m]; int[] l = new int[m]; int[] r = new int[m]; for(int i = 0; i < m; i++) { time[i] = readInt(); l[i] = readInt(); r[i] = readInt(); } int timeIndex = 0; for(int qqq = 1; s != f; qqq++) { int next = s<f ? s+1 : s-1; if(timeIndex == time.length || time[timeIndex] != qqq) { if(s<f) { pw.print('R'); } else { pw.print('L'); } s = next; } else { boolean can = true; if(s >= l[timeIndex] && s <= r[timeIndex]){ can = false; } if(next >= l[timeIndex] && next <= r[timeIndex]){ can = false; } if(can) { if(s<f) { pw.print('R'); } else { pw.print('L'); } s = next; } else { pw.print('X'); } timeIndex++; } } } pw.close(); } public static int get(int s) { String ret = Integer.toString(s); while(ret.length() < 3) { ret = 0 + ret; } int mask = 0; for(int i = 0; i < 3; i++) { if(ret.charAt(i) != '0') { mask |= 1 << i; } } return mask; } private static void exitImmediately() { pw.close(); System.exit(0); } private static long readLong() throws IOException { return Long.parseLong(nextToken()); } private static double readDouble() throws IOException { return Double.parseDouble(nextToken()); } private static int readInt() throws IOException { return Integer.parseInt(nextToken()); } private static String nextToken() throws IOException { while(st == null || !st.hasMoreTokens()) { if(!br.ready()) { exitImmediately(); } st = new StringTokenizer(br.readLine().trim()); } 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
#include <bits/stdc++.h> using namespace std; using ll = long long; using lli = long long int; using ld = long double; const int N = 2e5 + 5, inf = 1e18; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, s, f; cin >> n >> m >> s >> f; map<int, pair<int, int>> mp; int cnt = 1; for (int i = 0; i < m; i++) { int t, l, r; cin >> t >> l >> r; mp[t] = {l, r}; } if (s < f) { while (s != f) { if ((s >= mp[cnt].first && s <= mp[cnt].second) || (s + 1 >= mp[cnt].first && s + 1 <= mp[cnt].second)) cout << 'X'; else cout << 'R', s++; cnt++; } } else { while (s != f) { if ((s >= mp[cnt].first && s <= mp[cnt].second) || (s - 1 >= mp[cnt].first && s - 1 <= mp[cnt].second)) cout << 'X'; else cout << 'L', s--; cnt++; } } 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 main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, s, f; cin >> n >> m >> s >> f; long long t, l, r, h = 0; for (int i = 0; i < m; i++) { cin >> t >> l >> r; if (s == f) break; if (t - h != 1) { long long x = t - h - 1; while (x > 0) { if (s < f) { cout << 'R'; s++; } else if (s > f) { cout << 'L'; s--; } if (s == f) return 0; x--; } } if (s < f) { if ((s + 1 >= l && s + 1 <= r) || (s == l || s == r)) cout << 'X'; else { cout << 'R'; s++; } } else { if ((s - 1 >= l && s - 1 <= r) || (s == l || s == r)) cout << 'X'; else { cout << 'L'; s--; } } h = t; } if (s != f) { if (s < f) while (s != f) { cout << 'R'; s++; } else while (s != f) { cout << 'L'; s--; } } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> const double pi = acos(-1.0); using namespace std; int al[100005], ar[100005], at[100005]; int main() { int n, m, s, f; cin >> n >> m >> s >> f; for (int i = 0; i < m; ++i) { scanf("%d %d %d", &at[i + 1], &al[i + 1], &ar[i + 1]); } at[m + 1] = 2e9; int cur = s; int et = 1, curt = 1; while (cur != f) { if (et != at[curt]) { if (s < f) ++cur; else --cur; cout << (s < f ? "R" : "L"); } else { int next = s < f ? cur + 1 : cur - 1; if (!(al[curt] <= next && ar[curt] >= next) && !(al[curt] <= cur && ar[curt] >= cur)) { cout << (s < f ? "R" : "L"); if (s < f) ++cur; else --cur; } else cout << "X"; ++curt; } ++et; } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x > 0 ? x : -x; } int n; int m; int s, f; int t[100000]; int x[100000]; int y[100000]; 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]); t[i]--; } int i = 0, j = 0; string res = ""; while (s != f) { int ok = 1; int ns = s; char c; if (s < f) { ns = s + 1; c = 'R'; } else { ns = s - 1; c = 'L'; } if (i < m && t[i] == j) { if (s >= x[i] && s <= y[i] || ns >= x[i] && ns <= y[i]) ok = 0; i++; } if (ok) { res += c; s = ns; } else res += 'X'; j++; } 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 main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m, f, s; cin >> n >> m >> s >> f; char x = (s < f ? 'R' : 'L'); bool ok = 0; string ans; int hold = 0; while (m--) { int t, l, r; cin >> t >> l >> r; if (ok) { continue; } int xx = t - hold - 1; while (xx > 0 && s != f) { if (s < f) { s++; } if (s > f) { s--; } ans.push_back(x); xx--; } if (s == f) { ok = 1; continue; } hold = t; if (s <= r && s >= l) { ans.push_back('X'); continue; } if (s < f) { if (s + 1 < l || s > r) { s++; ans.push_back(x); } else { ans.push_back('X'); } } else { if (s - 1 > r || s < l) { s--; ans.push_back(x); } else { ans.push_back('X'); } } if (s == f) { ok = 1; } } if (!ok) { int rem = abs(f - s); while (rem--) { ans.push_back(x); } } cout << ans << endl; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> int t[100001], l[100001], r[100001]; int main() { int n, m, s, f, i, j; while (scanf("%d%d%d%d", &n, &m, &s, &f) != EOF) { for (i = 1; i <= m; i++) { scanf("%d%d%d", &t[i], &l[i], &r[i]); } if (s < f) { for (i = 1, j = 1; i <= m; i++) { if (t[j] == i) { if (l[j] <= s && r[j] >= s || l[j] <= s + 1 && r[j] >= s + 1) printf("X"); else { printf("R"); s++; } j++; } else { printf("R"); s++; } if (s == f) break; } for (; s < f; s++) printf("R"); } else { for (i = 1, j = 1; i <= m; i++) { if (t[j] == i) { if (l[j] <= s && r[j] >= s || l[j] <= s - 1 && r[j] >= s - 1) printf("X"); else { printf("L"); s--; } j++; } else { printf("L"); s--; } if (s == f) break; } for (; s > f; s--) printf("L"); } 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 main() { int n, m, s, f; int arr[100000][3]; cin >> n >> m >> s >> f; for (int i = 0; i < m; i++) for (int j = 0; j < 3; j++) cin >> arr[i][j]; int i = 0, c = 0, l, r; while (s != f) { if (i + 1 == arr[c][0]) { l = arr[c][1]; r = arr[c][2]; c++; } else { l = -1; r = -1; } if (s < f && (((s + 1) > l && (s + 1) > r) || ((s + 1) < l && (s + 1) < r)) && ((s > l && s > r) || (s < l && s < r))) { cout << "R"; s++; } else if (s > f && (((s - 1) > l && (s - 1) > r) || ((s - 1) < l && (s - 1) < r)) && ((s > l && s > r) || (s < l && s < r))) { cout << "L"; s--; } else cout << "X"; 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> using namespace std; int main() { int n, m, s, f, t = 1; char k; cin >> n >> m >> s >> f; int mv = (f > s ? 1 : -1); k = (f > s ? 'R' : 'L'); for (int x = 1; x <= m; x++) { int a, b, c; cin >> a >> b >> c; if (s == f) break; while (t < a && s != f) { cout << k; s += mv; t++; } if (s == f) break; else if ((s >= b && s <= c) || (s + mv >= b && s + mv <= c)) { cout << 'X'; t++; } else { cout << k; s += mv; t++; } } while (s != f) { cout << k; s += mv; } 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> typedef struct steps { int t; int l; int r; }; using namespace std; int main() { int n, m, s, f; vector<steps> a; cin >> n >> m >> s >> f; for (int i = 0; i < m; i++) { steps step; cin >> step.t >> step.l >> step.r; a.push_back(step); } int idx = s; int step = 1; int i = 0; string ss = ""; char p; int k; if (s > f) { p = 'L'; k = -1; } else { p = 'R'; k = 1; } while (i <= m) { if (a[i].t == step) { if ((idx <= a[i].r && idx >= a[i].l) || ((idx + k) <= a[i].r && (idx + k) >= a[i].l)) { ss += "X"; i++; } else { ss += p; i++; idx += k; } } else { ss += p; idx += k; } step++; if (idx == f) break; } cout << ss << 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.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.util.InputMismatchException; 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); long N = in.readInt(); long M = in.readInt(); long s = in.readInt(); long f = in.readInt(); long currPos = s; long dir = 1; if (f < s) { dir = -1; } else { dir = 1; } int step = 1; StringBuffer ans = new StringBuffer(""); boolean cond = true; for (int I = 1; I < M + 1; I++) { int t = in.readInt(); int l = in.readInt(); int r = in.readInt(); if(f==currPos){cond = false; break;} if (cond) { if (t > step) { long x = currPos; currPos += dir * (t - step); for (int J = 0; J < Math.min((t - step), Math.abs(x - f)); J++) { ans.append(dir == -1 ? "L" : "R"); } step = t; if (currPos <= f && dir == -1 || (currPos >= f && dir == 1)) { cond = false; break; } } if (cond) { if (!((currPos >= l && currPos <= r) || (currPos + dir >= l && currPos + dir <= r))) { // System.out.println("Hello"+I); currPos += dir; ans.append(dir == -1 ? "L" : "R"); } else { //System.out.println("X.."+step+" "+t+" "+currPos); ans.append("X"); } if (currPos <= f && dir == -1 || (currPos >= f && dir == 1)) { cond = false; break; } step += 1; } } } if(currPos <f && dir ==1 || currPos >f && dir ==-1 ){ for(int I =0;I<Math.abs(currPos-f);I++){ ans.append(dir==-1?"L":"R"); } } out.printLine(ans); out.close(); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; 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 long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(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 printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } }
JAVA