I hope you liked problems!
Sorry for incorrect placement of problems. I had to do swap(E, F).
Tutorial is loading...
Solution C++
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m; cin >> n >> m;
string black_row(m, 'B');
vector<string> result(n, black_row);
result[0][0] = 'W';
for (int i = 0; i < n; ++i) {
cout << result[i] << '\n';
}
}
int main() {
int t; cin >> t;
while(t--) solve();
}
Tutorial is loading...
Solution C++
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n; cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; ++i) {
cin >> b[i];
}
vector<int> good(2, 0);
for (int i = 0; i < n; ++i) {
if (a[i] > b[i] && !good[0]) {
cout << "NO\n";
return;
} else if (a[i] < b[i] && !good[1]) {
cout << "NO\n";
return;
}
if (a[i] == -1) good[0] = 1;
if (a[i] == 1) good[1] = 1;
}
cout << "YES\n";
}
int main() {
int t; cin >> t;
while(t--) {
solve();
}
}
Tutorial is loading...
Solution C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int n; cin >> n;
vector<long long> prefix(n + 1, 0);
for (int i = 0; i < n; ++i) {
int x; cin >> x;
prefix[i + 1] = prefix[i] + x;
}
int begin = 0, end = 0;
long long ans = 0;
set<long long> s = {0};
while(begin < n) {
while(end < n && !s.count(prefix[end + 1])) {
++end;
s.insert(prefix[end]);
}
ans += end - begin;
s.erase(prefix[begin]);
++begin;
}
cout << ans << endl;
}
Tutorial is loading...
Solution C++
#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<int> find_steps(const vector<int>& a) {
vector<int> steps;
for (int i = 0; i < n - 1; ++i) {
if (a[i] == 1 && a[i + 1] == 0) steps.push_back(i);
}
return steps;
}
int main() {
cin >> n >> k;
string s; cin >> s;
vector<int> a(n);
for (int i = 0; i < n; ++i) a[i] = (s[i] == 'L') ? 0 : 1;
int maxi = 0, mini = 0;
int cnt = 0;
int last = -1;
for (int i = n - 1; i >= 0; --i) {
if (a[i] == 0) {
++cnt;
} else {
if (cnt == 0) continue;
maxi += cnt;
mini = max(cnt, last + 1);
last = mini;
}
}
if (k < mini || k > maxi) {
cout << -1;
return 0;
}
bool is_min = false;
vector<int> have_step;
for (int i = 0; i < k; ++i) {
if (!is_min) {
auto steps = find_steps(a);
cout << min(int(steps.size()), maxi - k + i + 1) << ' ';
int cur = 0;
while (k - i - 1 < maxi && cur < steps.size()) {
cout << steps[cur] + 1 << ' ';
a[steps[cur]] = 0;
a[steps[cur] + 1] = 1;
++cur;
--maxi;
}
if (maxi == k - i - 1) {
is_min = true;
have_step = find_steps(a);
}
} else {
int v = have_step.back();
have_step.pop_back();
cout << "1 " << v + 1;
a[v] = 0;
a[v + 1] = 1;
if (v > 0 && a[v - 1] == 1) {
have_step.push_back(v - 1);
}
if (v + 2 < n && a[v + 2] == 0) {
have_step.push_back(v + 1);
}
}
cout << '\n';
}
}
Tutorial is loading...
Solution C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int n; cin >> n;
if (n < 3) {
cout << -1;
return 0;
}
vector<int> solution = {
1, 3, 4, 8, 2, 7, 9, 5, 6
};
vector<vector<int>> table(n, vector<int>(n, 0));
int cur = 1;
for (int i = 0; i < n - 3; ++i) {
if (i & 1) {
for (int j = n - 1; j >= 0; --j) {
table[i][j] = cur;
++cur;
}
} else {
for (int j = 0; j < n; ++j) {
table[i][j] = cur;
++cur;
}
}
}
if ((n - 3) & 1) {
for (int j = n - 1; j >= 0; --j) {
if (j & 1) {
for (int i = n - 3; i < n; ++i) {
if (j > 2) {
table[i][j] = cur;
++cur;
} else {
table[i][j] = solution[(2 - j) * 3 + i - n + 3] + n * n - 9;
}
}
} else {
for (int i = n - 1; i >= n - 3; --i) {
if (j > 2) {
table[i][j] = cur;
++cur;
} else {
table[i][j] = solution[(2 - j) * 3 + n - 1 - i] + n * n - 9;
}
}
}
}
} else {
for (int j = 0; j < n; ++j) {
if (j & 1) {
for (int i = n - 1; i >= n - 3; --i) {
if (j < n - 3) {
table[i][j] = cur;
++cur;
} else {
table[i][j] = solution[(j - n + 3) * 3 + n - 1 - i] + n * n - 9;
}
}
} else {
for (int i = n - 3; i < n; ++i) {
if (j < n - 3) {
table[i][j] = cur;
++cur;
} else {
table[i][j] = solution[(j - n + 3) * 3 + i - n + 3] + n * n - 9;
}
}
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << table[i][j] << ' ';
}
cout << '\n';
}
}
Tutorial is loading...
Solution C++
#include <bits/stdc++.h>
using namespace std;
vector<int> max_div;
void eratosthenes(int limit) {
max_div.assign(limit + 1, 0);
max_div[0] = limit + 10;
max_div[1] = 1;
for (int i = 2; i <= limit; ++i) {
if (max_div[i]) continue;
for (int j = i; j <= limit; j += i) {
if (max_div[j]) continue;
max_div[j] = j / i;
}
}
}
int main() {
int n; cin >> n;
eratosthenes(n);
sort(max_div.begin(), max_div.end());
for (int i = 1; i < n; ++i) {
cout << max_div[i] << ' ';
}
}