Codeforces Round #771 (Div. 2)
A
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n;
int a[N], b[N];
void solve()
{
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
int pos = 0, pos1 = 0;
for (int i = 1; i <= n; i ++ ) {
if (a[i] != i) {
pos = i;
break;
}
}
for (int i = 1; i <= n; i ++ ) {
if (a[i] == pos) {
pos1 = i;
break;
}
}
for (int i = pos; i <= (pos1 + pos) / 2; i ++ )
swap(a[i], a[pos1 - (i - pos)]);
for (int i = 1; i <= n; i ++ ) cout << a[i] << " ";
cout << endl;
return;
}
int main() {
int T;
cin >> T;
while (T -- ) solve();
return 0;
}
B
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n;
int a[N];
void solve()
{
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
vector<int> b, c;
for (int i = 1; i <= n; i ++ )
if (a[i] % 2 == 0) b.push_back(a[i]);
else c.push_back(a[i]);
for (int i = 1; i < b.size(); i ++ )
if (b[i] < b[i - 1]) {
puts("NO");
return;
}
for (int i = 1; i < c.size(); i ++ )
if (c[i] < c[i - 1]) {
puts("NO");
return;
}
puts("YES");
return;
}
int main() {
int T;
cin >> T;
while (T -- ) solve();
return 0;
}
C
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n;
int a[N];
void solve()
{
cin >> n;
int res = 0;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
for (int i = 1; i <= n; ) {
if (a[i] == i) {
i ++;
res ++;
continue;
}
int j = i + 1;
vector<int> v;
v.push_back(a[i]);
int minn = a[i], maxx = a[i];
while (j <= n) {
v.push_back(a[j]);
minn = min(minn, a[j]);
maxx = max(maxx, a[j]);
if (maxx - minn + 1 == v.size() && maxx == j && minn == i) {
res ++;
break;
}
j ++;
}
i = j + 1;
}
cout << res << endl;
return;
}
int main() {
int T;
cin >> T;
while (T -- ) solve();
return 0;
}
D
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, m;
int a[N][N];
struct Node {
int x, y, z;
};
vector<Node> ans;
void dfs(int x, int y)
{
if (x <= 0 || x >= n || y <= 0 || y >= m) return;
int c = 0;
for (int i = x; i <= x + 1; i ++ ) {
for (int j = y; j <= y + 1; j ++ ) {
if (a[i][j]) {
if (c && c != a[i][j]) return;
c = a[i][j];
}
}
}
if (c) {
ans.push_back({x, y, c});
for (int i = x; i <= x + 1; i ++ )
for (int j = y; j <= y + 1; j ++ )
a[i][j] = 0;
for (int i = x - 1; i <= x + 1; i ++ )
for (int j = y - 1; j <= y + 1; j ++ )
dfs(i, j);
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
scanf("%d", &a[i][j]);
for (int i = 1; i < n; i ++ )
for (int j = 1; j < m; j ++ )
dfs(i, j);
for (int i = 1; i <= n; i ++ ) {
for (int j = 1; j <= m; j ++ ) {
if (a[i][j]) {
printf("-1\n");
return 0;
}
}
}
printf("%d\n", ans.size());
reverse(ans.begin(), ans.end());
for (auto [x, y, z] : ans)
printf("%d %d %d\n", x, y, z);
return 0;
}