Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int t[100007], l[100007], r[100007];
int main() {
int n, m, s, f;
cin >> n >> m >> s >> f;
char ch = 'R';
int go = 1;
if (s > f) ch = 'L', go = -1;
for (int i = 0; i < m; ++i) cin >> t[i] >> l[i] >> r[i];
int curr = s, cu = 0, step = 1;
while (curr != f) {
if (cu < m and step == t[cu]) {
if ((curr >= l[cu] and curr <= r[cu]) or
(l[cu] <= curr + go and r[cu] >= curr + go))
cout << 'X';
else {
cout << ch;
curr += go;
}
++cu;
} else {
cout << ch;
curr += go;
}
++step;
}
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.util.*;
import java.io.*;
public class solution
{
public static void main(String ar[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int s=sc.nextInt();
int f=sc.nextInt();
int arr[][]=new int[m][3];
for(int i=0;i<m;i++)
{
arr[i][0]=sc.nextInt();
arr[i][1]=sc.nextInt();
arr[i][2]=sc.nextInt();
}
int step=1,c=0;
StringBuilder ans=new StringBuilder();
char move;
int inc;
if(s<f)
{
move='R';
inc=1;
}
else
{
move='L';
inc=-1;
}
while(s!=f)
{
if(c<m&&step==arr[c][0])
{
if(s>=arr[c][1]&&s<=arr[c][2])
ans.append('X');
else if(s+inc>=arr[c][1]&&s+inc<=arr[c][2])
{
ans.append('X');
}
else
{
ans.append(move);
s+=inc;
}
c++;
}
else
{
ans.append(move);
s+=inc;
}
step++;
}
System.out.println(ans.toString());
}
} | JAVA |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 412345;
const int MAXINT = 2047483098;
const long long int MOD = 1e9 + 7;
const int MAX = 1e4;
const long double pi = 2 * acosl(0);
const long double EPS = 1e-10;
bool compo(pair<long long int, long long int> &a,
pair<long long int, long long int> &b) {
return a.first > b.first;
}
void solve() {
long long int n, m, s, f;
cin >> n >> m >> s >> f;
long long int curr = s;
vector<tuple<long long int, long long int, long long int> > v(m);
long long int t, l, r;
for (long long int i = 0; i < m; i++) {
cin >> t >> l >> r;
v[i] = make_tuple(t, l, r);
}
long long int exp = 1;
if (f >= s) {
for (auto x : v) {
long long int a = get<0>(x);
long long int b = get<1>(x);
long long int c = get<2>(x);
if (curr == f) break;
if (a != exp) {
while (a != exp && curr != f) {
exp++;
curr++;
cout << 'R';
}
}
if (a == exp && curr != f) {
if (curr >= b - 1 && curr <= c) {
cout << 'X';
} else {
curr++;
cout << 'R';
}
}
exp = a + 1;
}
if (curr != f) {
while (curr != f) {
cout << 'R';
curr++;
}
}
} else {
for (auto x : v) {
long long int a = get<0>(x);
long long int b = get<1>(x);
long long int c = get<2>(x);
if (curr == f) break;
if (a != exp) {
while (a != exp && curr != f) {
exp++;
curr--;
cout << 'L';
}
}
if (a == exp && curr != f) {
if (curr >= b && curr - 1 <= c) {
cout << 'X';
} else {
curr--;
cout << 'L';
}
}
exp = a + 1;
}
if (curr != f) {
while (curr != f) {
cout << 'L';
curr--;
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int T = 1;
while (T--) {
solve();
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;
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
;
long long t = 1;
while (t--) {
long long n, m, s, f;
cin >> n >> m >> s >> f;
string ans = "";
long long stp = 0;
bool chk = 1;
for (long long i = 0; i < m; i++) {
long long a, b, c;
cin >> a >> b >> c;
if (chk) {
stp++;
for (long long j = stp; j < a; j++) {
if (s > f) {
s--;
ans += 'L';
} else if (s < f) {
s++;
ans += 'R';
} else {
chk = false;
break;
}
stp++;
}
if (stp == a) {
if (s > f) {
if ((b > s || c < s) && ((s - 1) < b || (s - 1) > c)) {
s--;
ans += 'L';
} else
ans += 'X';
} else if (s < f) {
if ((b > s || c < s) && ((s + 1) < b || (s + 1) > c)) {
s++;
ans += 'R';
} else
ans += 'X';
} else
chk = false;
}
}
}
while (s > f) {
s--;
ans += 'L';
}
while (s < f) {
s++;
ans += 'R';
}
cout << ans << "\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>
#pragma comment(linker, "/STACK:60777216")
using namespace std;
const double EPS = 1e-9;
const int MOD = int(1e9) + 7;
const double PI = acos(-1.0);
template <class T>
void printV(vector<T> a) {
for (int _n = int((a).size()), i = 0; i < _n; ++i) cout << a[i] << " ";
cout << endl;
}
template <class T>
void printA(T a[], int n) {
for (int _n = n, i = 0; i < _n; ++i) cout << a[i] << " ";
cout << endl;
}
template <class T>
void printVV(vector<vector<T> > a) {
for (int _n = int((a).size()), i = 0; i < _n; ++i) {
for (int _n = int((a[0]).size()), j = 0; j < _n; ++j)
cout << a[i][j] << " ";
cout << endl;
}
}
template <class T>
void printAA(T **a, int n, int m) {
for (int _n = n, i = 0; i < _n; ++i) {
for (int _n = m, j = 0; j < _n; ++j) cout << a[i] << " ";
cout << endl;
}
}
const int N = 100000;
map<int, pair<int, int> > mp;
int main() {
int n, m, f, s;
cin >> n >> m >> s >> f;
for (int _n = m, i = 0; i < _n; ++i) {
int t, x, y;
cin >> t >> x >> y;
mp[t] = make_pair(x, y);
}
string ans = "";
if (s < f) {
int cur = s;
for (int t = (1), _b = (1000000); t < _b; ++t) {
if (cur == f) {
break;
}
int toMove = true;
if (mp.find(t) != mp.end()) {
int x = mp[t].first;
int y = mp[t].second;
if (cur >= x && cur <= y) {
toMove = false;
}
if (cur + 1 >= x && cur + 1 <= y) {
toMove = false;
}
}
if (toMove) {
cur++;
ans += 'R';
} else {
ans += 'X';
}
}
} else {
int cur = s;
for (int t = (1), _b = (1000000); t < _b; ++t) {
if (cur == f) {
break;
}
int toMove = true;
if (mp.find(t) != mp.end()) {
int x = mp[t].first;
int y = mp[t].second;
if (cur >= x && cur <= y) {
toMove = false;
}
if (cur - 1 >= x && cur - 1 <= y) {
toMove = false;
}
}
if (toMove) {
cur--;
ans += 'L';
} else {
ans += 'X';
}
}
}
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;
inline bool bad(int s, int l, int r) { return s <= r && s >= l; }
int main() {
int n, m, s, f, inc, pascr;
string str;
char lorr;
cin >> n >> m >> s >> f;
if (s > f) {
inc = -1;
lorr = 'L';
} else {
inc = 1;
lorr = 'R';
}
int t, l, r, q;
bool stop = 0;
pascr = 1;
if (f == s) stop = 1;
for (int i = 1; i <= m && (!stop); i++) {
cin >> t >> l >> r;
if (t == pascr) {
if (bad(s, l, r) || bad(s + inc, l, r))
str += 'X';
else {
str += lorr;
s += inc;
}
} else {
q = min(t - pascr, (f - s) * inc);
str += string(q, lorr);
s += q * inc;
if (!(s == f))
if (bad(s, l, r) || bad(s + inc, l, r))
str += 'X';
else {
str += lorr;
s += inc;
}
}
if (s == f) stop = 1;
pascr = t + 1;
}
if (s != f) {
q = (f - s) * inc;
str += string(q, lorr);
}
cout << str;
return 0;
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, s, f, i, j, curr = 1, t, l, r;
cin >> n >> m >> s >> f;
for (i = 1; i <= m; i++) {
cin >> t >> l >> r;
if (s == f) break;
for (j = curr; j < t; j++) {
if (s < f)
cout << "R", s++;
else if (s > f)
cout << "L", s--;
}
if ((s + 1 < l || s + 1 > r) && (s < l || s > r) && (s < f))
cout << "R", s++;
else if ((s < l || s > r) && (s - 1 < l || s - 1 > r) && (s > f))
cout << "L", s--;
else if (s != f)
cout << "X";
curr = t + 1;
}
while (s < f) cout << "R", s++;
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>
using namespace std;
int main() {
int n, m, s, f;
cin >> n >> m >> s >> f;
int ct = 0;
for (int i = 1; i <= m; i++) {
int t, l, r;
scanf("%d %d %d", &t, &l, &r);
while (ct < t - 1) {
ct++;
if (s < f) {
s++;
putchar('R');
} else if (s > f) {
s--;
putchar('L');
} else
return 0;
}
int ts;
if (s < f) {
ts = s + 1;
} else if (s > f) {
ts = s - 1;
} else
return 0;
if (ts <= r && ts >= l || s <= r && s >= l)
putchar('X');
else {
if (ts > s)
putchar('R');
else
putchar('L');
s = ts;
}
ct = t;
}
if (s < f)
while (s++ < f) putchar('R');
else
while (s-- > f) 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 | #include <bits/stdc++.h>
using namespace std;
struct stof {
int st, fi;
};
int main() {
int n, m, s, f, l, r;
cin >> n >> m >> s >> f;
map<long, struct stof> mp;
long t, i = 1;
while (m--) {
cin >> t >> l >> r;
mp[t].st = l, mp[t].fi = r;
}
map<long, struct stof>::iterator it;
it = mp.begin();
if (f > s) {
while (s != f && it != mp.end()) {
if (it->first != i || (it->second).st > s + 1 || (it->second).fi < s) {
cout << "R";
s++;
} else
cout << "X";
if (it->first == i) it++;
i++;
}
while (s != f) {
cout << "R";
s++;
}
} else {
while (s != f && it != mp.end()) {
if (it->first != i || (it->second).st > s || (it->second).fi < s - 1) {
cout << "L";
s--;
} else
cout << "X";
if (it->first == i) it++;
i++;
}
while (s != f) {
cout << "L";
s--;
}
}
}
| 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 = 100010;
vector<char> ve;
struct node {
int t, l, r;
bool operator<(const node& cc) const { return cc.t > t; }
} A[MAXN];
int main() {
int n, m, s, f, t, l, r, i, cur, fl, cnt;
while (scanf("%d%d%d%d", &n, &m, &s, &f) != EOF) {
ve.clear();
cur = 1;
cnt = s;
if (s > f)
fl = -1;
else
fl = 1;
for (i = 1; i <= m; i++) {
scanf("%d%d%d", &A[i].t, &A[i].l, &A[i].r);
}
sort(A + 1, A + m + 1);
for (i = 1; i <= m; i++) {
t = A[i].t;
l = A[i].l;
r = A[i].r;
while (cur < t) {
cur++;
if (fl == -1) {
ve.push_back('L');
cnt--;
} else if (fl == 1) {
ve.push_back('R');
cnt++;
}
if (cnt == f) break;
}
if (cnt == f)
break;
else {
if (fl == 1) {
if (!(cnt >= l && cnt <= r) && !(cnt + 1 >= l && cnt + 1 <= r)) {
ve.push_back('R');
cnt++;
} else {
ve.push_back('X');
}
cur++;
} else if (fl == -1) {
if (!(cnt >= l && cnt <= r) && !(cnt - 1 >= l && cnt - 1 <= r)) {
ve.push_back('L');
cnt--;
} else {
ve.push_back('X');
}
cur++;
}
if (cnt == f) break;
}
}
while (cnt != f) {
if (fl == 1) {
ve.push_back('R');
cnt++;
} else if (fl == -1) {
ve.push_back('L');
cnt--;
}
}
cur = ve.size();
for (i = 0; i < cur; i++) printf("%c", ve[i]);
printf("\n");
}
return 0;
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | import java.util.*;
import java.io.*;
public class Main342B
{
static PrintWriter out=new PrintWriter(System.out);
public static void main(String[] args) throws IOException
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int s=sc.nextInt();
int f=sc.nextInt();
Vector<Character> ans=new Vector<Character>();
int[] l=new int[m];
int[] t=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 step=1;
while(s!=f)
{
int id=-1;
if(step-1<m&&step==t[step-1])
id=step-1;
else if(step<=t[m-1])
id=Arrays.binarySearch(t,step);
//out.println(id);
if(id>=0)
{
if(s<f)
{
if(l[id]>s+1||r[id]<s)
{
ans.add('R');
s++;
}
else
ans.add('X');
}
else
{
if(r[id]<s-1||l[id]>s)
{
ans.add('L');
s--;
}
else
ans.add('X');
}
step++;
}
else
{
if(s<f){
ans.add('R');
s++;
}
else {
ans.add('L');
s-=1;
}
step++;
}
}
//out.println(ans);
for(char ch:ans)
out.print(ch);
out.flush();
}
static class Scanner
{
BufferedReader br;
StringTokenizer tk=new StringTokenizer("");
public Scanner(InputStream is)
{
br=new BufferedReader(new InputStreamReader(is));
}
public int nextInt() throws IOException
{
if(tk.hasMoreTokens())
return Integer.parseInt(tk.nextToken());
tk=new StringTokenizer(br.readLine());
return nextInt();
}
public long nextLong() throws IOException
{
if(tk.hasMoreTokens())
return Long.parseLong(tk.nextToken());
tk=new StringTokenizer(br.readLine());
return nextLong();
}
public String next() throws IOException
{
if(tk.hasMoreTokens())
return (tk.nextToken());
tk=new StringTokenizer(br.readLine());
return next();
}
public String nextLine() throws IOException
{
tk=new StringTokenizer("");
return br.readLine();
}
public double nextDouble() throws IOException
{
if(tk.hasMoreTokens())
return Double.parseDouble(tk.nextToken());
tk=new StringTokenizer(br.readLine());
return nextDouble();
}
public char nextChar() throws IOException
{
if(tk.hasMoreTokens())
return (tk.nextToken().charAt(0));
tk=new StringTokenizer(br.readLine());
return nextChar();
}
public int[] nextIntArray(int n) throws IOException
{
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=nextInt();
return a;
}
public long[] nextLongArray(int n) throws IOException
{
long a[]=new long[n];
for(int i=0;i<n;i++)
a[i]=nextLong();
return a;
}
public int[] nextIntArrayOneBased(int n) throws IOException
{
int a[]=new int[n+1];
for(int i=1;i<=n;i++)
a[i]=nextInt();
return a;
}
public long[] nextLongArrayOneBased(int n) throws IOException
{
long a[]=new long[n+1];
for(int i=1;i<=n;i++)
a[i]=nextLong();
return a;
}
}
}
| JAVA |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
final static double EPS = 1e-7;
final static long MOD = 1000000007;
static void solve() throws Exception {
int n = nextInt();
int m = nextInt();
int s = nextInt();
int f = nextInt();
int t = 0, l = 0, r = 0;
int q = 0;
for (int i = 1; s != f; i++) {
if (q < m && t < i) {
t = nextInt();
l = nextInt();
r = nextInt();
q++;
}
if (s < f) {
if (i == t) {
if (!(s+1 >= l && s+1 <= r) && !(s >= l && s <= r)) {
s++;
out.print("R");
} else {
out.print("X");
}
} else {
s++;
out.print("R");
}
} else {
if (i == t) {
if (!(s-1 >= l && s-1 <= r) && !(s >= l && s <= r)) {
s--;
out.print("L");
} else {
out.print("X");
}
} else {
s--;
out.print("L");
}
}
}
out.println();
}
static int sqr(int x) {
return x*x;
}
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());
}
static BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
static String next() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
static String nextLine() throws IOException {
tok = new StringTokenizer("");
return in.readLine();
}
static boolean hasNext() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
String s = in.readLine();
if (s == null) {
return false;
}
tok = new StringTokenizer(s);
}
return true;
}
public static void main(String args[]) {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
//in = new BufferedReader(new FileReader("input.in"));
//out = new PrintWriter(new FileWriter("output.out"));
solve();
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
java.lang.System.exit(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>
long labs(long input) { return (input < 0) ? -input : input; }
int main() {
long n, m, s, f;
scanf("%ld %ld %ld %ld\n", &n, &m, &s, &f);
long holder(s), dir(0);
char move;
if (s < f) {
dir = 1;
move = 'R';
} else {
dir = -1;
move = 'L';
}
long currentTime(0);
bool done(0);
for (int k = 0; k < m; k++) {
long t, l, r;
scanf("%ld %ld %ld\n", &t, &l, &r);
++currentTime;
if (currentTime < t) {
long meantime = t - currentTime;
if (meantime >= labs(f - holder)) {
meantime = labs(f - holder);
}
std::string breakMoves(meantime, move);
std::cout << breakMoves;
holder += dir * meantime;
currentTime = t;
}
if (holder == f) {
break;
}
if ((dir > 0 && l - 1 <= holder && holder <= r) ||
(dir < 0 && l <= holder && holder <= r + 1)) {
printf("X");
} else {
printf("%c", move);
holder += dir;
}
if (holder == f) {
done = 1;
break;
}
}
if (!done) {
std::string moreMoves(labs(f - holder), move);
std::cout << moreMoves;
}
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;
const int maxn = 100001;
int l[maxn], r[maxn], t[maxn], n, m, b, e, f[maxn], pt, pp;
int tt, dis;
inline bool underspy(int s) { return ((s >= l[pt]) && (s <= r[pt])); }
int main() {
scanf("%d%d%d%d", &n, &m, &b, &e);
memset(f, -1, n);
for (int i = 0; i < m; i++) scanf("%d%d%d", t + i, l + i, r + i);
pt = 0;
if (e == b) {
printf("0\n");
return 0;
}
if (e < b)
dis = -1;
else
dis = 1;
pp = b;
f[pp] = 0;
tt = 0;
while (pp != e) {
tt++;
if (tt == t[pt]) {
if (!(underspy(pp) || underspy(pp + dis))) {
printf("%c", (dis == -1) ? 'L' : 'R');
f[pp + dis] = tt;
pp += dis;
} else {
printf("X");
f[pp]++;
}
pt++;
} else {
printf("%c", (dis == -1) ? 'L' : 'R');
f[pp + dis] = tt;
pp += dis;
}
}
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 n, m, s, f, p = 1, v = 1;
cin >> n >> m >> s >> f;
string g;
int t[m], a[m], b[m];
for (int i = 0; i < m; i++) {
cin >> t[i] >> a[i] >> b[i];
}
if (s < f) {
for (int i = 0; i < m; i++) {
if (((a[i] <= s + 1 && s + 1 <= b[i]) || (a[i] <= s && s <= b[i])) &&
t[i] == v) {
g += 'X', v++, p = t[i];
} else {
if (t[i] != v) {
while (s != f && p < t[i]) {
p++;
g += 'R';
s += 1;
v++;
}
i--;
} else {
while (s != f && p < t[i]) {
p++;
g += 'R';
s += 1;
v++;
}
}
if (s == f) break;
}
}
if (s != f) {
while (s != f) {
g += 'R';
s++;
}
}
} else if (s > f) {
for (int i = 0; i < m; i++) {
if (((a[i] <= s - 1 && s - 1 <= b[i]) || (a[i] <= s && s <= b[i])) &&
t[i] == v) {
g += 'X', v++, p = t[i];
} else {
if (t[i] != v) {
while (s != f && p < t[i]) {
p++;
g += 'L';
s -= 1;
v++;
}
i--;
} else {
while (s != f && p < t[i]) {
p++;
g += 'L';
s -= 1;
v++;
}
}
if (s == f) break;
}
}
if (s != f) {
while (s != f) {
g += 'L';
s--;
}
}
}
cout << g;
return 0;
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
public class ProblemB {
static final int INF = 100000000;
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
String[] nmsf = in.readLine().split(" ");
int n = Integer.valueOf(nmsf[0]);
int m = Integer.valueOf(nmsf[1]);
int s = Integer.valueOf(nmsf[2]);
int f = Integer.valueOf(nmsf[3]);
int[][] check = new int[m][3];
for (int i = 0 ; i < m ; i++) {
String[] tlr = in.readLine().split(" ");
check[i][0] = Integer.valueOf(tlr[0]);
check[i][1] = Integer.valueOf(tlr[1]);
check[i][2] = Integer.valueOf(tlr[2]);
}
int dir = (f-s)/Math.abs(f-s);
char mv = (dir == 1) ? 'R' : 'L';
int time = 1;
int ci = 0;
while (f != s) {
while (ci < m && check[ci][0] < time) {
ci++;
}
if (ci < m && time == check[ci][0] && ((check[ci][1] <= s && s <= check[ci][2]) || (check[ci][1] <= s+dir && s+dir <= check[ci][2]))) {
out.print('X');
} else {
out.print(mv);
s += dir;
}
time++;
}
out.println();
out.flush();
}
public static void debug(Object... o) {
System.err.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>
using namespace std;
int n, m, f, s, l[100011], r[100011], pos, del, ps = 1, timer = 0, l1;
long t[100011];
char sym;
int main() {
cin >> n >> m >> s >> f;
for (int i = 1; i <= m; i++) cin >> t[i] >> l[i] >> r[i];
t[m + 1] = -1;
pos = s;
if (s < f) {
sym = 'R';
del = 1;
l1 = 0;
} else {
sym = 'L';
del = -1;
l1 = -1;
}
while (pos != f) {
timer++;
if (timer != t[ps]) {
cout << sym;
pos += del;
} else if (t[ps] != -1) {
if (l[ps] > pos + 1 + l1 || r[ps] < pos + l1) {
cout << sym;
pos += del;
} else
cout << "X";
ps++;
}
}
cin >> 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 n, m, s, f;
cin >> n >> m >> s >> f;
int w[m][3];
for (int i = 0; i < m; i++) cin >> w[i][0] >> w[i][1] >> w[i][2];
string ans = "";
char d = s > f ? 'L' : 'R';
int watch = w[0][0];
int wi = 0;
for (int i = 1; s != f; i++) {
if (watch == i) {
if ((s >= w[wi][1] && s <= w[wi][2]) ||
(s + (d == 'L' ? -1 : 1) >= w[wi][1] &&
s + (d == 'L' ? -1 : 1) <= w[wi][2]))
ans += 'X';
else {
ans += d;
s += (d == 'L' ? -1 : 1);
}
wi++;
watch = w[wi][0];
} else {
ans += d;
s += (d == 'L' ? -1 : 1);
}
}
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 | def checkKey(dict, key):
if key in dict:
return True
return False
# def helper(s):
# l=len(s)
# if (l==1):
# l=[]
# l.append(s)
# return l
# ch=s[0]
# recresult=helper(s[1:])
# myresult=[]
# myresult.append(ch)
# for st in recresult:
# myresult.append(st)
# ts=ch+st
# myresult.append(ts)
# return myresult
# mod=1000000000+7
# def helper(s,n,open,close,i):
# if(i==2*n):
# for i in s:
# print(i,end='')
# print()
# return
# if(open<n):
# s[i]='('
# helper(s,n,open+1,close,i+1)
# if(close<open):
# s[i]=')'
# helper(s,n,open,close+1,i+1)
# def helper(arr,i,n):
# if(i==n-1):
# recresult=[arr[i]]
# return recresult
# digit=arr[i]
# recresult=helper(arr,i+1,n)
# myresult=[]
# for i in recresult:
# myresult.append(i)
# myresult.append(i+digit);
# myresult.append(digit)
# return myresult
# import copy
# n=int(input())
# arr=list(map(int,input().split()))
# ans=[]
# def helper(arr,i,n):
# if(i==n-1):
# # for a in arr:
# # print(a,end=" ")
# # print()
# l=copy.deepcopy(arr)
# ans.append(l)
# return
# for j in range(i,n):
# if(i!=j):
# if(arr[i]==arr[j]):
# continue
# else:
# arr[i],arr[j]=arr[j],arr[i]
# helper(arr,i+1,n)
# arr[j],arr[i]=arr[i],arr[j]
# else:
# arr[i],arr[j]=arr[j],arr[i]
# helper(arr,i+1,n)
# def helper(sol,n,m):
# for i in range(n+1):
# for j in range(m+1):
# print(sol[i][j],end=" ")
# print()
# def rat_in_a_maze(maze,sol,i,j,n,m):
# if(i==n and j==m):
# sol[i][j]=1
# helper(sol,n,m)
# exit()
# if(i>n or j>m):
# return False
# if(maze[i][j]=='X'):
# return False
# maze[i][j]='X'
# sol[i][j]=1
# if(rat_in_a_maze(maze,sol,i,j+1,n,m)):
# return True
# elif(rat_in_a_maze(maze,sol,i+1,j,n,m)):
# return True
# sol[i][j]=0
# return False
n,m,s,f=map(int,input().split())
d={}
for i__ in range(m):
t,l,r=map(int,input().split())
d[t]=(l,r)
if(f>s):
ans=""
cp=s
time=1
while(cp!=f):
if time in d:
start=d[time][0]
end=d[time][1]
if(cp>=start and cp<=end or cp+1>=start and cp+1<=end):
ans+='X'
time+=1
else:
ans+='R'
time+=1
cp+=1
else:
ans+='R'
time+=1
cp+=1
print(ans)
elif(f<s):
ans=""
cp=s
time=1
while(cp!=f):
if time in d:
start=d[time][0]
end=d[time][1]
if(cp>=start and cp<=end or cp-1>=start and cp-1<=end):
ans+='X'
time+=1
else:
ans+='L'
time+=1
cp-=1
else:
ans+='L'
time+=1
cp-=1
print(ans)
| PYTHON3 |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
#pragma GCC optimise("Ofast")
using namespace std;
const int mod = 1e+9 + 7;
long long binomialCoeff(long long n, long long k) {
long long res = 1;
if (n < k) return 0;
if (k > n - k) k = n - k;
for (long long i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
bool tracked(int ptr, pair<int, int> p) {
if (ptr >= p.first && ptr <= p.second)
return true;
else
return false;
}
void solve() {
int n, m, second, first, t, l, r, step = 1, ptr, chg;
cin >> n >> m >> second >> first;
ptr = second;
pair<int, int> p;
string ans = "", def;
map<int, pair<int, int> > mp;
for (long long i = 0; i < m; i++) {
cin >> t >> l >> r;
mp[t] = {l, r};
}
chg = (first > second ? 1 : -1);
if (chg == 1) {
def = "R";
} else
def = "L";
while (ptr != first) {
if (mp.find(step) != mp.end()) {
p = mp[step];
if (!tracked(ptr, p) && !tracked(ptr + chg, p)) {
ptr = ptr + chg;
cout << def;
} else
cout << "X";
} else {
ptr = ptr + chg;
cout << def;
}
step++;
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
while (t--) solve();
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, s, f;
cin >> n >> m >> s >> f;
int t = 1;
char ch = 'R';
if (f < s) {
ch = 'L';
t = -1;
}
int track = 0;
string ans = "";
map<int, pair<int, int> > M;
for (int i = 0; i < m; i++) {
track++;
if (s == f) break;
int k, l, r;
cin >> k >> l >> r;
M[k] = {l, r};
if (M.count(track) == 0) {
ans += ch;
s += t;
} else {
if ((M[track].first <= s && s <= M[track].second) ||
(M[track].first <= s + t && s + t <= M[track].second)) {
ans += 'X';
} else {
ans += ch;
s += t;
}
}
}
while (s != f) {
ans += ch;
s += t;
}
cout << ans;
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, s, f;
cin >> n >> m >> s >> f;
long long t[m + 5], l[m + 5], r[m + 5];
memset(t, 0, sizeof(t));
long long pos = 0;
for (int i = 1; i <= m; i++) cin >> t[i] >> l[i] >> r[i];
int current = s;
if (f > s) {
for (int i = 1; i <= m; i++) {
if (current == f) return 0;
long long dif = t[i] - pos;
pos = t[i];
long long mn = min(dif - 1, f - current);
for (int j = 1; j <= mn; j++) cout << "R";
current += mn;
if (current == f) return 0;
if (current >= l[i] && current <= r[i]) {
cout << "X";
} else if ((current + 1) >= l[i] && (current + 1) <= r[i]) {
cout << "X";
} else {
cout << "R";
current++;
}
}
if (current != f) {
for (int i = current; i < f; i++) cout << "R";
}
}
if (f < s) {
for (int i = 1; i <= m; i++) {
if (current == f) return 0;
long long dif = t[i] - pos;
pos = t[i];
long long mn = min(dif - 1, current - f);
for (int j = 1; j <= mn; j++) cout << "L";
current -= mn;
if (current == f) return 0;
if (current >= l[i] && current <= r[i]) {
cout << "X";
} else if ((current - 1) >= l[i] && (current - 1) <= r[i]) {
cout << "X";
} else {
cout << "L";
current--;
}
}
if (current != f) {
for (int i = f; i < current; i++) cout << "L";
}
}
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, m, s, f, t[200000], l[200000], r[200000];
int main() {
string ans = "";
int moment = 0, poss;
cin >> n >> m >> s >> f;
for (int i = 0; i < m; i++) {
cin >> t[i] >> l[i] >> r[i];
}
while (true) {
if (s == f) break;
moment++;
poss = lower_bound(t, t + m, moment) - t;
if (t[poss] == moment) {
if (l[poss] <= s && s <= r[poss]) {
ans += 'X';
continue;
}
if (s < f && l[poss] <= s + 1 && s + 1 <= r[poss]) {
ans += 'X';
continue;
}
if (s > f && l[poss] <= s - 1 && s - 1 <= r[poss]) {
ans += 'X';
continue;
}
if (s < f)
ans += 'R', s++;
else
ans += 'L', s--;
} else {
if (s < f)
ans += 'R', s++;
else
ans += 'L', s--;
}
}
cout << ans;
return 0;
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | n,m,s,f=map(int,raw_input().split())
now=s
spy=[]
for i in range(m):
x,l,r=map(int,raw_input().split())
spy.append([x,l,r])
spy.sort(key=lambda x:[x[0],x[1]])
if s>f:
move=-1
x="L"
if s<=f:
move=1
x="R"
cnt=0
tm=0
i=0
ans=''
while i<m:
tm+=1
if now==f:
#print('jo')
break
a,b,c=spy[i][0],spy[i][1],spy[i][2]
if a>tm:
ans+=x
now+=move
if tm>a:
#print('ji')
#ans+=x
now+=move
#i+=1
if a==tm:
# print(now,now+move)
if b<=now<=c or b<=now+move<=c:
ans+='X'
else:
ans+=x
now+=move
i+=1
#print(ans,now)
#print(now)
while now!=f:
ans+=x
now+=move
print(ans) | PYTHON |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, s, f, t, l, r;
cin >> n >> m >> s >> f;
int cur = s, lastt = 1;
for (int i = 0; i < m; i++) {
cin >> t >> l >> r;
if (cur != f) {
for (int i = lastt; i < t; i++) {
if (s < f) {
cur++;
cout << "R";
} else {
cur--;
cout << "L";
}
if (cur == f) break;
}
}
if (cur != f) {
if (cur > (l - 1) && cur < (r + 1))
cout << "X";
else if (s < f) {
if (cur != (l - 1)) {
cur++;
cout << "R";
} else
cout << "X";
} else {
if (cur != (r + 1)) {
cur--;
cout << "L";
} else
cout << "X";
}
}
lastt = t + 1;
}
while (cur != f) {
if (s < f) {
cur++;
cout << "R";
} else {
cur--;
cout << "L";
}
}
return 0;
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, s, f, t = 1, a = 0;
cin >> n >> m >> s >> f;
int arr[m][3];
for (int i = 0; i < m; i++) cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
while (s != f) {
if (s < f) {
if (t == arr[a][0]) {
if (s + 1 >= arr[a][1] && s <= arr[a][2] && a < m) {
cout << "X";
} else {
cout << "R";
s++;
}
a++;
t++;
} else {
cout << "R";
s++;
t++;
}
}
if (s > f) {
if (t == arr[a][0]) {
if (s >= arr[a][1] && s - 1 <= arr[a][2] && a < m) {
cout << "X";
} else {
cout << "L";
s--;
}
a++;
t++;
} else {
cout << "L";
s--;
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 | #include <bits/stdc++.h>
using namespace std;
int t[100005], l[100005], r[100005], n, m, s, f, k;
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 p;
if (s < f)
p = 1;
else
p = -1;
for (int i = 0; i < m && s != f; ++i) {
int tt = (i == 0 ? t[i] : t[i] - t[i - 1]);
int q;
if (i == 0)
q = t[i];
else
q = t[i] - t[i - 1];
q--;
if ((f - s) / p < q) {
q = (f - s) / p;
s = f;
} else
s += p * q;
for (int j = 1; j <= q; ++j) printf("%c", p == 1 ? 'R' : 'L');
if (s == f) break;
k = s + p;
if (k >= l[i] && k <= r[i] || s >= l[i] && s <= r[i])
printf("X");
else {
printf("%c", p == 1 ? 'R' : 'L');
s = k;
}
}
if (s != f) {
int q = (f - s) / p;
for (int j = 1; j <= q; ++j) printf("%c", p == 1 ? 'R' : 'L');
}
printf("\n");
return 0;
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, s, f, t, l, r, j = 1, get = 0;
char str[100005] = {'\0'};
scanf("%d %d %d %d", &n, &m, &s, &f);
if (s < f) {
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &t, &l, &r);
if (get == 0) {
if (j != t) {
while (j < t) {
cout << 'R';
j++;
s++;
if (s == f) {
get = 1;
break;
}
}
}
if (j == t && get == 0) {
if ((s >= l && s <= r) || (s + 1 >= l && s + 1 <= r)) {
cout << 'X';
j++;
} else {
cout << 'R';
j++;
s++;
if (s == f) {
get = 1;
}
}
}
}
}
if (s < f) {
while (s != f) {
s++;
cout << 'R';
j++;
}
}
} else {
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &t, &l, &r);
if (get == 0) {
if (j != t) {
while (j < t) {
cout << 'L';
j++;
s--;
if (s == f) {
get = 1;
break;
}
}
}
if (j == t && get == 0) {
if ((s >= l && s <= r) || (s - 1 >= l && s - 1 <= r)) {
cout << 'X';
j++;
} else {
cout << 'L';
j++;
s--;
if (s == f) {
get = 1;
}
}
}
}
}
if (s > f) {
while (s != f) {
s--;
cout << 'L';
j++;
}
}
}
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;
int n, m, s, f, l[100014], r[100014], ti[100014], cp, cm, t, d;
string res;
int main(int argc, char* argv[]) {
cin >> n >> m >> s >> f;
for (int i = 0; i < (m); i++) cin >> ti[i] >> l[i] >> r[i];
cp = s;
cm = 0;
t = 1;
while (cp != f) {
++t;
d = (cp > f ? -1 : 1);
if (ti[cm] == t - 1) {
++cm;
if ((cp >= l[cm - 1] && cp <= r[cm - 1]) ||
(cp + d >= l[cm - 1] && cp + d <= r[cm - 1])) {
res += "X";
continue;
}
}
res += (d > 0 ? "R" : "L");
cp += d;
}
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 | import java.util.*;
import java.io.*;
public class Main342B
{
static PrintWriter out=new PrintWriter(System.out);
public static void main(String[] args) throws IOException
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int s=sc.nextInt();
int f=sc.nextInt();
Vector<Character> ans=new Vector<Character>();
int[] l=new int[m];
int[] t=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 step=1;
while(s!=f)
{
int id=-1;
id=Arrays.binarySearch(t,step);
if(id>=0)
{
if(s<f)
{
if(l[id]>s+1||r[id]<s)
{
ans.add('R');
s++;
}
else
ans.add('X');
}
else
{
if(r[id]<s-1||l[id]>s)
{
ans.add('L');
s--;
}
else
ans.add('X');
}
step++;
}
else
{
if(s<f){
ans.add('R');
s++;
}
else {
ans.add('L');
s-=1;
}
step++;
}
}
for(char ch:ans)
out.print(ch);
out.flush();
}
static class Scanner
{
BufferedReader br;
StringTokenizer tk=new StringTokenizer("");
public Scanner(InputStream is)
{
br=new BufferedReader(new InputStreamReader(is));
}
public int nextInt() throws IOException
{
if(tk.hasMoreTokens())
return Integer.parseInt(tk.nextToken());
tk=new StringTokenizer(br.readLine());
return nextInt();
}
public long nextLong() throws IOException
{
if(tk.hasMoreTokens())
return Long.parseLong(tk.nextToken());
tk=new StringTokenizer(br.readLine());
return nextLong();
}
public String next() throws IOException
{
if(tk.hasMoreTokens())
return (tk.nextToken());
tk=new StringTokenizer(br.readLine());
return next();
}
public String nextLine() throws IOException
{
tk=new StringTokenizer("");
return br.readLine();
}
public double nextDouble() throws IOException
{
if(tk.hasMoreTokens())
return Double.parseDouble(tk.nextToken());
tk=new StringTokenizer(br.readLine());
return nextDouble();
}
public char nextChar() throws IOException
{
if(tk.hasMoreTokens())
return (tk.nextToken().charAt(0));
tk=new StringTokenizer(br.readLine());
return nextChar();
}
public int[] nextIntArray(int n) throws IOException
{
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=nextInt();
return a;
}
public long[] nextLongArray(int n) throws IOException
{
long a[]=new long[n];
for(int i=0;i<n;i++)
a[i]=nextLong();
return a;
}
public int[] nextIntArrayOneBased(int n) throws IOException
{
int a[]=new int[n+1];
for(int i=1;i<=n;i++)
a[i]=nextInt();
return a;
}
public long[] nextLongArrayOneBased(int n) throws IOException
{
long a[]=new long[n+1];
for(int i=1;i<=n;i++)
a[i]=nextLong();
return a;
}
}
}
| JAVA |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long INF = 1000000007;
long long INFLL = (long long)INF * (long long)INF;
long double EPS = 10e-9;
long double pi = 2 * acos(0.0);
long long n, m, s, f;
map<long long, long long> L, R;
void solve() {
cin >> n >> m >> s >> f;
for (long long i = 1; i <= m; i++) {
long long t, l, r;
cin >> t >> l >> r;
L[t] = l;
R[t] = r;
}
for (long long i = 1;; i++) {
if (s == f) break;
if (s < f) {
if (max(L[i], s) <= min(R[i], s + 1))
cout << "X";
else
cout << "R", s++;
} else {
if (max(L[i], s - 1) <= min(R[i], s))
cout << "X";
else
cout << "L", s--;
}
}
}
int32_t main() {
clock_t start, end;
start = clock();
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) solve();
end = clock();
double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
cerr << "\nTime taken by program is : " << fixed << time_taken
<< setprecision(5);
cerr << " sec "
<< "\n";
return 0;
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int N=in.readInt();
int M=in.readInt();
int S=in.readInt();
int F=in.readInt();
int T[]=new int[M+1];
int L[]=new int[M];
int R[]=new int[M];
for (int i=0;i<M;i++){
T[i]=in.readInt();
L[i]=in.readInt();
R[i]=in.readInt();
}
T[M]=-1;
int curr=S,step=0,mark=0,k=(F-S)/(Math.abs(S-F));
int pos=Math.abs(S-F);
char ch=F-S>0?'R':'L';
while (true){
if(T[mark]==step+1){
if((curr>=L[mark]&&curr<=R[mark])||(curr+k>=L[mark]&&curr+k<=R[mark])) out.print('X');
else{ out.print(ch); curr+=k; }
mark++;
}
else{ out.print(ch); curr+=k; }
if (curr==F) break;
step++;
}
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return 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>
int main() {
int n, m, s, f;
scanf("%d%d%d%d", &n, &m, &s, &f);
int now = 0, pos = s, g;
g = s < f ? 1 : -1;
char go = s < f ? 'R' : 'L';
int arr = 0;
for (int i = 0; i < m; ++i) {
int t, l, r;
scanf("%d%d%d", &t, &l, &r);
for (int j = now; j < t - 1; ++j) {
pos += g;
printf("%c", go);
if (pos == f) return 0;
}
if ((l <= pos && r >= pos) || (pos + g >= l && pos + g <= r))
printf("%c", 'X');
else {
pos += g;
printf("%c", go);
if (pos == f) return 0;
}
now = t;
}
while (pos != f) {
pos += g;
printf("%c", go);
}
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;
static const double PI = 3.14159265358979323846264338327950288419716939937511;
int main() {
long long n, m, s, f;
long long l, r, t;
cin >> n >> m >> s >> f;
char c = (s > f) ? 'L' : 'R';
long long cnt = 0, step;
for (long long i = 0; i < m; i++) {
cin >> t >> l >> r;
if (s < f) {
long long mm = min(f - s, t - 1 - cnt);
s += mm;
cnt = t - 1;
cout << string(mm, 'R');
if (s == f)
break;
else if (s + 1 <= r && s + 1 >= l || s <= r && s >= l)
cout << "X";
else {
s++;
cout << "R";
}
cnt++;
if (s == f) break;
}
if (s > f) {
long long mm = min(s - f, t - 1 - cnt);
s -= mm;
cnt = t - 1;
cout << string(mm, 'L');
if (s == f)
break;
else if (s - 1 <= r && s - 1 >= l || s <= r && s >= l)
cout << "X";
else {
s--;
cout << "L";
}
cnt++;
if (s == f) break;
}
}
long long mm;
if (s < f) {
mm = f - s;
cout << string(mm, 'R');
} else if (s > f) {
mm = s - f;
cout << string(mm, 'L');
}
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | import java.io.*;
import java.util.*;
public class XeniaAndSpies {
public static void main(String[] args) throws IOException {
BufferedReader ff = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(ff.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[] t = new int[m+1];
int[] l = new int[m+1];
int[] r = new int[m+1];
for (int i = 0; i < m; i++) {
st = new StringTokenizer(ff.readLine());
t[i] = Integer.parseInt(st.nextToken());
l[i] = Integer.parseInt(st.nextToken());
r[i] = Integer.parseInt(st.nextToken());
}
t[m] = Integer.MAX_VALUE;
StringBuffer sb = new StringBuffer();
int tt = 0;
int index = 0;
while (s != f) {
tt++;
int x = s + (s<f?1:-1);
if (t[index] == tt) {
if ((s >= l[index] && s <= r[index]) || (x >= l[index] && x <= r[index])) {
index++;
sb.append("X");
continue;
}
index++;
}
sb.append(s<f?'R':'L');
s = x;
}
System.out.println(sb);
}
} | JAVA |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 |
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Problem63 {
public static void main(String[] args) {
// TODO Auto-generated method stub
out=new PrintWriter (new BufferedOutputStream(System.out));
FastReader s=new FastReader();
int n=s.nextInt();
int m=s.nextInt();
int start=s.nextInt();
int end=s.nextInt();
StringBuilder ans=new StringBuilder();
int countmove=1;
int[][] input=new int[m][3];
for(int i=0;i<m;i++) {
input[i][0]=s.nextInt();
input[i][1]=s.nextInt();
input[i][2]=s.nextInt();
}
int ptr=0;
while(start!=end) {
int move=start>end?-1:1;
if(ptr<m && countmove==input[ptr][0]) {
if((input[ptr][1]<=start && input[ptr][2]>=start)||(input[ptr][1]<=start+move && input[ptr][2]>=start+move)) {
ans.append("X");
}else {
if(move==-1)ans.append('L');
else ans.append("R");
start+=move;
}
ptr++;
}else {
if(move==-1)ans.append('L');
else ans.append("R");
start+=move;
}
countmove++;
}
out.println(ans);
out.close();
}
public static PrintWriter out;
public static class FastReader {
BufferedReader br;
StringTokenizer st;
//it reads the data about the specified point and divide the data about it ,it is quite fast
//than using direct
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());//converts string to integer
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
}
| JAVA |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
template <typename... T>
void rd(T&... args) {
((cin >> args), ...);
}
template <typename... T>
void wr(T&... args) {
((cout << args << " "), ...);
}
int gcd(int a, int b);
int lcm(int a, int b);
long long _sum(vector<int> a);
void Amritesh() {
int n, m, s, f;
rd(n, m, s, f);
int count = 1;
vector<vector<int> > times;
for (int(i) = (0); (i) < (m); (i)++) {
int t, l, r;
rd(t, l, r);
vector<int> x = {t, l, r};
times.push_back(x);
}
int index = 0;
while (s != f) {
if (index < m && count == times[index][0]) {
int l = times[index][1];
int r = times[index][2];
if (f > s) {
if ((s >= l && s <= r) || (s + 1 >= l && s + 1 <= r))
cout << "X";
else {
cout << "R";
s += 1;
}
} else if (f < s) {
if ((s >= l && s <= r) || (s - 1 >= l && s - 1 <= r))
cout << "X";
else {
cout << "L";
s -= 1;
}
}
index++;
} else {
if (f > s) {
cout << "R";
s += 1;
} else if (f < s) {
cout << "L";
s -= 1;
}
}
count++;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
while (t--) {
Amritesh();
}
return 0;
}
int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
int lcm(int a, int b) { return (a * b) / gcd(a, b); }
long long _sum(vector<int> a) { return accumulate(a.begin(), a.end(), 0ll); }
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
const long long M = 1e9 + 7;
const long long N = 2e5 + 5;
using namespace std;
const double pi = 3.14159265358979323846264338327950;
const long long A = 1e9 + 1;
double esp = 1e-3;
vector<long long> square;
int main() {
ios::sync_with_stdio(0);
;
cin.tie(0);
;
long long n, m, s, f;
cin >> n >> m >> s >> f;
long long x = s;
string str;
long long tt = 0;
while (m--) {
long long t, l, r;
cin >> t >> l >> r;
if (t - tt != 1) {
if (x > f) {
while (t - tt != 1 && x != f) {
str.push_back('L');
x--;
tt++;
}
} else if (x < f) {
while (t - tt != 1 && x != f) {
str.push_back('R');
x++;
tt++;
}
} else
break;
}
if (x == f) break;
if (x < f) {
if (x >= l && x <= r) {
str.push_back('X');
} else if (x + 1 >= l && x + 1 <= r) {
str.push_back('X');
} else {
str.push_back('R');
x++;
}
}
if (x > f) {
if (x >= l && x <= r) {
str.push_back('X');
} else if (x - 1 >= l && x - 1 <= r) {
str.push_back('X');
} else {
str.push_back('L');
x--;
}
}
tt = t;
}
if (x < f) {
while (x != f) {
str.push_back('R');
x++;
}
}
if (x > f) {
while (x != f) {
str.push_back('L');
x--;
}
}
cout << str << 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.util.*;
import java.io.*;
public class File {
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
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());
}
}
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int n = sc.nextInt();
int m = sc.nextInt();
int s = sc.nextInt(); // Start spy (current pos)
int f = sc.nextInt(); // End spy
int currT = 1;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < m && s != f; i++) {
int t = sc.nextInt();
int L = sc.nextInt();
int R = sc.nextInt();
while (currT < t) {
if (s > f) {
s--;
sb.append("L");
}
else if (s < f) {
s++;
sb.append("R");
}
else {
break;
}
currT++;
}
if (s == f) {
break; // We've finished.
}
else if (L <= s && s <= R) {
sb.append("X");
}
else if (s > f && !(L <= s-1 && s-1 <= R)) {
s--;
sb.append("L");
}
else if (s < f && !(L <= s+1 && s+1 <= R)) {
s++;
sb.append("R");
}
else {
sb.append("X");
}
currT++;
}
if (s > f) {
for (int i = 0; i < s - f; i++) {
sb.append("L");
}
}
else if (s < f) {
for (int i = 0; i < f - s; i++) {
sb.append("R");
}
}
out.println(sb.toString());
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>
int main() {
int n, m, s, f, d, ti = 1, t = 1, a, b, x = 0;
scanf("%d%d%d%d", &n, &m, &s, &f);
(s < f) ? d = 1 : d = -1;
scanf("%d%d%d", &t, &a, &b);
x++;
while (s != f) {
if (ti == t) {
if (d == 1) {
if (a <= s + 1 and s <= b)
printf("X");
else
printf("R"), s++;
} else {
if (a <= s and s <= b + 1)
printf("X");
else
printf("L"), s--;
}
if (x != m) {
scanf("%d%d%d", &t, &a, &b);
x++;
}
} else {
s += d;
(d == 1) ? printf("R") : printf("L");
}
ti++;
}
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | /**
* Created by Omar on 2/22/2016.
*/
import java.util.*;
import java.io.*;
public class Xenia {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
//int n=Integer.parseInt(br.readLine());
String[] parts=br.readLine().split(" ");
//int[]a= new int[n];
//for(int i=0;i<n;i++){a[i]=Integer.parseInt(parts[i]);}
//n, m, s and f
int n=Integer.parseInt(parts[0]);
int m=Integer.parseInt(parts[1]);
int s=Integer.parseInt(parts[2]);
int f=Integer.parseInt(parts[3]);
int t;
int l,r;
// l= new int[(int)1e9+1];
// r= new int[(int)1e9+1];
HashSet<Integer> watching= new HashSet<Integer>();
HashMap<Integer,Pair> eyes= new HashMap<Integer,Pair>();
for(int i=1;i<=m;i++){
parts=br.readLine().split(" ");
t=Integer.parseInt(parts[0]);
watching.add(t);
l=Integer.parseInt(parts[1]);
r=Integer.parseInt(parts[2]);
eyes.put(t,new Pair(l,r));
}
int L,R;
boolean pass;
for(int i=1;i<=(int)1e9+1;i++){
if(watching.contains(i)) {
L=eyes.get(i).l;
R=eyes.get(i).r;
if (L <= s && s <= R) {
pw.print("X");
} else if (s > f) {
if (L <= s - 1 && s - 1 <= R) {
pw.print("X");
} else {
pw.print("L");
s--;
}
} else if (s < f) {
if (L <= s + 1 && s + 1 <= R) {
pw.print("X");
} else {
pw.print("R");
s++;
}
}
if(s==f) {
pw.print("\n");
pw.close();
return;
}
}
else{
if (s > f) {
pw.print("L");
s--;
} else if (s < f) {
pw.print("R");
s++;
} if(s==f) {
pw.print("\n");
pw.close();
return;
}
}
}
pw.close();
br.close();
}
static class Pair implements Comparable<Pair>{
int l,r;
Pair(int l, int r){
this.l=l; this.r=r;
}
public String toString(){
return l+" "+r;
}
public int hashCode(){
return (int)(l^ r);
}
public boolean equals(Object arg0){
Pair p = (Pair)arg0;
return (l == p.l && r == p.r);
}
@Override
public int compareTo(Pair o) {
if (l==o.l)
return Integer.compare(r, o.r);
return Integer.compare(l, o.l);
}
}
}
| JAVA |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | import java.util.*;
import java.math.BigInteger;
public class Watermelon {
static int x;
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[][] arr=new int[m][3];
for(int i=0;i<m;i++){
for(int j=0;j<3;j++){
arr[i][j]=sc.nextInt();
}
}
int count=s;
int counter=1;
for(int i=0;i<m;i++){
// System.out.println(count);
if(count==f)
break;
// System.out.println(counter+"c"+count);
if(arr[i][0]!=counter){
if(count>f){
System.out.print("L");
count--;
}
else if(count<f)
{System.out.print("R");count++;}
else{}
counter++;
i--;
// continue;
}
else{ counter++;
if(s<f){
// System.out.println(arr[i][1]+" "+arr[i][2]+" "+count);
if((arr[i][1]>count&&arr[i][1]>(count+1))||(arr[i][2]<count&&arr[i][2]<(count+1))){
count++;
System.out.print("R");
continue;
}
else {System.out.print("X");continue;}
}
if(s>f){
// System.out.println(arr[i][1]+" "+arr[i][2]+" "+count);
if((arr[i][1]>count&&arr[i][1]>(count-1))||(arr[i][2]<count&&arr[i][2]<(count-1))){
count--;
System.out.print("L");
continue;
}
else {System.out.print("X");continue;}
}
}
}
while(true){
if(count==f)
break;
if(count!=f){
if(count<f){
count++;
System.out.print("R");
}
if(count>f){
count--;
System.out.print("L");
}
}}
}
private static int gcd(int a, int b)
{
while (b > 0)
{
int temp = b;
b = a % b; // % is remainder
a = temp;
}
return a;
}
private static int lcm(int a, int b)
{
return a * (b / gcd(a, b));
}
static int[] toFractionPos(int x,int y){
int a=gcd(x,y);
int[] arr={x/a,y/a};
//display(arr);
return arr;
}
static void display(int[][] arr){
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
System.out.println();
}
static void display(String[] arr){
for(int i=0;i<arr.length;i++){
}
System.out.println();
}
static void display(long[] arr){
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
System.out.println();
}
static void display(double[] arr){
// System.out.println();
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]+" ");
}
// System.out.println();
}
static String str(char[] carr){
String str="";
for(int i=0;i<carr.length;i++){
str=str+carr[i];
}
return str;
}
}
class Count implements Comparable{
int i;
ArrayList<Integer> list=new ArrayList<>();
Count(int i,int c){
this.i=i;
list.add(c);
}
Count(int i){
this.i=i;
}
void display(){
System.out.println(i+" "+list);
}
@Override
public int compareTo(Object o) {
Count co=(Count)o;
if(co.i<this.i){
return 1;
}
else if(this.i<co.i){
return -1;
}
else
return 0;
}
}
| JAVA |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, m, s, f;
string ans;
int main() {
cin >> n >> m >> s >> f;
int a;
if (s > f)
a = -1;
else
a = 1;
ans = "";
int t, l, r, last = 0;
bool done = false;
for (int i = 0; i < m; i++) {
cin >> t >> l >> r;
if (done) continue;
for (int j = 1; j < t - last; j++) {
if (a == 1)
ans += "R";
else
ans += "L";
s += a;
if (s == f) {
done = true;
break;
}
}
if (done) continue;
if ((s >= l && s <= r) || (s + a >= l && s + a <= r)) {
ans += "X";
} else {
if (a == 1)
ans += "R";
else
ans += "L";
s += a;
if (s == f) done = true;
}
last = t;
}
int diff = abs(s - f);
for (int i = 0; i < diff; i++)
if (a == 1)
ans += "R";
else
ans += "L";
cout << ans << endl;
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m, s, f;
cin >> n >> m >> s >> f;
map<int, pair<int, int> > ma;
int h = -1;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
h = max(h, a);
ma[a] = {b, c};
}
for (int i = 1; i <= h; i++) {
if (s >= ma[i].first && s <= ma[i].second) {
cout << "X";
} else {
if ((s == ma[i].first - 1 && s < f) || (s == ma[i].second + 1 && s > f)) {
cout << "X";
continue;
}
if (s < f) {
cout << "R";
s++;
if (s == f) break;
} else if (s > f) {
cout << "L";
s--;
if (s == f) break;
}
if (s == f) break;
}
}
if (s < f) {
while (s < f) {
cout << "R";
s++;
}
} else if (s > f) {
while (s > f) {
cout << "L";
s--;
}
}
}
| 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.TreeMap;
public class Spies {
public static void main(String[] args) throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String[] ns = bf.readLine().split(" ");
TreeMap<Integer, int[]> stepsWatched = new TreeMap<>();
int n,m,s,f;
n = Integer.parseInt(ns[0]);
m = Integer.parseInt(ns[1]);
s = Integer.parseInt(ns[2]);
f = Integer.parseInt(ns[3]);
for(int i = 0;i<m;i++){
ns = bf.readLine().split(" ");
// stepsWatched[i][0] = Integer.parseInt(ns[0]);
// stepsWatched[i][1] = Integer.parseInt(ns[1]);
// stepsWatched[i][2] = Integer.parseInt(ns[2]);
stepsWatched.put(Integer.parseInt(ns[0]), new int[]{Integer.parseInt(ns[1]),Integer.parseInt(ns[2])});
}
int watching = 0;
boolean right = s<f;
int[] watched;
int target = 0;
for(int i = 1;s!=f;i++){
target = right?s+1:s-1;
watched = stepsWatched.get(i);
if(watched==null|(watched!=null&&!contains(watched,s,target))){
System.out.print(target>s?"R":"L");
s = target;
}
else{
System.out.print("X");
}
}
}
public static boolean contains(int[] arr,int x,int y){
for(int i = 0;i<arr.length;i++){
if((x>=arr[0]&x<=arr[1])|(y>=arr[0]&y<=arr[1]))
return true;
}
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 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class XeniaAndSpies {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int s = sc.nextInt();
int f = sc.nextInt();
int dx = f>s?1:-1;
char c = f>s?'R':'L';
int cur_time =1;
StringBuilder sb = new StringBuilder();
for(int i=0;i<m;i++ , cur_time++)
{
int t1 = sc.nextInt();
int l = sc.nextInt();
int r = sc.nextInt();
if(s == f)
continue;
while(cur_time<t1 && ((dx==1 && s<f) || (dx==-1 && s>f)))
{
s+=dx;
sb.append(c);
cur_time++;
}
if(s == f)
continue;
if( (s+dx<=r && s+dx>=l) || (s<=r && s>=l) )
sb.append('X');
else
{
s+=dx;
sb.append(c);
}
}
while(s!=f)
{
s+=dx;
sb.append(c);
}
System.out.println(sb);
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public boolean ready() throws IOException {return br.ready();}
}
}
| JAVA |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque, Counter as c
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
# sys.setrecursionlimit(2*pow(10, 6))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(var): sys.stdout.write(str(var))
def outln(var): sys.stdout.write(str(var)+"\n")
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
n, m, s, f = sp()
answer = []
last = 0
for _ in range(m):
ti, li, ri = sp()
diff = ti-last-1
if f < s:
while f < s and diff:
s -= 1
diff -= 1
answer.append('L')
else:
while f > s and diff:
s += 1
answer.append('R')
diff -= 1
if f < s:
if li <= s <= ri or li <= s-1 <= ri:
answer.append('X')
else:
answer.append('L')
s -= 1
elif f > s:
if li <= s <= ri or li <= s+1 <= ri:
answer.append('X')
else:
answer.append('R')
s += 1
else:
break
last = ti
if f > s:
for i in range(f-s):
answer.append('R')
elif f < s:
for i in range(s-f):
answer.append('L')
outln(''.join(answer)) | PYTHON3 |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long Min(long long a, long long b) {
if (a >= b)
return b;
else
return a;
}
long long Max(long long a, long long b) {
if (a >= b)
return a;
else
return b;
}
long long n, i, j;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long m, s, f;
cin >> n >> m >> s >> f;
long long a[m][3];
for (i = 0; i < m; i++) {
for (j = 0; j < 3; j++) {
cin >> a[i][j];
}
}
long long step = 1, c = 0;
char o;
if (s < f) {
o = 'R';
} else {
o = 'L';
}
while (s != f) {
if (a[c][0] == step) {
if (s >= a[c][1] && s <= a[c][2]) {
cout << 'X';
} else if (o == 'R') {
if (s + 1 >= a[c][1] && s + 1 <= a[c][2]) {
cout << 'X';
} else {
cout << o;
s++;
}
} else if (o == 'L') {
if (s - 1 >= a[c][1] && s - 1 <= a[c][2]) {
cout << 'X';
} else {
cout << o;
s--;
}
}
step++;
c++;
} else {
cout << o;
if (o == 'R') {
s++;
} else {
s--;
}
step++;
}
}
return 0;
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
const long double pi = 3.14159265358979323846;
const int inf = (int)1e9;
const int ss = (int)1e6 + 3;
const int base = inf;
bool pred(const pair<int, int>& i, const pair<int, int>& j) {
if (i.first == j.first) {
return i.second > j.second;
} else {
return i.first > j.first;
}
}
bool pred1(const string& i, const string& j) { return i.size() > j.size(); }
int main() {
int n, m, s, f;
cin >> n >> m >> s >> f;
char c;
int sign = 0;
if (s > f) {
c = 'L';
sign = -1;
} else {
c = 'R';
sign = 1;
}
int now = 1;
int k = abs(s - f);
for (int i = 0; i < m; ++i) {
int t, l, r;
scanf("%d%d%d", &t, &l, &r);
if (t > now) {
for (int j = 0; j < t - now && k > 0; ++j, --k) {
cout << c;
s += sign;
}
now = t;
}
++now;
if (k == 0) {
break;
}
int s1 = s + sign;
if ((s < l || s > r) && (s1 < l || s1 > r)) {
s += sign;
--k;
cout << c;
} else {
cout << "X";
}
if (k == 0) {
break;
}
}
while (k > 0) {
cout << c;
--k;
}
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, t[100000], l[100000], r[100000], step, now, a;
int main(int argc, char** argv) {
int i, j, k;
while (scanf(" %d %d %d %d", &n, &m, &s, &f) == 4) {
for (i = 0; i < m; i++) {
scanf(" %d %d %d", &t[i], &l[i], &r[i]);
}
now = s;
a = 0;
step = 1;
if (f > s) {
for (i = s; i < f; i += 0) {
if (t[a] == step &&
((i >= l[a] && i <= r[a]) || (i + 1 >= l[a] && i + 1 <= r[a]))) {
printf("%c", 'X');
a++;
step++;
if (a >= m) {
a--;
}
} else {
printf("%c", 'R');
i++;
if (step >= t[a]) {
a++;
}
step++;
if (a >= m) {
a--;
}
}
}
} else {
for (i = s; i > f; i += 0) {
if (t[a] == step &&
((i >= l[a] && i <= r[a]) || (i - 1 >= l[a] && i - 1 <= r[a]))) {
printf("%c", 'X');
a++;
if (a >= m) {
a--;
}
step++;
} else {
printf("%c", 'L');
i--;
if (step >= t[a]) {
a++;
}
step++;
if (a >= m) {
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 | import java.util.Scanner;
public class NotePasser {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int current = scan.nextInt();
int goal = scan.nextInt();
StringBuilder sb = new StringBuilder();
int step = 1;
int counter = 1;
int watchStep = scan.nextInt();
int watchMin = scan.nextInt();
int watchMax = scan.nextInt();
while(true){
if(current == goal)
break;
if(step == watchStep){
if((current >= watchMin && current <= watchMax) || (current > goal && (current - 1) >= watchMin && (current - 1) <= watchMax) || (current < goal && (current + 1) >= watchMin && (current + 1) <= watchMax)){
sb.append('X');
if(m > counter){
watchStep = scan.nextInt();
watchMin = scan.nextInt();
watchMax = scan.nextInt();
}
step++;
counter++;
continue;
}if(m > counter){
watchStep = scan.nextInt();
watchMin = scan.nextInt();
watchMax = scan.nextInt();
}
counter++;
}
if(current > goal){
current--;
sb.append('L');
}
else {
current++;
sb.append('R');
}
step++;
}
System.out.println(sb.toString());
scan.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.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingDeque;
public class Main {
static class Par {
public int iz;
public int de;
public int ti;
public Par(int t, int i, int d) {
ti = t;
iz = i;
de = d;
}
}
public static void main(String[] args) {
InputStreamReader st = new InputStreamReader(System.in);
Scanner in = new Scanner(new BufferedReader(st));
int n = in.nextInt();
int m = in.nextInt();
int s = in.nextInt();
int f = in.nextInt();
LinkedList<Par> lista = new LinkedList<Par>();
for (int i = 0; i < m; i++) {
lista.add(new Par(in.nextInt(), in.nextInt(), in.nextInt()));
}
int pos = s;
int t = 1;
boolean fin = false;
Iterator iterator = lista.iterator();
Par par = null;
if (s == f) {
System.out.println("");
} else if (s < f) {
for (; iterator.hasNext() && !fin;) {
if (par == null || par.ti < t) {
par = (Par) iterator.next();
}
if (par.ti > t) {
System.out.print("R");
pos++;
} else if (par.ti == t) {
if ((par.iz <= (pos + 1) && par.de >= (pos + 1))
|| (par.iz <= (pos) && par.de >= (pos))) {
System.out.print("X");
} else {
System.out.print("R");
pos++;
}
}
if (pos == f) {
System.out.println();
fin = true;
}
t++;
}
if (!fin) {
for (int i = pos; i < f; i++) {
System.out.print("R");
}
System.out.println();
}
} else {
for (; iterator.hasNext() && !fin;) {
if (par == null || par.ti < t) {
par = (Par) iterator.next();
}
if (par.ti > t) {
System.out.print("L");
pos--;
} else if (par.ti == t) {
if ((par.iz <= (pos - 1) && par.de >= (pos - 1))
|| (par.iz <= (pos) && par.de >= (pos))) {
System.out.print("X");
} else {
System.out.print("L");
pos--;
}
}
if (pos == f) {
System.out.println();
fin = true;
}
t++;
}
if (!fin) {
for (int i = pos; i > f; i--) {
System.out.print("L");
}
System.out.println();
}
}
}
}
| JAVA |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Main {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
String[] param = br.readLine().split(" ");
int n = Integer.parseInt(param[0]);
int m = Integer.parseInt(param[1]);
int s = Integer.parseInt(param[2]);
int f = Integer.parseInt(param[3]);
int[] t = new int[m+1];
int[] l = new int[m+1];
int[] r = new int[m+1];
for (int i = 0; i < m; i++) {
param = br.readLine().split(" ");
t[i] = Integer.parseInt(param[0]);
l[i] = Integer.parseInt(param[1]);
r[i] = Integer.parseInt(param[2]);
}
t[m] = Integer.MAX_VALUE;
if (s < f) {
for (int cp = s, ct = 1, wt = 0; cp != f; ct++) {
if (ct < t[wt]) { pw.print("R"); cp++; }
else {
if (cp+1<l[wt] || r[wt]<cp) { pw.print("R"); cp++; }
else pw.print("X");
wt++;
}
}
} else {
for (int cp = s, ct = 1, wt = 0; cp != f; ct++) {
if (ct < t[wt]) { pw.print("L"); cp--; }
else {
if (cp<l[wt] || r[wt]<cp-1) { pw.print("L"); cp--; }
else pw.print("X");
wt++;
}
}
}
pw.println();
pw.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
| JAVA |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | import java.util.*;
public class B {
public static void main(String [] args){
final Scanner reader = new Scanner(System.in);
final int n = reader.nextInt();
final int m = reader.nextInt();
final int s = reader.nextInt();
final int f = reader.nextInt();
final StringBuilder sb = new StringBuilder();
int current = s;
int phase = 1;
outer:
for (int i = 0; i < m; ++i){
final int t = reader.nextInt();
final int l = reader.nextInt();
final int r = reader.nextInt();
for (int j = phase; j < t; ++j){
final int next = current < f ? current + 1 : current - 1;
sb.append(current < f ? 'R' : 'L');
if (next == f){
current = next;
break outer;
}
current = next;
}
final int next = current < f ? current + 1 : current - 1;
if ((next < l || next > r) && (current < l || current > r)){
sb.append(current < f ? 'R' : 'L');
if (next == f){
current = next;
break;
}
current = next;
}else{
sb.append('X');
}
phase = t + 1;
}
while (current != f){
final int next = current < f ? current + 1 : current - 1;
sb.append(current < f ? 'R' : 'L');
if (next == f){
break;
}
current = next;
}
System.out.println(sb);
reader.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 n, m, s, f, g, t[333333], l[333333], r[333333];
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]);
if (s < f) {
for (int i = 0; i < m; ++i) {
if (i == 0)
g = t[i] - 1;
else
g = t[i] - t[i - 1] - 1;
while (g > 0 && s < f) {
cout << "R";
g--;
s++;
}
if (s < f && ((s + 1) < l[i] || (s + 1) > r[i]) &&
(s < l[i] || s > r[i])) {
s++;
cout << "R";
} else if (s != f)
cout << "X";
}
while (s < f) {
cout << "R";
s++;
}
cout << endl;
} else {
for (int i = 0; i < m; ++i) {
if (i == 0)
g = t[i] - 1;
else
g = t[i] - t[i - 1] - 1;
while (g > 0 && s > f) {
cout << "L";
g--;
s--;
}
if (s > f && ((s - 1) < l[i] || (s - 1) > r[i]) &&
(s < l[i] || s > r[i])) {
s--;
cout << "L";
} else if (s != f)
cout << "X";
}
while (s > f) {
s--;
cout << "L";
}
cout << endl;
}
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | import java.io.*;
import java.util.*;
public class second
{
static long fast_power(long a,long n,long m)
{
if(n==1)
{
return a%m;
}
if(n%2==1)
{
long power = fast_power(a,(n-1)/2,m)%m;
return ((a%m) * ((power*power)%m))%m;
}
long power = fast_power(a,n/2,m)%m;
return (power*power)%m;
}
public static void main(String arr[])
{
Scanner sc= new Scanner(System.in);
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];
int curt = 1;
for(int i =0;i<m;i++)
{
t[i] = sc.nextInt();
l[i] = sc.nextInt();
r[i] = sc.nextInt();
}
StringBuilder response = new StringBuilder();
int dec=1;
if(s>f)dec=-1;
int i=0;
while(s!=f)
{
if(i>=m || t[i]>curt)
{
if(s<f)response.append("R");
else response.append("L");
s=s+dec;
}
else
{
if(s<l[i] || s>r[i])
{
if((s+dec)<l[i] || (s+dec)>r[i])
{
if(s<f)response.append("R");
else response.append("L");
s=s+dec;
}
else response.append("X");
}
else response.append("X");
i++;
}
curt++;
}
System.out.println(response.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 int maxn = 100000 + 10;
struct node {
int t;
int x;
int y;
} tt[maxn];
int main() {
int n, m, s, f;
while (scanf("%d%d%d%d", &n, &m, &s, &f) != EOF) {
memset(tt, 0, sizeof tt);
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &tt[i].t, &tt[i].x, &tt[i].y);
}
int cnt = 0;
int time = 1;
int peo = s;
int jj;
if (f > s)
jj = 1;
else
jj = -1;
while (peo != f) {
if (tt[cnt].t != time) {
peo += jj;
if (jj == 1) {
printf("R");
} else
printf("L");
} else if ((peo < tt[cnt].x || peo > tt[cnt].y) &&
((peo + jj) < tt[cnt].x || (peo + jj) > tt[cnt].y)) {
peo += jj;
if (jj == 1) {
printf("R");
} else
printf("L");
cnt++;
} else {
printf("X");
cnt++;
}
time++;
}
printf("\n");
}
return 0;
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long int MAX = 1e9, max_int = 1e6;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, m, s, f, t, i;
string ans = "";
cin >> n >> m >> s >> f;
map<long long, long long> l, r;
for (i = 0; i < m; i++) {
cin >> t;
cin >> l[t];
cin >> r[t];
}
i = 1;
t = s;
while (1) {
if (l[i] > 0) {
if (f < t) {
if ((t < l[i]) || (t > r[i] + 1)) {
cout << "L";
t--;
} else
cout << "X";
} else if (f > t) {
if ((t > r[i]) || (t < l[i] - 1)) {
cout << "R";
t++;
} else
cout << "X";
} else {
return 0;
}
} else {
if (f < t) {
cout << "L";
t--;
} else if (f > t) {
cout << "R";
t++;
} else
return 0;
}
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 | #include <bits/stdc++.h>
using namespace std;
int main() {
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++;
}
ct = t + 1;
if (s < f) {
if (r < s || l > s + 1) {
cout << "R";
s++;
} else {
cout << "X";
}
} else if (s > f) {
if (r < s - 1 || l > s) {
cout << "L";
s--;
} else {
cout << "X";
}
} else {
break;
}
}
while (s < f) {
cout << "R";
s++;
}
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 | import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Laputa
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, InputReader cin, OutputWriter out) {
int n=cin.nextInt();
int m=cin.nextInt();
int s=cin.nextInt();
int f=cin.nextInt();
int now=s;
int time=1;
int dir=1;
char dirchar;
if(s<f){
dirchar='R';
dir=1;
} else {
dirchar='L';
dir=-1;
}
Nod[] mm=new Nod[m];
for(int i=0;i<m;i++){
mm[i]=new Nod();
mm[i].t=cin.nextInt();
mm[i].left=cin.nextInt();
mm[i].right=cin.nextInt();
}
int cntmm=0;
while(now!=f){
if( cntmm<m &&time==mm[cntmm].t && now>=mm[cntmm].left &&now<=mm[cntmm].right ){
if(mm[cntmm].t==time){
cntmm++;
}
time++;
out.print("X");
continue;
}
int newpos=now+dir;
if( cntmm<m&& time==mm[cntmm].t && newpos>=mm[cntmm].left &&newpos<=mm[cntmm].right ){
if(mm[cntmm].t==time){
cntmm++;
}
time++;
out.print("X");
continue;
}
if(cntmm<m&&mm[cntmm].t==time){
cntmm++;
}
now=newpos;
time++;
out.print(dirchar);
}
out.println();
}
}
class Nod{
int t;
int left,right;
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int 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);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void print(char i) {
writer.print(i);
}
public void println() {
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 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class CF {
public static class Stage {
int num;
int left;
int right;
}
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int n = in.nextInt();
Stage[] stages = new Stage[in.nextInt()];
int start = in.nextInt();
int finish = in.nextInt();
for (int i = 0; i < stages.length; i++) {
Stage stage = new Stage();
stage.num = in.nextInt();
stage.left = in.nextInt();
stage.right = in.nextInt();
stages[i] = stage;
}
if (start < finish) {
int currentStage = 1;
int currentPos = start;
for (int i = 0; i < stages.length; i++) {
if (stages[i].num != currentStage) {
while (currentPos != finish && stages[i].num != currentStage) {
out.print('R');
currentStage++;
currentPos++;
}
}
if (currentPos == finish) {
out.close();
return;
}
if (!(stages[i].left <= currentPos && currentPos <= stages[i].right)
&& currentPos + 1 != stages[i].left) {
currentPos++;
out.print('R');
} else {
out.print('X');
}
currentStage++;
if (currentPos == finish) {
out.close();
return;
}
}
while (currentPos != finish) {
out.print('R');
currentPos++;
}
out.close();
return;
} else {
int currentStage = 1;
int currentPos = start;
for (int i = 0; i < stages.length; i++) {
if (stages[i].num != currentStage) {
while (currentPos != finish && stages[i].num != currentStage) {
out.print('L');
currentStage++;
currentPos--;
}
}
if (currentPos == finish) {
out.close();
return;
}
if (!(stages[i].left <= currentPos && currentPos <= stages[i].right)
&& currentPos - 1 != stages[i].right) {
currentPos--;
out.print('L');
} else {
out.print('X');
}
currentStage++;
if (currentPos == finish) {
out.close();
return;
}
}
while (currentPos != finish) {
out.print('L');
currentPos--;
}
out.close();
return;
}
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
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>
using namespace std;
int main() {
int watchList[100005][2], t[100005], pos, i, time, s, f, n, m;
char ch;
string result;
scanf("%d %d %d %d", &n, &m, &s, &f);
for (i = 0; i < m; i++)
scanf("%d %d %d", &t[i], &watchList[i][0], &watchList[i][1]);
time = 1, i = 0;
while (s != f) {
if (s < f)
ch = 'R', pos = s + 1;
else
ch = 'L', pos = s - 1;
if (time == t[i]) {
if ((pos >= watchList[i][0] && pos <= watchList[i][1]) ||
(s >= watchList[i][0] && s <= watchList[i][1]))
ch = 'X';
i++;
}
if (ch != 'X') s = pos;
result += ch;
time++;
}
printf("%s\n", result.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 | import java.io.IOException;
import java.util.InputMismatchException;
/**
* Created by jizhe on 2016/1/3.
*/
public class XeniaAndSpies {
public static class FasterScanner {
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = System.in.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
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 long nextLong() {
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 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 int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
public static void main(String[] args) {
//Scanner in = new Scanner(new BufferedInputStream(System.in));
FasterScanner in = new FasterScanner();
StringBuilder out = new StringBuilder();
int N = in.nextInt();
int M = in.nextInt();
int S = in.nextInt();
int F = in.nextInt();
int note = S;
int t = 0;
for( int i = 0; i < M; i++ )
{
int T = in.nextInt();
int l = in.nextInt();
int r = in.nextInt();
int tDiff = T-1-t;
int posDiff = F-note;
int steps = Math.min(tDiff, Math.abs(posDiff));
char move;
int next;
//System.out.printf("i: %d, init move steps: %d\n", i, steps);
if( posDiff > 0 )
{
note += steps;
move = 'R';
next = note+1;
}
else
{
note -= steps;
move = 'L';
next = note-1;
}
t += steps;
while( steps != 0 )
{
out.append(move);
if( out.length() >= 50000 )
{
System.out.printf("%s", out.toString());
out.delete(0, out.length());
}
steps--;
}
//System.out.printf("After init move, t: %d, note: %d, next: %d\n", t, note, next);
if( note == F )
{
break;
}
if( (note >= l && note <= r) || (next >= l && next <= r) )
{
out.append('X');
}
else
{
note = next;
out.append(move);
}
t = T;
//System.out.printf("After watching move, t: %d, note: %d\n", t, note);
if( note == F )
{
break;
}
}
if( note != F )
{
char move;
if( note > F )
{
move = 'L';
}
else
{
move = 'R';
}
while( note != F )
{
out.append(move);
if( out.length() >= 50000 )
{
System.out.printf("%s", out.toString());
out.delete(0, out.length());
}
if( note > F )
{
note--;
}
else
{
note++;
}
}
}
System.out.printf("%s\n", out.toString());
}
}
| JAVA |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | import java.io.*;
import java.util.*;
import java.math.*;
public class Main
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokenizer=null;
public static void main(String[] args) throws IOException
{
new Main().execute();
}
void debug(Object...os)
{
System.out.println(Arrays.deepToString(os));
}
int ni() throws IOException
{
return Integer.parseInt(ns());
}
long nl() throws IOException
{
return Long.parseLong(ns());
}
double nd() throws IOException
{
return Double.parseDouble(ns());
}
String ns() throws IOException
{
while (tokenizer == null || !tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(br.readLine());
return tokenizer.nextToken();
}
String nline() throws IOException
{
tokenizer=null;
return br.readLine();
}
//Main Code starts Here
int totalCases, testnum;
int n,m,s,f;
long t[];
int l[];
int r[];
void execute() throws IOException
{
totalCases = 1;
for(testnum = 1; testnum <= totalCases; testnum++)
{
if(!input())
break;
solve();
}
}
void solve() throws IOException
{
int index = s;
long currStep = 0;
int stepIndex = 0;
int offset = 1;
String appendS = "R";
if (s>f) {
offset = -1;
appendS = "L";
}
StringBuffer result = new StringBuffer();
while(true) {
currStep++;
//debug(s,f, index,currStep, stepIndex);
if (stepIndex < m && currStep == t[stepIndex]) {
if ((index >= l[stepIndex] && index <= r[stepIndex]) || (index+offset >= l[stepIndex] && index+offset <= r[stepIndex])) {
result.append("X");
}
else {
index+=offset;
result.append(appendS);
}
stepIndex++;
}
else {
index+=offset;
result.append(appendS);
}
if (index == f) {
break;
}
}
System.out.println(result.toString());
}
boolean input() throws IOException
{
n = ni();
m = ni();
s = ni();
f = ni();
t = new long[m];
l = new int[m];
r = new int[m];
for (int i = 0; i<m; i++) {
t[i] = nl();
l[i] = ni();
r[i] = ni();
}
return true;
}
} | JAVA |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t, l, r, n, m, s, f;
cin >> n >> m >> s >> f;
int cnt = 1;
for (int i = 0; i < m; i++) {
cin >> t >> l >> r;
while (t > cnt && s != f) {
cnt++;
if (s < f)
s++, cout << "R";
else
s--, cout << "L";
}
if (s == f) break;
if (t == cnt) {
if (s > f) {
if (s >= l && s - 1 <= r)
cout << "X", cnt++;
else
cout << "L", cnt++, s--;
} else if (s < f) {
if (s + 1 >= l && s <= r)
cout << "X", cnt++;
else
cout << "R", cnt++, s++;
}
}
}
if (s > f) {
while (s != f) {
cout << "L";
s--;
}
} else if (s < f) {
while (s != f) cout << "R", s++;
}
return 0;
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | import java.util.*;
import java.io.*;
public class Main{
BufferedReader in;
StringTokenizer str = null;
PrintWriter out;
private String next() throws Exception{
if (str == null || !str.hasMoreElements())
str = new StringTokenizer(in.readLine());
return str.nextToken();
}
private int nextInt() throws Exception{
return Integer.parseInt(next());
}
public void run() throws Exception{
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int n = nextInt(), m = nextInt();
int s = nextInt(), f = nextInt();
StringBuilder ret = new StringBuilder();
int t[] = new int[m];
int left[] = new int[m];
int right[] = new int[m];
for(int i=0;i<m;i++) {
t[i] = nextInt();
left[i] = nextInt();
right[i] = nextInt();
}
for(int i=0;i<m;i++) {
int l = left[i];
int r = right[i];
int diff = t[i];
if (i > 0) {
diff-=t[i-1];
}
for(int j=0;j<diff-1;j++) {
if (s < f) {
ret.append("R");
s++;
}else if (s > f) {
ret.append("L");
s--;
}else {
out.println(ret.toString());
out.close();
return;
}
}
if (s == f) {
out.println(ret.toString());
out.close();
return;
}
if (l <= s && s <= r) {
ret.append("X");
}else {
if (s < f) {
if (l <= s+1 && s+1 <= r) {
ret.append("X");
}else{
ret.append("R");
s++;
}
}else{
if (l <= s-1 && s-1 <= r) {
ret.append("X");
}else{
ret.append("L");
s--;
}
}
}
if (s == f) {
out.println(ret.toString());
out.close();
return;
}
}
while(s != f) {
if (s > f) {
ret.append("L");
s--;
}else{
ret.append("R");
s++;
}
}
out.println(ret.toString());
out.close();
}
public static void main(String args[]) throws Exception {
new Main().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;
template <class T>
bool checkMin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool checkMax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
const int INF = 1e9;
const int MN = -1;
const int N = 100000;
int n, m, s, f;
int t[N], l[N], r[N];
int main() {
while (scanf("%d%d%d%d", &n, &m, &s, &f) != EOF) {
for (int i = 0; i < m; i++) scanf("%d%d%d", t + i, l + i, r + i);
int d = f - s < 0 ? -1 : 1;
char print = (d < 0 ? 'L' : 'R');
int time = 1, i = 0;
while (s != f) {
if (i < m && t[i] == time &&
(l[i] <= s && s <= r[i] || l[i] <= s + d && s + d <= r[i])) {
putchar('X');
} else {
putchar(print);
s += d;
}
if (t[i] == time) i++;
time++;
}
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 mod = 1e9 + 7;
int infinite = INT_MAX - 10;
template <typename T>
T power(T x, T y) {
T temp;
if (y == 0) return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else {
if (y > 0)
return x * temp * temp;
else
return (temp * temp) / x;
}
}
int countDigit(long long n) { return floor(log10(n) + 1); }
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
class util {
public:
int t, l, r;
};
void solve() {
int n, m, s, f;
cin >> n >> m >> s >> f;
int t, l, r;
int step = 0, note = s;
vector<util> arr(m);
for (int i = 0; 0 < m ? i < m : i > m; 0 < m ? i += 1 : i -= 1) {
cin >> arr[i].t >> arr[i].l >> arr[i].r;
}
int i = 0;
while (true) {
++step;
t = arr[i].t, l = arr[i].l, r = arr[i].r;
if (note == f) break;
if (step != t) {
if (f > note)
note++, cout << "R";
else if (f < note)
note--, cout << "L";
} else {
if (note >= l and note <= r)
cout << "X";
else if (f > note) {
if (note + 1 >= l and note + 1 <= r)
cout << "X";
else
note++, cout << "R";
} else if (f < note) {
if (note - 1 >= l and note - 1 <= r)
cout << "X";
else
note--, cout << "L";
}
++i;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
while (t--) {
solve();
}
return 0;
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Scanner;
import java.util.TreeMap;
public class Solution{
///////////////////////////////////////////////////////////////////////////
static class FastScanner{
BufferedReader s;
StringTokenizer st;
public FastScanner(){
st = new StringTokenizer("");
s = new BufferedReader(new InputStreamReader(System.in));
}
public FastScanner(File f) throws FileNotFoundException{
st = new StringTokenizer("");
s = new BufferedReader (new FileReader(f));
}
public int nextInt() throws IOException{
if(st.hasMoreTokens())
return Integer.parseInt(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextInt();
}
}
public double nextDouble() throws IOException{
if(st.hasMoreTokens())
return Double.parseDouble(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextDouble();
}
}
public long nextLong() throws IOException{
if(st.hasMoreTokens())
return Long.parseLong(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextLong();
}
}
public String nextString() throws IOException{
if(st.hasMoreTokens())
return st.nextToken();
else{
st = new StringTokenizer(s.readLine());
return nextString();
}
}
public String readLine() throws IOException{
return s.readLine();
}
public void close() throws IOException{
s.close();
}
}
////////////////////////////////////////////////////////////////////
// Number Theory
public static int divisor(int x){
int limit = x;
int numberOfDivisors = 0;
for (int i=1; i < limit; ++i) {
if (x % i == 0) {
limit = x / i;
if (limit != i) {
numberOfDivisors++;
}
numberOfDivisors++;
}
}
return numberOfDivisors;
}
public static long gcd(long a, long b){
return b == 0 ? a : gcd(b,a%b);
}
public static long lcm(long a,long b, long c){
return lcm(lcm(a,b),c);
}
public static long lcm(long a, long b){
return a*b/gcd(a,b);
}
////////////////////////////////////////////////////////////////////
// private static FastScanner s = new FastScanner(new File("input.txt"));
// private static PrintWriter ww = new PrintWriter(new FileWriter("output.txt"));
private static FastScanner s = new FastScanner();
private static PrintWriter ww = new PrintWriter(new OutputStreamWriter(System.out));
// private static Scanner s = new Scanner(System.in);
////////////////////////////////////////////////////////////////////
static class node{
int a;
int b;
public node(int x, int y){
this.a = x;
this.b = y;
}
}
public static void main(String[] args) throws IOException{
int n = s.nextInt();
int m = s.nextInt();
int ss = s.nextInt();
int ff = s.nextInt();
Map<Integer,node> mm = new HashMap<Integer,node>();
node[] no = new node[m];
int cn = 0;
int cnt= 0;
int r = 0; int l = 0;
if(ss > ff) l = 1;
else r = 1;
while(true){
cn++;
int ii = 0; int x = 0; int y = 0;
if(cn <= m){
ii = s.nextInt();
x = s.nextInt();
y = s.nextInt();
mm.put(ii,no[cnt++] = new node(x,y));
}
if(!mm.containsKey(cn)){
if(l == 1){
ww.print("L");
ss--;
}else{
ww.print("R");
ss++;
}
}
else{
node xx = mm.get(cn);
x = xx.a; y = xx.b;
if(l == 1){
if(ss < x && (ss-1) < x || ss > y && (ss-1) > y){
ww.print("L");
ss--;
}
else
ww.print("X");
}else if(r == 1){
if((ss < x && (ss+1) < x) || (ss > y && (ss+1) > y)){
ww.print("R");
ss++;
}
else
ww.print("X");
}
}
if(ss == ff) break;
}
s.close();
ww.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>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
template <class T>
T gcd(T x, T y) {
while (T t = x % y) x = y, y = t;
return y;
}
const double eps = 1e-9;
const double PI = acos(-1.);
const int INF = 1000000000;
const int MOD = 1000000007;
const double E = 2.7182818284590452353602874713527;
bool isdig(char x) { return x >= '0' && x <= '9'; }
bool isup(char x) { return x >= 'A' && x <= 'Z'; }
bool isdown(char x) { return x >= 'a' && x <= 'z'; }
bool islet(char x) { return isup(x) || isdown(x); }
const int NN = 100005;
int N, M, S, F;
int T[NN], L[NN], R[NN];
void GetData() {
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]);
}
}
void Run() {
int now = S;
int inc = (F > S) ? 1 : -1;
char c = (F > S) ? 'R' : 'L';
int count = 0;
int view = 0;
while (now != F) {
count++;
if (view < M && count == T[view]) {
if (now >= L[view] && now <= R[view] ||
now + inc >= L[view] && now + inc <= R[view]) {
printf("X");
} else {
now += inc;
printf("%c", c);
}
view++;
} else {
now += inc;
printf("%c", c);
}
}
}
int main() {
{
GetData();
Run();
}
return 0;
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | def checkKey(dict, key):
if key in dict:
return True
return False
# def helper(s):
# l=len(s)
# if (l==1):
# l=[]
# l.append(s)
# return l
# ch=s[0]
# recresult=helper(s[1:])
# myresult=[]
# myresult.append(ch)
# for st in recresult:
# myresult.append(st)
# ts=ch+st
# myresult.append(ts)
# return myresult
# mod=1000000000+7
# def helper(s,n,open,close,i):
# if(i==2*n):
# for i in s:
# print(i,end='')
# print()
# return
# if(open<n):
# s[i]='('
# helper(s,n,open+1,close,i+1)
# if(close<open):
# s[i]=')'
# helper(s,n,open,close+1,i+1)
# def helper(arr,i,n):
# if(i==n-1):
# recresult=[arr[i]]
# return recresult
# digit=arr[i]
# recresult=helper(arr,i+1,n)
# myresult=[]
# for i in recresult:
# myresult.append(i)
# myresult.append(i+digit);
# myresult.append(digit)
# return myresult
# import copy
# n=int(input())
# arr=list(map(int,input().split()))
# ans=[]
# def helper(arr,i,n):
# if(i==n-1):
# # for a in arr:
# # print(a,end=" ")
# # print()
# l=copy.deepcopy(arr)
# ans.append(l)
# return
# for j in range(i,n):
# if(i!=j):
# if(arr[i]==arr[j]):
# continue
# else:
# arr[i],arr[j]=arr[j],arr[i]
# helper(arr,i+1,n)
# arr[j],arr[i]=arr[i],arr[j]
# else:
# arr[i],arr[j]=arr[j],arr[i]
# helper(arr,i+1,n)
# def helper(sol,n,m):
# for i in range(n+1):
# for j in range(m+1):
# print(sol[i][j],end=" ")
# print()
# def rat_in_a_maze(maze,sol,i,j,n,m):
# if(i==n and j==m):
# sol[i][j]=1
# helper(sol,n,m)
# exit()
# if(i>n or j>m):
# return False
# if(maze[i][j]=='X'):
# return False
# maze[i][j]='X'
# sol[i][j]=1
# if(rat_in_a_maze(maze,sol,i,j+1,n,m)):
# return True
# elif(rat_in_a_maze(maze,sol,i+1,j,n,m)):
# return True
# sol[i][j]=0
# return False
n,m,s,f=map(int,input().split())
d={}
for i__ in range(m):
t,l,r=map(int,input().split())
d[t]=(l,r)
if(f>s):
ans=""
time=1
while(s!=f):
if time in d:
(start,end)=d[time]
if(start<=s<=end or start<=s+1<=end):
ans+='X'
time+=1
else:
ans+='R'
time+=1
s+=1
else:
ans+='R'
time+=1
s+=1
print(ans)
exit()
if(f<s):
ans=""
time=1
while(s!=f):
if time in d:
start,end=d[time]
if(start<=s<=end or start<=s-1<=end):
ans+='X'
time+=1
else:
ans+='L'
time+=1
s-=1
else:
ans+='L'
time+=1
s-=1
print(ans)
| PYTHON3 |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.IOException;
public class Code {
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer=new PrintWriter(System.out);
int [] readInts()throws IOException{
String s[]=reader.readLine().split(" ");
int [] ints=new int[s.length];
for (int i=0;i<ints.length;i++)
ints[i]=Integer.parseInt(s[i]);
return ints;
}
int tmpn=0;
int [] tmp;
int readInt() throws IOException{
while(tmp==null||tmpn>tmp.length){
tmp=readInts();
tmpn=0;
}
return tmp[tmpn++];
}
void solve() throws IOException{
int [] Q=readInts();
int n=Q[0],m=Q[1],s=Q[2],f=Q[3],k=0,d,t=0,l=0,r=0,M=0;
if (s==f) return;
String QQQ;
if (s<f){
d=1;QQQ="R";
}else{
d=-1;QQQ="L";
}
while(1==1){
k++;
if (m==M){
M++;t=0;
}
if (t==k-1&&M!=m){
M++;
Q=readInts();
t=Q[0];l=Q[1];r=Q[2];
}
if (t==k){
if ((s<l||s>r)&&(s+d<l||s+d>r)){
writer.print(QQQ);
s+=d;
}else writer.print("X");
}else{
writer.print(QQQ);
s+=d;
}
if (s==f) {writer.flush();return;}
}
}
public static void main(String args[]) throws IOException{
new Code().solve();
}
} | 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.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class cf342b {
static FastIO in = new FastIO(), out = in;
public static void main(String[] args) {
int n = in.nextInt();
int m = in.nextInt();
int s = in.nextInt();
int f = in.nextInt();
int dir = 1;
char move = 'R';
if (f < s) {
dir = -1;
move = 'L';
}
int curL = 0, curR = 0, curT = -1;
int time = 0;
while (s != f) {
++time;
int next = s + dir;
if (time > curT && m > 0) {
m--;
curT = in.nextInt();
curL = in.nextInt();
curR = in.nextInt();
}
if (time == curT
&& ((s >= curL && s <= curR) || (next >= curL && next <= curR))) {
out.print("X");
} else {
s = next;
out.print(move);
}
}
out.close();
}
static class FastIO extends PrintWriter {
BufferedReader br;
StringTokenizer st;
public FastIO() {
this(System.in, System.out);
}
public FastIO(InputStream in, OutputStream out) {
super(new BufferedWriter(new OutputStreamWriter(out)));
br = new BufferedReader(new InputStreamReader(in));
scanLine();
}
public void scanLine() {
try {
st = new StringTokenizer(br.readLine().trim());
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
public int numTokens() {
if (!st.hasMoreTokens()) {
scanLine();
return numTokens();
}
return st.countTokens();
}
public String next() {
if (!st.hasMoreTokens()) {
scanLine();
return next();
}
return st.nextToken();
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| JAVA |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int f_max(int x, int y) { return x > y ? x : y; }
int f_min(int x, int y) { return x < y ? x : y; }
void f_swap(int &x, int &y) {
int t;
t = x, x = y, y = t;
}
int f_abs(int x) { return x > 0 ? x : -x; }
string l, r;
struct po {
int ti, l, r;
} a[100009];
int n, m;
bool ok(int x, int y, int tag) {
if (tag == -1) return true;
if (x == 0) return false;
if (x == n + 1) return false;
if (y == 0) return false;
if (y == n + 1) return false;
if (x >= a[tag].l && x <= a[tag].r) return false;
if (y >= a[tag].l && y <= a[tag].r) return false;
return true;
}
int main() {
int testcase, cas, tt, i, s, t;
while (~scanf("%d%d%d%d", &n, &m, &s, &t)) {
l = "";
r = "";
for (i = 1; i <= m; i++) scanf("%d%d%d", &a[i].ti, &a[i].l, &a[i].r);
a[m + 1].ti = -1;
int lx = s, rx = s, tag = 1;
int tg;
for (i = 1;; i++) {
if (a[tag].ti == i) {
tg = tag;
tag++;
} else
tg = -1;
if (ok(lx - 1, lx, tg)) {
l += 'L';
lx--;
} else
l += 'X';
if (ok(rx + 1, rx, tg)) {
r += 'R';
rx++;
} else
r += 'X';
if (rx == t || lx == t) break;
}
if (rx == t)
cout << r << endl;
else
cout << l << 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 n, m, st, ed, tem, p, pt;
long long stp;
bool seeked;
string ch;
struct node {
long long t, l, r;
} a[200000];
bool cmp(node aa, node bb) { return aa.t < bb.t; }
long long i;
int main() {
cin >> n >> m >> st >> ed;
if (st > ed) {
ch = "L";
stp = -1;
} else {
ch = "R";
stp = 1;
}
if (st == ed) {
return 0;
}
for (i = 0; i < m; i++) {
cin >> a[i].t >> a[i].l >> a[i].r;
}
tem = st;
p = 0;
pt = 1;
while (tem != ed) {
seeked = false;
while (a[p].t <= pt && p <= m) {
if ((a[p].l <= tem && a[p].r >= tem) ||
(a[p].l <= tem + stp && a[p].r >= tem + stp))
seeked = true;
p++;
}
pt++;
if (seeked) {
cout << "X";
} else {
cout << ch;
tem += stp;
}
}
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;
class step {
public:
int st, r, l;
} step[100005];
int main() {
int n, m, s, f;
int i, j, l, r, t;
while (cin >> n >> m >> s >> f) {
int t_now = 1;
for (i = 0; i < m; i++) cin >> step[i].st >> step[i].l >> step[i].r;
for (i = 0; i < m; i++) {
if (s == f) break;
if (t_now != step[i].st) {
if (s < f) {
cout << "R";
s++;
} else {
cout << "L";
s--;
}
i--;
} else if (s < f && (s + 1 < step[i].l || s > step[i].r)) {
cout << "R";
s++;
} else if (s > f && (s - 1 > step[i].r || s < step[i].l)) {
cout << "L";
s--;
} else
cout << "X";
t_now++;
}
while (s != f) {
if (s < f) {
cout << "R";
s++;
} else {
cout << "L";
s--;
}
}
}
return 0;
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | n,m,s,f=list(map(int,input().split()));
d={};
for i in range(m):
t,l,r=list(map(int,input().split()));
d[t]=[l,r];
ans="";
for i in range(1,n+m):
if(s==f):
print(ans);
exit(0);
t=-1;
if(f<s):
t=s-1;
else:
t=s+1;
if i in d:
if((d[i][0]<=s and s<=d[i][1]) or (d[i][0]<=t and t<=d[i][1])):
t=-1;
if(t==-1):
ans+="X";
else:
if(f<s):
ans+="L";
else:
ans+="R";
s=t;
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 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.math.BigInteger;
import java.util.Map;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class Dragons {
static class Pair {
int a;
int b;
public Pair(int a, int b){
this.a=a;
this.b=b;
}
}
public static void main(String[] args) throws IOException {
// BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
HashMap<Integer,Pair> hm=new HashMap<Integer,Pair>();
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
int d=sc.nextInt();
int mum=0;
for(int i=0;i<b;i++) {
mum=sc.nextInt();
hm.put(mum, new Pair(sc.nextInt(),sc.nextInt()));
}
int y=c;
StringBuilder sb=new StringBuilder();
if(c<d) {
int i=1;
while(i>0) {
if(hm.containsKey(i)) {
Pair ab=hm.get(i);
if((ab.a<=c && ab.b>=c) || (ab.a<=c+1 && ab.b>=c+1)) {
sb.append('X');
}else {
sb.append('R');
c++;
}
}else {
sb.append('R');
c++;
}
if(c==d) {
break;
}
i++;
}
}else {
int i=1;
while( i>0) {
if(hm.containsKey(i)) {
Pair ab=hm.get(i);
if((ab.a<=c && ab.b>=c) || (ab.a<=c-1 && ab.b>=c-1)) {
sb.append('X');
}else {
sb.append('L');
c--;
}
}else {
sb.append('L');
c--;
}
if(c==d) {
break;
}
i++;
}
}
System.out.println(sb);
}
public static int prog(List<Integer> list){
if(list.isEmpty()) return -1;
if(list.size()==1) return 0;
int p = list.get(1)-list.get(0);
for(int i = 0; i < list.size()-1; ++i){
if(list.get(i+1) - list.get(i) != p)
return -1;
}
return p;
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
static int gcd(int a, int b) {
return a%b == 0 ? b : gcd(b,a%b);
}
static boolean[] sieve(int n) {
boolean isPrime[] = new boolean[n+1];
for(int i = 2; i <= n; i++) {
if(isPrime[i]) continue;
for(int j = 2*i; j <= n; j+=i) {
isPrime[j] = true;
}
}
return isPrime;
}
} | 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 a[100005][3];
int main() {
scanf("%d%d%d%d", &n, &m, &s, &f);
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &a[i][0], &a[i][1], &a[i][2]);
}
int k = 0;
int last = 1;
if (s < f) {
for (int i = 0; i < m; i++) {
if (a[i][0] > last) {
int t = 0;
while (s < f) {
printf("R");
s++;
t++;
if (t == a[i][0] - last) break;
}
if (s == f) break;
}
if ((s >= a[i][1] && s <= a[i][2]) ||
(s + 1 >= a[i][1] && s + 1 <= a[i][2])) {
printf("X");
last = a[i][0] + 1;
} else {
printf("R");
s++;
if (s == f) break;
last = a[i][0] + 1;
}
}
while (s < f) {
printf("R");
s++;
}
} else {
for (int i = 0; i < m; i++) {
if (a[i][0] > last) {
int t = 0;
while (s > f) {
printf("L");
s--;
t++;
if (t == a[i][0] - last) break;
}
if (s == f) break;
}
if ((s >= a[i][1] && s <= a[i][2]) ||
(s - 1 >= a[i][1] && s - 1 <= a[i][2])) {
printf("X");
last = a[i][0] + 1;
} else {
printf("L");
s--;
if (s == f) break;
last = a[i][0] + 1;
}
}
while (s > f) {
printf("L");
s--;
}
}
return 0;
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | import java.util.*;
import java.io.*;
import java.math.*;
import static java.lang.Math.*;
public class Solution implements Runnable {
public void solve() throws Exception {
int n = sc.nextInt();
int m = sc.nextInt();
int s = sc.nextInt() - 1;
int f = sc.nextInt() - 1;
int l [] = new int [m];
int r [] = new int [m];
int t [] = new int [m];
for (int i = 0; i < m; i++) {
t[i] = sc.nextInt();
l[i] = sc.nextInt() - 1;
r[i] = sc.nextInt() - 1;
}
char stay = 'X';
char go = 'R';
int add = 1;
if (s > f) {
go = 'L';
add = -1;
}
int idx = 0;
StringBuilder sb = new StringBuilder();
for (int time = 1; s != f; time++) {
if (idx < m && t[idx] == time) {
if (s >= l[idx] && s <= r[idx] || s + add >= l[idx] && s + add <= r[idx]) {
sb.append(stay);
} else {
sb.append(go);
s += add;
}
idx++;
} else {
sb.append(go);
s += add;
}
}
out.println(sb.toString());
}
static Throwable t;
BufferedReader in;
FastScanner sc;
PrintWriter out;
@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
sc = new FastScanner(in);
solve();
} catch (Throwable t) {
Solution.t = t;
} finally {
out.close();
}
}
public static void main(String[] args) throws Throwable {
Thread thread = new Thread(null, new Solution(), "", 1 << 26);
thread.start();
thread.join();
if (Solution.t != null)
throw t;
}
}
class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
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() {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long f = 0, j, q = 1, i, n;
while (q--) {
long long y, k = 1, x, M, s;
cin >> n >> M >> s >> f;
map<int, pair<int, int> > m;
for (i = 0; i < M; i++) {
long long X, Y, Z;
cin >> X >> Y >> Z;
m.insert(make_pair(X, make_pair(Y, Z)));
}
if (s < f) {
while (s != f) {
if (m.count(k)) {
if ((s >= m[k].first && s <= m[k].second) ||
(s + 1 >= m[k].first && s + 1 <= m[k].second))
cout << "X";
else {
cout << "R";
s++;
}
} else {
cout << "R";
s++;
}
k++;
}
} else {
while (s != f) {
if (m.count(k)) {
if ((s >= m[k].first && s <= m[k].second) ||
(s - 1 >= m[k].first && s - 1 <= m[k].second))
cout << "X";
else {
cout << "L";
s--;
}
} else {
cout << "L";
s--;
}
k++;
}
}
cout << endl;
}
}
| CPP |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class Codeforces {
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;
}
}
public static void main(String args[]) {
FastReader sc = new FastReader();
// Scanner sc = new Scanner(System.in);
//System.out.println(java.time.LocalTime.now());
int n = sc.nextInt();
int m = sc.nextInt();
int s = sc.nextInt();
int f = sc.nextInt();
StringBuilder sb = new StringBuilder();
int prev = 0;
boolean bool = false;
if(s>f)bool = true;
if(bool) {
for(int i = 0; i<m; i++) {
int step = sc.nextInt();
if(s == f)break;
int loop = step-prev-1;
while(loop>0 && s != f) {
sb.append('L');
s--;
loop--;
}
if(s == f)break;
int l = sc.nextInt();
int r = sc.nextInt();
if(s<l || s>r+1) {
s--;
sb.append('L');
}else sb.append('X');
prev = step;
}
while(s!=f) {
sb.append('L');
s--;
}
}else {
for(int i = 0; i<m; i++) {
int step = sc.nextInt();
if(s == f)break;
int loop = step-prev-1;
while(loop>0 && s!=f) {
s++;
sb.append('R');
loop--;
}
if(s == f)break;
int l = sc.nextInt();
int r = sc.nextInt();
if(s<l-1 || s>r) {
s++;
sb.append('R');
}else sb.append('X');
prev = step;
}
while(s!=f) {
sb.append('R');
s++;
}
}
System.out.println(sb);
}
}
| JAVA |
342_B. Xenia and Spies | Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, s, f, x = 0, t = 1, i;
vector<pair<int, pair<int, int> > > v;
scanf("%d %d %d %d", &n, &m, &s, &f);
for (i = 0; i < m; i++) {
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
v.push_back(make_pair(x, make_pair(y, z)));
}
while (1) {
if (s == f) break;
if (x == v.size() || v[x].first != t) {
if (s < f) {
putchar('R');
s++;
} else {
putchar('L');
s--;
}
} else {
if (s < f) {
if ((s >= v[x].second.first && s <= v[x].second.second) ||
(s + 1 >= v[x].second.first && s + 1 <= v[x].second.second)) {
putchar('X');
} else {
putchar('R');
s++;
}
} else {
if ((s >= v[x].second.first && s <= v[x].second.second) ||
(s - 1 >= v[x].second.first && s - 1 <= v[x].second.second)) {
putchar('X');
} else {
putchar('L');
s--;
}
}
x++;
}
t++;
}
puts("");
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, d;
int dp[1130000];
int main() {
scanf("%d %d", &n, &d);
int s = 0;
dp[0] = 1;
for (int i = 0; i < n; i++) {
int ci;
scanf("%d", &ci);
s += ci;
for (int v = s; v >= ci; v--) {
dp[v] |= dp[v - ci];
}
}
int lla = -1e9;
int la = 0;
int days = 0;
for (int i = 1; i <= s; i++) {
if (dp[i] && i - la <= d) {
if (i - lla > d) {
days++;
lla = la;
}
la = i;
}
}
printf("%d %d\n", la, days);
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int MAXINT = 1111 * 1111 * 1111;
const long long MAXLINT = MAXINT * 1ll * MAXINT;
const long double EPS = 1e-10;
int n, k;
vector<int> sum;
bool exist[500005];
int main() {
scanf("%d%d", &n, &k);
sum.push_back(0);
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
for (int j = (int)sum.size(); j--;)
if (sum[j] + x <= 500000 && !exist[sum[j] + x]) {
sum.push_back(sum[j] + x);
exist[sum[j] + x] = true;
}
}
sort(sum.begin(), sum.end());
int ans = 0, val = 0;
for (int i = 0; i < (int)sum.size();) {
for (; i < (int)sum.size() && val + k >= sum[i]; i++)
;
if (sum[i - 1] == val) break;
ans++, val = sum[i - 1];
}
printf("%d %d", val, ans);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 500010;
int f[maxn], tmp, ans, sum, n, d, r, x;
int main() {
scanf("%d %d", &n, &d);
memset(f, 0, sizeof(f));
f[0] = 1;
for (int i = 0; i < n; i++) {
scanf("%d", &x);
for (int j = maxn - 1 - x; j >= 0; j--) f[j + x] |= f[j];
}
r = 1;
sum = 0;
ans = 0;
while (r < maxn) {
tmp = -1;
while (r <= sum + d && r < maxn) {
if (f[r]) tmp = r;
r++;
}
if (tmp == -1) break;
sum = tmp;
ans++;
}
printf("%d %d\n", sum, ans);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 55;
const int D = 10100;
char dp[N * D];
int n, d;
int A[N];
void do_package() {
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for (int i = 0; i < n; i++) {
for (int j = N * D - 1; j >= 0; j--) {
if (dp[j] && j + A[i] < N * D) {
dp[j + A[i]] = 1;
}
}
}
}
void solve(int &val, int &day) {
val = day = 0;
while (1) {
bool flag = false;
for (int i = val + d; i > val; i--) {
if (dp[i]) {
val = i;
day++;
flag = true;
break;
}
}
if (!flag) break;
}
}
int main() {
while (cin >> n >> d) {
for (int i = 0; i < n; i++) {
cin >> A[i];
}
do_package();
int a, b;
solve(a, b);
cout << a << ' ' << b << endl;
}
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
const int MOD = 1e9 + 7;
int dp[maxn];
int c[52];
void calc(int n, int maxW) {
for (int i = 1; i <= n; ++i) {
for (int w = maxW; w >= c[i - 1]; --w)
dp[w] = max(dp[w - c[i - 1]] + c[i - 1], dp[w]);
}
}
int main() {
int n, d;
scanf("%d %d", &n, &d);
for (int i = 0; i < n; ++i) scanf("%d ", &(c[i]));
int maxW = accumulate(c, c + n, 0);
calc(n, maxW);
int ans = 0, days = 0;
while (1) {
int max_cost = min(ans + d, maxW);
int new_ans = dp[max_cost];
if (ans == new_ans) break;
ans = new_ans;
days++;
}
cout << ans << " " << days << endl;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | import java.util.*;
import java.io.*;
public class Main{
BufferedReader in;
StringTokenizer str = null;
PrintWriter out;
private String next() throws Exception{
if (str == null || !str.hasMoreElements())
str = new StringTokenizer(in.readLine());
return str.nextToken();
}
private int nextInt() throws Exception{
return Integer.parseInt(next());
}
public void run() throws Exception{
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int n = nextInt();
int d = nextInt();
int c[] = new int[n];
int sum = 0;
for(int i=0;i<n;i++){
c[i] = nextInt();
sum+=c[i];
}
boolean f[][] = new boolean[n+1][sum+1];
f[0][0] = true;
for(int i=1;i<=n;i++){
for(int j=0;j<=sum;j++){
f[i][j] = f[i-1][j];
if (j - c[i-1] >=0){
f[i][j]|=f[i-1][j-c[i-1]];
}
}
}
int r = 0, days = 0;
while(true){
int next = -1;
for(int i=Math.min(r + d, sum);i>r;i--){
if (f[n][i]){
next = i;
break;
}
}
if (next == -1) break;
r = next;
days++;
}
out.println(r + " " + days);
out.close();
}
public static void main(String args[]) throws Exception{
new Main().run();
}
} | JAVA |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long INF = 4e18;
const int inf = 2e9;
const int N = 5e5 + 5;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
vector<int> pos;
bool ada[N];
int ans, day;
int main(void) {
int d, i, j, n, x;
scanf("%d %d", &n, &d);
pos.push_back(0);
for (i = 0; i < n; ++i) {
scanf("%d", &x);
int len = (int)pos.size();
for (j = 0; j < len; ++j)
if (!ada[pos[j] + x]) {
pos.push_back(pos[j] + x);
ada[pos[j] + x] = 1;
}
}
sort(pos.begin(), pos.end());
i = 0;
while (i < pos.size()) {
while (i < pos.size() && ans + d >= pos[i]) i++;
if (ans != pos[i - 1]) {
ans = pos[i - 1];
day++;
} else
break;
}
printf("%d %d\n", ans, day);
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class D {
static byte[][] dp;
static int[] ar;
public static void solve(int idx, int sum) {
// System.out.println(idx+" "+sum);
if (dp[idx][sum] != 0)
return;
dp[idx][sum] = 1;
if (idx >= ar.length)
return;
solve(idx + 1, sum);
solve(idx + 1, sum + ar[idx]);
}
public static void main(String[] args) throws Exception {
int itemCnt = nextInt();
int d = nextInt();
ar = new int[itemCnt];
int maxCost = 0;
for (int i = 0; i < itemCnt; i++) {
ar[i] = nextInt();
maxCost += ar[i];
}
dp = new byte[ar.length+1][maxCost + 1];
solve(0, 0);
boolean[] can = new boolean[maxCost + 1];
for (int i = 0; i < maxCost + 1; i++) {
for (int j = 0; j <= ar.length; j++) {
if (dp[j][i] == 1) {
can[i] = true;
break;
}
}
}
// System.out.println(Arrays.toString(can));
int[] minDays = new int[can.length];
Arrays.fill(minDays, -1);
minDays[0] = 0;
int prev = 0 - d;
for (int i = 0;;) {
// System.out.println(prev + " " + i+" "+(prev+d));
int next = -1;
for (int j = prev + d+1; j < can.length && j <= i + d; j++) {
if (can[j]) {
minDays[j] = 1 + minDays[i];
next = j;
}
}
// System.out.println(Arrays.toString(minDays));
if (next == -1) {
break;
} else {
prev = i;
i = next;
}
}
for (int i = minDays.length - 1; i >= 0; i--) {
if (minDays[i] != -1) {
System.out.println((i) + " " + (minDays[i]));
return;
}
}
}
static BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
static int nextInt() throws Exception {
return Integer.parseInt(next());
}
static double nextDouble() throws Exception {
return Double.parseDouble(next());
}
static String next() throws Exception {
while (true) {
if (tokenizer.hasMoreTokens()) {
return tokenizer.nextToken();
}
String s = br.readLine();
if (s == null) {
return null;
}
tokenizer = new StringTokenizer(s);
}
}
}
| JAVA |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;
const int maxn = 2000000;
int n, m;
long long a[maxn], sum[maxn], cur, d, ans, can[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> d;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];
for (int i = 1; i <= n;) {
cur = sum[i - 1];
int j = i - 1;
while (j + 1 <= n && cur + d >= sum[j + 1] - cur) j++;
if (j == i - 1) {
can[0] = true;
for (int k = 1; k <= j; k++)
for (int l = sum[j]; l >= 0; l--) can[l + a[k]] |= can[l];
cur = 0;
while (cur != sum[j]) {
int mx = 0;
for (int k = cur; k <= cur + d; k++)
if (can[k]) mx = k;
cur = mx;
++ans;
}
cout << sum[j] << ' ' << ans;
return 0;
}
i = j + 1;
}
can[0] = true;
for (int k = 1; k <= n; k++)
for (int l = sum[n]; l >= 0; l--) can[l + a[k]] |= can[l];
cur = 0;
while (cur != sum[n]) {
int mx = 0;
for (int k = cur; k <= cur + d; k++)
if (can[k]) mx = k;
cur = mx;
++ans;
}
cout << sum[n] << ' ' << ans;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int mx = 600000;
int n, m;
int ans[mx + 5];
bool dp[mx + 5];
multiset<int> q;
int x;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
dp[0] = true;
for (int i = 1; i <= n; ++i) {
cin >> x;
for (int j = mx; j >= 0; --j)
if (dp[j]) dp[j + x] = true;
}
q.insert(0);
for (int i = 1; i <= mx; ++i) {
ans[i] = 1e9;
if (i > m && ans[i - m - 1] < 1e9) {
q.erase(q.find(ans[i - m - 1]));
}
if (dp[i] == false) continue;
if (q.size() == 0) continue;
int x = *q.begin();
ans[i] = x + 1;
q.insert(ans[i]);
}
for (int i = mx; i >= 0; --i)
if (ans[i] < 1e9) {
cout << i << " " << ans[i];
return 0;
}
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
bool dp[1000010];
int main() {
int n, d, c;
cin >> n >> d;
dp[0] = 1;
for (int i = 1; i <= n; i++) {
cin >> c;
for (int j = 500000; j >= c; j--) dp[j] |= dp[j - c];
}
int day = 0, now = 0;
bool ok = 1;
while (ok) {
for (int i = now + d; i > now; i--) {
if (dp[i]) {
now = i;
break;
}
if (i <= now + 1) ok = 0;
}
day++;
}
cout << now << ' ' << day - 1 << endl;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 29;
const double dinf = 1e30;
const long long linf = 1LL << 55;
const int N = 55 * 10000;
int n, d;
bool dp[N];
int main() {
while (cin >> n >> d) {
memset(dp, false, sizeof(dp));
dp[0] = true;
for (int i = 0, x; i < n; i++) {
cin >> x;
for (int j = N - 1; j >= x; j--) {
dp[j] |= dp[j - x];
}
}
int max_value = 0, steps = 0;
while (true) {
int t = -1;
for (int i = max_value + 1; i <= min(max_value + d, N - 1); i++) {
if (dp[i]) t = i;
}
if (t == -1) break;
steps++;
max_value = t;
}
cout << max_value << " " << steps << endl;
}
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
long long mpow(long long a, long long n, long long mod) {
long long ret = 1;
long long b = a;
while (n) {
if (n & 1) ret = (ret * b) % mod;
b = (b * b) % mod;
n >>= 1;
}
return (long long)ret;
}
using namespace std;
bool a[6000001];
int x[51];
int main() {
int n, d, i, j;
cin >> n >> d;
for (j = 0; j < n; j++) cin >> x[j];
int sum = 0;
a[0] = true;
for (i = 0; i < n; i++) {
sum += x[i];
for (j = sum; j >= x[i]; j--) {
a[j] |= a[j - x[i]];
}
}
int cnt = 0, res = 0;
for (;; cnt++) {
for (j = d; j; j--) {
if (a[res + j]) {
res += j;
break;
}
}
if (!j) break;
}
cout << res << " " << cnt;
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:16777216")
#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:16777216")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
const int inf = 2000000000;
const long long linf = 5000000000000000000;
const long double LDINF = 2e18;
template <typename T>
void print(vector<T>& a) {
for (int i = 0; i < a.size(); i++) cout << a[i] << ' ';
cout << '\n';
}
template <typename T>
void print(deque<T>& a) {
for (int i = 0; i < a.size(); i++) cout << a[i] << ' ';
cout << '\n';
}
template <typename T>
void print(vector<vector<T>>& a) {
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[i].size(); j++) cout << a[i][j] << ' ';
cout << '\n';
}
}
template <typename T>
void input(vector<T>& a) {
for (int i = 0; i < a.size(); i++) cin >> a[i];
}
template <typename T>
void input(deque<T>& a) {
for (int i = 0; i < a.size(); i++) cin >> a[i];
}
template <typename T>
void input(vector<vector<T>>& a) {
for (int i = 0; i < a.size(); i++)
for (int j = 0; j < a[i].size(); j++) cin >> a[i][j];
}
long long mod = 1e9 + 7;
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
vector<int> t, tAdd;
void push(int v, int vl, int vr) {
if (tAdd[v] == 0) return;
t[v] += tAdd[v];
if (vl != vr) {
tAdd[2 * v + 1] += tAdd[v];
tAdd[2 * v + 2] += tAdd[v];
}
tAdd[v] = 0;
}
long long query(int v, int vl, int vr, int l, int r) {
push(v, vl, vr);
if (vl >= l && vr <= r)
return t[v];
else if (vl > r || l > vr)
return 0;
int vm = (vl + vr) / 2;
return query(2 * v + 1, vl, vm, l, r) + query(2 * v + 2, vm + 1, vr, l, r);
}
void modify(int v, int vl, int vr, int l, int r, int val) {
push(v, vl, vr);
if (vl >= l && vr <= r) {
tAdd[v] += val;
push(v, vl, vr);
return;
}
if (vl > r || l > vr) return;
int vm = (vl + vr) / 2;
modify(2 * v + 1, vl, vm, l, r, val);
modify(2 * v + 2, vm + 1, vr, l, r, val);
t[v] = t[2 * v + 1] + t[2 * v + 2];
}
int n, k, cnt = 0;
void check(string& str) {
vector<bool> used(k + 1, false);
for (int i = 0; i < str.length(); i++) used[str[i] - '0'] = true;
for (int i = 0; i < k + 1; i++) {
if (!used[i]) return;
}
cnt++;
}
void solve() {
long long n, d;
cin >> n >> d;
vector<long long> c(n);
input(c);
long long sum = 0;
for (int i = 0; i < n; i++) {
sum += c[i];
}
vector<vector<bool>> dp(n + 1, vector<bool>(sum + 1, false));
dp[0][0] = true;
for (int i = 0; i < n; i++) {
for (int w = 0; w <= sum; w++) {
if (dp[i][w]) dp[i + 1][w + c[i]] = true;
if (dp[i][w]) dp[i + 1][w] = true;
}
}
vector<int> vals;
for (int w = 0; w <= sum; w++) {
for (int i = 0; i <= n; i++) {
if (dp[i][w]) {
vals.push_back(w);
break;
}
}
}
long long now = 0;
int cnt = 0;
while (true) {
auto it = upper_bound((vals).begin(), (vals).end(), now + d);
it--;
if (*it == now) break;
now = *it;
cnt++;
}
cout << now << ' ' << cnt;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tst = 1;
while (tst--) solve();
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
inline void setmin(int &x, int y) {
if (y < x) x = y;
}
inline void setmax(int &x, int y) {
if (y > x) x = y;
}
inline void setmin(long long &x, long long y) {
if (y < x) x = y;
}
inline void setmax(long long &x, long long y) {
if (y > x) x = y;
}
const int N = 100000;
const int inf = (int)1e9 + 1;
const long long big = (long long)1e18 + 1;
const int P = 239;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;
bitset<50 * 10000 + 1> dp;
int ans[50 * 10000 + 1];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.precision(20);
cout << fixed;
int n, d;
cin >> n >> d;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
int pos = 0, sum = 0;
while (pos < n && sum + d >= a[pos]) sum += a[pos++];
n = pos;
dp.set(0);
for (int i = 0; i < n; i++) dp |= (dp << a[i]);
fill(ans, ans + 50 * 10000 + 1, inf);
ans[0] = 0;
multiset<int> vals;
vals.insert(0);
pos = 0;
for (int i = 1; i < 50 * 10000 + 1; i++) {
while (pos + d < i) vals.erase(vals.find(ans[pos++]));
if (dp.test(i)) setmin(ans[i], (*vals.begin()) + 1);
vals.insert(ans[i]);
}
for (int i = 50 * 10000; i > -1; i--)
if (ans[i] != inf) {
cout << i << " " << ans[i] << "\n";
break;
}
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int N = 10010;
int n, d;
bool f[N * 55];
int c[55];
int main() {
while (~scanf("%d%d", &n, &d)) {
int sum = 0;
for (int i = 0; i < n; ++i) {
scanf("%d", &c[i]);
sum += c[i];
}
memset(f, 0, sizeof(f));
f[0] = 1;
for (int i = 0; i < n; ++i)
for (int j = sum; j >= c[i]; --j) f[j] |= f[j - c[i]];
int own = 0, cnt = 0;
while (true) {
bool ok = true;
int tmp = own + d;
for (; tmp > own; --tmp)
if (tmp <= sum && f[tmp]) {
ok = false;
++cnt;
break;
}
if (ok || tmp == own) break;
own = tmp;
}
printf("%d %d\n", own, cnt);
}
return 0;
}
| CPP |
365_D. Free Market | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
Input
The first line contains two space-separated integers n, d (1 β€ n β€ 50, 1 β€ d β€ 104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 β€ ci β€ 104).
Output
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note
In the first sample John can act like this:
* Take the first item (1 - 0 β€ 2).
* Exchange the first item for the second one (3 - 1 β€ 2).
* Take the first item (1 - 0 β€ 2). | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
int dp[55 * maxn];
int main() {
int n, d, x, cnt = 0, w = 0, i, j;
cin >> n >> d;
memset(dp, 0, sizeof(dp));
dp[0] = 1;
long long sum = 0;
for (i = 1; i <= n; i++) {
cin >> x;
sum += x;
for (j = sum; j >= x; j--)
if (dp[j - x]) dp[j] = 1;
}
while (0 == 0) {
j = w + d;
while (!dp[j] && j > w) j--;
if (j == w) break;
w = j;
cnt++;
}
printf("%d %d", w, cnt);
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.