output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
vector<int> x, y, z;
int xx = 0, yy = 0, zz = 0;
x.push_back(0);
y.push_back(0);
z.push_back(0);
for (char c : s) {
xx += (c == 'x');
yy += (c == 'y');
zz += (c == 'z');
x.push_back(xx);
y.push_back(yy);
z.push_back(zz);
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
vector<int> vc;
vc.push_back(x[r] - x[l - 1]);
vc.push_back(y[r] - y[l - 1]);
vc.push_back(z[r] - z[l - 1]);
sort(vc.begin(), vc.end());
if (r - l + 1 <= 2) {
cout << "YES" << endl;
continue;
}
if ((vc[0] != vc[1] && vc[1] != vc[2] && vc[2] != vc[0]) ||
vc[2] - vc[0] > 1) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
}
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
vector<int> x, y, z;
int xx = 0, yy = 0, zz = 0;
x.push_back(0);
y.push_back(0);
z.push_back(0);
for (char c : s) {
xx += (c == 'x');
yy += (c == 'y');
zz += (c == 'z');
x.push_back(xx);
y.push_back(yy);
z.push_back(zz);
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
vector<int> vc;
vc.push_back(x[r] - x[l - 1]);
vc.push_back(y[r] - y[l - 1]);
vc.push_back(z[r] - z[l - 1]);
sort(vc.begin(), vc.end());
if (r - l + 1 <= 2) {
cout << "YES" << endl;
continue;
}
if ((vc[0] != vc[1] && vc[1] != vc[2] && vc[2] != vc[0]) ||
vc[2] - vc[0] > 1) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
}
}
```
|
#include <bits/stdc++.h>
long long mod = 1000000007;
using namespace std;
long long power(long long x, long long y) {
if (y == 0)
return 1;
else if (y % 2 == 0)
return (power(x, y / 2) * power(x, y / 2)) % mod;
else
return (x * ((power(x, y / 2) * power(x, y / 2)) % mod) % mod);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> s;
int x[s.size()];
int y[s.size()];
int z[s.size()];
for (int i = 0; i < s.size(); i++) {
x[i] = (i == 0 ? 0 : x[i - 1]) + (s.at(i) == 'x' ? 1 : 0);
y[i] = (i == 0 ? 0 : y[i - 1]) + (s.at(i) == 'y' ? 1 : 0);
z[i] = (i == 0 ? 0 : z[i - 1]) + (s.at(i) == 'z' ? 1 : 0);
}
int queries;
cin >> queries;
while (queries--) {
int i, j;
cin >> i >> j;
int xS = x[j - 1] - (i == 1 ? 0 : x[i - 2]);
int yS = y[j - 1] - (i == 1 ? 0 : y[i - 2]);
int zS = z[j - 1] - (i == 1 ? 0 : z[i - 2]);
int score = max({xS - min({xS, yS, zS}), yS - min({xS, yS, zS}),
zS - min({xS, yS, zS})});
if (j - i <= 1 || (score <= 1)) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
long long mod = 1000000007;
using namespace std;
long long power(long long x, long long y) {
if (y == 0)
return 1;
else if (y % 2 == 0)
return (power(x, y / 2) * power(x, y / 2)) % mod;
else
return (x * ((power(x, y / 2) * power(x, y / 2)) % mod) % mod);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> s;
int x[s.size()];
int y[s.size()];
int z[s.size()];
for (int i = 0; i < s.size(); i++) {
x[i] = (i == 0 ? 0 : x[i - 1]) + (s.at(i) == 'x' ? 1 : 0);
y[i] = (i == 0 ? 0 : y[i - 1]) + (s.at(i) == 'y' ? 1 : 0);
z[i] = (i == 0 ? 0 : z[i - 1]) + (s.at(i) == 'z' ? 1 : 0);
}
int queries;
cin >> queries;
while (queries--) {
int i, j;
cin >> i >> j;
int xS = x[j - 1] - (i == 1 ? 0 : x[i - 2]);
int yS = y[j - 1] - (i == 1 ? 0 : y[i - 2]);
int zS = z[j - 1] - (i == 1 ? 0 : z[i - 2]);
int score = max({xS - min({xS, yS, zS}), yS - min({xS, yS, zS}),
zS - min({xS, yS, zS})});
if (j - i <= 1 || (score <= 1)) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
string s;
int f[3][maxn];
void precalc() {
for (int i = 1; i <= s.length(); ++i) {
for (int j = 0; j < 3; ++j) f[j][i] = f[j][i - 1];
++f[s[i - 1] - 'x'][i];
}
}
bool check(int x, int s) { return x == s || x == s + 1; }
void solve(int l, int r) {
int x = f[0][r] - f[0][l - 1];
int y = f[1][r] - f[1][l - 1];
int z = f[2][r] - f[2][l - 1];
int s = (r - l + 1) / 3;
if ((r - l + 1) < 3 || (check(x, s) && check(y, s) && check(z, s)))
puts("YES");
else
puts("NO");
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> s;
precalc();
int q;
cin >> q;
for (int cas = 1; cas <= q; ++cas) {
int l, r;
cin >> l >> r;
solve(l, r);
}
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
string s;
int f[3][maxn];
void precalc() {
for (int i = 1; i <= s.length(); ++i) {
for (int j = 0; j < 3; ++j) f[j][i] = f[j][i - 1];
++f[s[i - 1] - 'x'][i];
}
}
bool check(int x, int s) { return x == s || x == s + 1; }
void solve(int l, int r) {
int x = f[0][r] - f[0][l - 1];
int y = f[1][r] - f[1][l - 1];
int z = f[2][r] - f[2][l - 1];
int s = (r - l + 1) / 3;
if ((r - l + 1) < 3 || (check(x, s) && check(y, s) && check(z, s)))
puts("YES");
else
puts("NO");
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> s;
precalc();
int q;
cin >> q;
for (int cas = 1; cas <= q; ++cas) {
int l, r;
cin >> l >> r;
solve(l, r);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 * 1000;
int x[MAXN + 2];
int y[MAXN + 2];
int z[MAXN + 2];
int main() {
string s;
int y1, x1, z1, m, ix = 0, iy = 0, iz = 0, r, l;
cin >> s >> m;
for (int i = 0; i < s.length(); i++) {
if (s[i] == 'x')
++ix;
else if (s[i] == 'y')
++iy;
else
++iz;
x[i + 1] = ix;
y[i + 1] = iy;
z[i + 1] = iz;
}
for (int i = 0; i < m; i++) {
cin >> l >> r;
if (r - l < 2) {
cout << "YES" << endl;
continue;
}
y1 = y[r] - y[l - 1];
x1 = x[r] - x[l - 1];
z1 = z[r] - z[l - 1];
if ((x1 > y1 ? x1 - y1 : y1 - x1) > 1 ||
(x1 > z1 ? x1 - z1 : z1 - x1) > 1 || (z1 > y1 ? z1 - y1 : y1 - z1) > 1)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 * 1000;
int x[MAXN + 2];
int y[MAXN + 2];
int z[MAXN + 2];
int main() {
string s;
int y1, x1, z1, m, ix = 0, iy = 0, iz = 0, r, l;
cin >> s >> m;
for (int i = 0; i < s.length(); i++) {
if (s[i] == 'x')
++ix;
else if (s[i] == 'y')
++iy;
else
++iz;
x[i + 1] = ix;
y[i + 1] = iy;
z[i + 1] = iz;
}
for (int i = 0; i < m; i++) {
cin >> l >> r;
if (r - l < 2) {
cout << "YES" << endl;
continue;
}
y1 = y[r] - y[l - 1];
x1 = x[r] - x[l - 1];
z1 = z[r] - z[l - 1];
if ((x1 > y1 ? x1 - y1 : y1 - x1) > 1 ||
(x1 > z1 ? x1 - z1 : z1 - x1) > 1 || (z1 > y1 ? z1 - y1 : y1 - z1) > 1)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int m;
string s;
int x[N], y[N], z[N];
int l, r;
bool ans[N];
int X, Y, Z;
int abs(int n) { return n < 0 ? -n : n; }
int main() {
cin >> s >> m;
for (int i = 1; i <= s.length(); ++i) {
x[i] = (s[i - 1] == 'x') + x[i - 1];
y[i] = (s[i - 1] == 'y') + y[i - 1];
z[i] = (s[i - 1] == 'z') + z[i - 1];
}
for (int i = 0; i < m; ++i) {
cin >> l >> r;
--l;
X = abs(x[r] - x[l]);
Y = abs(y[r] - y[l]);
Z = abs(z[r] - z[l]);
if ((r - l) % 3 == 0)
if (X == Y && X == Z) ans[i] = true;
if ((r - l) % 3 != 0)
if (abs(X - Y) + abs(X - Z) + abs(Y - Z) <= 2) ans[i] = true;
if (r - l < 3) ans[i] = true;
}
for (int i = 0; i < m; ++i) cout << (ans[i] ? "YES\n" : "NO\n");
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int m;
string s;
int x[N], y[N], z[N];
int l, r;
bool ans[N];
int X, Y, Z;
int abs(int n) { return n < 0 ? -n : n; }
int main() {
cin >> s >> m;
for (int i = 1; i <= s.length(); ++i) {
x[i] = (s[i - 1] == 'x') + x[i - 1];
y[i] = (s[i - 1] == 'y') + y[i - 1];
z[i] = (s[i - 1] == 'z') + z[i - 1];
}
for (int i = 0; i < m; ++i) {
cin >> l >> r;
--l;
X = abs(x[r] - x[l]);
Y = abs(y[r] - y[l]);
Z = abs(z[r] - z[l]);
if ((r - l) % 3 == 0)
if (X == Y && X == Z) ans[i] = true;
if ((r - l) % 3 != 0)
if (abs(X - Y) + abs(X - Z) + abs(Y - Z) <= 2) ans[i] = true;
if (r - l < 3) ans[i] = true;
}
for (int i = 0; i < m; ++i) cout << (ans[i] ? "YES\n" : "NO\n");
return 0;
}
```
|
#include <bits/stdc++.h>
int x[100010] = {0}, y[100010] = {0}, z[100010] = {0};
int main() {
int n, p, q, i, tmp;
char str[100010];
scanf("%s", str);
int len = strlen(str);
if (str[0] == 'x')
x[1] = 1;
else if (str[0] == 'y')
y[1] = 1;
else
z[1] = 1;
for (i = 1; i < len; i++) {
x[i + 1] = x[i];
y[i + 1] = y[i];
z[i + 1] = z[i];
if (str[i] == 'x')
x[i + 1]++;
else if (str[i] == 'y')
y[i + 1]++;
else
z[i + 1]++;
}
scanf("%d", &n);
while (n--) {
scanf("%d %d", &p, &q);
int x1 = x[q] - x[p - 1];
int y1 = y[q] - y[p - 1];
int z1 = z[q] - z[p - 1];
if ((q - p + 1) < 3)
printf("YES\n");
else {
int div = (q - p + 1) / 3;
int rem = (q - p + 1) % 3;
if (rem == 0) {
if (x1 == y1 && y1 == z1)
printf("YES\n");
else
printf("NO\n");
} else if (rem == 1) {
if (x1 == div && y1 == div && z1 == div + 1)
printf("YES\n");
else if (x1 == div && y1 == div + 1 && z1 == div)
printf("YES\n");
else if (x1 == div + 1 && y1 == div && z1 == div)
printf("YES\n");
else
printf("NO\n");
} else {
if (x1 == div && y1 == div + 1 && z1 == div + 1)
printf("YES\n");
else if (x1 == div + 1 && y1 == div && z1 == div + 1)
printf("YES\n");
else if (x1 == div + 1 && y1 == div + 1 && z1 == div)
printf("YES\n");
else
printf("NO\n");
}
}
}
return 0;
}
|
### Prompt
In CPP, your task is to solve the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
int x[100010] = {0}, y[100010] = {0}, z[100010] = {0};
int main() {
int n, p, q, i, tmp;
char str[100010];
scanf("%s", str);
int len = strlen(str);
if (str[0] == 'x')
x[1] = 1;
else if (str[0] == 'y')
y[1] = 1;
else
z[1] = 1;
for (i = 1; i < len; i++) {
x[i + 1] = x[i];
y[i + 1] = y[i];
z[i + 1] = z[i];
if (str[i] == 'x')
x[i + 1]++;
else if (str[i] == 'y')
y[i + 1]++;
else
z[i + 1]++;
}
scanf("%d", &n);
while (n--) {
scanf("%d %d", &p, &q);
int x1 = x[q] - x[p - 1];
int y1 = y[q] - y[p - 1];
int z1 = z[q] - z[p - 1];
if ((q - p + 1) < 3)
printf("YES\n");
else {
int div = (q - p + 1) / 3;
int rem = (q - p + 1) % 3;
if (rem == 0) {
if (x1 == y1 && y1 == z1)
printf("YES\n");
else
printf("NO\n");
} else if (rem == 1) {
if (x1 == div && y1 == div && z1 == div + 1)
printf("YES\n");
else if (x1 == div && y1 == div + 1 && z1 == div)
printf("YES\n");
else if (x1 == div + 1 && y1 == div && z1 == div)
printf("YES\n");
else
printf("NO\n");
} else {
if (x1 == div && y1 == div + 1 && z1 == div + 1)
printf("YES\n");
else if (x1 == div + 1 && y1 == div && z1 == div + 1)
printf("YES\n");
else if (x1 == div + 1 && y1 == div + 1 && z1 == div)
printf("YES\n");
else
printf("NO\n");
}
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
typedef struct {
int x, y, z;
} Data;
Data data[100001];
int main(void) {
int i;
char s[100001];
int n, m;
scanf("%s %d", s, &m);
n = strlen(s);
for (i = 1; i <= n; i++) {
data[i].x = data[i - 1].x + (s[i - 1] == 'x');
data[i].y = data[i - 1].y + (s[i - 1] == 'y');
data[i].z = data[i - 1].z + (s[i - 1] == 'z');
}
for (i = 0; i < m; i++) {
int l, r, x, y, z;
scanf("%d %d", &l, &r);
x = data[r].x - data[l - 1].x;
y = data[r].y - data[l - 1].y;
z = data[r].z - data[l - 1].z;
if (r - l == 1)
puts("YES");
else
puts(x == y && y == z || x == y && z == x + 1 || x == y && z == x - 1 ||
y == z && x == y + 1 || y == z && x == y - 1 ||
z == x && y == z + 1 || z == x && y == z - 1
? "YES"
: "NO");
}
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
typedef struct {
int x, y, z;
} Data;
Data data[100001];
int main(void) {
int i;
char s[100001];
int n, m;
scanf("%s %d", s, &m);
n = strlen(s);
for (i = 1; i <= n; i++) {
data[i].x = data[i - 1].x + (s[i - 1] == 'x');
data[i].y = data[i - 1].y + (s[i - 1] == 'y');
data[i].z = data[i - 1].z + (s[i - 1] == 'z');
}
for (i = 0; i < m; i++) {
int l, r, x, y, z;
scanf("%d %d", &l, &r);
x = data[r].x - data[l - 1].x;
y = data[r].y - data[l - 1].y;
z = data[r].z - data[l - 1].z;
if (r - l == 1)
puts("YES");
else
puts(x == y && y == z || x == y && z == x + 1 || x == y && z == x - 1 ||
y == z && x == y + 1 || y == z && x == y - 1 ||
z == x && y == z + 1 || z == x && y == z - 1
? "YES"
: "NO");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (!b) return a;
return gcd(b, a % b);
}
const int MAX = 100005;
char str[MAX];
int x[MAX], y[MAX], z[MAX];
int main() {
scanf("%s", str + 1);
int n;
int len = strlen(str + 1);
for (int i = (1); i <= (len); ++i) {
x[i] = x[i - 1];
y[i] = y[i - 1];
z[i] = z[i - 1];
if (str[i] == 'x')
++x[i];
else if (str[i] == 'y')
++y[i];
else if (str[i] == 'z')
++z[i];
}
scanf("%d", &n);
int a, b;
for (int i = (1); i <= (n); ++i) {
scanf("%d%d", &a, &b);
int k = b - a + 1;
if (k <= 2) {
printf("YES\n");
continue;
}
int p = x[b] - x[a - 1];
int q = y[b] - y[a - 1];
int r = z[b] - z[a - 1];
int s = max(p, max(q, r));
int t = min(p, min(q, r));
if (s - t <= 1) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (!b) return a;
return gcd(b, a % b);
}
const int MAX = 100005;
char str[MAX];
int x[MAX], y[MAX], z[MAX];
int main() {
scanf("%s", str + 1);
int n;
int len = strlen(str + 1);
for (int i = (1); i <= (len); ++i) {
x[i] = x[i - 1];
y[i] = y[i - 1];
z[i] = z[i - 1];
if (str[i] == 'x')
++x[i];
else if (str[i] == 'y')
++y[i];
else if (str[i] == 'z')
++z[i];
}
scanf("%d", &n);
int a, b;
for (int i = (1); i <= (n); ++i) {
scanf("%d%d", &a, &b);
int k = b - a + 1;
if (k <= 2) {
printf("YES\n");
continue;
}
int p = x[b] - x[a - 1];
int q = y[b] - y[a - 1];
int r = z[b] - z[a - 1];
int s = max(p, max(q, r));
int t = min(p, min(q, r));
if (s - t <= 1) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const int MAXN = 1e6 + 5;
void cp() {
string s;
cin >> s;
int n = ((int)s.length());
int pref[n + 1][3];
memset(pref, 0, sizeof(pref));
for (int i = 1; i <= n; i++) {
for (char j = 'x'; j <= 'z'; j++) {
pref[i][j - 'x'] = pref[i - 1][j - 'x'] + (s[i - 1] == j);
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
vector<int> cnt(3);
for (int i = 0; i < 3; i++) cnt[i] = pref[r][i] - pref[l - 1][i];
int len = r - l + 1;
int lo = len / 3;
int hi = (len + 2) / 3;
bool ok = true;
for (int c : cnt)
if ((c < lo) || (c > hi)) ok = false;
if (len < 3) ok = true;
cout << (ok ? "YES\n" : "NO\n");
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
t = 1;
while (t--) {
cp();
}
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const int MAXN = 1e6 + 5;
void cp() {
string s;
cin >> s;
int n = ((int)s.length());
int pref[n + 1][3];
memset(pref, 0, sizeof(pref));
for (int i = 1; i <= n; i++) {
for (char j = 'x'; j <= 'z'; j++) {
pref[i][j - 'x'] = pref[i - 1][j - 'x'] + (s[i - 1] == j);
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
vector<int> cnt(3);
for (int i = 0; i < 3; i++) cnt[i] = pref[r][i] - pref[l - 1][i];
int len = r - l + 1;
int lo = len / 3;
int hi = (len + 2) / 3;
bool ok = true;
for (int c : cnt)
if ((c < lo) || (c > hi)) ok = false;
if (len < 3) ok = true;
cout << (ok ? "YES\n" : "NO\n");
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
t = 1;
while (t--) {
cp();
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int X[MAXN] = {0}, Y[MAXN] = {0}, Z[MAXN] = {0};
int N = 1, M, l, r;
char ch;
int a, b, c;
int main() {
while (scanf("%c", &ch), ch != '\n') {
X[N] = X[N - 1];
Y[N] = Y[N - 1];
Z[N] = Z[N - 1];
if (ch == 'x')
X[N]++;
else if (ch == 'y')
Y[N]++;
else if (ch == 'z')
Z[N]++;
N++;
}
scanf("%d", &M);
while (M--) {
scanf("%d%d", &l, &r);
if (r - l <= 1) {
cout << "YES\n";
continue;
}
a = X[r] - X[l - 1];
b = Y[r] - Y[l - 1];
c = Z[r] - Z[l - 1];
int m = min(min(a, b), c);
if (a - m > 1 || b - m > 1 || c - m > 1)
cout << "NO\n";
else
cout << "YES\n";
}
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int X[MAXN] = {0}, Y[MAXN] = {0}, Z[MAXN] = {0};
int N = 1, M, l, r;
char ch;
int a, b, c;
int main() {
while (scanf("%c", &ch), ch != '\n') {
X[N] = X[N - 1];
Y[N] = Y[N - 1];
Z[N] = Z[N - 1];
if (ch == 'x')
X[N]++;
else if (ch == 'y')
Y[N]++;
else if (ch == 'z')
Z[N]++;
N++;
}
scanf("%d", &M);
while (M--) {
scanf("%d%d", &l, &r);
if (r - l <= 1) {
cout << "YES\n";
continue;
}
a = X[r] - X[l - 1];
b = Y[r] - Y[l - 1];
c = Z[r] - Z[l - 1];
int m = min(min(a, b), c);
if (a - m > 1 || b - m > 1 || c - m > 1)
cout << "NO\n";
else
cout << "YES\n";
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int tu(int val) { return (1 << val); }
bool iset(int mask, int id) {
if ((mask & tu(id)) != 0) return true;
return false;
}
void doset(int &mask, int id) { mask |= tu(id); }
void dounset(int &mask, int id) { mask = mask & (~tu(id)); }
template <typename T>
string tos(T a) {
stringstream ss;
string ret;
ss << a;
ss >> ret;
return ret;
}
int cum[3][100009], N;
string in;
int main() {
while (cin >> in) {
N = in.size();
memset(cum, 0, sizeof cum);
;
for (int(i) = (0); (i) < (N); (i)++) {
for (int(j) = (0); (j) < (3); (j)++) cum[j][i + 1] = cum[j][i];
cum['z' - in[i]][i + 1]++;
}
int L, R, m;
cin >> m;
for (int(i) = (0); (i) < (m); (i)++) {
cin >> L >> R;
vector<int> vv;
for (int(j) = (0); (j) < (3); (j)++) {
int cnt = cum[j][R] - cum[j][L - 1];
vv.push_back(cnt);
}
sort(vv.begin(), vv.end());
if ((R - L + 1) == 3) {
string st = in.substr(L - 1, 3);
sort(st.begin(), st.end());
if (st != "xyz")
cout << "NO" << endl;
else
cout << "YES" << endl;
} else if ((R - L + 1 < 3) || vv[2] - vv[0] <= 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int tu(int val) { return (1 << val); }
bool iset(int mask, int id) {
if ((mask & tu(id)) != 0) return true;
return false;
}
void doset(int &mask, int id) { mask |= tu(id); }
void dounset(int &mask, int id) { mask = mask & (~tu(id)); }
template <typename T>
string tos(T a) {
stringstream ss;
string ret;
ss << a;
ss >> ret;
return ret;
}
int cum[3][100009], N;
string in;
int main() {
while (cin >> in) {
N = in.size();
memset(cum, 0, sizeof cum);
;
for (int(i) = (0); (i) < (N); (i)++) {
for (int(j) = (0); (j) < (3); (j)++) cum[j][i + 1] = cum[j][i];
cum['z' - in[i]][i + 1]++;
}
int L, R, m;
cin >> m;
for (int(i) = (0); (i) < (m); (i)++) {
cin >> L >> R;
vector<int> vv;
for (int(j) = (0); (j) < (3); (j)++) {
int cnt = cum[j][R] - cum[j][L - 1];
vv.push_back(cnt);
}
sort(vv.begin(), vv.end());
if ((R - L + 1) == 3) {
string st = in.substr(L - 1, 3);
sort(st.begin(), st.end());
if (st != "xyz")
cout << "NO" << endl;
else
cout << "YES" << endl;
} else if ((R - L + 1 < 3) || vv[2] - vv[0] <= 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int z[2000001], x[2000001], y[2000001];
int main() {
string s;
cin >> s;
if (s[0] == 'x') x[0]++;
if (s[0] == 'y') y[0]++;
if (s[0] == 'z') z[0]++;
for (int i = 1; i < s.length(); i++) {
x[i] = x[i - 1], y[i] = y[i - 1], z[i] = z[i - 1];
if (s[i] == 'x') x[i]++;
if (s[i] == 'y') y[i]++;
if (s[i] == 'z') z[i]++;
}
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int X, Y, Z;
int l, r;
cin >> l >> r;
l--, r--;
if (r - l < 2) {
cout << "YES" << endl;
continue;
}
if (l == 0) {
Z = z[r];
X = x[r];
Y = y[r];
} else {
Z = z[r] - z[l - 1];
X = x[r] - x[l - 1];
Y = y[r] - y[l - 1];
}
if (abs(X - Y) <= 1 && abs(X - Z) <= 1 && abs(Y - Z) <= 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int z[2000001], x[2000001], y[2000001];
int main() {
string s;
cin >> s;
if (s[0] == 'x') x[0]++;
if (s[0] == 'y') y[0]++;
if (s[0] == 'z') z[0]++;
for (int i = 1; i < s.length(); i++) {
x[i] = x[i - 1], y[i] = y[i - 1], z[i] = z[i - 1];
if (s[i] == 'x') x[i]++;
if (s[i] == 'y') y[i]++;
if (s[i] == 'z') z[i]++;
}
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int X, Y, Z;
int l, r;
cin >> l >> r;
l--, r--;
if (r - l < 2) {
cout << "YES" << endl;
continue;
}
if (l == 0) {
Z = z[r];
X = x[r];
Y = y[r];
} else {
Z = z[r] - z[l - 1];
X = x[r] - x[l - 1];
Y = y[r] - y[l - 1];
}
if (abs(X - Y) <= 1 && abs(X - Z) <= 1 && abs(Y - Z) <= 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
unsigned int const L = 1e5 + 42;
char S[L];
unsigned int cx[L] = {};
unsigned int cy[L] = {};
unsigned int cz[L] = {};
int main() {
scanf("%s", S);
unsigned int N = strlen(S);
for (unsigned int i = 0; i < N; ++i) {
cx[i + 1] = cx[i] + (S[i] == 'x');
cy[i + 1] = cy[i] + (S[i] == 'y');
cz[i + 1] = cz[i] + (S[i] == 'z');
}
unsigned int M = 0;
scanf("%u", &M);
for (unsigned int i = 0; i < M; ++i) {
unsigned int l, r;
scanf("%u%u", &l, &r);
unsigned int n = r - l + 1;
unsigned int n0 = n / 3;
unsigned int nx = cx[r] - cx[l - 1];
unsigned int ny = cy[r] - cy[l - 1];
unsigned int nz = cz[r] - cz[l - 1];
if (n < 3 || n0 <= nx && nx <= n0 + 1 && n0 <= ny && ny <= n0 + 1 &&
n0 <= nz && nz <= n0 + 1)
printf("YES\n");
else
printf("NO\n");
}
}
|
### Prompt
Create a solution in Cpp for the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
unsigned int const L = 1e5 + 42;
char S[L];
unsigned int cx[L] = {};
unsigned int cy[L] = {};
unsigned int cz[L] = {};
int main() {
scanf("%s", S);
unsigned int N = strlen(S);
for (unsigned int i = 0; i < N; ++i) {
cx[i + 1] = cx[i] + (S[i] == 'x');
cy[i + 1] = cy[i] + (S[i] == 'y');
cz[i + 1] = cz[i] + (S[i] == 'z');
}
unsigned int M = 0;
scanf("%u", &M);
for (unsigned int i = 0; i < M; ++i) {
unsigned int l, r;
scanf("%u%u", &l, &r);
unsigned int n = r - l + 1;
unsigned int n0 = n / 3;
unsigned int nx = cx[r] - cx[l - 1];
unsigned int ny = cy[r] - cy[l - 1];
unsigned int nz = cz[r] - cz[l - 1];
if (n < 3 || n0 <= nx && nx <= n0 + 1 && n0 <= ny && ny <= n0 + 1 &&
n0 <= nz && nz <= n0 + 1)
printf("YES\n");
else
printf("NO\n");
}
}
```
|
#include <bits/stdc++.h>
char s[100010];
int a[100100];
int f1[100100], f2[100100], f3[100100];
int main() {
int n, m, i, a, b, c, sum1, sum2, sum3, flag;
while (scanf("%s", s) != EOF) {
scanf("%d", &m);
memset(f1, 0, sizeof(f1));
memset(f2, 0, sizeof(f2));
memset(f3, 0, sizeof(f3));
flag = 0;
for (i = 0; s[i]; i++) {
if (flag == 0) {
flag = 1;
if (s[i] == 'x') f1[i + 1] = 1;
if (s[i] == 'y') f2[i + 1] = 1;
if (s[i] == 'z') f3[i + 1] = 1;
} else {
if (s[i] == 'x') f1[i + 1]++;
if (s[i] == 'y') f2[i + 1]++;
if (s[i] == 'z') f3[i + 1]++;
f1[i + 1] += f1[i];
f2[i + 1] += f2[i];
f3[i + 1] += f3[i];
}
}
while (m--) {
scanf("%d%d", &b, &c);
if (c - b + 1 < 3) {
printf("YES\n");
continue;
}
sum1 = f1[c] - f1[b - 1];
sum2 = f2[c] - f2[b - 1];
sum3 = f3[c] - f3[b - 1];
if (sum1 == 0 || sum2 == 0 || sum3 == 0) {
printf("NO\n");
continue;
}
if (fabs(sum1 - sum2) >= 2 || fabs(sum1 - sum3) >= 2 ||
fabs(sum2 - sum3) >= 2)
printf("NO\n");
else
printf("YES\n");
}
}
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
char s[100010];
int a[100100];
int f1[100100], f2[100100], f3[100100];
int main() {
int n, m, i, a, b, c, sum1, sum2, sum3, flag;
while (scanf("%s", s) != EOF) {
scanf("%d", &m);
memset(f1, 0, sizeof(f1));
memset(f2, 0, sizeof(f2));
memset(f3, 0, sizeof(f3));
flag = 0;
for (i = 0; s[i]; i++) {
if (flag == 0) {
flag = 1;
if (s[i] == 'x') f1[i + 1] = 1;
if (s[i] == 'y') f2[i + 1] = 1;
if (s[i] == 'z') f3[i + 1] = 1;
} else {
if (s[i] == 'x') f1[i + 1]++;
if (s[i] == 'y') f2[i + 1]++;
if (s[i] == 'z') f3[i + 1]++;
f1[i + 1] += f1[i];
f2[i + 1] += f2[i];
f3[i + 1] += f3[i];
}
}
while (m--) {
scanf("%d%d", &b, &c);
if (c - b + 1 < 3) {
printf("YES\n");
continue;
}
sum1 = f1[c] - f1[b - 1];
sum2 = f2[c] - f2[b - 1];
sum3 = f3[c] - f3[b - 1];
if (sum1 == 0 || sum2 == 0 || sum3 == 0) {
printf("NO\n");
continue;
}
if (fabs(sum1 - sum2) >= 2 || fabs(sum1 - sum3) >= 2 ||
fabs(sum2 - sum3) >= 2)
printf("NO\n");
else
printf("YES\n");
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const int MAXN = 1e6 + 5;
void cp() {
string s;
cin >> s;
int n = ((int)s.length());
int pref[n + 1][3];
memset(pref, 0, sizeof(pref));
for (int i = 1; i <= n; i++) {
for (char j = 'x'; j <= 'z'; j++) {
pref[i][j - 'x'] = pref[i - 1][j - 'x'] + (s[i - 1] == j);
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
int cnt_x, cnt_y, cnt_z;
cnt_x = pref[r][0] - pref[l - 1][0];
cnt_y = pref[r][1] - pref[l - 1][1];
cnt_z = pref[r][2] - pref[l - 1][2];
vector<int> cnt = {cnt_x, cnt_y, cnt_z};
int len = r - l + 1;
int lo = len / 3;
int hi = (len + 2) / 3;
bool ok = true;
for (int c : cnt)
if ((c < lo) || (c > hi)) ok = false;
if (len < 3) ok = true;
cout << (ok ? "YES\n" : "NO\n");
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
t = 1;
while (t--) {
cp();
}
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const int MAXN = 1e6 + 5;
void cp() {
string s;
cin >> s;
int n = ((int)s.length());
int pref[n + 1][3];
memset(pref, 0, sizeof(pref));
for (int i = 1; i <= n; i++) {
for (char j = 'x'; j <= 'z'; j++) {
pref[i][j - 'x'] = pref[i - 1][j - 'x'] + (s[i - 1] == j);
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
int cnt_x, cnt_y, cnt_z;
cnt_x = pref[r][0] - pref[l - 1][0];
cnt_y = pref[r][1] - pref[l - 1][1];
cnt_z = pref[r][2] - pref[l - 1][2];
vector<int> cnt = {cnt_x, cnt_y, cnt_z};
int len = r - l + 1;
int lo = len / 3;
int hi = (len + 2) / 3;
bool ok = true;
for (int c : cnt)
if ((c < lo) || (c > hi)) ok = false;
if (len < 3) ok = true;
cout << (ok ? "YES\n" : "NO\n");
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
t = 1;
while (t--) {
cp();
}
return 0;
}
```
|
#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;
const ll inf = 2e18;
const ll mod = 1e9 + 7;
const ll N = 2e5 + 5;
string s;
int q;
int mapping(char c) {
if (c == 'x')
return 0;
else if (c == 'y')
return 1;
return 2;
}
void code() {
cin >> s;
int n = s.size();
vector<vector<int>> pref(n + 1, vector<int>(3, 0));
for (int i = 0; i < n; i++) {
for (char c : {'x', 'y', 'z'}) {
pref[i + 1][mapping(c)] = pref[i][mapping(c)] + (s[i] == c);
}
}
cin >> q;
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
int dist = (r - l + 1), f = 0;
if (dist < 3) {
cout << "YES\n";
continue;
}
int mn = 1e9, mx = -1e9;
for (int j = 0; j < 3; j++) {
int cnt = pref[r][j] - pref[l - 1][j];
mn = min(mn, cnt);
mx = max(mx, cnt);
}
cout << (abs(mx - mn) > 1 ? "NO\n" : "YES\n");
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
while (t-- > 0) {
code();
}
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;
const ll inf = 2e18;
const ll mod = 1e9 + 7;
const ll N = 2e5 + 5;
string s;
int q;
int mapping(char c) {
if (c == 'x')
return 0;
else if (c == 'y')
return 1;
return 2;
}
void code() {
cin >> s;
int n = s.size();
vector<vector<int>> pref(n + 1, vector<int>(3, 0));
for (int i = 0; i < n; i++) {
for (char c : {'x', 'y', 'z'}) {
pref[i + 1][mapping(c)] = pref[i][mapping(c)] + (s[i] == c);
}
}
cin >> q;
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
int dist = (r - l + 1), f = 0;
if (dist < 3) {
cout << "YES\n";
continue;
}
int mn = 1e9, mx = -1e9;
for (int j = 0; j < 3; j++) {
int cnt = pref[r][j] - pref[l - 1][j];
mn = min(mn, cnt);
mx = max(mx, cnt);
}
cout << (abs(mx - mn) > 1 ? "NO\n" : "YES\n");
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
while (t-- > 0) {
code();
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
int cnt[100009], a[100009][3];
int main() {
string s;
cin >> s;
int n = s.length(), i, q, l, r;
s = "0" + s + "00";
for (i = 1; i <= n; i++) {
a[i][0] = a[i - 1][0], a[i][1] = a[i - 1][1], a[i][2] = a[i - 1][2];
if (s[i] == 'x') a[i][0]++;
if (s[i] == 'y') a[i][1]++;
if (s[i] == 'z') a[i][2]++;
cnt[i] = cnt[i - 1];
if (s[i] == 'z' && s[i + 1] == 'y' && s[i + 2] == 'x')
cnt[i]++;
else if (s[i] == 'x' && s[i + 1] == 'z' && s[i + 2] == 'y')
cnt[i]++;
else if (s[i] == 'y' && s[i + 1] == 'x' && s[i + 2] == 'z')
cnt[i]++;
}
cin >> q;
while (q--) {
cin >> l >> r;
if (r - l + 1 < 3) {
cout << "YES\n";
continue;
}
if (cnt[r - 2] - cnt[l - 1] == r - l + 1 - 2) {
cout << "YES\n";
continue;
}
int x = a[r][0] - a[l - 1][0];
int y = a[r][1] - a[l - 1][1];
int z = a[r][2] - a[l - 1][2];
int xx = abs(x - y);
int yy = abs(y - z);
int zz = abs(x - z);
if (xx <= 1 && yy <= 1 && zz <= 1) {
cout << "YES\n";
continue;
}
cout << "NO\n";
}
return 0;
}
|
### Prompt
Generate a CPP solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int cnt[100009], a[100009][3];
int main() {
string s;
cin >> s;
int n = s.length(), i, q, l, r;
s = "0" + s + "00";
for (i = 1; i <= n; i++) {
a[i][0] = a[i - 1][0], a[i][1] = a[i - 1][1], a[i][2] = a[i - 1][2];
if (s[i] == 'x') a[i][0]++;
if (s[i] == 'y') a[i][1]++;
if (s[i] == 'z') a[i][2]++;
cnt[i] = cnt[i - 1];
if (s[i] == 'z' && s[i + 1] == 'y' && s[i + 2] == 'x')
cnt[i]++;
else if (s[i] == 'x' && s[i + 1] == 'z' && s[i + 2] == 'y')
cnt[i]++;
else if (s[i] == 'y' && s[i + 1] == 'x' && s[i + 2] == 'z')
cnt[i]++;
}
cin >> q;
while (q--) {
cin >> l >> r;
if (r - l + 1 < 3) {
cout << "YES\n";
continue;
}
if (cnt[r - 2] - cnt[l - 1] == r - l + 1 - 2) {
cout << "YES\n";
continue;
}
int x = a[r][0] - a[l - 1][0];
int y = a[r][1] - a[l - 1][1];
int z = a[r][2] - a[l - 1][2];
int xx = abs(x - y);
int yy = abs(y - z);
int zz = abs(x - z);
if (xx <= 1 && yy <= 1 && zz <= 1) {
cout << "YES\n";
continue;
}
cout << "NO\n";
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int PINF = numeric_limits<int>::max();
const int NINF = numeric_limits<int>::min();
const long long M = 1E9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin >> s;
int m;
cin >> m;
int n = (int)s.size();
vector<int> xcnt(n + 1, 0), ycnt(n + 1, 0), zcnt(n + 1, 0);
for (int i = 1; i <= n; i++) {
xcnt[i] = xcnt[i - 1];
ycnt[i] = ycnt[i - 1];
zcnt[i] = zcnt[i - 1];
if (s[i - 1] == 'x') xcnt[i]++;
if (s[i - 1] == 'y') ycnt[i]++;
if (s[i - 1] == 'z') zcnt[i]++;
}
while (m--) {
int l, r;
cin >> l >> r;
int x, y, z;
x = xcnt[r] - xcnt[l - 1];
y = ycnt[r] - ycnt[l - 1];
z = zcnt[r] - zcnt[l - 1];
int range = r - l + 1;
int req = range / 3;
string ans = "NO";
if (range < 3)
ans = "YES";
else if (x >= req && y >= req && z >= req && abs(x - y) <= 1 &&
abs(x - z) <= 1 && abs(y - z) <= 1) {
ans = "YES";
}
cout << ans << "\n";
}
return 0;
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int PINF = numeric_limits<int>::max();
const int NINF = numeric_limits<int>::min();
const long long M = 1E9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin >> s;
int m;
cin >> m;
int n = (int)s.size();
vector<int> xcnt(n + 1, 0), ycnt(n + 1, 0), zcnt(n + 1, 0);
for (int i = 1; i <= n; i++) {
xcnt[i] = xcnt[i - 1];
ycnt[i] = ycnt[i - 1];
zcnt[i] = zcnt[i - 1];
if (s[i - 1] == 'x') xcnt[i]++;
if (s[i - 1] == 'y') ycnt[i]++;
if (s[i - 1] == 'z') zcnt[i]++;
}
while (m--) {
int l, r;
cin >> l >> r;
int x, y, z;
x = xcnt[r] - xcnt[l - 1];
y = ycnt[r] - ycnt[l - 1];
z = zcnt[r] - zcnt[l - 1];
int range = r - l + 1;
int req = range / 3;
string ans = "NO";
if (range < 3)
ans = "YES";
else if (x >= req && y >= req && z >= req && abs(x - y) <= 1 &&
abs(x - z) <= 1 && abs(y - z) <= 1) {
ans = "YES";
}
cout << ans << "\n";
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int const MAXSIZE = 100000 + 10;
char s[MAXSIZE];
int cnt[MAXSIZE][4];
int main() {
int m, l, r;
while (scanf("%s", s + 1) != EOF) {
scanf("%d", &m);
memset(cnt, 0, sizeof(cnt));
int len = strlen(s + 1);
for (int i = 1; i <= len; i++) {
cnt[i][1] = cnt[i - 1][1];
cnt[i][2] = cnt[i - 1][2];
cnt[i][3] = cnt[i - 1][3];
if (s[i] == 'x') {
cnt[i][1]++;
} else if (s[i] == 'y') {
cnt[i][2]++;
} else {
cnt[i][3]++;
}
}
while (m--) {
scanf("%d%d", &l, &r);
int cntx = cnt[r][1] - cnt[l - 1][1];
int cnty = cnt[r][2] - cnt[l - 1][2];
int cntz = cnt[r][3] - cnt[l - 1][3];
int mincnt = min(cntx, cnty);
mincnt = min(mincnt, cntz);
if (r - l <= 1 ||
cntx - mincnt <= 1 && cnty - mincnt <= 1 && cntz - mincnt <= 1)
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int const MAXSIZE = 100000 + 10;
char s[MAXSIZE];
int cnt[MAXSIZE][4];
int main() {
int m, l, r;
while (scanf("%s", s + 1) != EOF) {
scanf("%d", &m);
memset(cnt, 0, sizeof(cnt));
int len = strlen(s + 1);
for (int i = 1; i <= len; i++) {
cnt[i][1] = cnt[i - 1][1];
cnt[i][2] = cnt[i - 1][2];
cnt[i][3] = cnt[i - 1][3];
if (s[i] == 'x') {
cnt[i][1]++;
} else if (s[i] == 'y') {
cnt[i][2]++;
} else {
cnt[i][3]++;
}
}
while (m--) {
scanf("%d%d", &l, &r);
int cntx = cnt[r][1] - cnt[l - 1][1];
int cnty = cnt[r][2] - cnt[l - 1][2];
int cntz = cnt[r][3] - cnt[l - 1][3];
int mincnt = min(cntx, cnty);
mincnt = min(mincnt, cntz);
if (r - l <= 1 ||
cntx - mincnt <= 1 && cnty - mincnt <= 1 && cntz - mincnt <= 1)
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct qq {
int x, y, z;
} q[100006], q1;
int main() {
string s;
int m, l, r;
cin >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'x') {
q[i].x = q[i - 1].x + 1;
q[i].y = q[i - 1].y;
q[i].z = q[i - 1].z;
}
if (s[i] == 'y') {
q[i].x = q[i - 1].x;
q[i].y = q[i - 1].y + 1;
q[i].z = q[i - 1].z;
}
if (s[i] == 'z') {
q[i].x = q[i - 1].x;
q[i].y = q[i - 1].y;
q[i].z = q[i - 1].z + 1;
}
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> l >> r;
if (r - l < 2) {
cout << "YES" << endl;
continue;
}
q1.x = q[r - 1].x - q[l - 2].x;
q1.y = q[r - 1].y - q[l - 2].y;
q1.z = q[r - 1].z - q[l - 2].z;
if (abs(q1.x - q1.y) > 1 || abs(q1.x - q1.z) > 1 || abs(q1.y - q1.z) > 1)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
}
|
### Prompt
Create a solution in CPP for the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct qq {
int x, y, z;
} q[100006], q1;
int main() {
string s;
int m, l, r;
cin >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'x') {
q[i].x = q[i - 1].x + 1;
q[i].y = q[i - 1].y;
q[i].z = q[i - 1].z;
}
if (s[i] == 'y') {
q[i].x = q[i - 1].x;
q[i].y = q[i - 1].y + 1;
q[i].z = q[i - 1].z;
}
if (s[i] == 'z') {
q[i].x = q[i - 1].x;
q[i].y = q[i - 1].y;
q[i].z = q[i - 1].z + 1;
}
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> l >> r;
if (r - l < 2) {
cout << "YES" << endl;
continue;
}
q1.x = q[r - 1].x - q[l - 2].x;
q1.y = q[r - 1].y - q[l - 2].y;
q1.z = q[r - 1].z - q[l - 2].z;
if (abs(q1.x - q1.y) > 1 || abs(q1.x - q1.z) > 1 || abs(q1.y - q1.z) > 1)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int m;
cin >> m;
vector<int> x(s.size() + 1), y(s.size() + 1), z(s.size() + 1);
for (int i = 1; i <= s.size(); ++i) {
x[i] = x[i - 1];
y[i] = y[i - 1];
z[i] = z[i - 1];
switch (s[i - 1]) {
case 'x':
++x[i];
break;
case 'y':
++y[i];
break;
case 'z':
++z[i];
break;
}
}
int l, r, a, b, c;
for (int i = 0; i < m; ++i) {
cin >> l >> r;
a = x[r] - x[l - 1];
b = y[r] - y[l - 1];
c = z[r] - z[l - 1];
if (r - l + 1 < 3)
cout << "YES" << endl;
else if (abs((double)a - b) > 1 || abs((double)a - c) > 1 ||
abs((double)c - b) > 1)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int m;
cin >> m;
vector<int> x(s.size() + 1), y(s.size() + 1), z(s.size() + 1);
for (int i = 1; i <= s.size(); ++i) {
x[i] = x[i - 1];
y[i] = y[i - 1];
z[i] = z[i - 1];
switch (s[i - 1]) {
case 'x':
++x[i];
break;
case 'y':
++y[i];
break;
case 'z':
++z[i];
break;
}
}
int l, r, a, b, c;
for (int i = 0; i < m; ++i) {
cin >> l >> r;
a = x[r] - x[l - 1];
b = y[r] - y[l - 1];
c = z[r] - z[l - 1];
if (r - l + 1 < 3)
cout << "YES" << endl;
else if (abs((double)a - b) > 1 || abs((double)a - c) > 1 ||
abs((double)c - b) > 1)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
string str;
int sum[100024][3];
void precompute() {
memset(sum, 0, sizeof(sum));
if (str[0] == 'x')
sum[0][0]++;
else if (str[0] == 'y')
sum[0][1]++;
else
sum[0][2]++;
for (int i = 1; i < str.size(); i++) {
if (str[i] == 'x') {
sum[i][0] = sum[i - 1][0] + 1;
sum[i][1] = sum[i - 1][1];
sum[i][2] = sum[i - 1][2];
} else if (str[i] == 'y') {
sum[i][0] = sum[i - 1][0];
sum[i][1] = sum[i - 1][1] + 1;
sum[i][2] = sum[i - 1][2];
} else {
sum[i][0] = sum[i - 1][0];
sum[i][1] = sum[i - 1][1];
sum[i][2] = sum[i - 1][2] + 1;
}
}
}
bool solve(int l, int r) {
l--, r--;
int a[3];
for (int i = 0; i < 3; i++) a[i] = sum[r][i] - sum[l][i];
if (str[l] == 'x')
a[0]++;
else if (str[l] == 'y')
a[1]++;
else
a[2]++;
if (r - l + 1 < 3) return true;
int rem = (r - l + 1) % 3;
sort(a, a + 3);
switch (rem) {
case 0:
if (a[0] == a[1] && a[1] == a[2]) return true;
break;
case 1:
if (a[0] == a[1] && a[2] - a[1] == 1) return true;
break;
case 2:
if (a[1] == a[2] && a[1] - a[0] == 1) return true;
break;
}
return false;
}
int main() {
int m, l, r;
cin >> str;
scanf("%d", &m);
precompute();
for (int i = 0; i < m; i++) {
scanf("%d%d", &l, &r);
if (solve(l, r))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
string str;
int sum[100024][3];
void precompute() {
memset(sum, 0, sizeof(sum));
if (str[0] == 'x')
sum[0][0]++;
else if (str[0] == 'y')
sum[0][1]++;
else
sum[0][2]++;
for (int i = 1; i < str.size(); i++) {
if (str[i] == 'x') {
sum[i][0] = sum[i - 1][0] + 1;
sum[i][1] = sum[i - 1][1];
sum[i][2] = sum[i - 1][2];
} else if (str[i] == 'y') {
sum[i][0] = sum[i - 1][0];
sum[i][1] = sum[i - 1][1] + 1;
sum[i][2] = sum[i - 1][2];
} else {
sum[i][0] = sum[i - 1][0];
sum[i][1] = sum[i - 1][1];
sum[i][2] = sum[i - 1][2] + 1;
}
}
}
bool solve(int l, int r) {
l--, r--;
int a[3];
for (int i = 0; i < 3; i++) a[i] = sum[r][i] - sum[l][i];
if (str[l] == 'x')
a[0]++;
else if (str[l] == 'y')
a[1]++;
else
a[2]++;
if (r - l + 1 < 3) return true;
int rem = (r - l + 1) % 3;
sort(a, a + 3);
switch (rem) {
case 0:
if (a[0] == a[1] && a[1] == a[2]) return true;
break;
case 1:
if (a[0] == a[1] && a[2] - a[1] == 1) return true;
break;
case 2:
if (a[1] == a[2] && a[1] - a[0] == 1) return true;
break;
}
return false;
}
int main() {
int m, l, r;
cin >> str;
scanf("%d", &m);
precompute();
for (int i = 0; i < m; i++) {
scanf("%d%d", &l, &r);
if (solve(l, r))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline int mod(int x) {
if (x < 0) {
return -x;
}
return x;
}
int main() {
string s;
cin >> s;
s = " " + s;
int count_x[100002], count_y[100002], count_z[100002], i;
count_x[0] = count_y[0] = count_z[0] = 0;
for (i = 1; i <= s.length(); i++) {
count_x[i] = count_x[i - 1];
count_y[i] = count_y[i - 1];
count_z[i] = count_z[i - 1];
if (s[i] == 'x') {
count_x[i]++;
} else if (s[i] == 'y') {
count_y[i]++;
} else if (s[i] == 'z') {
count_z[i]++;
}
}
int q, x, y, xc, yc, zc;
cin >> q;
while (q--) {
cin >> x >> y;
if (y - x < 2) {
cout << "YES" << endl;
continue;
}
xc = count_x[y] - count_x[x - 1];
yc = count_y[y] - count_y[x - 1];
zc = count_z[y] - count_z[x - 1];
if (mod(xc - yc) >= 2 or mod(yc - zc) >= 2 or mod(zc - xc) >= 2) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
}
return 0;
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int mod(int x) {
if (x < 0) {
return -x;
}
return x;
}
int main() {
string s;
cin >> s;
s = " " + s;
int count_x[100002], count_y[100002], count_z[100002], i;
count_x[0] = count_y[0] = count_z[0] = 0;
for (i = 1; i <= s.length(); i++) {
count_x[i] = count_x[i - 1];
count_y[i] = count_y[i - 1];
count_z[i] = count_z[i - 1];
if (s[i] == 'x') {
count_x[i]++;
} else if (s[i] == 'y') {
count_y[i]++;
} else if (s[i] == 'z') {
count_z[i]++;
}
}
int q, x, y, xc, yc, zc;
cin >> q;
while (q--) {
cin >> x >> y;
if (y - x < 2) {
cout << "YES" << endl;
continue;
}
xc = count_x[y] - count_x[x - 1];
yc = count_y[y] - count_y[x - 1];
zc = count_z[y] - count_z[x - 1];
if (mod(xc - yc) >= 2 or mod(yc - zc) >= 2 or mod(zc - xc) >= 2) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
char str[100005];
int m, l, r;
while (scanf("%s", str) != EOF) {
int len = strlen(str);
scanf("%d", &m);
int visit[len + 10][256];
memset(visit, 0, sizeof(visit));
for (int i = len - 1; i >= 0; i--) {
visit[i][str[i]]++;
visit[i]['x'] += visit[i + 1]['x'];
visit[i]['y'] += visit[i + 1]['y'];
visit[i]['z'] += visit[i + 1]['z'];
}
while (m--) {
scanf("%d %d", &l, &r);
int sublen = r - l + 1;
int x = visit[l - 1]['x'] - visit[r]['x'];
int y = visit[l - 1]['y'] - visit[r]['y'];
int z = visit[l - 1]['z'] - visit[r]['z'];
if (sublen < 3)
printf("YES\n");
else if (sublen % 3 == 0) {
printf(x == y && y == z && z == sublen / 3 ? "YES\n" : "NO\n");
} else {
if (abs(x - y) <= 1 && abs(y - z) <= 1 && abs(z - x) <= 1)
printf("YES\n");
else
printf("NO\n");
}
}
}
return 0;
}
|
### Prompt
In cpp, your task is to solve the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
char str[100005];
int m, l, r;
while (scanf("%s", str) != EOF) {
int len = strlen(str);
scanf("%d", &m);
int visit[len + 10][256];
memset(visit, 0, sizeof(visit));
for (int i = len - 1; i >= 0; i--) {
visit[i][str[i]]++;
visit[i]['x'] += visit[i + 1]['x'];
visit[i]['y'] += visit[i + 1]['y'];
visit[i]['z'] += visit[i + 1]['z'];
}
while (m--) {
scanf("%d %d", &l, &r);
int sublen = r - l + 1;
int x = visit[l - 1]['x'] - visit[r]['x'];
int y = visit[l - 1]['y'] - visit[r]['y'];
int z = visit[l - 1]['z'] - visit[r]['z'];
if (sublen < 3)
printf("YES\n");
else if (sublen % 3 == 0) {
printf(x == y && y == z && z == sublen / 3 ? "YES\n" : "NO\n");
} else {
if (abs(x - y) <= 1 && abs(y - z) <= 1 && abs(z - x) <= 1)
printf("YES\n");
else
printf("NO\n");
}
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, m1, m2;
int num[100100][3];
int temp[3];
string s;
int main() {
while (cin >> s) {
memset(num, 0, sizeof(num));
memset(temp, 0, sizeof(temp));
for (int i = 0; i < s.size(); i++) {
for (int j = 0; j < 3; j++) {
num[i + 1][j] = num[i][j];
}
num[i + 1][s[i] - 'x']++;
}
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &m1, &m2);
if ((m2 - m1 + 1) < 3) {
printf("YES\n");
} else {
temp[0] = num[m2][0] - num[m1 - 1][0];
temp[1] = num[m2][1] - num[m1 - 1][1];
temp[2] = num[m2][2] - num[m1 - 1][2];
sort(temp, temp + 3);
temp[1] -= temp[0];
temp[2] -= temp[0];
temp[0] = 0;
int flag = 1;
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += temp[i];
if (temp[i] > 1) {
flag = 0;
}
}
if (sum > 2) {
flag = 0;
}
if (flag == 1) {
printf("YES\n");
} else {
printf("NO\n");
}
}
}
}
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m1, m2;
int num[100100][3];
int temp[3];
string s;
int main() {
while (cin >> s) {
memset(num, 0, sizeof(num));
memset(temp, 0, sizeof(temp));
for (int i = 0; i < s.size(); i++) {
for (int j = 0; j < 3; j++) {
num[i + 1][j] = num[i][j];
}
num[i + 1][s[i] - 'x']++;
}
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &m1, &m2);
if ((m2 - m1 + 1) < 3) {
printf("YES\n");
} else {
temp[0] = num[m2][0] - num[m1 - 1][0];
temp[1] = num[m2][1] - num[m1 - 1][1];
temp[2] = num[m2][2] - num[m1 - 1][2];
sort(temp, temp + 3);
temp[1] -= temp[0];
temp[2] -= temp[0];
temp[0] = 0;
int flag = 1;
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += temp[i];
if (temp[i] > 1) {
flag = 0;
}
}
if (sum > 2) {
flag = 0;
}
if (flag == 1) {
printf("YES\n");
} else {
printf("NO\n");
}
}
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
void CyBerForCe() {
string s;
cin >> s;
long long m, l = s.length();
cin >> m;
long long a[l], b[l], c[l];
a[0] = 0;
b[0] = 0;
c[0] = 0;
for (long long i = 0; i < l; i++) {
a[i + 1] = a[i];
b[i + 1] = b[i];
c[i + 1] = c[i];
if (s[i] == 'x')
a[i + 1]++;
else if (s[i] == 'y')
b[i + 1]++;
else
c[i + 1]++;
}
for (long long i = 0; i < m; i++) {
long long l, r;
cin >> l >> r;
long long x = 0, y = 0, z = 0;
x = a[r] - a[l - 1];
y = b[r] - b[l - 1];
z = c[r] - c[l - 1];
if (r - l + 1 < 3)
cout << "YES\n";
else {
if (x == y && y == z || x == y && (z - 1 == x || z + 1 == x) ||
x == z && (y - 1 == x || y + 1 == x) ||
y == z && (x - 1 == y || x + 1 == y))
cout << "YES\n";
else
cout << "NO\n";
}
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
CyBerForCe();
return 0;
}
|
### Prompt
In cpp, your task is to solve the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void CyBerForCe() {
string s;
cin >> s;
long long m, l = s.length();
cin >> m;
long long a[l], b[l], c[l];
a[0] = 0;
b[0] = 0;
c[0] = 0;
for (long long i = 0; i < l; i++) {
a[i + 1] = a[i];
b[i + 1] = b[i];
c[i + 1] = c[i];
if (s[i] == 'x')
a[i + 1]++;
else if (s[i] == 'y')
b[i + 1]++;
else
c[i + 1]++;
}
for (long long i = 0; i < m; i++) {
long long l, r;
cin >> l >> r;
long long x = 0, y = 0, z = 0;
x = a[r] - a[l - 1];
y = b[r] - b[l - 1];
z = c[r] - c[l - 1];
if (r - l + 1 < 3)
cout << "YES\n";
else {
if (x == y && y == z || x == y && (z - 1 == x || z + 1 == x) ||
x == z && (y - 1 == x || y + 1 == x) ||
y == z && (x - 1 == y || x + 1 == y))
cout << "YES\n";
else
cout << "NO\n";
}
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
CyBerForCe();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int m;
cin >> m;
int n = s.size();
int sum[n + 1][3];
sum[0][0] = sum[0][1] = sum[0][2] = 0;
for (int i = 1; i <= n; ++i) {
if (s[i - 1] == 'x') {
sum[i][0] = sum[i - 1][0] + 1;
sum[i][1] = sum[i - 1][1];
sum[i][2] = sum[i - 1][2];
}
if (s[i - 1] == 'y') {
sum[i][1] = sum[i - 1][1] + 1;
sum[i][0] = sum[i - 1][0];
sum[i][2] = sum[i - 1][2];
}
if (s[i - 1] == 'z') {
sum[i][2] = sum[i - 1][2] + 1;
sum[i][1] = sum[i - 1][1];
sum[i][0] = sum[i - 1][0];
}
}
int l, r;
for (int i = 0; i < m; ++i) {
cin >> l >> r;
if (r - l < 2) {
cout << "YES" << endl;
continue;
}
int s1 = sum[l - 1][0] + sum[l - 1][1] + sum[l - 1][2];
int s2 = sum[r][0] + sum[r][1] + sum[r][2];
bool ans = false;
if ((s2 - s1) % 3 == 0) {
if (sum[r][0] - sum[l - 1][0] == sum[r][1] - sum[l - 1][1] &&
sum[r][1] - sum[l - 1][1] == sum[r][2] - sum[l - 1][2]) {
ans = true;
}
} else {
if (sum[r][0] - sum[l - 1][0] == sum[r][1] - sum[l - 1][1]) {
if (sum[r][2] - sum[l - 1][2] - (sum[r][0] - sum[l - 1][0]) == 1 ||
sum[r][2] - sum[l - 1][2] - (sum[r][0] - sum[l - 1][0]) == -1) {
ans = true;
}
} else if (sum[r][1] - sum[l - 1][1] == sum[r][2] - sum[l - 1][2]) {
if (sum[r][0] - sum[l - 1][0] - (sum[r][1] - sum[l - 1][1]) == 1 ||
sum[r][0] - sum[l - 1][0] - (sum[r][1] - sum[l - 1][1]) == -1) {
ans = true;
}
} else if (sum[r][0] - sum[l - 1][0] == sum[r][2] - sum[l - 1][2]) {
if (sum[r][1] - sum[l - 1][1] - (sum[r][0] - sum[l - 1][0]) == 1 ||
sum[r][1] - sum[l - 1][1] - (sum[r][0] - sum[l - 1][0]) == -1) {
ans = true;
}
}
}
if (ans) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
}
|
### Prompt
In cpp, your task is to solve the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int m;
cin >> m;
int n = s.size();
int sum[n + 1][3];
sum[0][0] = sum[0][1] = sum[0][2] = 0;
for (int i = 1; i <= n; ++i) {
if (s[i - 1] == 'x') {
sum[i][0] = sum[i - 1][0] + 1;
sum[i][1] = sum[i - 1][1];
sum[i][2] = sum[i - 1][2];
}
if (s[i - 1] == 'y') {
sum[i][1] = sum[i - 1][1] + 1;
sum[i][0] = sum[i - 1][0];
sum[i][2] = sum[i - 1][2];
}
if (s[i - 1] == 'z') {
sum[i][2] = sum[i - 1][2] + 1;
sum[i][1] = sum[i - 1][1];
sum[i][0] = sum[i - 1][0];
}
}
int l, r;
for (int i = 0; i < m; ++i) {
cin >> l >> r;
if (r - l < 2) {
cout << "YES" << endl;
continue;
}
int s1 = sum[l - 1][0] + sum[l - 1][1] + sum[l - 1][2];
int s2 = sum[r][0] + sum[r][1] + sum[r][2];
bool ans = false;
if ((s2 - s1) % 3 == 0) {
if (sum[r][0] - sum[l - 1][0] == sum[r][1] - sum[l - 1][1] &&
sum[r][1] - sum[l - 1][1] == sum[r][2] - sum[l - 1][2]) {
ans = true;
}
} else {
if (sum[r][0] - sum[l - 1][0] == sum[r][1] - sum[l - 1][1]) {
if (sum[r][2] - sum[l - 1][2] - (sum[r][0] - sum[l - 1][0]) == 1 ||
sum[r][2] - sum[l - 1][2] - (sum[r][0] - sum[l - 1][0]) == -1) {
ans = true;
}
} else if (sum[r][1] - sum[l - 1][1] == sum[r][2] - sum[l - 1][2]) {
if (sum[r][0] - sum[l - 1][0] - (sum[r][1] - sum[l - 1][1]) == 1 ||
sum[r][0] - sum[l - 1][0] - (sum[r][1] - sum[l - 1][1]) == -1) {
ans = true;
}
} else if (sum[r][0] - sum[l - 1][0] == sum[r][2] - sum[l - 1][2]) {
if (sum[r][1] - sum[l - 1][1] - (sum[r][0] - sum[l - 1][0]) == 1 ||
sum[r][1] - sum[l - 1][1] - (sum[r][0] - sum[l - 1][0]) == -1) {
ans = true;
}
}
}
if (ans) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const int MAXN = 1e6 + 5;
void cp() {
string s;
cin >> s;
int n = ((int)s.length());
int pref[n + 1][3];
memset(pref, 0, sizeof(pref));
for (int i = 1; i <= n; i++) {
for (char j = 'x'; j <= 'z'; j++) {
pref[i][j - 'x'] = pref[i - 1][j - 'x'] + (s[i - 1] == j);
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
int cnt_x, cnt_y, cnt_z;
cnt_x = pref[r][0] - pref[l - 1][0];
cnt_y = pref[r][1] - pref[l - 1][1];
cnt_z = pref[r][2] - pref[l - 1][2];
int len = r - l + 1;
if (len < 3) {
cout << "YES\n";
continue;
}
if (abs(cnt_x - cnt_y) > 1 || abs(cnt_z - cnt_y) > 1 ||
abs(cnt_x - cnt_z) > 1)
cout << "NO\n";
else
cout << "YES\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
t = 1;
while (t--) {
cp();
}
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const int MAXN = 1e6 + 5;
void cp() {
string s;
cin >> s;
int n = ((int)s.length());
int pref[n + 1][3];
memset(pref, 0, sizeof(pref));
for (int i = 1; i <= n; i++) {
for (char j = 'x'; j <= 'z'; j++) {
pref[i][j - 'x'] = pref[i - 1][j - 'x'] + (s[i - 1] == j);
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
int cnt_x, cnt_y, cnt_z;
cnt_x = pref[r][0] - pref[l - 1][0];
cnt_y = pref[r][1] - pref[l - 1][1];
cnt_z = pref[r][2] - pref[l - 1][2];
int len = r - l + 1;
if (len < 3) {
cout << "YES\n";
continue;
}
if (abs(cnt_x - cnt_y) > 1 || abs(cnt_z - cnt_y) > 1 ||
abs(cnt_x - cnt_z) > 1)
cout << "NO\n";
else
cout << "YES\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
t = 1;
while (t--) {
cp();
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
string str;
int X[200000];
int Y[200000];
int Z[200000];
int main() {
cin >> str;
for (int l = 0; str[l]; l++) {
int i = l + 1;
if (str[l] == 'z') {
Z[i] = Z[i - 1] + 1;
Y[i] = Y[i - 1];
X[i] = X[i - 1];
} else if (str[l] == 'y') {
Y[i] = Y[i - 1] + 1;
Z[i] = Z[i - 1];
X[i] = X[i - 1];
} else if (str[l] == 'x') {
X[i] = X[i - 1] + 1;
Y[i] = Y[i - 1];
Z[i] = Z[i - 1];
}
}
int m;
cin >> m;
for (int i = 1; i <= m; i++) {
int l, r;
cin >> l >> r;
int res1 = X[r] - X[l - 1];
int res2 = Y[r] - Y[l - 1];
int res3 = Z[r] - Z[l - 1];
if (res1 > res2) swap(res1, res2);
if (res2 > res3) swap(res2, res3);
if (res1 > res2) swap(res1, res2);
if (r - l < 2) {
cout << "YES\n";
continue;
} else if (res3 - res1 <= 1)
cout << "YES\n";
else {
cout << "NO\n";
}
}
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
string str;
int X[200000];
int Y[200000];
int Z[200000];
int main() {
cin >> str;
for (int l = 0; str[l]; l++) {
int i = l + 1;
if (str[l] == 'z') {
Z[i] = Z[i - 1] + 1;
Y[i] = Y[i - 1];
X[i] = X[i - 1];
} else if (str[l] == 'y') {
Y[i] = Y[i - 1] + 1;
Z[i] = Z[i - 1];
X[i] = X[i - 1];
} else if (str[l] == 'x') {
X[i] = X[i - 1] + 1;
Y[i] = Y[i - 1];
Z[i] = Z[i - 1];
}
}
int m;
cin >> m;
for (int i = 1; i <= m; i++) {
int l, r;
cin >> l >> r;
int res1 = X[r] - X[l - 1];
int res2 = Y[r] - Y[l - 1];
int res3 = Z[r] - Z[l - 1];
if (res1 > res2) swap(res1, res2);
if (res2 > res3) swap(res2, res3);
if (res1 > res2) swap(res1, res2);
if (r - l < 2) {
cout << "YES\n";
continue;
} else if (res3 - res1 <= 1)
cout << "YES\n";
else {
cout << "NO\n";
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
char s[100005];
int n, m;
int g[100005];
using namespace std;
const int maxn = 100001;
int x[maxn], y[maxn], z[maxn];
int main() {
int i, j, k, a, b;
while (cin >> s) {
n = strlen(s);
scanf("%d", &m);
memset(g, 0, sizeof(g));
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
memset(z, 0, sizeof(z));
for (i = 0; i < n; i++) {
x[i + 1] = x[i] + (s[i] == 'x' ? 1 : 0);
y[i + 1] = y[i] + (s[i] == 'y' ? 1 : 0);
z[i + 1] = z[i] + (s[i] == 'z' ? 1 : 0);
}
while (m--) {
k++;
scanf("%d%d", &a, &b);
if (b - a + 1 < 3) {
cout << "YES" << endl;
continue;
}
{
int num1 = x[b] - x[a - 1];
int num2 = y[b] - y[a - 1];
int num3 = z[b] - z[a - 1];
if ((num1 >= (b - a + 1) / 3 && num2 >= (b - a + 1) / 3 &&
num3 >= (b - a + 1) / 3)) {
if ((num1 - (b - a + 1) / 3) <= 1 && (num2 - (b - a + 1) / 3) <= 1 &&
(num3 - (b - a + 1) / 3) <= 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
} else
cout << "NO" << endl;
}
}
}
}
|
### Prompt
In cpp, your task is to solve the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char s[100005];
int n, m;
int g[100005];
using namespace std;
const int maxn = 100001;
int x[maxn], y[maxn], z[maxn];
int main() {
int i, j, k, a, b;
while (cin >> s) {
n = strlen(s);
scanf("%d", &m);
memset(g, 0, sizeof(g));
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
memset(z, 0, sizeof(z));
for (i = 0; i < n; i++) {
x[i + 1] = x[i] + (s[i] == 'x' ? 1 : 0);
y[i + 1] = y[i] + (s[i] == 'y' ? 1 : 0);
z[i + 1] = z[i] + (s[i] == 'z' ? 1 : 0);
}
while (m--) {
k++;
scanf("%d%d", &a, &b);
if (b - a + 1 < 3) {
cout << "YES" << endl;
continue;
}
{
int num1 = x[b] - x[a - 1];
int num2 = y[b] - y[a - 1];
int num3 = z[b] - z[a - 1];
if ((num1 >= (b - a + 1) / 3 && num2 >= (b - a + 1) / 3 &&
num3 >= (b - a + 1) / 3)) {
if ((num1 - (b - a + 1) / 3) <= 1 && (num2 - (b - a + 1) / 3) <= 1 &&
(num3 - (b - a + 1) / 3) <= 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
} else
cout << "NO" << endl;
}
}
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct st {
int x, y, z;
};
st operator-(const st& a, const st& b) {
st res;
res.x = b.x - a.x;
res.y = b.y - a.y;
res.z = b.z - a.z;
return res;
}
int main() {
int n, m;
string s;
cin >> s;
n = s.length();
scanf("%d", &m);
vector<st> d;
st cur;
cur.x = 0;
cur.y = 0;
cur.z = 0;
d.push_back(cur);
for (int i = 0; i < int(n); ++i) {
if (s[i] == 'x') {
cur.x++;
}
if (s[i] == 'y') {
cur.y++;
}
if (s[i] == 'z') {
cur.z++;
}
d.push_back(cur);
}
for (int i = 0; i < int(m); ++i) {
int l, r;
scanf("%d%d", &l, &r);
cur = d[r] - d[l - 1];
if (r - l < 2 || max(abs(cur.x - cur.y),
max(abs(cur.x - cur.z), abs(cur.y - cur.z))) <= 1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
|
### Prompt
Please formulate a cpp solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct st {
int x, y, z;
};
st operator-(const st& a, const st& b) {
st res;
res.x = b.x - a.x;
res.y = b.y - a.y;
res.z = b.z - a.z;
return res;
}
int main() {
int n, m;
string s;
cin >> s;
n = s.length();
scanf("%d", &m);
vector<st> d;
st cur;
cur.x = 0;
cur.y = 0;
cur.z = 0;
d.push_back(cur);
for (int i = 0; i < int(n); ++i) {
if (s[i] == 'x') {
cur.x++;
}
if (s[i] == 'y') {
cur.y++;
}
if (s[i] == 'z') {
cur.z++;
}
d.push_back(cur);
}
for (int i = 0; i < int(m); ++i) {
int l, r;
scanf("%d%d", &l, &r);
cur = d[r] - d[l - 1];
if (r - l < 2 || max(abs(cur.x - cur.y),
max(abs(cur.x - cur.z), abs(cur.y - cur.z))) <= 1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
char s[100004];
int i, n, l, r, xnum, ynum, znum;
scanf("%s", s);
n = strlen(s);
int x[100004] = {0};
int y[100004] = {0};
int z[100004] = {0};
if (s[0] == 'x')
x[0]++;
else if (s[0] == 'y')
y[0]++;
else
z[0]++;
for (i = 1; i < n; i++) {
if (s[i] == 'x') {
x[i] = x[i - 1] + 1;
y[i] = y[i - 1];
z[i] = z[i - 1];
} else if (s[i] == 'y') {
y[i] = y[i - 1] + 1;
x[i] = x[i - 1];
z[i] = z[i - 1];
} else {
z[i] = z[i - 1] + 1;
x[i] = x[i - 1];
y[i] = y[i - 1];
}
}
scanf("%d", &n);
for (int i = (int)0; i < (int)n; i++) {
scanf("%d", &l);
scanf("%d", &r);
if (r - l < 2) {
printf("%s\n", "YES");
continue;
}
l--;
r--;
xnum = x[r];
if (l > 0) xnum -= x[l - 1];
ynum = y[r];
if (l > 0) ynum -= y[l - 1];
znum = z[r];
if (l > 0) znum -= z[l - 1];
if ((abs(xnum - ynum) > 1) || (abs(ynum - znum) > 1) ||
(abs(znum - xnum) > 1))
printf("%s\n", "NO");
else
printf("%s\n", "YES");
}
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
char s[100004];
int i, n, l, r, xnum, ynum, znum;
scanf("%s", s);
n = strlen(s);
int x[100004] = {0};
int y[100004] = {0};
int z[100004] = {0};
if (s[0] == 'x')
x[0]++;
else if (s[0] == 'y')
y[0]++;
else
z[0]++;
for (i = 1; i < n; i++) {
if (s[i] == 'x') {
x[i] = x[i - 1] + 1;
y[i] = y[i - 1];
z[i] = z[i - 1];
} else if (s[i] == 'y') {
y[i] = y[i - 1] + 1;
x[i] = x[i - 1];
z[i] = z[i - 1];
} else {
z[i] = z[i - 1] + 1;
x[i] = x[i - 1];
y[i] = y[i - 1];
}
}
scanf("%d", &n);
for (int i = (int)0; i < (int)n; i++) {
scanf("%d", &l);
scanf("%d", &r);
if (r - l < 2) {
printf("%s\n", "YES");
continue;
}
l--;
r--;
xnum = x[r];
if (l > 0) xnum -= x[l - 1];
ynum = y[r];
if (l > 0) ynum -= y[l - 1];
znum = z[r];
if (l > 0) znum -= z[l - 1];
if ((abs(xnum - ynum) > 1) || (abs(ynum - znum) > 1) ||
(abs(znum - xnum) > 1))
printf("%s\n", "NO");
else
printf("%s\n", "YES");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int s1[100005];
int s2[100005];
int s3[100005];
string s11 = "xzy";
string s22 = "yxz";
string s33 = "zyx";
bool ok(int l, int r) {
int a, b, c, d;
a = s1[r];
b = s2[r];
c = s3[r];
if (l != 0) {
a -= s1[l - 1];
b -= s2[l - 1];
c -= s3[l - 1];
}
if (r - l + 1 < 3) return true;
if (a == 0 || b == 0 || c == 0) return false;
d = (r - l + 1) / 3;
if (d != a && d != a - 1) return false;
if (d != b && d != b - 1) return false;
if (d != c && d != c - 1) return false;
return true;
}
bool ok1(string s1, string s2) {
int cnt = 0;
for (int i = 0; i < s1.size(); i++) {
if (s1[i] == s2[cnt]) {
cnt++;
if (cnt == 3) return 1;
}
}
return 0;
}
int main() {
string s;
cin >> s;
int l = s.size();
for (int i = 0; i < l; i++) {
if (i != 0) {
s1[i] = s1[i - 1];
s2[i] = s2[i - 1];
s3[i] = s3[i - 1];
}
if (s[i] == 'x') s1[i]++;
if (s[i] == 'y') s2[i]++;
if (s[i] == 'z') s3[i]++;
}
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
int a = s1[y] - s1[x - 1];
int b = s2[y] - s2[x - 1];
int c = s3[y] - s3[x - 1];
if (ok(x, y)) {
printf("YES\n");
} else {
printf("NO\n");
}
}
}
|
### Prompt
Please create a solution in cpp to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int s1[100005];
int s2[100005];
int s3[100005];
string s11 = "xzy";
string s22 = "yxz";
string s33 = "zyx";
bool ok(int l, int r) {
int a, b, c, d;
a = s1[r];
b = s2[r];
c = s3[r];
if (l != 0) {
a -= s1[l - 1];
b -= s2[l - 1];
c -= s3[l - 1];
}
if (r - l + 1 < 3) return true;
if (a == 0 || b == 0 || c == 0) return false;
d = (r - l + 1) / 3;
if (d != a && d != a - 1) return false;
if (d != b && d != b - 1) return false;
if (d != c && d != c - 1) return false;
return true;
}
bool ok1(string s1, string s2) {
int cnt = 0;
for (int i = 0; i < s1.size(); i++) {
if (s1[i] == s2[cnt]) {
cnt++;
if (cnt == 3) return 1;
}
}
return 0;
}
int main() {
string s;
cin >> s;
int l = s.size();
for (int i = 0; i < l; i++) {
if (i != 0) {
s1[i] = s1[i - 1];
s2[i] = s2[i - 1];
s3[i] = s3[i - 1];
}
if (s[i] == 'x') s1[i]++;
if (s[i] == 'y') s2[i]++;
if (s[i] == 'z') s3[i]++;
}
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
int a = s1[y] - s1[x - 1];
int b = s2[y] - s2[x - 1];
int c = s3[y] - s3[x - 1];
if (ok(x, y)) {
printf("YES\n");
} else {
printf("NO\n");
}
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) {
if (a > b)
return a;
else
return b;
}
long long min(long long a, long long b) {
if (a < b)
return a;
else
return b;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
string s;
int cumx[(int)1e5 + 5], cumy[(int)1e5 + 5], cumz[(int)1e5 + 5], m, l, r;
bool can(int first, int second, int z) {
if (abs(first - second) > 1 || (abs(first - z) > 1 || (abs(z - second) > 1)))
return 0;
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
cin >> s >> m;
for (int i = 0; i < s.size(); i++)
cumx[i + 1] = cumx[i] + (s[i] == 'x'),
cumy[i + 1] = cumy[i] + (s[i] == 'y'),
cumz[i + 1] = cumz[i] + (s[i] == 'z');
for (int i = 0; i < m; i++) {
cin >> l >> r;
if (r - l < 2)
cout << "YES\n";
else
cout << (can(cumx[r] - cumx[l - 1], cumy[r] - cumy[l - 1],
cumz[r] - cumz[l - 1])
? "YES\n"
: "NO\n");
}
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) {
if (a > b)
return a;
else
return b;
}
long long min(long long a, long long b) {
if (a < b)
return a;
else
return b;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
string s;
int cumx[(int)1e5 + 5], cumy[(int)1e5 + 5], cumz[(int)1e5 + 5], m, l, r;
bool can(int first, int second, int z) {
if (abs(first - second) > 1 || (abs(first - z) > 1 || (abs(z - second) > 1)))
return 0;
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
cin >> s >> m;
for (int i = 0; i < s.size(); i++)
cumx[i + 1] = cumx[i] + (s[i] == 'x'),
cumy[i + 1] = cumy[i] + (s[i] == 'y'),
cumz[i + 1] = cumz[i] + (s[i] == 'z');
for (int i = 0; i < m; i++) {
cin >> l >> r;
if (r - l < 2)
cout << "YES\n";
else
cout << (can(cumx[r] - cumx[l - 1], cumy[r] - cumy[l - 1],
cumz[r] - cumz[l - 1])
? "YES\n"
: "NO\n");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const* argv[]) {
string s;
int m, l, r;
cin >> s >> m;
int tx[s.size()], ty[s.size()], tz[s.size()];
memset(tx, 0, sizeof tx);
memset(ty, 0, sizeof ty);
memset(tz, 0, sizeof tz);
for (int i = 0; i < s.size(); ++i) {
if (i > 0) {
tx[i] = tx[i - 1];
ty[i] = ty[i - 1];
tz[i] = tz[i - 1];
}
if (s[i] == 'x') tx[i]++;
if (s[i] == 'y') ty[i]++;
if (s[i] == 'z') tz[i]++;
}
for (int i = 0; i < m; i++) {
cin >> l >> r;
r -= 1;
l -= 1;
if (s[l] == 'x') tx[l]--;
if (s[l] == 'y') ty[l]--;
if (s[l] == 'z') tz[l]--;
int x = tx[r] - tx[l];
int y = ty[r] - ty[l];
int z = tz[r] - tz[l];
if (r - l < 2 || max(x, max(y, z)) - min(x, min(y, z)) <= 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
if (s[l] == 'x') tx[l]++;
if (s[l] == 'y') ty[l]++;
if (s[l] == 'z') tz[l]++;
}
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const* argv[]) {
string s;
int m, l, r;
cin >> s >> m;
int tx[s.size()], ty[s.size()], tz[s.size()];
memset(tx, 0, sizeof tx);
memset(ty, 0, sizeof ty);
memset(tz, 0, sizeof tz);
for (int i = 0; i < s.size(); ++i) {
if (i > 0) {
tx[i] = tx[i - 1];
ty[i] = ty[i - 1];
tz[i] = tz[i - 1];
}
if (s[i] == 'x') tx[i]++;
if (s[i] == 'y') ty[i]++;
if (s[i] == 'z') tz[i]++;
}
for (int i = 0; i < m; i++) {
cin >> l >> r;
r -= 1;
l -= 1;
if (s[l] == 'x') tx[l]--;
if (s[l] == 'y') ty[l]--;
if (s[l] == 'z') tz[l]--;
int x = tx[r] - tx[l];
int y = ty[r] - ty[l];
int z = tz[r] - tz[l];
if (r - l < 2 || max(x, max(y, z)) - min(x, min(y, z)) <= 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
if (s[l] == 'x') tx[l]++;
if (s[l] == 'y') ty[l]++;
if (s[l] == 'z') tz[l]++;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int a[100010][3];
char s[100010];
int main() {
scanf("%s", s);
int len = strlen(s);
memset(a, 0, sizeof(a));
for (int i = 1; i <= len; i++) {
for (int j = 0; j < 3; j++) a[i][j] = a[i - 1][j];
a[i][s[i - 1] - 'x']++;
}
int T;
scanf("%d", &T);
while (T--) {
int l, r;
scanf("%d %d", &l, &r);
if (r - l + 1 <= 2) {
puts("YES");
continue;
}
int temp[3];
for (int i = 0; i <= 2; i++) temp[i] = a[r][i] - a[l - 1][i];
int maxx = 0, minn = 0x7fffffff;
for (int i = 0; i <= 2; i++) {
maxx = max(maxx, temp[i]);
minn = min(minn, temp[i]);
}
if (maxx - minn >= 2)
puts("NO");
else
puts("YES");
}
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a[100010][3];
char s[100010];
int main() {
scanf("%s", s);
int len = strlen(s);
memset(a, 0, sizeof(a));
for (int i = 1; i <= len; i++) {
for (int j = 0; j < 3; j++) a[i][j] = a[i - 1][j];
a[i][s[i - 1] - 'x']++;
}
int T;
scanf("%d", &T);
while (T--) {
int l, r;
scanf("%d %d", &l, &r);
if (r - l + 1 <= 2) {
puts("YES");
continue;
}
int temp[3];
for (int i = 0; i <= 2; i++) temp[i] = a[r][i] - a[l - 1][i];
int maxx = 0, minn = 0x7fffffff;
for (int i = 0; i <= 2; i++) {
maxx = max(maxx, temp[i]);
minn = min(minn, temp[i]);
}
if (maxx - minn >= 2)
puts("NO");
else
puts("YES");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long int inf = 1e18;
const long long int mod = 1e9 + 7;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
long long int n, i, l, x, y, z, a, b;
string str;
cin >> str;
x = y = z = 0;
l = str.length();
vector<tuple<long long int, long long int, long long int>> v;
v.push_back(make_tuple(0, 0, 0));
for (long long int i = 0; i < l; i++) {
if (str[i] == 'x')
x++;
else if (str[i] == 'y')
y++;
else
z++;
v.push_back(make_tuple(x, y, z));
}
cin >> n;
for (long long int i = 0; i < n; i++) {
cin >> a >> b;
x = get<0>(v[b]) - get<0>(v[a - 1]);
y = get<1>(v[b]) - get<1>(v[a - 1]);
z = get<2>(v[b]) - get<2>(v[a - 1]);
if ((abs(x - y) > 1 || abs(x - z) > 1 || abs(y - z) > 1) &&
(b - a + 1) >= 3)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int inf = 1e18;
const long long int mod = 1e9 + 7;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
long long int n, i, l, x, y, z, a, b;
string str;
cin >> str;
x = y = z = 0;
l = str.length();
vector<tuple<long long int, long long int, long long int>> v;
v.push_back(make_tuple(0, 0, 0));
for (long long int i = 0; i < l; i++) {
if (str[i] == 'x')
x++;
else if (str[i] == 'y')
y++;
else
z++;
v.push_back(make_tuple(x, y, z));
}
cin >> n;
for (long long int i = 0; i < n; i++) {
cin >> a >> b;
x = get<0>(v[b]) - get<0>(v[a - 1]);
y = get<1>(v[b]) - get<1>(v[a - 1]);
z = get<2>(v[b]) - get<2>(v[a - 1]);
if ((abs(x - y) > 1 || abs(x - z) > 1 || abs(y - z) > 1) &&
(b - a + 1) >= 3)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
char s[100005];
int x[100005], y[100005], z[100005];
int main() {
while (scanf("%s", s) > 0) {
int n;
scanf("%d", &n);
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
memset(z, 0, sizeof(z));
int len = strlen(s);
for (int i = 0; i < len; i++) {
if (i == 0) {
if (s[i] == 'x') x[i]++;
if (s[i] == 'y') y[i]++;
if (s[i] == 'z') z[i]++;
} else {
x[i] = x[i - 1];
y[i] = y[i - 1];
z[i] = z[i - 1];
if (s[i] == 'x') x[i] = x[i - 1] + 1;
if (s[i] == 'y') y[i] = y[i - 1] + 1;
if (s[i] == 'z') z[i] = z[i - 1] + 1;
}
}
while (n--) {
int ll, rr;
scanf("%d%d", &ll, &rr);
if (rr - ll + 1 < 3)
printf("YES\n");
else {
ll--;
rr--;
int x1 = x[rr] - x[ll - 1];
int y1 = y[rr] - y[ll - 1];
int z1 = z[rr] - z[ll - 1];
if (x1 == y1 && y1 == z1) {
printf("YES\n");
} else if (x1 - 1 == y1 && y1 == z1)
printf("YES\n");
else if (x1 == y1 - 1 && x1 == z1)
printf("YES\n");
else if (x1 == y1 && y1 == z1 - 1)
printf("YES\n");
else if (x1 == y1 && z1 == y1 - 1)
printf("YES\n");
else if (x1 == z1 && y1 == z1 - 1)
printf("YES\n");
else if (y1 == z1 && x1 == y1 - 1)
printf("YES\n");
else
printf("NO\n");
}
}
}
return 0;
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char s[100005];
int x[100005], y[100005], z[100005];
int main() {
while (scanf("%s", s) > 0) {
int n;
scanf("%d", &n);
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
memset(z, 0, sizeof(z));
int len = strlen(s);
for (int i = 0; i < len; i++) {
if (i == 0) {
if (s[i] == 'x') x[i]++;
if (s[i] == 'y') y[i]++;
if (s[i] == 'z') z[i]++;
} else {
x[i] = x[i - 1];
y[i] = y[i - 1];
z[i] = z[i - 1];
if (s[i] == 'x') x[i] = x[i - 1] + 1;
if (s[i] == 'y') y[i] = y[i - 1] + 1;
if (s[i] == 'z') z[i] = z[i - 1] + 1;
}
}
while (n--) {
int ll, rr;
scanf("%d%d", &ll, &rr);
if (rr - ll + 1 < 3)
printf("YES\n");
else {
ll--;
rr--;
int x1 = x[rr] - x[ll - 1];
int y1 = y[rr] - y[ll - 1];
int z1 = z[rr] - z[ll - 1];
if (x1 == y1 && y1 == z1) {
printf("YES\n");
} else if (x1 - 1 == y1 && y1 == z1)
printf("YES\n");
else if (x1 == y1 - 1 && x1 == z1)
printf("YES\n");
else if (x1 == y1 && y1 == z1 - 1)
printf("YES\n");
else if (x1 == y1 && z1 == y1 - 1)
printf("YES\n");
else if (x1 == z1 && y1 == z1 - 1)
printf("YES\n");
else if (y1 == z1 && x1 == y1 - 1)
printf("YES\n");
else
printf("NO\n");
}
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
char s[maxn];
int m;
int dpx[maxn], dpy[maxn], dpz[maxn];
int main() {
scanf("%s", s + 1);
int lens = strlen(s + 1);
cin >> m;
for (int i = 1; i <= lens; i++) {
if (s[i] == 'x')
dpx[i] = dpx[i - 1] + 1;
else
dpx[i] = dpx[i - 1];
if (s[i] == 'y')
dpy[i] = dpy[i - 1] + 1;
else
dpy[i] = dpy[i - 1];
if (s[i] == 'z')
dpz[i] = dpz[i - 1] + 1;
else
dpz[i] = dpz[i - 1];
}
while (m--) {
int l = 0, r = 0;
scanf("%d%d", &l, &r);
if (r - l + 1 < 3)
cout << "YES" << endl;
else {
int cntx = dpx[r] - dpx[l - 1];
int cnty = dpy[r] - dpy[l - 1];
int cntz = dpz[r] - dpz[l - 1];
int maxi = max(cntx, cnty);
maxi = max(maxi, cntz);
int mini = min(cntx, cnty);
mini = min(mini, cntz);
if (maxi == mini || maxi == mini + 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
return 0;
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
char s[maxn];
int m;
int dpx[maxn], dpy[maxn], dpz[maxn];
int main() {
scanf("%s", s + 1);
int lens = strlen(s + 1);
cin >> m;
for (int i = 1; i <= lens; i++) {
if (s[i] == 'x')
dpx[i] = dpx[i - 1] + 1;
else
dpx[i] = dpx[i - 1];
if (s[i] == 'y')
dpy[i] = dpy[i - 1] + 1;
else
dpy[i] = dpy[i - 1];
if (s[i] == 'z')
dpz[i] = dpz[i - 1] + 1;
else
dpz[i] = dpz[i - 1];
}
while (m--) {
int l = 0, r = 0;
scanf("%d%d", &l, &r);
if (r - l + 1 < 3)
cout << "YES" << endl;
else {
int cntx = dpx[r] - dpx[l - 1];
int cnty = dpy[r] - dpy[l - 1];
int cntz = dpz[r] - dpz[l - 1];
int maxi = max(cntx, cnty);
maxi = max(maxi, cntz);
int mini = min(cntx, cnty);
mini = min(mini, cntz);
if (maxi == mini || maxi == mini + 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int N, M;
char s[MAXN];
int sum[MAXN][5];
int main() {
scanf("%s%d", s + 1, &M);
N = strlen(s + 1);
for (int i = 1; i <= N; i++)
for (int j = 0; j < 3; j++) sum[i][j] = sum[i - 1][j] + (s[i] == 'x' + j);
while (M--) {
int l, r;
scanf("%d%d", &l, &r);
int a[5];
for (int i = 0; i < 3; i++) a[i] = sum[r][i] - sum[l - 1][i];
sort(a, a + 3);
puts(r - l > 1 && a[2] - a[0] > 1 ? "NO" : "YES");
}
return 0;
}
|
### Prompt
In CPP, your task is to solve the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int N, M;
char s[MAXN];
int sum[MAXN][5];
int main() {
scanf("%s%d", s + 1, &M);
N = strlen(s + 1);
for (int i = 1; i <= N; i++)
for (int j = 0; j < 3; j++) sum[i][j] = sum[i - 1][j] + (s[i] == 'x' + j);
while (M--) {
int l, r;
scanf("%d%d", &l, &r);
int a[5];
for (int i = 0; i < 3; i++) a[i] = sum[r][i] - sum[l - 1][i];
sort(a, a + 3);
puts(r - l > 1 && a[2] - a[0] > 1 ? "NO" : "YES");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int x[100000 + 100];
int y[100000 + 100];
int z[100000 + 100];
int main() {
string s;
int n, m, l, r;
cin >> s;
n = s.size();
cin >> m;
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
memset(z, 0, sizeof(z));
for (int i = 1; i <= n; i++) {
x[i] = x[i - 1];
y[i] = y[i - 1];
z[i] = z[i - 1];
if (s[i - 1] == 'x') x[i]++;
if (s[i - 1] == 'y') y[i]++;
if (s[i - 1] == 'z') z[i]++;
}
for (int i = 0; i < m; i++) {
cin >> l >> r;
if (min(x[r] - x[l - 1], min(y[r] - y[l - 1], z[r] - z[l - 1])) + 1 >=
max(x[r] - x[l - 1], max(y[r] - y[l - 1], z[r] - z[l - 1])))
cout << "YES\n";
else if (r - l + 1 < 3)
cout << "YES\n";
else
cout << "NO\n";
}
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int x[100000 + 100];
int y[100000 + 100];
int z[100000 + 100];
int main() {
string s;
int n, m, l, r;
cin >> s;
n = s.size();
cin >> m;
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
memset(z, 0, sizeof(z));
for (int i = 1; i <= n; i++) {
x[i] = x[i - 1];
y[i] = y[i - 1];
z[i] = z[i - 1];
if (s[i - 1] == 'x') x[i]++;
if (s[i - 1] == 'y') y[i]++;
if (s[i - 1] == 'z') z[i]++;
}
for (int i = 0; i < m; i++) {
cin >> l >> r;
if (min(x[r] - x[l - 1], min(y[r] - y[l - 1], z[r] - z[l - 1])) + 1 >=
max(x[r] - x[l - 1], max(y[r] - y[l - 1], z[r] - z[l - 1])))
cout << "YES\n";
else if (r - l + 1 < 3)
cout << "YES\n";
else
cout << "NO\n";
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int cnt[5];
char a[maxn];
int x[maxn], y[maxn], z[maxn];
int Abs(int x) { return x < 0 ? (-x) : x; }
bool can(int x, int y, int z) {
if (x == y && (Abs(z - x) <= 1)) return true;
return false;
}
int main() {
int n, l, r;
scanf("%s%d", a, &n);
int len = strlen(a);
x[0] = y[0] = z[0] = 0;
for (int i = 0; i < len; i++) {
if (a[i] == 'x') {
x[i + 1] = x[i] + 1;
y[i + 1] = y[i];
z[i + 1] = z[i];
} else if (a[i] == 'y') {
y[i + 1] = y[i] + 1;
x[i + 1] = x[i];
z[i + 1] = z[i];
} else {
z[i + 1] = z[i] + 1;
x[i + 1] = x[i];
y[i + 1] = y[i];
}
}
while (n--) {
scanf("%d%d", &l, &r);
if (r - l + 1 < 3) {
printf("YES\n");
continue;
}
int numx = x[r] - x[l - 1], numy = y[r] - y[l - 1], numz = z[r] - z[l - 1];
if (can(numx, numy, numz) || can(numx, numz, numy) || can(numy, numz, numx))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int cnt[5];
char a[maxn];
int x[maxn], y[maxn], z[maxn];
int Abs(int x) { return x < 0 ? (-x) : x; }
bool can(int x, int y, int z) {
if (x == y && (Abs(z - x) <= 1)) return true;
return false;
}
int main() {
int n, l, r;
scanf("%s%d", a, &n);
int len = strlen(a);
x[0] = y[0] = z[0] = 0;
for (int i = 0; i < len; i++) {
if (a[i] == 'x') {
x[i + 1] = x[i] + 1;
y[i + 1] = y[i];
z[i + 1] = z[i];
} else if (a[i] == 'y') {
y[i + 1] = y[i] + 1;
x[i + 1] = x[i];
z[i + 1] = z[i];
} else {
z[i + 1] = z[i] + 1;
x[i + 1] = x[i];
y[i + 1] = y[i];
}
}
while (n--) {
scanf("%d%d", &l, &r);
if (r - l + 1 < 3) {
printf("YES\n");
continue;
}
int numx = x[r] - x[l - 1], numy = y[r] - y[l - 1], numz = z[r] - z[l - 1];
if (can(numx, numy, numz) || can(numx, numz, numy) || can(numy, numz, numx))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int size = 1e5 + 10;
int x[size], y[size], z[size], l[size], r[size];
char a[size];
int main() {
scanf("%s", (a));
int(m);
scanf("%d", &m);
for (int i = 0; i < (m); ++i) scanf("%d%d", &(l[i]), &(r[i]));
int len = int(strlen(a));
for (int i = 0; i < (len); ++i) {
x[i + 1] = x[i] + (a[i] == 'x');
y[i + 1] = y[i] + (a[i] == 'y');
z[i + 1] = z[i] + (a[i] == 'z');
}
for (int i = 0; i < (m); ++i) {
int dx = x[r[i]] - x[l[i] - 1];
int dy = y[r[i]] - y[l[i] - 1];
int dz = z[r[i]] - z[l[i] - 1];
int mx = max(max(dx, dy), dz);
int mn = min(min(dx, dy), dz);
if (r[i] - l[i] < 2) {
printf("YES");
} else if (mx - mn > 1) {
printf("NO");
} else {
printf("YES");
}
printf("\n");
}
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int size = 1e5 + 10;
int x[size], y[size], z[size], l[size], r[size];
char a[size];
int main() {
scanf("%s", (a));
int(m);
scanf("%d", &m);
for (int i = 0; i < (m); ++i) scanf("%d%d", &(l[i]), &(r[i]));
int len = int(strlen(a));
for (int i = 0; i < (len); ++i) {
x[i + 1] = x[i] + (a[i] == 'x');
y[i + 1] = y[i] + (a[i] == 'y');
z[i + 1] = z[i] + (a[i] == 'z');
}
for (int i = 0; i < (m); ++i) {
int dx = x[r[i]] - x[l[i] - 1];
int dy = y[r[i]] - y[l[i] - 1];
int dz = z[r[i]] - z[l[i] - 1];
int mx = max(max(dx, dy), dz);
int mn = min(min(dx, dy), dz);
if (r[i] - l[i] < 2) {
printf("YES");
} else if (mx - mn > 1) {
printf("NO");
} else {
printf("YES");
}
printf("\n");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
string s;
inline int t(int i, char c) { return s[i] == c ? 1 : 0; }
int main() {
cin >> s;
int n = s.size();
vector<int> nx(n + 1), ny(n + 1), nz(n + 1);
for (int i = 1; i < n + 1; ++i) {
nx[i] = nx[i - 1] + t(i - 1, 'x');
ny[i] = ny[i - 1] + t(i - 1, 'y');
nz[i] = nz[i - 1] + t(i - 1, 'z');
}
int m;
cin >> m;
stringstream ss;
while (m--) {
int l, r;
cin >> l >> r;
int x = nx[r] - nx[l - 1];
int y = ny[r] - ny[l - 1];
int z = nz[r] - nz[l - 1];
if ((r - l + 1 < 3) || (x >= 1 && y >= 1 && z >= 1 && abs(x - y) <= 1 &&
abs(x - z) <= 1 && abs(y - z) <= 1))
ss << "YES\n";
else
ss << "NO\n";
}
cout << ss.str();
return 0;
}
|
### Prompt
Generate a Cpp solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
string s;
inline int t(int i, char c) { return s[i] == c ? 1 : 0; }
int main() {
cin >> s;
int n = s.size();
vector<int> nx(n + 1), ny(n + 1), nz(n + 1);
for (int i = 1; i < n + 1; ++i) {
nx[i] = nx[i - 1] + t(i - 1, 'x');
ny[i] = ny[i - 1] + t(i - 1, 'y');
nz[i] = nz[i - 1] + t(i - 1, 'z');
}
int m;
cin >> m;
stringstream ss;
while (m--) {
int l, r;
cin >> l >> r;
int x = nx[r] - nx[l - 1];
int y = ny[r] - ny[l - 1];
int z = nz[r] - nz[l - 1];
if ((r - l + 1 < 3) || (x >= 1 && y >= 1 && z >= 1 && abs(x - y) <= 1 &&
abs(x - z) <= 1 && abs(y - z) <= 1))
ss << "YES\n";
else
ss << "NO\n";
}
cout << ss.str();
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma GCC optimize "03"
using namespace std;
const long long int inf = INT_MAX;
const long long int MOD = 1e9 + 7;
const long long int N = 1e5 + 5;
long long int c[N][3];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
for (long long int i = 0; i < s.length(); i++) {
for (long long int j = 0; j < 3; j++) {
if (s[i] - 'x' == j)
c[i + 1][j] = c[i][j] + 1;
else
c[i + 1][j] = c[i][j];
}
}
long long int t;
cin >> t;
while (t--) {
long long int a, b;
cin >> a >> b;
a--;
long long int cx = c[b][0] - c[a][0];
long long int cy = c[b][1] - c[a][1];
long long int cz = c[b][2] - c[a][2];
if (max({cx, cy, cz}) - min({cx, cy, cz}) > 1 && b - a >= 3)
cout << "NO\n";
else
cout << "YES\n";
}
return 0;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize "03"
using namespace std;
const long long int inf = INT_MAX;
const long long int MOD = 1e9 + 7;
const long long int N = 1e5 + 5;
long long int c[N][3];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
for (long long int i = 0; i < s.length(); i++) {
for (long long int j = 0; j < 3; j++) {
if (s[i] - 'x' == j)
c[i + 1][j] = c[i][j] + 1;
else
c[i + 1][j] = c[i][j];
}
}
long long int t;
cin >> t;
while (t--) {
long long int a, b;
cin >> a >> b;
a--;
long long int cx = c[b][0] - c[a][0];
long long int cy = c[b][1] - c[a][1];
long long int cz = c[b][2] - c[a][2];
if (max({cx, cy, cz}) - min({cx, cy, cz}) > 1 && b - a >= 3)
cout << "NO\n";
else
cout << "YES\n";
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main(void) {
string s;
cin >> s;
vector<int> x(s.size()), y(s.size()), z(s.size());
for (int i = 0; i < (int)s.size(); ++i) {
if (i != 0)
x[i] = x[i - 1], y[i] = y[i - 1], z[i] = z[i - 1];
else
x[i] = 0, y[i] = 0, z[i] = 0;
if (s[i] == 'x') x[i]++;
if (s[i] == 'y') y[i]++;
if (s[i] == 'z') z[i]++;
}
int m;
cin >> m;
int l, r;
int cx, cy, cz;
for (int i = 0; i < m; ++i) {
cin >> l >> r;
if (r - l + 1 < 3) {
cout << "YES\n";
continue;
}
l--, r--;
cx = x[r];
cy = y[r];
cz = z[r];
if (l > 0) cx -= x[l - 1], cy -= y[l - 1], cz -= z[l - 1];
if ((abs(cx - cy) > 1) || (abs(cz - cy) > 1) || (abs(cx - cz) > 1))
cout << "NO\n";
else
cout << "YES\n";
}
}
|
### Prompt
Generate a cpp solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main(void) {
string s;
cin >> s;
vector<int> x(s.size()), y(s.size()), z(s.size());
for (int i = 0; i < (int)s.size(); ++i) {
if (i != 0)
x[i] = x[i - 1], y[i] = y[i - 1], z[i] = z[i - 1];
else
x[i] = 0, y[i] = 0, z[i] = 0;
if (s[i] == 'x') x[i]++;
if (s[i] == 'y') y[i]++;
if (s[i] == 'z') z[i]++;
}
int m;
cin >> m;
int l, r;
int cx, cy, cz;
for (int i = 0; i < m; ++i) {
cin >> l >> r;
if (r - l + 1 < 3) {
cout << "YES\n";
continue;
}
l--, r--;
cx = x[r];
cy = y[r];
cz = z[r];
if (l > 0) cx -= x[l - 1], cy -= y[l - 1], cz -= z[l - 1];
if ((abs(cx - cy) > 1) || (abs(cz - cy) > 1) || (abs(cx - cz) > 1))
cout << "NO\n";
else
cout << "YES\n";
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
long prefix[100001][3];
int main() {
string s;
cin >> s;
long len = s.length();
long i;
for (i = 0; i < len; i++) {
prefix[i + 1][s[i] - 'x']++;
}
for (i = 1; i <= len; i++) {
prefix[i][0] += prefix[i - 1][0];
prefix[i][1] += prefix[i - 1][1];
prefix[i][2] += prefix[i - 1][2];
}
long a[3];
long m, l, r;
cin >> m;
while (m--) {
cin >> l >> r;
if (r - l < 2) {
cout << "YES" << endl;
continue;
}
a[0] = -prefix[l - 1][0] + prefix[r][0];
a[1] = -prefix[l - 1][1] + prefix[r][1];
a[2] = -prefix[l - 1][2] + prefix[r][2];
sort(a, a + 3);
if (a[2] - a[0] > 1)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long prefix[100001][3];
int main() {
string s;
cin >> s;
long len = s.length();
long i;
for (i = 0; i < len; i++) {
prefix[i + 1][s[i] - 'x']++;
}
for (i = 1; i <= len; i++) {
prefix[i][0] += prefix[i - 1][0];
prefix[i][1] += prefix[i - 1][1];
prefix[i][2] += prefix[i - 1][2];
}
long a[3];
long m, l, r;
cin >> m;
while (m--) {
cin >> l >> r;
if (r - l < 2) {
cout << "YES" << endl;
continue;
}
a[0] = -prefix[l - 1][0] + prefix[r][0];
a[1] = -prefix[l - 1][1] + prefix[r][1];
a[2] = -prefix[l - 1][2] + prefix[r][2];
sort(a, a + 3);
if (a[2] - a[0] > 1)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int cnt[5];
char a[maxn];
int x[maxn], y[maxn], z[maxn];
int Abs(int x) { return x < 0 ? (-x) : x; }
bool can(int x, int y, int z) {
if (x == y && (Abs(z - x) <= 1)) return true;
return false;
}
int main() {
int n, l, r;
scanf("%s%d", a, &n);
int len = strlen(a);
x[0] = y[0] = z[0] = 0;
for (int i = 0; i < len; i++) {
if (a[i] == 'x') {
x[i + 1] = x[i] + 1;
y[i + 1] = y[i];
z[i + 1] = z[i];
} else if (a[i] == 'y') {
y[i + 1] = y[i] + 1;
x[i + 1] = x[i];
z[i + 1] = z[i];
} else {
z[i + 1] = z[i] + 1;
x[i + 1] = x[i];
y[i + 1] = y[i];
}
}
while (n--) {
scanf("%d%d", &l, &r);
if (r - l + 1 < 3) {
printf("YES\n");
continue;
}
int numx = x[r] - x[l - 1], numy = y[r] - y[l - 1], numz = z[r] - z[l - 1];
if (can(numx, numy, numz) || can(numx, numz, numy) || can(numy, numz, numx))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int cnt[5];
char a[maxn];
int x[maxn], y[maxn], z[maxn];
int Abs(int x) { return x < 0 ? (-x) : x; }
bool can(int x, int y, int z) {
if (x == y && (Abs(z - x) <= 1)) return true;
return false;
}
int main() {
int n, l, r;
scanf("%s%d", a, &n);
int len = strlen(a);
x[0] = y[0] = z[0] = 0;
for (int i = 0; i < len; i++) {
if (a[i] == 'x') {
x[i + 1] = x[i] + 1;
y[i + 1] = y[i];
z[i + 1] = z[i];
} else if (a[i] == 'y') {
y[i + 1] = y[i] + 1;
x[i + 1] = x[i];
z[i + 1] = z[i];
} else {
z[i + 1] = z[i] + 1;
x[i + 1] = x[i];
y[i + 1] = y[i];
}
}
while (n--) {
scanf("%d%d", &l, &r);
if (r - l + 1 < 3) {
printf("YES\n");
continue;
}
int numx = x[r] - x[l - 1], numy = y[r] - y[l - 1], numz = z[r] - z[l - 1];
if (can(numx, numy, numz) || can(numx, numz, numy) || can(numy, numz, numx))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
using namespace std;
const int maxn = 100010;
char s[maxn];
int a[maxn][5];
int c[3];
int m;
int main() {
scanf("%s", s + 1);
int n = strlen(s + 1);
memset(a, 0, sizeof(a));
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 3; j++) a[i][j] = a[i - 1][j];
a[i][s[i] - 'x']++;
}
scanf("%d", &m);
for (int i = 0; i < m; i++) {
int l, r;
scanf("%d%d", &l, &r);
for (int i = 0; i < 3; i++) c[i] = a[r][i] - a[l - 1][i];
int Max = c[0], Min = c[0];
for (int i = 1; i < 3; i++) Max = max(Max, c[i]), Min = min(Min, c[i]);
puts(r - l + 1 < 3 || Max - Min <= 1 ? "YES" : "NO");
}
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using namespace std;
const int maxn = 100010;
char s[maxn];
int a[maxn][5];
int c[3];
int m;
int main() {
scanf("%s", s + 1);
int n = strlen(s + 1);
memset(a, 0, sizeof(a));
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 3; j++) a[i][j] = a[i - 1][j];
a[i][s[i] - 'x']++;
}
scanf("%d", &m);
for (int i = 0; i < m; i++) {
int l, r;
scanf("%d%d", &l, &r);
for (int i = 0; i < 3; i++) c[i] = a[r][i] - a[l - 1][i];
int Max = c[0], Min = c[0];
for (int i = 1; i < 3; i++) Max = max(Max, c[i]), Min = min(Min, c[i]);
puts(r - l + 1 < 3 || Max - Min <= 1 ? "YES" : "NO");
}
return 0;
}
```
|
#include <bits/stdc++.h>
const int MAXINT = 1111 * 1111 * 1111;
const long long MAXLINT = MAXINT * 1ll * MAXINT;
const long double EPS = 1e-10;
using namespace std;
int t[3][111111];
int get(int x, int y, int z) {
int a[11111];
int cnt = 0;
for (int i = 0; i < x; i++) a[cnt++] = 0;
for (int i = 0; i < y; i++) a[cnt++] = 1;
for (int i = 0; i < z; i++) a[cnt++] = 2;
int n = cnt;
cnt = 0;
int f = 1;
while (f) {
f = 0;
cnt++;
if (cnt > 100000) return 0;
for (int i = 0; i < n - 2; i++) {
if (a[i] == a[i + 1] && a[i] == a[i + 2]) {
f = 1;
continue;
}
if (((a[i] != 0) && (a[i + 1] != 1) && (a[i + 2] != 2)) ||
((a[i] != 0 && a[i + 1] != 2 && a[i + 2] != 1) ||
((a[i] != 2 && a[i + 1] != 1 && a[i + 2] != 0)))) {
random_shuffle(a + i, a + i + 2);
f = 1;
}
}
}
return 1;
}
void solve() {
string s;
cin >> s;
int n = s.size();
int a[111111];
for (int i = 0; i < s.size(); i++) a[i] = int(s[i] - 'x');
memset(t, 0, sizeof(t));
for (int i = 0; i < n; i++) {
if (i != 0)
for (int j = 0; j < 3; j++) t[j][i] = t[j][i - 1];
t[a[i]][i]++;
}
int q[11];
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
q[0] = t[0][y] - t[0][x];
q[1] = t[1][y] - t[1][x];
q[2] = t[2][y] - t[2][x];
q[a[x]]++;
sort(q, q + 3);
if (y - x >= 2) {
if ((q[1] - q[0]) > 1 || (q[2] - q[1] > 1) ||
(q[0] != q[1] && q[1] != q[2]))
cout << "NO" << endl;
else
cout << "YES" << endl;
} else
cout << "YES" << endl;
}
}
int main() { solve(); }
|
### Prompt
Your task is to create a CPP solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
const int MAXINT = 1111 * 1111 * 1111;
const long long MAXLINT = MAXINT * 1ll * MAXINT;
const long double EPS = 1e-10;
using namespace std;
int t[3][111111];
int get(int x, int y, int z) {
int a[11111];
int cnt = 0;
for (int i = 0; i < x; i++) a[cnt++] = 0;
for (int i = 0; i < y; i++) a[cnt++] = 1;
for (int i = 0; i < z; i++) a[cnt++] = 2;
int n = cnt;
cnt = 0;
int f = 1;
while (f) {
f = 0;
cnt++;
if (cnt > 100000) return 0;
for (int i = 0; i < n - 2; i++) {
if (a[i] == a[i + 1] && a[i] == a[i + 2]) {
f = 1;
continue;
}
if (((a[i] != 0) && (a[i + 1] != 1) && (a[i + 2] != 2)) ||
((a[i] != 0 && a[i + 1] != 2 && a[i + 2] != 1) ||
((a[i] != 2 && a[i + 1] != 1 && a[i + 2] != 0)))) {
random_shuffle(a + i, a + i + 2);
f = 1;
}
}
}
return 1;
}
void solve() {
string s;
cin >> s;
int n = s.size();
int a[111111];
for (int i = 0; i < s.size(); i++) a[i] = int(s[i] - 'x');
memset(t, 0, sizeof(t));
for (int i = 0; i < n; i++) {
if (i != 0)
for (int j = 0; j < 3; j++) t[j][i] = t[j][i - 1];
t[a[i]][i]++;
}
int q[11];
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
q[0] = t[0][y] - t[0][x];
q[1] = t[1][y] - t[1][x];
q[2] = t[2][y] - t[2][x];
q[a[x]]++;
sort(q, q + 3);
if (y - x >= 2) {
if ((q[1] - q[0]) > 1 || (q[2] - q[1] > 1) ||
(q[0] != q[1] && q[1] != q[2]))
cout << "NO" << endl;
else
cout << "YES" << endl;
} else
cout << "YES" << endl;
}
}
int main() { solve(); }
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x[100007], y[100007], z[100007], i, j, len, a, b, cnt_x, cnt_y, cnt_z,
now[4];
string s;
cin >> s >> n;
len = s.size();
cnt_x = 0;
cnt_y = 0;
cnt_z = 0;
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
memset(z, 0, sizeof(z));
for (i = 0; i < len; i++) {
char ch = s[i];
if (ch == 'x') cnt_x++;
x[i] = cnt_x;
if (ch == 'y') cnt_y++;
y[i] = cnt_y;
if (ch == 'z') cnt_z++;
z[i] = cnt_z;
}
for (i = 0; i < n; i++) {
cin >> a >> b;
if (b - a < 2) {
cout << "YES\n";
continue;
}
a--;
b--;
now[0] = x[b] - x[a];
now[1] = y[b] - y[a];
now[2] = z[b] - z[a];
if (s[a] == 'x')
now[0]++;
else if (s[a] == 'y')
now[1]++;
else if (s[a] == 'z')
now[2]++;
sort(now, now + 3);
if (now[0] >= now[2] - 1)
cout << "YES\n";
else
cout << "NO\n";
}
return 0;
}
|
### Prompt
Generate a Cpp solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x[100007], y[100007], z[100007], i, j, len, a, b, cnt_x, cnt_y, cnt_z,
now[4];
string s;
cin >> s >> n;
len = s.size();
cnt_x = 0;
cnt_y = 0;
cnt_z = 0;
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
memset(z, 0, sizeof(z));
for (i = 0; i < len; i++) {
char ch = s[i];
if (ch == 'x') cnt_x++;
x[i] = cnt_x;
if (ch == 'y') cnt_y++;
y[i] = cnt_y;
if (ch == 'z') cnt_z++;
z[i] = cnt_z;
}
for (i = 0; i < n; i++) {
cin >> a >> b;
if (b - a < 2) {
cout << "YES\n";
continue;
}
a--;
b--;
now[0] = x[b] - x[a];
now[1] = y[b] - y[a];
now[2] = z[b] - z[a];
if (s[a] == 'x')
now[0]++;
else if (s[a] == 'y')
now[1]++;
else if (s[a] == 'z')
now[2]++;
sort(now, now + 3);
if (now[0] >= now[2] - 1)
cout << "YES\n";
else
cout << "NO\n";
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e8 + 7;
const int MAX = 1e5 + 5;
const int MOD = 1e9 + 7;
string s;
int cx[MAX], cy[MAX], cz[MAX];
int main() {
std::ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
cin >> s;
cx[0] = 0;
cy[0] = 0;
cz[0] = 0;
for (int i = 1; i <= s.size(); i++) {
cx[i] = cx[i - 1];
cy[i] = cy[i - 1];
cz[i] = cz[i - 1];
if (s[i - 1] == 'x') cx[i]++;
if (s[i - 1] == 'y') cy[i]++;
if (s[i - 1] == 'z') cz[i]++;
}
int m;
cin >> m;
for (int i = 1; i <= m; i++) {
int l, r;
cin >> l >> r;
if ((r - l + 1) < 3) {
cout << "YES\n";
continue;
}
int x = cx[r] - cx[l - 1];
int y = cy[r] - cy[l - 1];
int z = cz[r] - cz[l - 1];
if (x == 0 || y == 0 || z == 0) {
cout << "NO\n";
continue;
}
if (max(x, max(y, z)) - min(x, min(y, z)) <= 1)
cout << "YES\n";
else
cout << "NO\n";
}
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e8 + 7;
const int MAX = 1e5 + 5;
const int MOD = 1e9 + 7;
string s;
int cx[MAX], cy[MAX], cz[MAX];
int main() {
std::ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
cin >> s;
cx[0] = 0;
cy[0] = 0;
cz[0] = 0;
for (int i = 1; i <= s.size(); i++) {
cx[i] = cx[i - 1];
cy[i] = cy[i - 1];
cz[i] = cz[i - 1];
if (s[i - 1] == 'x') cx[i]++;
if (s[i - 1] == 'y') cy[i]++;
if (s[i - 1] == 'z') cz[i]++;
}
int m;
cin >> m;
for (int i = 1; i <= m; i++) {
int l, r;
cin >> l >> r;
if ((r - l + 1) < 3) {
cout << "YES\n";
continue;
}
int x = cx[r] - cx[l - 1];
int y = cy[r] - cy[l - 1];
int z = cz[r] - cz[l - 1];
if (x == 0 || y == 0 || z == 0) {
cout << "NO\n";
continue;
}
if (max(x, max(y, z)) - min(x, min(y, z)) <= 1)
cout << "YES\n";
else
cout << "NO\n";
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
int n;
cin >> n;
pair<pair<int, int>, int> ar[s.size()];
int x = 0, y = 0, z = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'x') {
x++;
} else if (s[i] == 'y') {
y++;
} else {
z++;
}
ar[i].first.first = x;
ar[i].first.second = y;
ar[i].second = z;
}
for (int i = 0; i < n; i++) {
int l, r;
cin >> l >> r;
if (r - l <= 1) {
cout << "YES" << endl;
continue;
}
l--;
r--;
int a, b, c;
if (l == 0) {
a = ar[r].first.first;
b = ar[r].first.second;
c = ar[r].second;
} else {
l--;
a = ar[r].first.first - ar[l].first.first;
b = ar[r].first.second - ar[l].first.second;
c = ar[r].second - ar[l].second;
}
if ((a == b && b == c) || (a == b && c == a + 1) ||
(a == c && b == a + 1) || (b == c && a == b + 1) ||
(a == b && c == a - 1) || (a == c && b == a - 1) ||
(b == c && a == b - 1)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
|
### Prompt
Generate a Cpp solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
int n;
cin >> n;
pair<pair<int, int>, int> ar[s.size()];
int x = 0, y = 0, z = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'x') {
x++;
} else if (s[i] == 'y') {
y++;
} else {
z++;
}
ar[i].first.first = x;
ar[i].first.second = y;
ar[i].second = z;
}
for (int i = 0; i < n; i++) {
int l, r;
cin >> l >> r;
if (r - l <= 1) {
cout << "YES" << endl;
continue;
}
l--;
r--;
int a, b, c;
if (l == 0) {
a = ar[r].first.first;
b = ar[r].first.second;
c = ar[r].second;
} else {
l--;
a = ar[r].first.first - ar[l].first.first;
b = ar[r].first.second - ar[l].first.second;
c = ar[r].second - ar[l].second;
}
if ((a == b && b == c) || (a == b && c == a + 1) ||
(a == c && b == a + 1) || (b == c && a == b + 1) ||
(a == b && c == a - 1) || (a == c && b == a - 1) ||
(b == c && a == b - 1)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct pairvar {
int a;
int b;
int c;
int max;
};
bool isPrime(long long x) {
if (x == 1) {
return false;
}
long long a = (long long)x;
for (int i = 2; i <= (long long)sqrt((double)a); i++) {
if (a % i == 0) {
return false;
}
}
return true;
}
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
long long factorial(long long x) {
long long sum = 1;
for (long long i = 1; i <= x; i++) {
sum *= i;
}
return sum;
}
int toint(string s) {
int x = 0;
int k = (int)pow(10, (double)s.length() - 1);
for (int i = 0; i < (int)s.length(); i++) {
x += (s[i] - '0') * k;
k /= 10;
}
return x;
}
string tostr(int x) {
if (x == 0) {
return "0";
}
string s = "";
bool f = x < 0;
x = abs(x);
while (x != 0) {
s += (x % 10) + '0';
x /= 10;
}
string t = "";
if (f) {
t = "-";
}
for (int i = s.length() - 1; i >= 0; i--) {
t += s[i];
}
return t;
}
bool cmp(const pairvar &a, const pairvar &b) {
if (a.a == b.a) return a.b > b.b;
return a.a > b.a;
}
int lcm(int a, int b) { return (a * b) / gcd(a, b); }
int x[100005];
int y[100005];
int z[100005];
int main() {
string s;
cin >> s;
int n = s.length();
x[n + 1];
y[n + 1];
z[n + 1];
int s1 = 0;
int s2 = 0;
int s3 = 0;
z[0] = x[0] = y[0] = 0;
for (int i = 1; i <= n; i++) {
if (s[i - 1] == 'x') {
s1++;
}
if (s[i - 1] == 'y') {
s2++;
}
if (s[i - 1] == 'z') {
s3++;
}
x[i] = s1;
y[i] = s2;
z[i] = s3;
}
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int l, r;
cin >> l >> r;
l++;
r++;
vector<int> cnt;
if (r - l + 1 < 3) {
cout << "YES" << endl;
continue;
}
cnt.push_back(x[r - 1] - x[l - 2]);
cnt.push_back(y[r - 1] - y[l - 2]);
cnt.push_back(z[r - 1] - z[l - 2]);
sort(cnt.begin(), cnt.end());
if (cnt[2] - cnt[0] > 1) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
}
return 0;
}
|
### Prompt
Create a solution in cpp for the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct pairvar {
int a;
int b;
int c;
int max;
};
bool isPrime(long long x) {
if (x == 1) {
return false;
}
long long a = (long long)x;
for (int i = 2; i <= (long long)sqrt((double)a); i++) {
if (a % i == 0) {
return false;
}
}
return true;
}
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
long long factorial(long long x) {
long long sum = 1;
for (long long i = 1; i <= x; i++) {
sum *= i;
}
return sum;
}
int toint(string s) {
int x = 0;
int k = (int)pow(10, (double)s.length() - 1);
for (int i = 0; i < (int)s.length(); i++) {
x += (s[i] - '0') * k;
k /= 10;
}
return x;
}
string tostr(int x) {
if (x == 0) {
return "0";
}
string s = "";
bool f = x < 0;
x = abs(x);
while (x != 0) {
s += (x % 10) + '0';
x /= 10;
}
string t = "";
if (f) {
t = "-";
}
for (int i = s.length() - 1; i >= 0; i--) {
t += s[i];
}
return t;
}
bool cmp(const pairvar &a, const pairvar &b) {
if (a.a == b.a) return a.b > b.b;
return a.a > b.a;
}
int lcm(int a, int b) { return (a * b) / gcd(a, b); }
int x[100005];
int y[100005];
int z[100005];
int main() {
string s;
cin >> s;
int n = s.length();
x[n + 1];
y[n + 1];
z[n + 1];
int s1 = 0;
int s2 = 0;
int s3 = 0;
z[0] = x[0] = y[0] = 0;
for (int i = 1; i <= n; i++) {
if (s[i - 1] == 'x') {
s1++;
}
if (s[i - 1] == 'y') {
s2++;
}
if (s[i - 1] == 'z') {
s3++;
}
x[i] = s1;
y[i] = s2;
z[i] = s3;
}
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int l, r;
cin >> l >> r;
l++;
r++;
vector<int> cnt;
if (r - l + 1 < 3) {
cout << "YES" << endl;
continue;
}
cnt.push_back(x[r - 1] - x[l - 2]);
cnt.push_back(y[r - 1] - y[l - 2]);
cnt.push_back(z[r - 1] - z[l - 2]);
sort(cnt.begin(), cnt.end());
if (cnt[2] - cnt[0] > 1) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long mod(long long a, long long m) { return (a % m + m) % m; }
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string s;
int x[100100], y[100100], z[100100];
int m;
int main() {
cin >> s;
switch (s[0]) {
case 'x':
x[1] = 1;
break;
case 'y':
y[1] = 1;
break;
case 'z':
z[1] = 1;
break;
}
for (int i = 2; i < 1 + (int)s.length(); i++) {
switch (s[i - 1]) {
case 'x':
x[i] = x[i - 1] + 1;
y[i] = y[i - 1];
z[i] = z[i - 1];
break;
case 'y':
x[i] = x[i - 1];
y[i] = y[i - 1] + 1;
z[i] = z[i - 1];
break;
case 'z':
x[i] = x[i - 1];
y[i] = y[i - 1];
z[i] = z[i - 1] + 1;
break;
}
}
cin >> m;
for (int i = 0; i < m; i++) {
int l, r;
cin >> l >> r;
int xn, yn, zn;
xn = x[r] - x[l - 1];
yn = y[r] - y[l - 1];
zn = z[r] - z[l - 1];
bool ok = true;
if (xn == 0 || yn == 0 || zn == 0) ok = false;
if (max(xn, max(yn, zn)) - min(xn, min(yn, zn)) >= 2) ok = false;
if (r - l < 2) ok = true;
printf((ok) ? "YES\n" : "NO\n");
}
}
|
### Prompt
In cpp, your task is to solve the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long mod(long long a, long long m) { return (a % m + m) % m; }
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string s;
int x[100100], y[100100], z[100100];
int m;
int main() {
cin >> s;
switch (s[0]) {
case 'x':
x[1] = 1;
break;
case 'y':
y[1] = 1;
break;
case 'z':
z[1] = 1;
break;
}
for (int i = 2; i < 1 + (int)s.length(); i++) {
switch (s[i - 1]) {
case 'x':
x[i] = x[i - 1] + 1;
y[i] = y[i - 1];
z[i] = z[i - 1];
break;
case 'y':
x[i] = x[i - 1];
y[i] = y[i - 1] + 1;
z[i] = z[i - 1];
break;
case 'z':
x[i] = x[i - 1];
y[i] = y[i - 1];
z[i] = z[i - 1] + 1;
break;
}
}
cin >> m;
for (int i = 0; i < m; i++) {
int l, r;
cin >> l >> r;
int xn, yn, zn;
xn = x[r] - x[l - 1];
yn = y[r] - y[l - 1];
zn = z[r] - z[l - 1];
bool ok = true;
if (xn == 0 || yn == 0 || zn == 0) ok = false;
if (max(xn, max(yn, zn)) - min(xn, min(yn, zn)) >= 2) ok = false;
if (r - l < 2) ok = true;
printf((ok) ? "YES\n" : "NO\n");
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 55;
const int inf = 1e9 + 77;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
const double eps = 1e-7;
string s;
int cs[MAX][3];
int main() {
cin >> s;
int n = s.size();
cs[0][0] = (s[0] == 'x');
cs[0][1] = (s[0] == 'y');
cs[0][2] = (s[0] == 'z');
for (int i = 1; i < n; ++i) {
cs[i][0] = (s[i] == 'x') + cs[i - 1][0];
cs[i][1] = (s[i] == 'y') + cs[i - 1][1];
cs[i][2] = (s[i] == 'z') + cs[i - 1][2];
}
int m;
scanf("%d", &m);
while (m--) {
int l, r;
scanf("%d", &l);
scanf("%d", &r);
--l;
--r;
int cx = cs[r][0] - ((l > 0) ? cs[l - 1][0] : 0);
int cy = cs[r][1] - ((l > 0) ? cs[l - 1][1] : 0);
int cz = cs[r][2] - ((l > 0) ? cs[l - 1][2] : 0);
if (r - l + 1 < 3)
printf("YES");
else if (cs == 0 || cy == 0 || cz == 0)
printf("NO");
else {
int mn = min(cx, min(cy, cz));
cx -= mn;
cy -= mn;
cz -= mn;
if (cx > 1 || cy > 1 || cz > 1)
printf("NO");
else
printf("YES");
}
printf("\n");
}
return 0;
}
|
### Prompt
In CPP, your task is to solve the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 55;
const int inf = 1e9 + 77;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
const double eps = 1e-7;
string s;
int cs[MAX][3];
int main() {
cin >> s;
int n = s.size();
cs[0][0] = (s[0] == 'x');
cs[0][1] = (s[0] == 'y');
cs[0][2] = (s[0] == 'z');
for (int i = 1; i < n; ++i) {
cs[i][0] = (s[i] == 'x') + cs[i - 1][0];
cs[i][1] = (s[i] == 'y') + cs[i - 1][1];
cs[i][2] = (s[i] == 'z') + cs[i - 1][2];
}
int m;
scanf("%d", &m);
while (m--) {
int l, r;
scanf("%d", &l);
scanf("%d", &r);
--l;
--r;
int cx = cs[r][0] - ((l > 0) ? cs[l - 1][0] : 0);
int cy = cs[r][1] - ((l > 0) ? cs[l - 1][1] : 0);
int cz = cs[r][2] - ((l > 0) ? cs[l - 1][2] : 0);
if (r - l + 1 < 3)
printf("YES");
else if (cs == 0 || cy == 0 || cz == 0)
printf("NO");
else {
int mn = min(cx, min(cy, cz));
cx -= mn;
cy -= mn;
cz -= mn;
if (cx > 1 || cy > 1 || cz > 1)
printf("NO");
else
printf("YES");
}
printf("\n");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const long long INFLL = 1e18;
const int INF = 1e9;
const int NMAX = 1000001;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int fvX[NMAX];
int fvY[NMAX];
int fvZ[NMAX];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
int m, n = (int)s.length();
for (int i = 1; i <= n; ++i) {
fvX[i] = fvX[i - 1];
fvY[i] = fvY[i - 1];
fvZ[i] = fvZ[i - 1];
if (s[i - 1] == 'x')
fvX[i]++;
else if (s[i - 1] == 'y')
fvY[i]++;
else {
fvZ[i]++;
}
}
cin >> m;
int k = 0;
while (m--) {
k++;
int l, r;
cin >> l >> r;
if (r - l <= 1) {
cout << "YES" << '\n';
continue;
}
int nrX = fvX[r] - fvX[l - 1];
int nrY = fvY[r] - fvY[l - 1];
int nrZ = fvZ[r] - fvZ[l - 1];
bool ok = true;
if (abs(nrX - nrY) >= 2)
ok = false;
else if (abs(nrX - nrZ) >= 2)
ok = false;
else if (abs(nrY - nrZ) >= 2)
ok = false;
if (ok)
cout << "YES";
else {
cout << "NO";
}
cout << '\n';
}
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const long long INFLL = 1e18;
const int INF = 1e9;
const int NMAX = 1000001;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int fvX[NMAX];
int fvY[NMAX];
int fvZ[NMAX];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
int m, n = (int)s.length();
for (int i = 1; i <= n; ++i) {
fvX[i] = fvX[i - 1];
fvY[i] = fvY[i - 1];
fvZ[i] = fvZ[i - 1];
if (s[i - 1] == 'x')
fvX[i]++;
else if (s[i - 1] == 'y')
fvY[i]++;
else {
fvZ[i]++;
}
}
cin >> m;
int k = 0;
while (m--) {
k++;
int l, r;
cin >> l >> r;
if (r - l <= 1) {
cout << "YES" << '\n';
continue;
}
int nrX = fvX[r] - fvX[l - 1];
int nrY = fvY[r] - fvY[l - 1];
int nrZ = fvZ[r] - fvZ[l - 1];
bool ok = true;
if (abs(nrX - nrY) >= 2)
ok = false;
else if (abs(nrX - nrZ) >= 2)
ok = false;
else if (abs(nrY - nrZ) >= 2)
ok = false;
if (ok)
cout << "YES";
else {
cout << "NO";
}
cout << '\n';
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int order(string str, int a) {
long long sz = str.size();
if (a + 3 > sz) return sz;
long long par = str[a] - 'x';
a++;
par = (par + 2) % 3;
while (str[a] - 'x' == par) {
a++;
par = (par + 2) % 3;
}
return a;
}
int main() {
string str;
cin >> str;
long long k = str.size();
long long cx[k + 1], cy[k + 1], cz[k + 1];
cx[0] = cy[0] = cz[0] = 0;
for (long long i = 0; i < k; i++) {
if (str[i] == 'x')
cx[i + 1] = cx[i] + 1;
else
cx[i + 1] = cx[i];
if (str[i] == 'y')
cy[i + 1] = cy[i] + 1;
else
cy[i + 1] = cy[i];
if (str[i] == 'z')
cz[i + 1] = cz[i] + 1;
else
cz[i + 1] = cz[i];
}
long long m;
scanf("%lld", &m);
for (long long i = 0; i < m; i++) {
long long l, r;
scanf("%lld", &l);
scanf("%lld", &r);
l--;
if (r - l < 3) {
printf("YES\n");
continue;
}
long long ct[3];
ct[0] = cx[r] - cx[l], ct[1] = cy[r] - cy[l], ct[2] = cz[r] - cz[l];
sort(ct, ct + 3);
if (ct[2] - ct[0] > 1)
printf("NO\n");
else
printf("YES\n");
}
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int order(string str, int a) {
long long sz = str.size();
if (a + 3 > sz) return sz;
long long par = str[a] - 'x';
a++;
par = (par + 2) % 3;
while (str[a] - 'x' == par) {
a++;
par = (par + 2) % 3;
}
return a;
}
int main() {
string str;
cin >> str;
long long k = str.size();
long long cx[k + 1], cy[k + 1], cz[k + 1];
cx[0] = cy[0] = cz[0] = 0;
for (long long i = 0; i < k; i++) {
if (str[i] == 'x')
cx[i + 1] = cx[i] + 1;
else
cx[i + 1] = cx[i];
if (str[i] == 'y')
cy[i + 1] = cy[i] + 1;
else
cy[i + 1] = cy[i];
if (str[i] == 'z')
cz[i + 1] = cz[i] + 1;
else
cz[i + 1] = cz[i];
}
long long m;
scanf("%lld", &m);
for (long long i = 0; i < m; i++) {
long long l, r;
scanf("%lld", &l);
scanf("%lld", &r);
l--;
if (r - l < 3) {
printf("YES\n");
continue;
}
long long ct[3];
ct[0] = cx[r] - cx[l], ct[1] = cy[r] - cy[l], ct[2] = cz[r] - cz[l];
sort(ct, ct + 3);
if (ct[2] - ct[0] > 1)
printf("NO\n");
else
printf("YES\n");
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
string str;
int x[100001] = {0}, y[100001] = {0}, z[100001] = {0}, len, cx, cy, cz, m, l, r;
int main() {
ios_base::sync_with_stdio(false);
cin >> str;
len = str.length();
for (int i = 0; i < len; i++) {
if (str[i] == 'x')
x[i] = 1;
else if (str[i] == 'y')
y[i] = 1;
else
z[i] = 1;
if (i > 0) {
x[i] += x[i - 1];
y[i] += y[i - 1];
z[i] += z[i - 1];
}
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> l >> r;
l--;
r--;
if (l == 0) {
cx = x[r];
cy = y[r];
cz = z[r];
} else {
cx = x[r] - x[l - 1];
cy = y[r] - y[l - 1];
cz = z[r] - z[l - 1];
}
int d = (r - l + 1) / 3,
res = (int)ceil((double)((r - l + 1) - (d * 3)) / ((double)3.0));
if ((r - l + 1) < 3 || (cx >= d && cx <= d + res && cy >= d &&
cy <= d + res && cz >= d && cz <= d + res))
cout << "YES\n";
else
cout << "NO\n";
}
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
string str;
int x[100001] = {0}, y[100001] = {0}, z[100001] = {0}, len, cx, cy, cz, m, l, r;
int main() {
ios_base::sync_with_stdio(false);
cin >> str;
len = str.length();
for (int i = 0; i < len; i++) {
if (str[i] == 'x')
x[i] = 1;
else if (str[i] == 'y')
y[i] = 1;
else
z[i] = 1;
if (i > 0) {
x[i] += x[i - 1];
y[i] += y[i - 1];
z[i] += z[i - 1];
}
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> l >> r;
l--;
r--;
if (l == 0) {
cx = x[r];
cy = y[r];
cz = z[r];
} else {
cx = x[r] - x[l - 1];
cy = y[r] - y[l - 1];
cz = z[r] - z[l - 1];
}
int d = (r - l + 1) / 3,
res = (int)ceil((double)((r - l + 1) - (d * 3)) / ((double)3.0));
if ((r - l + 1) < 3 || (cx >= d && cx <= d + res && cy >= d &&
cy <= d + res && cz >= d && cz <= d + res))
cout << "YES\n";
else
cout << "NO\n";
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
class BIT {
std::vector<int> bit;
public:
BIT(int size) : bit(size + 1, 0) {}
void add(int i, int x) {
i++;
while (i < (int)bit.size()) {
bit[i] += x;
i += i & -i;
}
}
int sum(int a) {
a++;
int res = 0;
while (0 < a) {
res += bit[a];
a -= a & -a;
}
return res;
}
int sum(int a, int b) { return sum(b) - sum(a - 1); }
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
int n;
cin >> n;
BIT xs(s.size()), ys(s.size()), zs(s.size());
for (int i = 0; i < (s.size()); i++) {
if (s[i] == 'x')
xs.add(i, 1);
else if (s[i] == 'y')
ys.add(i, 1);
else
zs.add(i, 1);
}
for (int i = 0; i < (n); i++) {
int l, r;
cin >> l >> r;
l--;
r--;
int x, y, z;
x = xs.sum(l, r);
y = ys.sum(l, r);
z = zs.sum(l, r);
string ans = "NO";
if (abs(x - y) < 2 && abs(x - z) < 2 && abs(y - z) < 2 || r - l < 2)
ans = "YES";
cout << ans << endl;
}
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
Input
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
Examples
Input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
Output
YES
YES
NO
YES
NO
Note
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
class BIT {
std::vector<int> bit;
public:
BIT(int size) : bit(size + 1, 0) {}
void add(int i, int x) {
i++;
while (i < (int)bit.size()) {
bit[i] += x;
i += i & -i;
}
}
int sum(int a) {
a++;
int res = 0;
while (0 < a) {
res += bit[a];
a -= a & -a;
}
return res;
}
int sum(int a, int b) { return sum(b) - sum(a - 1); }
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
int n;
cin >> n;
BIT xs(s.size()), ys(s.size()), zs(s.size());
for (int i = 0; i < (s.size()); i++) {
if (s[i] == 'x')
xs.add(i, 1);
else if (s[i] == 'y')
ys.add(i, 1);
else
zs.add(i, 1);
}
for (int i = 0; i < (n); i++) {
int l, r;
cin >> l >> r;
l--;
r--;
int x, y, z;
x = xs.sum(l, r);
y = ys.sum(l, r);
z = zs.sum(l, r);
string ans = "NO";
if (abs(x - y) < 2 && abs(x - z) < 2 && abs(y - z) < 2 || r - l < 2)
ans = "YES";
cout << ans << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace ::std;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
bool visit[55][55];
pair<int, int> p[55][55];
stack<pair<int, int> > V;
int d[55][55], n, m, k, cont;
void bfs(int x, int y, int k) {
visit[x][y] = true;
d[x][y] = 1;
p[x][x] = make_pair(-1, -1);
queue<pair<int, int> > Q;
Q.push(make_pair(x, y));
while (not Q.empty() and k--) {
pair<int, int> q = Q.front();
Q.pop();
cont += d[q.first][q.second];
V.push(q);
for (int i = 0; i < 4; i++) {
int a = q.first + dy[i], b = q.second + dx[i];
if (a >= 0 and b >= 0 and a < n and b < m and not visit[a][b]) {
visit[a][b] = true;
p[a][b] = q;
d[a][b] = d[q.first][q.second] + 1;
Q.push(make_pair(a, b));
}
}
}
}
void path(int x, int y) {
if (x + y != -2) {
pair<int, int> q = p[x][y];
path(q.first, q.second);
printf("(%d,%d) ", x + 1, y + 1);
}
}
int main(void) {
scanf("%d %d %d", &n, &m, &k);
cont = 0;
bfs(0, 0, k);
printf("%d\n", cont);
while (not V.empty()) {
pair<int, int> q = V.top();
V.pop();
path(q.first, q.second);
cout << endl;
}
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace ::std;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
bool visit[55][55];
pair<int, int> p[55][55];
stack<pair<int, int> > V;
int d[55][55], n, m, k, cont;
void bfs(int x, int y, int k) {
visit[x][y] = true;
d[x][y] = 1;
p[x][x] = make_pair(-1, -1);
queue<pair<int, int> > Q;
Q.push(make_pair(x, y));
while (not Q.empty() and k--) {
pair<int, int> q = Q.front();
Q.pop();
cont += d[q.first][q.second];
V.push(q);
for (int i = 0; i < 4; i++) {
int a = q.first + dy[i], b = q.second + dx[i];
if (a >= 0 and b >= 0 and a < n and b < m and not visit[a][b]) {
visit[a][b] = true;
p[a][b] = q;
d[a][b] = d[q.first][q.second] + 1;
Q.push(make_pair(a, b));
}
}
}
}
void path(int x, int y) {
if (x + y != -2) {
pair<int, int> q = p[x][y];
path(q.first, q.second);
printf("(%d,%d) ", x + 1, y + 1);
}
}
int main(void) {
scanf("%d %d %d", &n, &m, &k);
cont = 0;
bfs(0, 0, k);
printf("%d\n", cont);
while (not V.empty()) {
pair<int, int> q = V.top();
V.pop();
path(q.first, q.second);
cout << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int const maxn = (int)1e2 + 111;
int const inf = (1 << 30) - 1;
int n, m, k;
bool used[maxn][maxn];
vector<vector<pair<int, int> > > ans;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
bool check(int x, int y) {
return x >= 1 && x <= n && y >= 1 && y <= m && !used[x][y];
}
void print(vector<pair<int, int> > &v) {
for (typeof(v.begin()) it = v.begin(); it != v.end(); it++) {
pair<int, int> temp = *it;
int x = temp.first;
int y = temp.second;
printf("(%d,%d) ", x, y);
}
printf("\n");
}
int main() {
cin >> n >> m >> k;
queue<vector<pair<int, int> > > Q;
vector<pair<int, int> > vv;
vv.push_back(make_pair(1, 1));
Q.push(vv);
used[1][1] = true;
int val = 0;
for (int i = 0; i < k; i++) {
vector<pair<int, int> > temp = Q.front();
Q.pop();
val += temp.size();
ans.push_back(temp);
int x = temp.back().first;
int y = temp.back().second;
for (int i = 0; i < 4; i++) {
int tox = x + dx[i];
int toy = y + dy[i];
if (check(tox, toy)) {
used[tox][toy] = true;
temp.push_back(make_pair(tox, toy));
Q.push(temp);
temp.pop_back();
}
}
}
reverse(ans.begin(), ans.end());
cout << val << endl;
for (int i = 0; i < ans.size(); i++) {
vector<pair<int, int> > q = ans[i];
print(q);
}
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int const maxn = (int)1e2 + 111;
int const inf = (1 << 30) - 1;
int n, m, k;
bool used[maxn][maxn];
vector<vector<pair<int, int> > > ans;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
bool check(int x, int y) {
return x >= 1 && x <= n && y >= 1 && y <= m && !used[x][y];
}
void print(vector<pair<int, int> > &v) {
for (typeof(v.begin()) it = v.begin(); it != v.end(); it++) {
pair<int, int> temp = *it;
int x = temp.first;
int y = temp.second;
printf("(%d,%d) ", x, y);
}
printf("\n");
}
int main() {
cin >> n >> m >> k;
queue<vector<pair<int, int> > > Q;
vector<pair<int, int> > vv;
vv.push_back(make_pair(1, 1));
Q.push(vv);
used[1][1] = true;
int val = 0;
for (int i = 0; i < k; i++) {
vector<pair<int, int> > temp = Q.front();
Q.pop();
val += temp.size();
ans.push_back(temp);
int x = temp.back().first;
int y = temp.back().second;
for (int i = 0; i < 4; i++) {
int tox = x + dx[i];
int toy = y + dy[i];
if (check(tox, toy)) {
used[tox][toy] = true;
temp.push_back(make_pair(tox, toy));
Q.push(temp);
temp.pop_back();
}
}
}
reverse(ans.begin(), ans.end());
cout << val << endl;
for (int i = 0; i < ans.size(); i++) {
vector<pair<int, int> > q = ans[i];
print(q);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
void solve();
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed;
cout.precision(12);
solve();
return 0;
}
template <typename T>
void prv(vector<T> v) {
for (int __ii = 0; __ii < ((int)v.size()); __ii++) {
if (__ii) cout << ' ';
cout << v[__ii];
}
cout << '\n';
}
template <typename T>
void prm(vector<vector<T>> v) {
for (int __ii = 0; __ii < ((int)v.size()); __ii++) {
for (int __jj = 0; __jj < v[__ii].size(); __jj++) {
if (__jj) cout << ' ';
cout << v[__ii][__jj];
}
cout << '\n';
}
}
template <typename T>
void sc(T& x) {
cin >> x;
}
template <typename Head, typename... Tail>
void sc(Head& head, Tail&... tail) {
cin >> head;
sc(tail...);
}
template <typename T>
void pr(const T& x) {
cout << x << '\n';
}
template <typename Head, typename... Tail>
void pr(const Head& head, const Tail&... tail) {
cout << head << ' ';
pr(tail...);
}
template <typename... T>
void err(const T&... cod) {
pr(cod...);
exit(0);
}
string cell(int y, int x) {
y++;
x++;
string ys, xs, ans;
ans.push_back('(');
while (y > 0) {
ys.push_back((y % 10) + '0');
y /= 10;
}
reverse(ys.begin(), ys.end());
ans += ys;
ans.push_back(',');
while (x > 0) {
xs.push_back((x % 10) + '0');
x /= 10;
}
reverse(xs.begin(), xs.end());
ans += xs;
ans.push_back(')');
return ans;
}
vector<string> get(int y, int x) {
vector<string> ans;
int cury = 0, curx = 0;
while (cury < y) {
ans.push_back(cell(cury, curx));
cury++;
}
while (curx < x) {
ans.push_back(cell(cury, curx));
curx++;
}
ans.push_back(cell(cury, curx));
return ans;
}
void solve() {
int n, m, k;
sc(n, m, k);
vector<vector<pair<int, int>>> pos(n + m);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) pos[i + j].emplace_back(i, j);
vector<vector<string>> ans;
int cost = 0;
for (int i = 0; i < n + m; i++) {
int pick = min(k, ((int)pos[i].size()));
cost += pick * (i + 1);
for (int j = 0; j < pick; j++)
ans.push_back(get(pos[i][j].first, pos[i][j].second));
k -= pick;
}
reverse(ans.begin(), ans.end());
pr(cost);
for (auto p : ans) prv(p);
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void solve();
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed;
cout.precision(12);
solve();
return 0;
}
template <typename T>
void prv(vector<T> v) {
for (int __ii = 0; __ii < ((int)v.size()); __ii++) {
if (__ii) cout << ' ';
cout << v[__ii];
}
cout << '\n';
}
template <typename T>
void prm(vector<vector<T>> v) {
for (int __ii = 0; __ii < ((int)v.size()); __ii++) {
for (int __jj = 0; __jj < v[__ii].size(); __jj++) {
if (__jj) cout << ' ';
cout << v[__ii][__jj];
}
cout << '\n';
}
}
template <typename T>
void sc(T& x) {
cin >> x;
}
template <typename Head, typename... Tail>
void sc(Head& head, Tail&... tail) {
cin >> head;
sc(tail...);
}
template <typename T>
void pr(const T& x) {
cout << x << '\n';
}
template <typename Head, typename... Tail>
void pr(const Head& head, const Tail&... tail) {
cout << head << ' ';
pr(tail...);
}
template <typename... T>
void err(const T&... cod) {
pr(cod...);
exit(0);
}
string cell(int y, int x) {
y++;
x++;
string ys, xs, ans;
ans.push_back('(');
while (y > 0) {
ys.push_back((y % 10) + '0');
y /= 10;
}
reverse(ys.begin(), ys.end());
ans += ys;
ans.push_back(',');
while (x > 0) {
xs.push_back((x % 10) + '0');
x /= 10;
}
reverse(xs.begin(), xs.end());
ans += xs;
ans.push_back(')');
return ans;
}
vector<string> get(int y, int x) {
vector<string> ans;
int cury = 0, curx = 0;
while (cury < y) {
ans.push_back(cell(cury, curx));
cury++;
}
while (curx < x) {
ans.push_back(cell(cury, curx));
curx++;
}
ans.push_back(cell(cury, curx));
return ans;
}
void solve() {
int n, m, k;
sc(n, m, k);
vector<vector<pair<int, int>>> pos(n + m);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) pos[i + j].emplace_back(i, j);
vector<vector<string>> ans;
int cost = 0;
for (int i = 0; i < n + m; i++) {
int pick = min(k, ((int)pos[i].size()));
cost += pick * (i + 1);
for (int j = 0; j < pick; j++)
ans.push_back(get(pos[i][j].first, pos[i][j].second));
k -= pick;
}
reverse(ans.begin(), ans.end());
pr(cost);
for (auto p : ans) prv(p);
}
```
|
#include <bits/stdc++.h>
using namespace std;
void print(long long int r, long long int c) {
for (long long int i = 1; i <= c; i++)
cout << "("
<< "1"
<< "," << i << ")"
<< " ";
for (long long int i = 2; i <= r; i++)
cout << "(" << i << "," << c << ")"
<< " ";
cout << endl;
}
int main() {
long long int i, j, n, m, k, ans = 0;
vector<pair<long long int, pair<long long int, long long int> > > v;
cin >> n >> m >> k;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) v.push_back(make_pair(i + j - 1, make_pair(i, j)));
}
sort(v.begin(), v.end());
for (i = 0; i < k; i++) ans += v[i].first;
cout << ans << endl;
for (i = k - 1; i >= 0; i--) print(v[i].second.first, v[i].second.second);
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void print(long long int r, long long int c) {
for (long long int i = 1; i <= c; i++)
cout << "("
<< "1"
<< "," << i << ")"
<< " ";
for (long long int i = 2; i <= r; i++)
cout << "(" << i << "," << c << ")"
<< " ";
cout << endl;
}
int main() {
long long int i, j, n, m, k, ans = 0;
vector<pair<long long int, pair<long long int, long long int> > > v;
cin >> n >> m >> k;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) v.push_back(make_pair(i + j - 1, make_pair(i, j)));
}
sort(v.begin(), v.end());
for (i = 0; i < k; i++) ans += v[i].first;
cout << ans << endl;
for (i = k - 1; i >= 0; i--) print(v[i].second.first, v[i].second.second);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e3 + 10, MAXS = 1e4 + 10, Mod = 1e9 + 7;
vector<pair<int, pair<int, int> > > v;
long long tot;
void print(int x, int y) {
for (int i = 0; i <= y; i++)
cout << "(" << 1 << "," << i + 1 << ")"
<< " ";
for (int i = 1; i <= x; i++)
cout << "(" << i + 1 << "," << y + 1 << ")"
<< " ";
cout << endl;
}
int main() {
ios::sync_with_stdio(false);
int n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
int d = i + j;
v.push_back(make_pair(d, make_pair(i, j)));
}
sort(v.begin(), v.end());
for (int i = k - 1; i >= 0; i--) tot += v[i].first + 1;
cout << tot << endl;
for (int i = k - 1; i >= 0; i--) print(v[i].second.first, v[i].second.second);
return 0;
}
|
### Prompt
Create a solution in cpp for the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e3 + 10, MAXS = 1e4 + 10, Mod = 1e9 + 7;
vector<pair<int, pair<int, int> > > v;
long long tot;
void print(int x, int y) {
for (int i = 0; i <= y; i++)
cout << "(" << 1 << "," << i + 1 << ")"
<< " ";
for (int i = 1; i <= x; i++)
cout << "(" << i + 1 << "," << y + 1 << ")"
<< " ";
cout << endl;
}
int main() {
ios::sync_with_stdio(false);
int n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
int d = i + j;
v.push_back(make_pair(d, make_pair(i, j)));
}
sort(v.begin(), v.end());
for (int i = k - 1; i >= 0; i--) tot += v[i].first + 1;
cout << tot << endl;
for (int i = k - 1; i >= 0; i--) print(v[i].second.first, v[i].second.second);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
bool used[53][53];
int n, m, k;
struct ans {
int y, x;
};
ans a[50 * 50 + 3][50 + 50 + 3];
int len[50 * 50 + 3];
ans p[53][53];
ans o[50 * 50 + 3];
int head, tail;
int ost = 1;
void bfs() {
int temx, temy;
int tem;
int i;
int x, y;
while (head > tail) {
if (ost == k) return;
x = o[tail].x;
y = o[tail].y;
if (o[tail].x + 1 < m && !used[o[tail].y][o[tail].x + 1]) {
p[o[tail].y][o[tail].x + 1].x = x;
p[o[tail].y][o[tail].x + 1].y = y;
used[o[tail].y][o[tail].x + 1] = true;
o[head] = o[tail];
o[head].x++;
len[ost] = o[head].x + o[head].y + 1;
temx = o[head].x;
temy = o[head].y;
for (i = 0; i < len[ost]; i++) {
a[ost][len[ost] - 1 - i].x = temx;
a[ost][len[ost] - 1 - i].y = temy;
tem = temx;
temx = p[temy][temx].x;
temy = p[temy][tem].y;
}
ost++;
head++;
}
if (ost == k) return;
if (o[tail].y + 1 < n && !used[o[tail].y + 1][o[tail].x]) {
p[o[tail].y + 1][o[tail].x].y = y;
p[o[tail].y + 1][o[tail].x].x = x;
used[o[tail].y + 1][o[tail].x] = true;
o[head] = o[tail];
o[head].y++;
len[ost] = o[head].x + o[head].y + 1;
temx = o[head].x;
temy = o[head].y;
for (i = 0; i < len[ost]; i++) {
a[ost][len[ost] - 1 - i].x = temx;
a[ost][len[ost] - 1 - i].y = temy;
tem = temx;
temx = p[temy][temx].x;
temy = p[temy][tem].y;
}
ost++;
head++;
}
tail++;
}
}
int main() {
int i, j;
scanf("%d %d %d", &n, &m, &k);
a[0][0].x = 0;
a[0][0].y = 0;
len[0] = 1;
used[0][0] = true;
head = 1;
tail = 0;
bfs();
int sum = 0;
for (i = 0; i < k; i++) sum += len[i];
printf("%d\n", sum);
for (i = k - 1; i > -1; i--) {
for (j = 0; j < len[i]; j++)
printf("(%d,%d) ", a[i][j].y + 1, a[i][j].x + 1);
printf("\n");
}
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool used[53][53];
int n, m, k;
struct ans {
int y, x;
};
ans a[50 * 50 + 3][50 + 50 + 3];
int len[50 * 50 + 3];
ans p[53][53];
ans o[50 * 50 + 3];
int head, tail;
int ost = 1;
void bfs() {
int temx, temy;
int tem;
int i;
int x, y;
while (head > tail) {
if (ost == k) return;
x = o[tail].x;
y = o[tail].y;
if (o[tail].x + 1 < m && !used[o[tail].y][o[tail].x + 1]) {
p[o[tail].y][o[tail].x + 1].x = x;
p[o[tail].y][o[tail].x + 1].y = y;
used[o[tail].y][o[tail].x + 1] = true;
o[head] = o[tail];
o[head].x++;
len[ost] = o[head].x + o[head].y + 1;
temx = o[head].x;
temy = o[head].y;
for (i = 0; i < len[ost]; i++) {
a[ost][len[ost] - 1 - i].x = temx;
a[ost][len[ost] - 1 - i].y = temy;
tem = temx;
temx = p[temy][temx].x;
temy = p[temy][tem].y;
}
ost++;
head++;
}
if (ost == k) return;
if (o[tail].y + 1 < n && !used[o[tail].y + 1][o[tail].x]) {
p[o[tail].y + 1][o[tail].x].y = y;
p[o[tail].y + 1][o[tail].x].x = x;
used[o[tail].y + 1][o[tail].x] = true;
o[head] = o[tail];
o[head].y++;
len[ost] = o[head].x + o[head].y + 1;
temx = o[head].x;
temy = o[head].y;
for (i = 0; i < len[ost]; i++) {
a[ost][len[ost] - 1 - i].x = temx;
a[ost][len[ost] - 1 - i].y = temy;
tem = temx;
temx = p[temy][temx].x;
temy = p[temy][tem].y;
}
ost++;
head++;
}
tail++;
}
}
int main() {
int i, j;
scanf("%d %d %d", &n, &m, &k);
a[0][0].x = 0;
a[0][0].y = 0;
len[0] = 1;
used[0][0] = true;
head = 1;
tail = 0;
bfs();
int sum = 0;
for (i = 0; i < k; i++) sum += len[i];
printf("%d\n", sum);
for (i = k - 1; i > -1; i--) {
for (j = 0; j < len[i]; j++)
printf("(%d,%d) ", a[i][j].y + 1, a[i][j].x + 1);
printf("\n");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int mat[55][55];
int dist[55][55];
int X[] = {-1, 0, 1, 0};
int Y[] = {0, 1, 0, -1};
int n, m, k;
bool vis[55][55];
int f_a, f_b;
bool valid(int x, int y) {
if (x >= 1 && x <= n && y >= 1 && y <= m) return true;
return false;
}
void print_path(int a, int b) {
printf("(%d,%d) ", a, b);
if (a < f_a)
print_path(a + 1, b);
else if (b < f_b)
print_path(a, b + 1);
return;
}
int main() {
cin >> n >> m >> k;
memset(dist, -1, sizeof dist);
dist[1][1] = 1;
queue<pair<int, int> > Q;
Q.push(make_pair(1, 1));
while (Q.size() > 0) {
pair<int, int> curr = Q.front();
int a = curr.first;
int b = curr.second;
Q.pop();
for (int i = 0; i < 4; ++i) {
int x = a + X[i];
int y = b + Y[i];
if (valid(x, y) && dist[x][y] == -1) {
dist[x][y] = 1 + dist[a][b];
Q.push(make_pair(x, y));
}
}
}
vector<pair<int, pair<int, int> > > v;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
v.push_back(make_pair(dist[i][j], make_pair(i, j)));
}
}
sort(v.begin(), v.end());
int sum = 0;
for (int i = 0; i < k; ++i) {
sum += v[i].first;
}
cout << sum << "\n";
for (int i = k - 1; i >= 0; --i) {
f_a = v[i].second.first;
f_b = v[i].second.second;
print_path(1, 1);
printf("\n");
}
return 0;
}
|
### Prompt
Create a solution in CPP for the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int mat[55][55];
int dist[55][55];
int X[] = {-1, 0, 1, 0};
int Y[] = {0, 1, 0, -1};
int n, m, k;
bool vis[55][55];
int f_a, f_b;
bool valid(int x, int y) {
if (x >= 1 && x <= n && y >= 1 && y <= m) return true;
return false;
}
void print_path(int a, int b) {
printf("(%d,%d) ", a, b);
if (a < f_a)
print_path(a + 1, b);
else if (b < f_b)
print_path(a, b + 1);
return;
}
int main() {
cin >> n >> m >> k;
memset(dist, -1, sizeof dist);
dist[1][1] = 1;
queue<pair<int, int> > Q;
Q.push(make_pair(1, 1));
while (Q.size() > 0) {
pair<int, int> curr = Q.front();
int a = curr.first;
int b = curr.second;
Q.pop();
for (int i = 0; i < 4; ++i) {
int x = a + X[i];
int y = b + Y[i];
if (valid(x, y) && dist[x][y] == -1) {
dist[x][y] = 1 + dist[a][b];
Q.push(make_pair(x, y));
}
}
}
vector<pair<int, pair<int, int> > > v;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
v.push_back(make_pair(dist[i][j], make_pair(i, j)));
}
}
sort(v.begin(), v.end());
int sum = 0;
for (int i = 0; i < k; ++i) {
sum += v[i].first;
}
cout << sum << "\n";
for (int i = k - 1; i >= 0; --i) {
f_a = v[i].second.first;
f_b = v[i].second.second;
print_path(1, 1);
printf("\n");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
T Bitcnt(T a) {
int sum = 0;
while (a) {
if (a & 1) sum++;
a /= 2;
}
return sum;
}
template <class T>
T Max3(T a, T b, T c) {
return max(a, max(b, c));
}
template <class T>
T Lcm(T a, T b) {
T tmp = __gcd(a, b);
return (a / tmp) * b;
}
template <class T>
T Pow(T a, T b) {
T ans = 1;
T base = a;
while (b) {
if (b & 1) ans = (ans * base);
base = (base * base);
b /= 2;
}
return ans;
}
long long Bigmod(long long a, long long b) {
long long res = 1;
long long pw = a % 1000000007LL;
while (b > 0) {
if (b & 1) res = (res * pw) % 1000000007LL;
pw = (pw * pw) % 1000000007LL;
b /= 2;
}
return res;
}
int a_x[] = {1, -1, 0, 0};
int a_y[] = {0, 0, 1, -1};
long long X, Y;
void extend_euclid(long long a, long long b) {
if (b == 0) {
X = a;
Y = 0;
return;
}
extend_euclid(b, a % b);
long long x, y;
x = Y;
y = X - (a / b) * Y;
X = x;
Y = y;
}
long long inverse_modulo(long long a, long long b) {
extend_euclid(a, b);
return (X + 1000000007LL) % 1000000007LL;
}
void Print(int x, int y) { cout << "(" << x << "," << y << ") "; }
void solve(pair<int, int> start, pair<int, int> en) {
Print(start.first, start.second);
while (start.first != en.first) {
start.first++;
Print(start.first, start.second);
}
while (start.second != en.second) {
start.second++;
Print(start.first, start.second);
}
}
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<pair<int, pair<int, int> > > lst;
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
lst.push_back(make_pair(i + j, make_pair(i, j)));
}
}
sort(lst.begin(), lst.end());
for (int i = 0; i < k; i++) {
ans += lst[i].first - lst[0].first + 1;
}
cout << ans << endl;
for (int i = k - 1; i >= 0; i--) {
solve(lst[0].second, lst[i].second);
cout << endl;
}
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
T Bitcnt(T a) {
int sum = 0;
while (a) {
if (a & 1) sum++;
a /= 2;
}
return sum;
}
template <class T>
T Max3(T a, T b, T c) {
return max(a, max(b, c));
}
template <class T>
T Lcm(T a, T b) {
T tmp = __gcd(a, b);
return (a / tmp) * b;
}
template <class T>
T Pow(T a, T b) {
T ans = 1;
T base = a;
while (b) {
if (b & 1) ans = (ans * base);
base = (base * base);
b /= 2;
}
return ans;
}
long long Bigmod(long long a, long long b) {
long long res = 1;
long long pw = a % 1000000007LL;
while (b > 0) {
if (b & 1) res = (res * pw) % 1000000007LL;
pw = (pw * pw) % 1000000007LL;
b /= 2;
}
return res;
}
int a_x[] = {1, -1, 0, 0};
int a_y[] = {0, 0, 1, -1};
long long X, Y;
void extend_euclid(long long a, long long b) {
if (b == 0) {
X = a;
Y = 0;
return;
}
extend_euclid(b, a % b);
long long x, y;
x = Y;
y = X - (a / b) * Y;
X = x;
Y = y;
}
long long inverse_modulo(long long a, long long b) {
extend_euclid(a, b);
return (X + 1000000007LL) % 1000000007LL;
}
void Print(int x, int y) { cout << "(" << x << "," << y << ") "; }
void solve(pair<int, int> start, pair<int, int> en) {
Print(start.first, start.second);
while (start.first != en.first) {
start.first++;
Print(start.first, start.second);
}
while (start.second != en.second) {
start.second++;
Print(start.first, start.second);
}
}
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<pair<int, pair<int, int> > > lst;
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
lst.push_back(make_pair(i + j, make_pair(i, j)));
}
}
sort(lst.begin(), lst.end());
for (int i = 0; i < k; i++) {
ans += lst[i].first - lst[0].first + 1;
}
cout << ans << endl;
for (int i = k - 1; i >= 0; i--) {
solve(lst[0].second, lst[i].second);
cout << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > path;
bool cmp(pair<int, int> A, pair<int, int> B) {
if (A.first + A.second != B.first + B.second)
return (A.first + A.second < B.first + B.second);
return (A.first < B.first);
}
void make_path(int x, int y) {
path.clear();
for (int i = 0; i < x; i++) path.push_back(make_pair(i, 0));
for (int i = 0; i < y; i++) path.push_back(make_pair(x, i));
path.push_back(make_pair(x, y));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector<pair<int, int> > v;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) v.push_back(make_pair(i, j));
sort(v.begin(), v.end(), cmp);
int pen = 0;
for (int i = 0; i < k; i++) pen += v[i].first + v[i].second + 1;
cout << pen << '\n';
for (int i = k - 1; i >= 0; i--) {
make_path(v[i].first, v[i].second);
for (int j = 0; j < path.size(); j++)
cout << "(" << path[j].first + 1 << "," << path[j].second + 1 << ")"
<< " ";
cout << '\n';
}
return 0;
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > path;
bool cmp(pair<int, int> A, pair<int, int> B) {
if (A.first + A.second != B.first + B.second)
return (A.first + A.second < B.first + B.second);
return (A.first < B.first);
}
void make_path(int x, int y) {
path.clear();
for (int i = 0; i < x; i++) path.push_back(make_pair(i, 0));
for (int i = 0; i < y; i++) path.push_back(make_pair(x, i));
path.push_back(make_pair(x, y));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector<pair<int, int> > v;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) v.push_back(make_pair(i, j));
sort(v.begin(), v.end(), cmp);
int pen = 0;
for (int i = 0; i < k; i++) pen += v[i].first + v[i].second + 1;
cout << pen << '\n';
for (int i = k - 1; i >= 0; i--) {
make_path(v[i].first, v[i].second);
for (int j = 0; j < path.size(); j++)
cout << "(" << path[j].first + 1 << "," << path[j].second + 1 << ")"
<< " ";
cout << '\n';
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > r[55 * 55];
vector<pair<int, int> > gen(int x, int y) {
vector<pair<int, int> > res;
for (int xt = 2, _c = x; xt <= _c; xt++) res.push_back(pair<int, int>(xt, 1));
for (int yt = 2, _c = y; yt <= _c; yt++) res.push_back(pair<int, int>(x, yt));
return res;
}
int main() {
int m, n, k, K;
cin >> m >> n >> k;
K = k;
int res = 0;
for (int l = 1, _c = m + n - 1; l <= _c; l++) {
int x = 1, y = l;
while (y > n) {
++x;
--y;
}
while (y >= 1 && x <= m && k > 0) {
r[k] = gen(x, y);
res += int(r[k].size()) + 1;
--k;
++x;
--y;
}
if (k == 0) break;
}
printf("%d\n", res);
for (int i = 1, _c = K; i <= _c; i++) {
printf("(1,1)");
for (int j = 0, _a = (int(r[i].size())); j < _a; ++j)
printf(" (%d,%d)", r[i][j].first, r[i][j].second);
puts("");
}
return 0;
}
|
### Prompt
Generate a CPP solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > r[55 * 55];
vector<pair<int, int> > gen(int x, int y) {
vector<pair<int, int> > res;
for (int xt = 2, _c = x; xt <= _c; xt++) res.push_back(pair<int, int>(xt, 1));
for (int yt = 2, _c = y; yt <= _c; yt++) res.push_back(pair<int, int>(x, yt));
return res;
}
int main() {
int m, n, k, K;
cin >> m >> n >> k;
K = k;
int res = 0;
for (int l = 1, _c = m + n - 1; l <= _c; l++) {
int x = 1, y = l;
while (y > n) {
++x;
--y;
}
while (y >= 1 && x <= m && k > 0) {
r[k] = gen(x, y);
res += int(r[k].size()) + 1;
--k;
++x;
--y;
}
if (k == 0) break;
}
printf("%d\n", res);
for (int i = 1, _c = K; i <= _c; i++) {
printf("(1,1)");
for (int j = 0, _a = (int(r[i].size())); j < _a; ++j)
printf(" (%d,%d)", r[i][j].first, r[i][j].second);
puts("");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k, u, ans;
vector<pair<int, int> > V[2999];
pair<int, int> s[2999];
int p;
int main() {
scanf("%d %d %d", &n, &m, &k);
for (int i = (2); i <= (n + m); i++)
for (int j = (1); j <= (i - 1); j++) {
int x = j, y = i - j;
if (x <= n && y <= m) {
s[++u] = make_pair(x, y);
ans += x + y - 1;
for (int xx = (1); xx <= (x); xx++) V[u].push_back(make_pair(xx, 1));
for (int yy = (2); yy <= (y); yy++) V[u].push_back(make_pair(x, yy));
if (k == u) goto end;
}
}
end:;
printf("%d\n", ans);
for (int i = (k); i >= (1); i--)
for (int j = (0); j <= ((int)V[i].size() - 1); j++) {
printf("(%d,%d)%c", V[i][j].first, V[i][j].second,
j == (int)V[i].size() - 1 ? '\n' : ' ');
}
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, k, u, ans;
vector<pair<int, int> > V[2999];
pair<int, int> s[2999];
int p;
int main() {
scanf("%d %d %d", &n, &m, &k);
for (int i = (2); i <= (n + m); i++)
for (int j = (1); j <= (i - 1); j++) {
int x = j, y = i - j;
if (x <= n && y <= m) {
s[++u] = make_pair(x, y);
ans += x + y - 1;
for (int xx = (1); xx <= (x); xx++) V[u].push_back(make_pair(xx, 1));
for (int yy = (2); yy <= (y); yy++) V[u].push_back(make_pair(x, yy));
if (k == u) goto end;
}
}
end:;
printf("%d\n", ans);
for (int i = (k); i >= (1); i--)
for (int j = (0); j <= ((int)V[i].size() - 1); j++) {
printf("(%d,%d)%c", V[i][j].first, V[i][j].second,
j == (int)V[i].size() - 1 ? '\n' : ' ');
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k, x, y;
stack<pair<int, int> > can;
int res = 0;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= n + m && k; i++) {
for (int j = 0; j < n + m && k; j++) {
x = i - j;
y = j + 1;
if (x > 0 && x <= n && y > 0 && y <= m) {
can.emplace(x, y);
k--;
res += x + y - 1;
}
}
}
cout << res << '\n';
while (can.size()) {
x = can.top().first, y = can.top().second;
can.pop();
int i = 1, j = 1;
cout << "(" << i << "," << j << ") ";
while (i < x) {
i++;
cout << "(" << i << "," << j << ") ";
}
while (j < y) {
j++;
cout << "(" << i << "," << j << ") ";
}
cout << '\n';
}
return 0;
}
|
### Prompt
Generate a Cpp solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, k, x, y;
stack<pair<int, int> > can;
int res = 0;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= n + m && k; i++) {
for (int j = 0; j < n + m && k; j++) {
x = i - j;
y = j + 1;
if (x > 0 && x <= n && y > 0 && y <= m) {
can.emplace(x, y);
k--;
res += x + y - 1;
}
}
}
cout << res << '\n';
while (can.size()) {
x = can.top().first, y = can.top().second;
can.pop();
int i = 1, j = 1;
cout << "(" << i << "," << j << ") ";
while (i < x) {
i++;
cout << "(" << i << "," << j << ") ";
}
while (j < y) {
j++;
cout << "(" << i << "," << j << ") ";
}
cout << '\n';
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct cd {
int nr, l[50 * 50 + 4], c[50 * 50 + 4];
};
cd r;
cd v[50 * 50 + 1];
int lin[] = {0, 0, 1, -1};
int col[] = {1, -1, 0, 0};
int a[54][54], i, n, sf, penal, k, m, in, j;
int main() {
scanf("%d%d%d", &n, &m, &k);
r.nr = 1;
r.l[1] = 1;
r.c[1] = 1;
in = sf = 1;
v[in] = r;
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++) a[i][j] = 1;
a[1][1] = 0;
penal = 1;
while (in <= sf && sf < k) {
for (i = 0; i < 4 && sf < k; i++) {
if (a[v[in].l[v[in].nr] + lin[i]][v[in].c[v[in].nr] + col[i]] == 1) {
a[v[in].l[v[in].nr] + lin[i]][v[in].c[v[in].nr] + col[i]] = 0;
r = v[in];
r.nr++;
penal += r.nr;
r.l[r.nr] = v[in].l[v[in].nr] + lin[i];
r.c[r.nr] = v[in].c[v[in].nr] + col[i];
sf++;
v[sf] = r;
}
}
in++;
}
printf("%d\n", penal);
for (i = sf; i >= 1; i--) {
for (k = 1; k <= v[i].nr; k++) {
printf("(%d,%d) ", v[i].l[k], v[i].c[k]);
}
printf("\n");
}
return 0;
}
|
### Prompt
Create a solution in CPP for the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct cd {
int nr, l[50 * 50 + 4], c[50 * 50 + 4];
};
cd r;
cd v[50 * 50 + 1];
int lin[] = {0, 0, 1, -1};
int col[] = {1, -1, 0, 0};
int a[54][54], i, n, sf, penal, k, m, in, j;
int main() {
scanf("%d%d%d", &n, &m, &k);
r.nr = 1;
r.l[1] = 1;
r.c[1] = 1;
in = sf = 1;
v[in] = r;
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++) a[i][j] = 1;
a[1][1] = 0;
penal = 1;
while (in <= sf && sf < k) {
for (i = 0; i < 4 && sf < k; i++) {
if (a[v[in].l[v[in].nr] + lin[i]][v[in].c[v[in].nr] + col[i]] == 1) {
a[v[in].l[v[in].nr] + lin[i]][v[in].c[v[in].nr] + col[i]] = 0;
r = v[in];
r.nr++;
penal += r.nr;
r.l[r.nr] = v[in].l[v[in].nr] + lin[i];
r.c[r.nr] = v[in].c[v[in].nr] + col[i];
sf++;
v[sf] = r;
}
}
in++;
}
printf("%d\n", penal);
for (i = sf; i >= 1; i--) {
for (k = 1; k <= v[i].nr; k++) {
printf("(%d,%d) ", v[i].l[k], v[i].c[k]);
}
printf("\n");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
string make(int i, int j) {
return "(" + to_string(i + 1) + "," + to_string(j + 1) + ")";
}
int main() {
int n = 4, m = 4, k = 16;
cin >> n >> m >> k;
vector<vector<string>> dp(n, vector<string>(m, ""));
dp[0][0] = "(1,1)";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int I = i + 1;
int J = j + 1;
if (I < n) {
dp[I][j] = dp[i][j] + make(I, j);
}
if (J < m) {
dp[i][J] = dp[i][j] + make(i, J);
}
}
}
vector<string> result(k);
int tot = 0;
for (int le = 0; le < n + m; le++) {
for (int i = 0; i <= le && k > 0; i++) {
int j = le - i;
if (i < n && j < m) {
result[k - 1] = dp[i][j];
k--;
tot += le + 1;
}
}
}
cout << tot << endl;
for (string i : result) {
cout << i << endl;
}
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
string make(int i, int j) {
return "(" + to_string(i + 1) + "," + to_string(j + 1) + ")";
}
int main() {
int n = 4, m = 4, k = 16;
cin >> n >> m >> k;
vector<vector<string>> dp(n, vector<string>(m, ""));
dp[0][0] = "(1,1)";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int I = i + 1;
int J = j + 1;
if (I < n) {
dp[I][j] = dp[i][j] + make(I, j);
}
if (J < m) {
dp[i][J] = dp[i][j] + make(i, J);
}
}
}
vector<string> result(k);
int tot = 0;
for (int le = 0; le < n + m; le++) {
for (int i = 0; i <= le && k > 0; i++) {
int j = le - i;
if (i < n && j < m) {
result[k - 1] = dp[i][j];
k--;
tot += le + 1;
}
}
}
cout << tot << endl;
for (string i : result) {
cout << i << endl;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, k;
cin >> n >> m >> k;
int p = 0, sum = 0;
int a[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) a[i][j] = 0;
}
for (int i = 0; i < (m - 1) + (n - 1) + 1; i++) {
int q = 0;
if (i >= m) q = i - (m - 1);
for (int v = q; v <= min(i, n - 1); v++) {
if (p < k) {
sum = sum + (v + (i - v) + 1);
a[v][i - v] = 1;
p++;
} else
a[v][i - v] = 0;
}
if (p == k) break;
}
cout << sum << endl;
for (int i = n - 1; i >= 0; i--) {
for (int j = m - 1; j >= 0; j--) {
if (a[i][j] == 1) {
for (int k = 0; k <= i; k++) {
cout << "(" << k + 1 << "," << 1 << ")"
<< " ";
}
for (int l = 1; l <= j; l++) {
cout << "(" << i + 1 << "," << l + 1 << ")"
<< " ";
}
cout << endl;
}
}
}
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, k;
cin >> n >> m >> k;
int p = 0, sum = 0;
int a[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) a[i][j] = 0;
}
for (int i = 0; i < (m - 1) + (n - 1) + 1; i++) {
int q = 0;
if (i >= m) q = i - (m - 1);
for (int v = q; v <= min(i, n - 1); v++) {
if (p < k) {
sum = sum + (v + (i - v) + 1);
a[v][i - v] = 1;
p++;
} else
a[v][i - v] = 0;
}
if (p == k) break;
}
cout << sum << endl;
for (int i = n - 1; i >= 0; i--) {
for (int j = m - 1; j >= 0; j--) {
if (a[i][j] == 1) {
for (int k = 0; k <= i; k++) {
cout << "(" << k + 1 << "," << 1 << ")"
<< " ";
}
for (int l = 1; l <= j; l++) {
cout << "(" << i + 1 << "," << l + 1 << ")"
<< " ";
}
cout << endl;
}
}
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
pair<int, int> p[2505];
bool cmp(pair<int, int> a, pair<int, int> b) {
return a.first + a.second > b.first + b.second;
}
int main() {
cin >> n >> m >> k;
int tot = 0, sum = 0;
for (int i = 2; i <= n + m; i++) {
for (int j = 1; j < i; j++) {
if (i - j > n || j > m) continue;
p[++tot].first = i - j;
p[tot].second = j;
sum += i - 1;
if (tot == k) break;
}
if (tot == k) break;
}
cout << sum << endl;
sort(p + 1, p + tot + 1, cmp);
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= p[i].first; j++) {
cout << '(' << j << ',' << 1 << ')' << ' ';
}
for (int j = 2; j <= p[i].second; j++) {
cout << '(' << p[i].first << ',' << j << ')' << ' ';
}
cout << endl;
}
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
pair<int, int> p[2505];
bool cmp(pair<int, int> a, pair<int, int> b) {
return a.first + a.second > b.first + b.second;
}
int main() {
cin >> n >> m >> k;
int tot = 0, sum = 0;
for (int i = 2; i <= n + m; i++) {
for (int j = 1; j < i; j++) {
if (i - j > n || j > m) continue;
p[++tot].first = i - j;
p[tot].second = j;
sum += i - 1;
if (tot == k) break;
}
if (tot == k) break;
}
cout << sum << endl;
sort(p + 1, p + tot + 1, cmp);
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= p[i].first; j++) {
cout << '(' << j << ',' << 1 << ')' << ' ';
}
for (int j = 2; j <= p[i].second; j++) {
cout << '(' << p[i].first << ',' << j << ')' << ' ';
}
cout << endl;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int INF = 1e9 + 5;
const double PI = acos(-1);
const int X[] = {1, -1, 0, 0};
const int Y[] = {0, 0, 1, -1};
const int mod = 1e9 + 7;
int n, m, k, us[55][55], out[55][55];
vector<pair<int, int>> ans;
void get() {
reverse(ans.begin(), ans.end());
int penalty = 0;
for (int i = 0; i < ans.size(); i++) {
penalty += (ans[i].first - 1) + (ans[i].second - 1);
}
cout << penalty + ans.size() << "\n";
for (int i = 0; i < ans.size(); i++) {
int x = 1, y = 1;
for (; x <= ans[i].first; x++) {
cout << '(' << x << ',' << y << ')' << " ";
out[x][y] = 1;
}
--x;
for (; y <= ans[i].second; y++) {
if (out[x][y]) {
continue;
}
cout << '(' << x << ',' << y << ')' << " ";
}
cout << "\n";
}
}
int main() {
cin >> n >> m >> k;
while (k--) {
int mn = INF, pi, pj;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (us[i][j]) {
continue;
}
if (mn > (i - 1) + (j - 1)) {
mn = (i - 1) + (j - 1);
pi = i;
pj = j;
}
}
}
us[pi][pj] = 1;
ans.push_back({pi, pj});
}
get();
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int INF = 1e9 + 5;
const double PI = acos(-1);
const int X[] = {1, -1, 0, 0};
const int Y[] = {0, 0, 1, -1};
const int mod = 1e9 + 7;
int n, m, k, us[55][55], out[55][55];
vector<pair<int, int>> ans;
void get() {
reverse(ans.begin(), ans.end());
int penalty = 0;
for (int i = 0; i < ans.size(); i++) {
penalty += (ans[i].first - 1) + (ans[i].second - 1);
}
cout << penalty + ans.size() << "\n";
for (int i = 0; i < ans.size(); i++) {
int x = 1, y = 1;
for (; x <= ans[i].first; x++) {
cout << '(' << x << ',' << y << ')' << " ";
out[x][y] = 1;
}
--x;
for (; y <= ans[i].second; y++) {
if (out[x][y]) {
continue;
}
cout << '(' << x << ',' << y << ')' << " ";
}
cout << "\n";
}
}
int main() {
cin >> n >> m >> k;
while (k--) {
int mn = INF, pi, pj;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (us[i][j]) {
continue;
}
if (mn > (i - 1) + (j - 1)) {
mn = (i - 1) + (j - 1);
pi = i;
pj = j;
}
}
}
us[pi][pj] = 1;
ans.push_back({pi, pj});
}
get();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
double eps = 1e-9;
const int INF = 1e9 + 7;
const int MAXN = int(2e5 + 7);
int n, m, k, ans;
vector<pair<int, int> > g;
vector<pair<int, pair<int, int> > > v;
void path(int x, int y) {
for (int i = 1; i <= y; i++) {
printf("(%d,%d) ", 1, i);
}
for (int i = 2; i <= x; i++) {
printf("(%d,%d) ", i, y);
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
v.push_back(make_pair(i + j - 1, make_pair(i, j)));
sort(v.begin(), v.end());
for (int i = 0; k && i < v.size(); i++) {
g.push_back(v[i].second);
ans += v[i].first;
k--;
}
reverse(g.begin(), g.end());
printf("%d\n", ans);
for (int i = 0; i < g.size(); i++) {
path(g[i].first, g[i].second);
printf("\n");
}
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
double eps = 1e-9;
const int INF = 1e9 + 7;
const int MAXN = int(2e5 + 7);
int n, m, k, ans;
vector<pair<int, int> > g;
vector<pair<int, pair<int, int> > > v;
void path(int x, int y) {
for (int i = 1; i <= y; i++) {
printf("(%d,%d) ", 1, i);
}
for (int i = 2; i <= x; i++) {
printf("(%d,%d) ", i, y);
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
v.push_back(make_pair(i + j - 1, make_pair(i, j)));
sort(v.begin(), v.end());
for (int i = 0; k && i < v.size(); i++) {
g.push_back(v[i].second);
ans += v[i].first;
k--;
}
reverse(g.begin(), g.end());
printf("%d\n", ans);
for (int i = 0; i < g.size(); i++) {
path(g[i].first, g[i].second);
printf("\n");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long infLL = 0x3f3f3f3f3f3f3f3fLL;
const int hash_mod = 1000037;
const double eps = 1e-10;
const double pi = acos(-1);
int n, m, k, tot;
vector<pair<int, int> > vec;
void print(int x, int y) {
if (x == 1 && y == 1) {
printf("(%d,%d)", x, y);
return;
}
if (y > 1)
print(x, y - 1);
else
print(x - 1, y);
printf(" (%d,%d)", x, y);
}
void solve() {
for (int d = 1; d <= n + m - 1; ++d) {
for (int i = 1; i <= n; ++i) {
int j = d - i + 1;
if (j < 1 || j > m) continue;
tot += d;
vec.push_back(make_pair(i, j));
if (vec.size() >= k) return;
}
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
solve();
printf("%d\n", tot);
for (int i = vec.size() - 1; i >= 0; --i) {
print(vec[i].first, vec[i].second);
puts("");
}
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long infLL = 0x3f3f3f3f3f3f3f3fLL;
const int hash_mod = 1000037;
const double eps = 1e-10;
const double pi = acos(-1);
int n, m, k, tot;
vector<pair<int, int> > vec;
void print(int x, int y) {
if (x == 1 && y == 1) {
printf("(%d,%d)", x, y);
return;
}
if (y > 1)
print(x, y - 1);
else
print(x - 1, y);
printf(" (%d,%d)", x, y);
}
void solve() {
for (int d = 1; d <= n + m - 1; ++d) {
for (int i = 1; i <= n; ++i) {
int j = d - i + 1;
if (j < 1 || j > m) continue;
tot += d;
vec.push_back(make_pair(i, j));
if (vec.size() >= k) return;
}
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
solve();
printf("%d\n", tot);
for (int i = vec.size() - 1; i >= 0; --i) {
print(vec[i].first, vec[i].second);
puts("");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 10;
string to_str(long long x) {
string res;
while (x) {
res.push_back(char('0' + x % 10));
x /= 10;
}
reverse((res).begin(), (res).end());
return res;
}
string get(pair<long long, long long> x) {
string res = "(1, 1)";
for (long long i = 2; i <= x.first; i++) {
res += " (";
res += to_str(i);
res += ", 1)";
}
for (long long i = 2; i <= x.second; i++) {
res += " (";
res += to_str(x.first);
res += ", ";
res += to_str(i);
res += ")";
}
return res;
}
signed main() {
ios_base::sync_with_stdio(0);
long long n, m, k;
cin >> n >> m >> k;
vector<vector<pair<long long, long long>>> have(n + m);
have[0].push_back({1, 1});
for (long long i = 2; i <= n; i++) {
pair<long long, long long> cur = {i, 1};
while (cur.first >= 1 && cur.second <= m) {
have[i - 1].push_back(cur);
cur.first--;
cur.second++;
}
}
for (long long i = 2; i <= m; i++) {
pair<long long, long long> cur = {n, i};
while (cur.first >= 1 && cur.second <= m) {
have[n + i - 2].push_back(cur);
cur.first--;
cur.second++;
}
}
vector<string> ans;
long long sum = 0;
for (long long i = 0; i < n + m; i++) {
if (k >= (long long)(have[i]).size()) {
for (auto it : have[i]) {
ans.push_back(get(it));
}
k -= (long long)(have[i]).size();
sum += (long long)(have[i]).size() * (i + 1);
} else {
sum += k * (i + 1);
for (long long j = 0; j < k; j++) {
ans.push_back(get(have[i][j]));
}
k = 0;
}
}
cout << sum << '\n';
reverse((ans).begin(), (ans).end());
for (auto it : ans) {
cout << it << '\n';
}
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 10;
string to_str(long long x) {
string res;
while (x) {
res.push_back(char('0' + x % 10));
x /= 10;
}
reverse((res).begin(), (res).end());
return res;
}
string get(pair<long long, long long> x) {
string res = "(1, 1)";
for (long long i = 2; i <= x.first; i++) {
res += " (";
res += to_str(i);
res += ", 1)";
}
for (long long i = 2; i <= x.second; i++) {
res += " (";
res += to_str(x.first);
res += ", ";
res += to_str(i);
res += ")";
}
return res;
}
signed main() {
ios_base::sync_with_stdio(0);
long long n, m, k;
cin >> n >> m >> k;
vector<vector<pair<long long, long long>>> have(n + m);
have[0].push_back({1, 1});
for (long long i = 2; i <= n; i++) {
pair<long long, long long> cur = {i, 1};
while (cur.first >= 1 && cur.second <= m) {
have[i - 1].push_back(cur);
cur.first--;
cur.second++;
}
}
for (long long i = 2; i <= m; i++) {
pair<long long, long long> cur = {n, i};
while (cur.first >= 1 && cur.second <= m) {
have[n + i - 2].push_back(cur);
cur.first--;
cur.second++;
}
}
vector<string> ans;
long long sum = 0;
for (long long i = 0; i < n + m; i++) {
if (k >= (long long)(have[i]).size()) {
for (auto it : have[i]) {
ans.push_back(get(it));
}
k -= (long long)(have[i]).size();
sum += (long long)(have[i]).size() * (i + 1);
} else {
sum += k * (i + 1);
for (long long j = 0; j < k; j++) {
ans.push_back(get(have[i][j]));
}
k = 0;
}
}
cout << sum << '\n';
reverse((ans).begin(), (ans).end());
for (auto it : ans) {
cout << it << '\n';
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
int break_point() {
char c;
while ((c = getchar()) != '\n')
;
return 0;
}
template <typename T>
void read_integer(T &r) {
bool sign = 0;
r = 0;
char c;
while (1) {
c = getchar();
if (c == '-') {
sign = 1;
break;
}
if (c != ' ' && c != '\n') {
r = c - '0';
break;
}
}
while (1) {
c = getchar();
if (c == ' ' || c == '\n') break;
r = r * 10 + (c - '0');
}
if (sign) r = -r;
}
long long binpowmod(long long a, long long b, long long mod) {
if (b == 0) return 1;
long long c = binpowmod(a, b >> 1, mod);
return (((c * c) % mod) * (b & 1 ? a : 1)) % mod;
}
long long binpow(long long a, long long b) {
if (b == 0) return 1;
long long c = binpow(a, b >> 1);
return c * c * (b & 1 ? a : 1);
}
inline int getbit(int x, int b) { return (x >> b) & 1; }
inline int setbit(int x, int b) { return x | (1 << b); }
inline void _setbit(int &x, int b) { x = setbit(x, b); }
inline long long setbit(long long x, int b) { return x | (1ll << b); }
inline void _setbit(long long &x, int b) { x = setbit(x, b); }
inline int unsetbit(int x, int b) { return x & (INT_MAX - (1 << b)); }
inline void _unsetbit(int &x, int b) { x = unsetbit(x, b); }
inline int countbit(int x) {
x = x - ((x >> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
return ((x + (x >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
inline long long countbit(long long x) {
return countbit(int(x & INT_MAX)) + countbit(int(x >> 32) & INT_MAX);
}
inline void printbit(int x, int len) {
for (int i = len - 1; i >= 0; i--) printf("%d", getbit(x, i));
}
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
template <typename A, typename B>
ostream &operator<<(ostream &stream, const pair<A, B> &p) {
stream << "{" << p.first << "," << p.second << "}";
return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const vector<A> &v) {
stream << "[";
for (auto itr = v.begin(); itr != v.end(); itr++) stream << *itr << " ";
stream << "]";
return stream;
}
template <typename A, typename B>
ostream &operator<<(ostream &stream, const map<A, B> &v) {
stream << "[";
for (auto itr = v.begin(); itr != v.end(); itr++) stream << *itr << " ";
stream << "]";
return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const set<A> &v) {
stream << "[";
for (auto itr = v.begin(); itr != v.end(); itr++) stream << *itr << " ";
stream << "]";
return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const stack<A> &v) {
stack<A> st = v;
stream << "[";
while (!st.empty()) {
stream << st.top() << " ";
st.pop();
}
stream << "]";
return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const priority_queue<A> &v) {
priority_queue<A> q = v;
stream << "[";
while (!q.empty()) {
stream << q.top() << " ";
q.pop();
}
stream << "]";
return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const queue<A> &v) {
queue<A> q = v;
stream << "[";
while (!q.empty()) {
stream << q.front() << " ";
q.pop();
}
stream << "]";
return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const deque<A> &v) {
deque<A> q = v;
stream << "[";
while (!q.empty()) {
stream << q.front() << " ";
q.pop_front();
}
stream << "]";
return stream;
}
void run();
int main() {
srand(time(NULL));
do {
run();
if (0) {
0 ? printf("-------------------------------\n") : 0;
0 ? printf("-------------------------------\n") : 0;
}
} while (0);
return 0;
}
int d[55][55];
int p[55][55];
void run() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < 55; ++i) d[i][0] = d[0][i] = 100000;
d[0][1] = 0;
vector<pair<int, int> > v;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1);
p[i][j] = d[i][j] == d[i - 1][j] + 1 ? 1 : 2;
v.push_back({i, j});
}
}
sort(v.begin(), v.end(),
[](const pair<int, int> &a, const pair<int, int> &b) {
return d[a.first][a.second] < d[b.first][b.second];
});
int res = 0;
stack<pair<int, int> > st;
for (int i = 0; i < k; i++) {
res += d[v[i].first][v[i].second];
st.push({-1, -1});
pair<int, int> x = v[i];
while (x.first != 0) {
st.push(x);
x = p[x.first][x.second] == 1 ? std::make_pair(x.first - 1, x.second)
: std::make_pair(x.first, x.second - 1);
}
}
printf("%d\n", res);
while (!st.empty()) {
auto x = st.top();
st.pop();
if (x.first == -1)
putchar('\n');
else
printf("(%d,%d) ", x.first, x.second);
}
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int break_point() {
char c;
while ((c = getchar()) != '\n')
;
return 0;
}
template <typename T>
void read_integer(T &r) {
bool sign = 0;
r = 0;
char c;
while (1) {
c = getchar();
if (c == '-') {
sign = 1;
break;
}
if (c != ' ' && c != '\n') {
r = c - '0';
break;
}
}
while (1) {
c = getchar();
if (c == ' ' || c == '\n') break;
r = r * 10 + (c - '0');
}
if (sign) r = -r;
}
long long binpowmod(long long a, long long b, long long mod) {
if (b == 0) return 1;
long long c = binpowmod(a, b >> 1, mod);
return (((c * c) % mod) * (b & 1 ? a : 1)) % mod;
}
long long binpow(long long a, long long b) {
if (b == 0) return 1;
long long c = binpow(a, b >> 1);
return c * c * (b & 1 ? a : 1);
}
inline int getbit(int x, int b) { return (x >> b) & 1; }
inline int setbit(int x, int b) { return x | (1 << b); }
inline void _setbit(int &x, int b) { x = setbit(x, b); }
inline long long setbit(long long x, int b) { return x | (1ll << b); }
inline void _setbit(long long &x, int b) { x = setbit(x, b); }
inline int unsetbit(int x, int b) { return x & (INT_MAX - (1 << b)); }
inline void _unsetbit(int &x, int b) { x = unsetbit(x, b); }
inline int countbit(int x) {
x = x - ((x >> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
return ((x + (x >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
inline long long countbit(long long x) {
return countbit(int(x & INT_MAX)) + countbit(int(x >> 32) & INT_MAX);
}
inline void printbit(int x, int len) {
for (int i = len - 1; i >= 0; i--) printf("%d", getbit(x, i));
}
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
template <typename A, typename B>
ostream &operator<<(ostream &stream, const pair<A, B> &p) {
stream << "{" << p.first << "," << p.second << "}";
return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const vector<A> &v) {
stream << "[";
for (auto itr = v.begin(); itr != v.end(); itr++) stream << *itr << " ";
stream << "]";
return stream;
}
template <typename A, typename B>
ostream &operator<<(ostream &stream, const map<A, B> &v) {
stream << "[";
for (auto itr = v.begin(); itr != v.end(); itr++) stream << *itr << " ";
stream << "]";
return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const set<A> &v) {
stream << "[";
for (auto itr = v.begin(); itr != v.end(); itr++) stream << *itr << " ";
stream << "]";
return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const stack<A> &v) {
stack<A> st = v;
stream << "[";
while (!st.empty()) {
stream << st.top() << " ";
st.pop();
}
stream << "]";
return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const priority_queue<A> &v) {
priority_queue<A> q = v;
stream << "[";
while (!q.empty()) {
stream << q.top() << " ";
q.pop();
}
stream << "]";
return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const queue<A> &v) {
queue<A> q = v;
stream << "[";
while (!q.empty()) {
stream << q.front() << " ";
q.pop();
}
stream << "]";
return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const deque<A> &v) {
deque<A> q = v;
stream << "[";
while (!q.empty()) {
stream << q.front() << " ";
q.pop_front();
}
stream << "]";
return stream;
}
void run();
int main() {
srand(time(NULL));
do {
run();
if (0) {
0 ? printf("-------------------------------\n") : 0;
0 ? printf("-------------------------------\n") : 0;
}
} while (0);
return 0;
}
int d[55][55];
int p[55][55];
void run() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < 55; ++i) d[i][0] = d[0][i] = 100000;
d[0][1] = 0;
vector<pair<int, int> > v;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1);
p[i][j] = d[i][j] == d[i - 1][j] + 1 ? 1 : 2;
v.push_back({i, j});
}
}
sort(v.begin(), v.end(),
[](const pair<int, int> &a, const pair<int, int> &b) {
return d[a.first][a.second] < d[b.first][b.second];
});
int res = 0;
stack<pair<int, int> > st;
for (int i = 0; i < k; i++) {
res += d[v[i].first][v[i].second];
st.push({-1, -1});
pair<int, int> x = v[i];
while (x.first != 0) {
st.push(x);
x = p[x.first][x.second] == 1 ? std::make_pair(x.first - 1, x.second)
: std::make_pair(x.first, x.second - 1);
}
}
printf("%d\n", res);
while (!st.empty()) {
auto x = st.top();
st.pop();
if (x.first == -1)
putchar('\n');
else
printf("(%d,%d) ", x.first, x.second);
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k, ans = 0;
vector<pair<int, int> > store;
void go(int x, int y) {
if (k == 0) return;
k--;
store.push_back(pair<int, int>(x, y));
ans += abs(x - 1) + abs(y - 1) + 1;
if (x - 1 == 0 || y + 1 == m + 1) return;
go(x - 1, y + 1);
}
void print(int x, int y) {
for (int i = 1; i < x + 1; i++) {
printf("(%d,%d) ", i, 1);
}
for (int i = 2; i < y + 1; i++) printf("(%d,%d) ", x, i);
printf("\n");
}
int main() {
cin >> n >> m >> k;
for (int i = 1; i < n + 1; i++) go(i, 1);
for (int i = 2; i < m + 1; i++) go(n, i);
cout << ans << endl;
for (int i = store.size() - 1; i >= 0; i--) {
print(store[i].first, store[i].second);
}
return 0;
}
|
### Prompt
Please create a solution in Cpp to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, k, ans = 0;
vector<pair<int, int> > store;
void go(int x, int y) {
if (k == 0) return;
k--;
store.push_back(pair<int, int>(x, y));
ans += abs(x - 1) + abs(y - 1) + 1;
if (x - 1 == 0 || y + 1 == m + 1) return;
go(x - 1, y + 1);
}
void print(int x, int y) {
for (int i = 1; i < x + 1; i++) {
printf("(%d,%d) ", i, 1);
}
for (int i = 2; i < y + 1; i++) printf("(%d,%d) ", x, i);
printf("\n");
}
int main() {
cin >> n >> m >> k;
for (int i = 1; i < n + 1; i++) go(i, 1);
for (int i = 2; i < m + 1; i++) go(n, i);
cout << ans << endl;
for (int i = store.size() - 1; i >= 0; i--) {
print(store[i].first, store[i].second);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
vector<pair<int, int>> order;
int total = 0;
vector<vector<pair<int, int>>> ans;
void solve(int i, int j) {
ans.push_back(vector<pair<int, int>>());
for (int x = 0; x <= i; x++) {
ans.back().push_back({x, 0});
}
for (int y = 1; y <= j; y++) {
ans.back().push_back({i, y});
}
total += ans.back().size();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
for (int id = 0, i = 0, j = 0; id < n * m; id++) {
order.push_back({i, j});
i++;
j--;
if (j < 0 || i >= n) {
j = i + j + 1;
i = max(0, j - m + 1);
j = min(m - 1, j);
}
}
while (k--) {
solve(order[k].first, order[k].second);
}
cout << total << "\n";
for (int i = 0; i < ans.size(); i++) {
for (auto [x, y] : ans[i]) {
cout << "(" << x + 1 << "," << y + 1 << ") ";
}
cout << "\n";
}
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
vector<pair<int, int>> order;
int total = 0;
vector<vector<pair<int, int>>> ans;
void solve(int i, int j) {
ans.push_back(vector<pair<int, int>>());
for (int x = 0; x <= i; x++) {
ans.back().push_back({x, 0});
}
for (int y = 1; y <= j; y++) {
ans.back().push_back({i, y});
}
total += ans.back().size();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
for (int id = 0, i = 0, j = 0; id < n * m; id++) {
order.push_back({i, j});
i++;
j--;
if (j < 0 || i >= n) {
j = i + j + 1;
i = max(0, j - m + 1);
j = min(m - 1, j);
}
}
while (k--) {
solve(order[k].first, order[k].second);
}
cout << total << "\n";
for (int i = 0; i < ans.size(); i++) {
for (auto [x, y] : ans[i]) {
cout << "(" << x + 1 << "," << y + 1 << ") ";
}
cout << "\n";
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k, dx[2] = {1, 0}, dy[2] = {0, 1}, cnt, ans;
bool visit[55][55];
struct Node {
int x, y;
Node(){};
Node(int _x, int _y) {
x = _x;
y = _y;
}
} st[11111], q[11111];
int l = 1, r = 1;
inline void bfs() {
q[++r] = Node(1, 1);
while (l <= r) {
Node p = q[++l];
for (int i = 0; i < 2; i++) {
int tx = p.x + dx[i], ty = p.y + dy[i];
if (tx > n || ty > m) continue;
if (!visit[tx][ty]) {
visit[tx][ty] = 1;
q[++r] = Node(tx, ty);
}
}
if (r > k) break;
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
bfs();
while (r > k + 1) r--;
for (int i = r; i >= 2; i--) {
int a = q[i].x, b = q[i].y;
int j = 1, k = 1;
memset(visit, 0, sizeof(visit));
for (; j <= a; j++) {
if (!visit[j][k]) ans++;
visit[j][k] = 1;
}
j--;
for (; k <= b; k++) {
if (!visit[j][k]) ans++;
visit[j][k] = 1;
}
}
printf("%d\n", ans);
for (int i = r; i >= 2; i--) {
int a = q[i].x, b = q[i].y;
int j = 1, k = 1;
memset(visit, 0, sizeof(visit));
for (; j <= a; j++) {
if (!visit[j][k]) printf("(%d,%d) ", j, k);
visit[j][k] = 1;
}
j--;
for (; k <= b; k++) {
if (!visit[j][k]) printf("(%d,%d) ", j, k);
visit[j][k] = 1;
}
printf("\n");
}
return 0;
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, k, dx[2] = {1, 0}, dy[2] = {0, 1}, cnt, ans;
bool visit[55][55];
struct Node {
int x, y;
Node(){};
Node(int _x, int _y) {
x = _x;
y = _y;
}
} st[11111], q[11111];
int l = 1, r = 1;
inline void bfs() {
q[++r] = Node(1, 1);
while (l <= r) {
Node p = q[++l];
for (int i = 0; i < 2; i++) {
int tx = p.x + dx[i], ty = p.y + dy[i];
if (tx > n || ty > m) continue;
if (!visit[tx][ty]) {
visit[tx][ty] = 1;
q[++r] = Node(tx, ty);
}
}
if (r > k) break;
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
bfs();
while (r > k + 1) r--;
for (int i = r; i >= 2; i--) {
int a = q[i].x, b = q[i].y;
int j = 1, k = 1;
memset(visit, 0, sizeof(visit));
for (; j <= a; j++) {
if (!visit[j][k]) ans++;
visit[j][k] = 1;
}
j--;
for (; k <= b; k++) {
if (!visit[j][k]) ans++;
visit[j][k] = 1;
}
}
printf("%d\n", ans);
for (int i = r; i >= 2; i--) {
int a = q[i].x, b = q[i].y;
int j = 1, k = 1;
memset(visit, 0, sizeof(visit));
for (; j <= a; j++) {
if (!visit[j][k]) printf("(%d,%d) ", j, k);
visit[j][k] = 1;
}
j--;
for (; k <= b; k++) {
if (!visit[j][k]) printf("(%d,%d) ", j, k);
visit[j][k] = 1;
}
printf("\n");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct dd {
int x;
int y;
} a[3000];
bool cmp(dd a, dd b) { return a.x + a.y < b.x + b.y; }
int main() {
int n, m, k, i, j, l, h, sum;
scanf("%d%d%d", &n, &m, &k);
h = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
a[h].x = i, a[h].y = j;
h++;
}
}
sort(a, a + h, cmp);
sum = 0;
for (i = 0; i < k; i++) {
sum = sum + a[i].x + a[i].y - 1;
}
printf("%d\n", sum);
for (i = k - 1; i >= 0; i--) {
for (j = 1; j <= a[i].x; j++) printf("(%d,1) ", j);
for (j = 2; j <= a[i].y; j++) printf("(%d,%d) ", a[i].x, j);
printf("\n");
}
}
|
### Prompt
Generate a CPP solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct dd {
int x;
int y;
} a[3000];
bool cmp(dd a, dd b) { return a.x + a.y < b.x + b.y; }
int main() {
int n, m, k, i, j, l, h, sum;
scanf("%d%d%d", &n, &m, &k);
h = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
a[h].x = i, a[h].y = j;
h++;
}
}
sort(a, a + h, cmp);
sum = 0;
for (i = 0; i < k; i++) {
sum = sum + a[i].x + a[i].y - 1;
}
printf("%d\n", sum);
for (i = k - 1; i >= 0; i--) {
for (j = 1; j <= a[i].x; j++) printf("(%d,1) ", j);
for (j = 2; j <= a[i].y; j++) printf("(%d,%d) ", a[i].x, j);
printf("\n");
}
}
```
|
#include <bits/stdc++.h>
int n, m, k;
int np;
int ans;
int rev;
void prt(int x, int y, int op = 0) {
int i, j;
if (op) {
ans += x + y - 1;
return;
}
for (i = 1; i <= y; i++) {
if (rev)
printf("(%d,1) ", i);
else
printf("(1,%d) ", i);
}
for (j = 2; j <= x; j++) {
if (rev)
printf("(%d,%d) ", y, j);
else
printf("(%d,%d) ", j, y);
}
printf("\n");
}
int main() {
int i, j, ii, jj;
scanf("%d%d%d", &n, &m, &k);
if (n > m) {
rev = 1;
int t = n;
n = m;
m = t;
}
if (k > n * m - n * (n - 1) / 2) {
int tt = n * m - k;
for (i = 0; i * (i + 1) / 2 < tt; i++)
;
for (j = n, ii = 1; ii <= i * (i + 1) / 2 - tt; ii++, j--)
prt(j, m - i + 1 + n - j, 1);
for (ii = i + 1; ii <= n - 1; ii++)
for (j = n; j >= 1 && m - ii + 1 + n - j <= m; j--)
prt(j, m - ii + 1 + n - j, 1);
for (i = m; i >= n; i--)
for (j = 1; j <= n; j++) prt(j, i - j + 1, 1);
for (i = n - 1; i >= 1; i--)
for (j = 1; j <= i; j++) prt(j, i - j + 1, 1);
printf("%d\n", ans);
for (i = 0; i * (i + 1) / 2 < tt; i++)
;
for (j = n, ii = 1; ii <= i * (i + 1) / 2 - tt; ii++, j--)
prt(j, m - i + 1 + n - j);
for (ii = i + 1; ii <= n - 1; ii++)
for (j = n; j >= 1 && m - ii + 1 + n - j <= m; j--)
prt(j, m - ii + 1 + n - j);
for (i = m; i >= n; i--)
for (j = 1; j <= n; j++) prt(j, i - j + 1);
for (i = n - 1; i >= 1; i--)
for (j = 1; j <= i; j++) prt(j, i - j + 1);
return 0;
}
if (k > n * (n - 1) / 2) {
int tt = k - n * (n - 1) / 2;
for (i = 1; i <= tt % n; i++) prt(i, tt / n + n - i + 1, 1);
for (i = tt / n + n - 1; i >= n; i--)
for (j = 1; j <= n; j++) prt(j, i - j + 1, 1);
for (i = n - 1; i >= 1; i--)
for (j = 1; j <= i; j++) prt(j, i - j + 1, 1);
printf("%d\n", ans);
for (i = 1; i <= tt % n; i++) prt(i, tt / n + n - i + 1);
for (i = tt / n + n - 1; i >= n; i--)
for (j = 1; j <= n; j++) prt(j, i - j + 1);
for (i = n - 1; i >= 1; i--)
for (j = 1; j <= i; j++) prt(j, i - j + 1);
return 0;
}
for (i = 1; i * (i + 1) / 2 < k; i++)
;
for (j = 1; j <= k - i * (i - 1) / 2; j++) prt(j, i - j + 1, 1);
for (ii = i - 1; ii >= 1; ii--)
for (j = 1; j <= ii; j++) prt(j, ii - j + 1, 1);
printf("%d\n", ans);
for (j = 1; j <= k - i * (i - 1) / 2; j++) prt(j, i - j + 1);
for (ii = i - 1; ii >= 1; ii--)
for (j = 1; j <= ii; j++) prt(j, ii - j + 1);
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
int n, m, k;
int np;
int ans;
int rev;
void prt(int x, int y, int op = 0) {
int i, j;
if (op) {
ans += x + y - 1;
return;
}
for (i = 1; i <= y; i++) {
if (rev)
printf("(%d,1) ", i);
else
printf("(1,%d) ", i);
}
for (j = 2; j <= x; j++) {
if (rev)
printf("(%d,%d) ", y, j);
else
printf("(%d,%d) ", j, y);
}
printf("\n");
}
int main() {
int i, j, ii, jj;
scanf("%d%d%d", &n, &m, &k);
if (n > m) {
rev = 1;
int t = n;
n = m;
m = t;
}
if (k > n * m - n * (n - 1) / 2) {
int tt = n * m - k;
for (i = 0; i * (i + 1) / 2 < tt; i++)
;
for (j = n, ii = 1; ii <= i * (i + 1) / 2 - tt; ii++, j--)
prt(j, m - i + 1 + n - j, 1);
for (ii = i + 1; ii <= n - 1; ii++)
for (j = n; j >= 1 && m - ii + 1 + n - j <= m; j--)
prt(j, m - ii + 1 + n - j, 1);
for (i = m; i >= n; i--)
for (j = 1; j <= n; j++) prt(j, i - j + 1, 1);
for (i = n - 1; i >= 1; i--)
for (j = 1; j <= i; j++) prt(j, i - j + 1, 1);
printf("%d\n", ans);
for (i = 0; i * (i + 1) / 2 < tt; i++)
;
for (j = n, ii = 1; ii <= i * (i + 1) / 2 - tt; ii++, j--)
prt(j, m - i + 1 + n - j);
for (ii = i + 1; ii <= n - 1; ii++)
for (j = n; j >= 1 && m - ii + 1 + n - j <= m; j--)
prt(j, m - ii + 1 + n - j);
for (i = m; i >= n; i--)
for (j = 1; j <= n; j++) prt(j, i - j + 1);
for (i = n - 1; i >= 1; i--)
for (j = 1; j <= i; j++) prt(j, i - j + 1);
return 0;
}
if (k > n * (n - 1) / 2) {
int tt = k - n * (n - 1) / 2;
for (i = 1; i <= tt % n; i++) prt(i, tt / n + n - i + 1, 1);
for (i = tt / n + n - 1; i >= n; i--)
for (j = 1; j <= n; j++) prt(j, i - j + 1, 1);
for (i = n - 1; i >= 1; i--)
for (j = 1; j <= i; j++) prt(j, i - j + 1, 1);
printf("%d\n", ans);
for (i = 1; i <= tt % n; i++) prt(i, tt / n + n - i + 1);
for (i = tt / n + n - 1; i >= n; i--)
for (j = 1; j <= n; j++) prt(j, i - j + 1);
for (i = n - 1; i >= 1; i--)
for (j = 1; j <= i; j++) prt(j, i - j + 1);
return 0;
}
for (i = 1; i * (i + 1) / 2 < k; i++)
;
for (j = 1; j <= k - i * (i - 1) / 2; j++) prt(j, i - j + 1, 1);
for (ii = i - 1; ii >= 1; ii--)
for (j = 1; j <= ii; j++) prt(j, ii - j + 1, 1);
printf("%d\n", ans);
for (j = 1; j <= k - i * (i - 1) / 2; j++) prt(j, i - j + 1);
for (ii = i - 1; ii >= 1; ii--)
for (j = 1; j <= ii; j++) prt(j, ii - j + 1);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = int(1e5) + 5;
const int INF = int(1e9) + 7;
const long long MINF = 1e18;
pair<int, int> p[55][55];
int dis[55][55];
bool used[55][55];
vector<pair<int, pair<int, int> > > x;
void solve() {
int n, m, k;
cin >> n >> m >> k;
queue<pair<int, int> > q;
q.push(make_pair(1, 1));
used[1][1] = 1;
while (q.size() != 0) {
pair<int, int> pos = q.front();
x.push_back(make_pair(dis[pos.first][pos.second], pos));
q.pop();
if (pos.first + 1 <= n && used[pos.first + 1][pos.second] == 0) {
used[pos.first + 1][pos.second] = 1;
q.push(make_pair(pos.first + 1, pos.second));
dis[pos.first + 1][pos.second] = dis[pos.first][pos.second] + 1;
p[pos.first + 1][pos.second] = pos;
}
if (pos.second + 1 <= m && used[pos.first][pos.second + 1] == 0) {
used[pos.first][pos.second + 1] = 1;
q.push(make_pair(pos.first, pos.second + 1));
dis[pos.first][pos.second + 1] = dis[pos.first][pos.second] + 1;
p[pos.first][pos.second + 1] = pos;
}
}
sort(x.begin(), x.end());
int sum = 0;
for (int i = 0; i < k; i++) {
sum += x[i].first + 1;
}
cout << sum << endl;
for (int i = k - 1; i >= 0; i--) {
pair<int, int> pos = x[i].second;
vector<pair<int, int> > ans;
while (pos.first != 1 || pos.second != 1) {
ans.push_back(pos);
pos = p[pos.first][pos.second];
}
cout << "(1,1)";
for (int i = ans.size() - 1; i >= 0; i--) {
cout << " "
<< "(" << ans[i].first << "," << ans[i].second << ")";
}
cout << endl;
}
}
int main() {
ios_base::sync_with_stdio(0);
int TT = 1;
while (TT--) {
solve();
}
return 0;
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = int(1e5) + 5;
const int INF = int(1e9) + 7;
const long long MINF = 1e18;
pair<int, int> p[55][55];
int dis[55][55];
bool used[55][55];
vector<pair<int, pair<int, int> > > x;
void solve() {
int n, m, k;
cin >> n >> m >> k;
queue<pair<int, int> > q;
q.push(make_pair(1, 1));
used[1][1] = 1;
while (q.size() != 0) {
pair<int, int> pos = q.front();
x.push_back(make_pair(dis[pos.first][pos.second], pos));
q.pop();
if (pos.first + 1 <= n && used[pos.first + 1][pos.second] == 0) {
used[pos.first + 1][pos.second] = 1;
q.push(make_pair(pos.first + 1, pos.second));
dis[pos.first + 1][pos.second] = dis[pos.first][pos.second] + 1;
p[pos.first + 1][pos.second] = pos;
}
if (pos.second + 1 <= m && used[pos.first][pos.second + 1] == 0) {
used[pos.first][pos.second + 1] = 1;
q.push(make_pair(pos.first, pos.second + 1));
dis[pos.first][pos.second + 1] = dis[pos.first][pos.second] + 1;
p[pos.first][pos.second + 1] = pos;
}
}
sort(x.begin(), x.end());
int sum = 0;
for (int i = 0; i < k; i++) {
sum += x[i].first + 1;
}
cout << sum << endl;
for (int i = k - 1; i >= 0; i--) {
pair<int, int> pos = x[i].second;
vector<pair<int, int> > ans;
while (pos.first != 1 || pos.second != 1) {
ans.push_back(pos);
pos = p[pos.first][pos.second];
}
cout << "(1,1)";
for (int i = ans.size() - 1; i >= 0; i--) {
cout << " "
<< "(" << ans[i].first << "," << ans[i].second << ")";
}
cout << endl;
}
}
int main() {
ios_base::sync_with_stdio(0);
int TT = 1;
while (TT--) {
solve();
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3000;
struct node {
int x, y;
int dis;
};
node p[N];
bool cmp(node a, node b) {
if (a.dis != b.dis)
return a.dis < b.dis;
else if (a.x != b.x)
return a.x < b.x;
return a.y < b.y;
}
int main() {
int n, m, k;
while (cin >> n >> m >> k) {
int cont = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
p[cont].x = i;
p[cont].y = j;
p[cont].dis = j + i - 1;
cont++;
}
}
sort(p, p + cont, cmp);
int sum = 0;
for (int i = 0; i < k; i++) sum += p[i].dis;
cout << sum << endl;
for (int i = k - 1; i >= 0; i--) {
for (int xx = 1; xx <= p[i].x; xx++) printf("(%d,%d) ", xx, 1);
for (int yy = 2; yy <= p[i].y; yy++) printf("(%d,%d) ", p[i].x, yy);
puts("");
}
}
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 3000;
struct node {
int x, y;
int dis;
};
node p[N];
bool cmp(node a, node b) {
if (a.dis != b.dis)
return a.dis < b.dis;
else if (a.x != b.x)
return a.x < b.x;
return a.y < b.y;
}
int main() {
int n, m, k;
while (cin >> n >> m >> k) {
int cont = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
p[cont].x = i;
p[cont].y = j;
p[cont].dis = j + i - 1;
cont++;
}
}
sort(p, p + cont, cmp);
int sum = 0;
for (int i = 0; i < k; i++) sum += p[i].dis;
cout << sum << endl;
for (int i = k - 1; i >= 0; i--) {
for (int xx = 1; xx <= p[i].x; xx++) printf("(%d,%d) ", xx, 1);
for (int yy = 2; yy <= p[i].y; yy++) printf("(%d,%d) ", p[i].x, yy);
puts("");
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int N, M, K;
vector<pair<int, int> > V;
void load() { scanf("%d%d%d", &N, &M, &K); }
bool cmp(const pair<int, int> &A, const pair<int, int> &B) {
if (A.first + A.second != B.first + B.second)
return A.first + A.second < B.first + B.second;
return A.first < B.first;
}
void output(int x, int y) {
for (int i = 0; i <= x; i++) printf("(%d, %d) ", i + 1, 1);
for (int i = 1; i <= y; i++) printf("(%d, %d) ", x + 1, i + 1);
puts("");
}
void solve() {
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++) V.push_back(pair<int, int>(i, j));
sort(V.begin(), V.end(), cmp);
int sol = 0;
for (int i = K - 1; i >= 0; i--) sol += V[i].first + V[i].second + 1;
printf("%d\n", sol);
for (int i = K - 1; i >= 0; i--) output(V[i].first, V[i].second);
}
int main() {
load();
solve();
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int N, M, K;
vector<pair<int, int> > V;
void load() { scanf("%d%d%d", &N, &M, &K); }
bool cmp(const pair<int, int> &A, const pair<int, int> &B) {
if (A.first + A.second != B.first + B.second)
return A.first + A.second < B.first + B.second;
return A.first < B.first;
}
void output(int x, int y) {
for (int i = 0; i <= x; i++) printf("(%d, %d) ", i + 1, 1);
for (int i = 1; i <= y; i++) printf("(%d, %d) ", x + 1, i + 1);
puts("");
}
void solve() {
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++) V.push_back(pair<int, int>(i, j));
sort(V.begin(), V.end(), cmp);
int sol = 0;
for (int i = K - 1; i >= 0; i--) sol += V[i].first + V[i].second + 1;
printf("%d\n", sol);
for (int i = K - 1; i >= 0; i--) output(V[i].first, V[i].second);
}
int main() {
load();
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long inf = 0xc0c0c0c0c0c0c0c0;
const double pi = acos(-1);
void show(int x, int y) {
for (int i = 1; i <= x; i++) printf("(%d,1) ", i);
for (int i = 2; i <= y; i++) printf("(%d,%d) ", x, i);
puts("");
}
int Mp[55][55];
int main() {
int N, M, K;
scanf("%d%d%d", &N, &M, &K);
int sum = 0;
for (int i = 2; i <= M + N; i++) {
for (int j = 1; j <= N; j++) {
int k = i - j;
if (1 <= k && k <= M) {
if (K) {
sum += i - 1;
K--;
Mp[j][k] = 1;
}
}
}
}
printf("%d\n", sum);
for (int i = N; i; i--)
for (int j = M; j; j--)
if (Mp[i][j]) show(i, j);
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long inf = 0xc0c0c0c0c0c0c0c0;
const double pi = acos(-1);
void show(int x, int y) {
for (int i = 1; i <= x; i++) printf("(%d,1) ", i);
for (int i = 2; i <= y; i++) printf("(%d,%d) ", x, i);
puts("");
}
int Mp[55][55];
int main() {
int N, M, K;
scanf("%d%d%d", &N, &M, &K);
int sum = 0;
for (int i = 2; i <= M + N; i++) {
for (int j = 1; j <= N; j++) {
int k = i - j;
if (1 <= k && k <= M) {
if (K) {
sum += i - 1;
K--;
Mp[j][k] = 1;
}
}
}
}
printf("%d\n", sum);
for (int i = N; i; i--)
for (int j = M; j; j--)
if (Mp[i][j]) show(i, j);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
stack<int> q;
int main() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
int tep = k;
int i = 0;
int j;
int all = 0;
q.push(0);
for (j = 1; tep > 0; j++) {
if (j <= max(n, m)) {
q.push(min(tep, min(i + 1, min(n, m))));
i = q.top();
all += i * j;
tep = tep - i;
} else {
i = min(tep, i - 1);
if (i != 0) q.push(i);
all += i * j;
tep = tep - i;
}
}
printf("%d\n", all);
int now = q.top();
q.pop();
if (m >= n) {
while (now != 0) {
for (int k = 1; k <= now; k++) {
int step = 1;
for (int w = 1; w < (j - 1 > max(n, m) ? max(n, m) - k + 1 : k); w++) {
printf("(%d,%d) ", w, 1);
step++;
}
for (int g = 1; g <= j - step; g++) {
printf("(%d,%d) ", (j - 1 > max(n, m) ? max(n, m) - k + 1 : k), g);
}
printf("\n");
}
j--;
now = q.top();
q.pop();
}
} else {
while (now != 0) {
for (int k = 1; k <= now; k++) {
int step = 1;
for (int w = 1; w < (j - 1 > max(n, m) ? max(n, m) - k + 1 : k); w++) {
printf("(%d,%d) ", 1, w);
step++;
}
for (int g = 1; g <= j - step; g++) {
printf("(%d,%d) ", g, (j - 1 > max(n, m) ? max(n, m) - k + 1 : k));
}
printf("\n");
}
j--;
now = q.top();
q.pop();
}
}
}
|
### Prompt
Generate a CPP solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
stack<int> q;
int main() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
int tep = k;
int i = 0;
int j;
int all = 0;
q.push(0);
for (j = 1; tep > 0; j++) {
if (j <= max(n, m)) {
q.push(min(tep, min(i + 1, min(n, m))));
i = q.top();
all += i * j;
tep = tep - i;
} else {
i = min(tep, i - 1);
if (i != 0) q.push(i);
all += i * j;
tep = tep - i;
}
}
printf("%d\n", all);
int now = q.top();
q.pop();
if (m >= n) {
while (now != 0) {
for (int k = 1; k <= now; k++) {
int step = 1;
for (int w = 1; w < (j - 1 > max(n, m) ? max(n, m) - k + 1 : k); w++) {
printf("(%d,%d) ", w, 1);
step++;
}
for (int g = 1; g <= j - step; g++) {
printf("(%d,%d) ", (j - 1 > max(n, m) ? max(n, m) - k + 1 : k), g);
}
printf("\n");
}
j--;
now = q.top();
q.pop();
}
} else {
while (now != 0) {
for (int k = 1; k <= now; k++) {
int step = 1;
for (int w = 1; w < (j - 1 > max(n, m) ? max(n, m) - k + 1 : k); w++) {
printf("(%d,%d) ", 1, w);
step++;
}
for (int g = 1; g <= j - step; g++) {
printf("(%d,%d) ", g, (j - 1 > max(n, m) ? max(n, m) - k + 1 : k));
}
printf("\n");
}
j--;
now = q.top();
q.pop();
}
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int mk[55][55];
vector<int> vx, vy, vz;
void to(int x, int y) {
int i = 1, j = 1;
printf("(1,1) ");
while (i != x) {
printf("(%d,%d) ", ++i, j);
}
while (j != y) {
printf("(%d,%d) ", i, ++j);
}
printf("\n");
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int t = 2; t <= n + m; ++t)
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (i + j == t) {
vx.push_back(i);
vy.push_back(j);
vz.push_back(t);
}
}
}
int sum = 0;
for (int i = k - 1; i >= 0; --i) {
sum += vz[i] - 1;
}
printf("%d\n", sum);
for (int i = k - 1; i >= 0; --i) {
to(vx[i], vy[i]);
}
return 0;
}
|
### Prompt
Generate a Cpp solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int mk[55][55];
vector<int> vx, vy, vz;
void to(int x, int y) {
int i = 1, j = 1;
printf("(1,1) ");
while (i != x) {
printf("(%d,%d) ", ++i, j);
}
while (j != y) {
printf("(%d,%d) ", i, ++j);
}
printf("\n");
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int t = 2; t <= n + m; ++t)
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (i + j == t) {
vx.push_back(i);
vy.push_back(j);
vz.push_back(t);
}
}
}
int sum = 0;
for (int i = k - 1; i >= 0; --i) {
sum += vz[i] - 1;
}
printf("%d\n", sum);
for (int i = k - 1; i >= 0; --i) {
to(vx[i], vy[i]);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool rd(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) pt(x / 10);
putchar(x % 10 + '0');
}
const int N = 55;
const int inf = 1e9;
const double eps = 1e-4;
int n, m, k;
struct Node {
int x, y, v;
bool operator<(const Node &a) const { return v < a.v; }
};
void work(Node a) {
for (int i = 1; i <= a.x; i++) printf("(%d,1) ", i);
for (int i = 2; i <= a.y; i++) printf("(%d,%d) ", a.x, i);
puts("");
}
vector<Node> s;
int main() {
rd(n);
rd(m);
rd(k);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
s.push_back({i, j, i + j});
}
sort(s.begin(), s.end());
s.erase(s.begin() + k, s.end());
int ans = 0;
for (auto i : s) ans += i.v - 1;
cout << ans << endl;
reverse(s.begin(), s.end());
for (auto i : s) work(i);
return 0;
}
|
### Prompt
In cpp, your task is to solve the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool rd(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) pt(x / 10);
putchar(x % 10 + '0');
}
const int N = 55;
const int inf = 1e9;
const double eps = 1e-4;
int n, m, k;
struct Node {
int x, y, v;
bool operator<(const Node &a) const { return v < a.v; }
};
void work(Node a) {
for (int i = 1; i <= a.x; i++) printf("(%d,1) ", i);
for (int i = 2; i <= a.y; i++) printf("(%d,%d) ", a.x, i);
puts("");
}
vector<Node> s;
int main() {
rd(n);
rd(m);
rd(k);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
s.push_back({i, j, i + j});
}
sort(s.begin(), s.end());
s.erase(s.begin() + k, s.end());
int ans = 0;
for (auto i : s) ans += i.v - 1;
cout << ans << endl;
reverse(s.begin(), s.end());
for (auto i : s) work(i);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct Point {
int dist, x, y;
operator int() const { return dist; }
void out() { printf("(%d,%d) ", x, y); }
};
int n, m, k;
vector<Point> p;
void EXEC() {
cin >> n >> m >> k;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) p.push_back({i + j - 1, i, j});
partial_sort(p.begin(), p.begin() + k, p.end(), less<int>());
p.erase(p.begin() + k, p.end());
reverse(p.begin(), p.end());
cout << accumulate(p.begin(), p.end(), 0) << endl;
for (auto i : p) {
Point t = {0, 1, 1};
t.out();
while (t.x < i.x) {
++t.x;
t.out();
}
while (t.y < i.y) {
++t.y;
t.out();
}
cout << endl;
}
}
int main() {
EXEC();
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct Point {
int dist, x, y;
operator int() const { return dist; }
void out() { printf("(%d,%d) ", x, y); }
};
int n, m, k;
vector<Point> p;
void EXEC() {
cin >> n >> m >> k;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) p.push_back({i + j - 1, i, j});
partial_sort(p.begin(), p.begin() + k, p.end(), less<int>());
p.erase(p.begin() + k, p.end());
reverse(p.begin(), p.end());
cout << accumulate(p.begin(), p.end(), 0) << endl;
for (auto i : p) {
Point t = {0, 1, 1};
t.out();
while (t.x < i.x) {
++t.x;
t.out();
}
while (t.y < i.y) {
++t.y;
t.out();
}
cout << endl;
}
}
int main() {
EXEC();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int K = 52;
const int N = 100010;
const int M = 1000000007;
struct tup {
int x, y, c;
};
bool cmp(tup a, tup b) { return a.c < b.c; }
tup v[N];
int n, m, k;
int main() {
ios::sync_with_stdio(0);
cin >> n >> m >> k;
int co = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
v[++co].c = i + j - 1;
v[co].x = i;
v[co].y = j;
}
sort(v + 1, v + co + 1, cmp);
int ans = 0;
for (int i = 1; i <= k; ++i) ans += v[i].c;
cout << ans << '\n';
for (int i = k; i >= 1; --i) {
int x = 1, y = 1;
for (; x <= v[i].x; ++x) cout << '(' << x << ',' << "1) ";
++y;
for (; y <= v[i].y; ++y) cout << '(' << v[i].x << ',' << y << ") ";
cout << '\n';
}
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int K = 52;
const int N = 100010;
const int M = 1000000007;
struct tup {
int x, y, c;
};
bool cmp(tup a, tup b) { return a.c < b.c; }
tup v[N];
int n, m, k;
int main() {
ios::sync_with_stdio(0);
cin >> n >> m >> k;
int co = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
v[++co].c = i + j - 1;
v[co].x = i;
v[co].y = j;
}
sort(v + 1, v + co + 1, cmp);
int ans = 0;
for (int i = 1; i <= k; ++i) ans += v[i].c;
cout << ans << '\n';
for (int i = k; i >= 1; --i) {
int x = 1, y = 1;
for (; x <= v[i].x; ++x) cout << '(' << x << ',' << "1) ";
++y;
for (; y <= v[i].y; ++y) cout << '(' << v[i].x << ',' << y << ") ";
cout << '\n';
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxN = 60;
int g[maxN][maxN], dist[maxN][maxN], t, n, m, k, cost;
pair<int, int> dad[maxN][maxN];
vector<pair<int, int> > path;
vector<pair<int, int> > ans;
queue<pair<int, int> > q;
void bfs() {
q.push(make_pair(1, 1));
dist[1][1] = 0;
dad[1][1] = make_pair(-1, -1);
while (!q.empty() && t < k) {
int x = q.front().first, y = q.front().second;
q.pop();
t++;
ans.push_back(make_pair(x, y));
cost += dist[x][y] + 1;
for (int i = -1; i < 2; i++)
for (int j = -1; j < 2; j++)
if (bool(i) ^ bool(j))
if (!(min(x + i, y + j) < 1 || x + i > n || y + j > m))
if (dist[x + i][y + j] == 0 && (x + i != 1 || y + j != 1)) {
dist[x + i][y + j] = dist[x][y] + 1;
dad[x + i][y + j] = make_pair(x, y);
q.push(make_pair(x + i, y + j));
}
}
reverse(ans.begin(), ans.end());
}
void getpath(int x, int y) {
path.clear();
while (dad[x][y] != make_pair(-1, -1)) {
path.push_back(make_pair(x, y));
int lx = x;
x = dad[x][y].first;
y = dad[lx][y].second;
}
path.push_back(make_pair(1, 1));
reverse(path.begin(), path.end());
}
int main() {
cin >> n >> m >> k;
bfs();
cout << cost << endl;
for (int i = 0; i < t; i++) {
getpath(ans[i].first, ans[i].second);
for (int i = 0; i < path.size(); i++)
cout << " (" << path[i].first << "," << path[i].second << ")";
cout << endl;
}
}
|
### Prompt
Create a solution in CPP for the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxN = 60;
int g[maxN][maxN], dist[maxN][maxN], t, n, m, k, cost;
pair<int, int> dad[maxN][maxN];
vector<pair<int, int> > path;
vector<pair<int, int> > ans;
queue<pair<int, int> > q;
void bfs() {
q.push(make_pair(1, 1));
dist[1][1] = 0;
dad[1][1] = make_pair(-1, -1);
while (!q.empty() && t < k) {
int x = q.front().first, y = q.front().second;
q.pop();
t++;
ans.push_back(make_pair(x, y));
cost += dist[x][y] + 1;
for (int i = -1; i < 2; i++)
for (int j = -1; j < 2; j++)
if (bool(i) ^ bool(j))
if (!(min(x + i, y + j) < 1 || x + i > n || y + j > m))
if (dist[x + i][y + j] == 0 && (x + i != 1 || y + j != 1)) {
dist[x + i][y + j] = dist[x][y] + 1;
dad[x + i][y + j] = make_pair(x, y);
q.push(make_pair(x + i, y + j));
}
}
reverse(ans.begin(), ans.end());
}
void getpath(int x, int y) {
path.clear();
while (dad[x][y] != make_pair(-1, -1)) {
path.push_back(make_pair(x, y));
int lx = x;
x = dad[x][y].first;
y = dad[lx][y].second;
}
path.push_back(make_pair(1, 1));
reverse(path.begin(), path.end());
}
int main() {
cin >> n >> m >> k;
bfs();
cout << cost << endl;
for (int i = 0; i < t; i++) {
getpath(ans[i].first, ans[i].second);
for (int i = 0; i < path.size(); i++)
cout << " (" << path[i].first << "," << path[i].second << ")";
cout << endl;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k, anscnt;
vector<vector<pair<int, int> > > ans;
vector<pair<int, pair<int, int> > > all;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
all.push_back(make_pair(i + j, make_pair(i, j)));
sort(all.begin(), all.end());
for (auto cel : all) {
if (!k) break;
k--;
pair<int, int> cell = cel.second;
vector<pair<int, int> > vec;
for (int i = 1; i <= cell.first; i++) vec.push_back(make_pair(i, 1));
for (int j = 2; j <= cell.second; j++)
vec.push_back(make_pair(cell.first, j));
ans.push_back(vec);
anscnt += ((int)(vec).size());
}
reverse(ans.begin(), ans.end());
cout << anscnt << '\n';
for (auto vec : ans) {
for (auto cell : vec)
cout << '(' << cell.first << ',' << cell.second << ')' << ' ';
cout << '\n';
}
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, k, anscnt;
vector<vector<pair<int, int> > > ans;
vector<pair<int, pair<int, int> > > all;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
all.push_back(make_pair(i + j, make_pair(i, j)));
sort(all.begin(), all.end());
for (auto cel : all) {
if (!k) break;
k--;
pair<int, int> cell = cel.second;
vector<pair<int, int> > vec;
for (int i = 1; i <= cell.first; i++) vec.push_back(make_pair(i, 1));
for (int j = 2; j <= cell.second; j++)
vec.push_back(make_pair(cell.first, j));
ans.push_back(vec);
anscnt += ((int)(vec).size());
}
reverse(ans.begin(), ans.end());
cout << anscnt << '\n';
for (auto vec : ans) {
for (auto cell : vec)
cout << '(' << cell.first << ',' << cell.second << ')' << ' ';
cout << '\n';
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100;
int n, m, k, ans = 0, a, b, x, y;
vector<pair<int, pair<int, int> > > q;
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) q.push_back(make_pair(i + j, make_pair(i, j)));
sort(q.begin(), q.end());
for (int i = k - 1; i >= 0; i--) ans += q[i].first + 1;
printf("%d\n", ans);
for (int i = k - 1; i >= 0; i--) {
a = q[i].second.first + 1;
b = q[i].second.second + 1;
x = y = 1;
printf("(1,1) ");
while (x < a) x++, printf("(%d,%d) ", x, y);
while (y < b) y++, printf("(%d,%d) ", x, y);
puts("");
}
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 100;
int n, m, k, ans = 0, a, b, x, y;
vector<pair<int, pair<int, int> > > q;
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) q.push_back(make_pair(i + j, make_pair(i, j)));
sort(q.begin(), q.end());
for (int i = k - 1; i >= 0; i--) ans += q[i].first + 1;
printf("%d\n", ans);
for (int i = k - 1; i >= 0; i--) {
a = q[i].second.first + 1;
b = q[i].second.second + 1;
x = y = 1;
printf("(1,1) ");
while (x < a) x++, printf("(%d,%d) ", x, y);
while (y < b) y++, printf("(%d,%d) ", x, y);
puts("");
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
inline long long int gcd(long long int a, long long int b) {
return b ? gcd(b, a % b) : a;
}
inline long long int lcm(long long int a, long long int b) {
return a * b / gcd(a, b);
}
inline long long int add(long long int a, long long int b) {
return ((a % MOD) + (b % MOD)) % MOD;
}
inline long long int multi(long long int a, long long int b) {
return ((a % MOD) * (b % MOD)) % MOD;
}
inline long long int sub(long long int a, long long int b) {
a %= MOD;
b %= MOD;
a -= b;
if (a < 0) a += MOD;
return a;
}
inline long long int power(long long int a, long long int b) {
a %= MOD;
long long int res = 1;
while (b > 0) {
if (b & 1) {
res = multi(res, a);
}
a = multi(a, a);
b >>= 1;
}
return res;
}
bool isPrime(long long int x) {
if (x <= 4 || x % 2 == 0 || x % 3 == 0) {
return x == 2 || x == 3;
}
for (long long int i = 5; i * i <= x; i += 6) {
if (x % i == 0 || x % (i + 2) == 0) {
return 0;
}
}
return 1;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int n, m, k, i, j, lj, ans(0);
cin >> n >> m >> k;
vector<pair<long long int, long long int> > q;
bool f = 0;
if (n > m) {
swap(n, m);
f = 1;
}
i = 1, j = 1, lj = 1;
;
while (k--) {
if (j <= m) {
q.push_back({i, j});
ans += (i + j - 1);
} else
k++;
i++;
j--;
if (i == n + 1 || j == 0) {
i = 1;
j = lj + 1;
lj++;
}
}
cout << ans << endl;
while (!q.empty()) {
long long int x = q.back().first;
long long int y = q.back().second;
q.pop_back();
if (f) swap(x, y);
i = 1, j = 1;
while (i < x) {
cout << "(" << i << "," << j << ") ";
i++;
}
while (j <= y) {
cout << "(" << i << "," << j << ") ";
j++;
}
cout << endl;
}
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
inline long long int gcd(long long int a, long long int b) {
return b ? gcd(b, a % b) : a;
}
inline long long int lcm(long long int a, long long int b) {
return a * b / gcd(a, b);
}
inline long long int add(long long int a, long long int b) {
return ((a % MOD) + (b % MOD)) % MOD;
}
inline long long int multi(long long int a, long long int b) {
return ((a % MOD) * (b % MOD)) % MOD;
}
inline long long int sub(long long int a, long long int b) {
a %= MOD;
b %= MOD;
a -= b;
if (a < 0) a += MOD;
return a;
}
inline long long int power(long long int a, long long int b) {
a %= MOD;
long long int res = 1;
while (b > 0) {
if (b & 1) {
res = multi(res, a);
}
a = multi(a, a);
b >>= 1;
}
return res;
}
bool isPrime(long long int x) {
if (x <= 4 || x % 2 == 0 || x % 3 == 0) {
return x == 2 || x == 3;
}
for (long long int i = 5; i * i <= x; i += 6) {
if (x % i == 0 || x % (i + 2) == 0) {
return 0;
}
}
return 1;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int n, m, k, i, j, lj, ans(0);
cin >> n >> m >> k;
vector<pair<long long int, long long int> > q;
bool f = 0;
if (n > m) {
swap(n, m);
f = 1;
}
i = 1, j = 1, lj = 1;
;
while (k--) {
if (j <= m) {
q.push_back({i, j});
ans += (i + j - 1);
} else
k++;
i++;
j--;
if (i == n + 1 || j == 0) {
i = 1;
j = lj + 1;
lj++;
}
}
cout << ans << endl;
while (!q.empty()) {
long long int x = q.back().first;
long long int y = q.back().second;
q.pop_back();
if (f) swap(x, y);
i = 1, j = 1;
while (i < x) {
cout << "(" << i << "," << j << ") ";
i++;
}
while (j <= y) {
cout << "(" << i << "," << j << ") ";
j++;
}
cout << endl;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
void print(int x, int y) {
if (x == 1 && y == 1) {
printf("(%d,%d)", 1, 1);
return;
}
if (x > 1)
print(x - 1, y);
else
print(x, y - 1);
printf(" (%d,%d)", x, y);
}
int main() {
vector<pair<int, int> > pp;
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for (int i = (1); i < (n + 1); ++i)
for (int j = (1); j < (m + 1); ++j) {
pp.push_back(make_pair(i + j, j));
}
sort((pp).begin(), (pp).end());
int an = 0;
for (int i = 0; i < k; i++) an += pp[i].first - 1;
printf("%d\n", an);
for (int i = k - 1; i >= 0; i--) {
print(pp[i].first - pp[i].second, pp[i].second);
puts("");
}
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).
Output
In the first line print an integer — Inna's minimum penalty in the game.
In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.
Examples
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note
Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void print(int x, int y) {
if (x == 1 && y == 1) {
printf("(%d,%d)", 1, 1);
return;
}
if (x > 1)
print(x - 1, y);
else
print(x, y - 1);
printf(" (%d,%d)", x, y);
}
int main() {
vector<pair<int, int> > pp;
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for (int i = (1); i < (n + 1); ++i)
for (int j = (1); j < (m + 1); ++j) {
pp.push_back(make_pair(i + j, j));
}
sort((pp).begin(), (pp).end());
int an = 0;
for (int i = 0; i < k; i++) an += pp[i].first - 1;
printf("%d\n", an);
for (int i = k - 1; i >= 0; i--) {
print(pp[i].first - pp[i].second, pp[i].second);
puts("");
}
return 0;
}
```
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.