solution
stringlengths 11
983k
| difficulty
int64 0
21
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
int n, k;
char w[3][100100];
int a[3][100100];
int s[3][100100];
typedef struct {
int s, lr, x;
} ele;
std::queue<ele> q;
void suru() {
scanf("%d %d", &n, &k);
scanf("%s %s", w[0], w[1]);
for (int i = 0; i < n; i++) a[0][i] = a[1][i] = 0;
a[0][0] = 1;
s[0][0] = 0;
for (; q.size(); q.pop())
;
q.push((ele){0, 0, 0});
}
void truli() {
while (q.size()) {
int s = q.front().s, lr = q.front().lr, x = q.front().x;
if (x + k >= n) break;
if (w[lr ^ 1][x + k] == '-' && a[lr ^ 1][x + k] == 0) {
a[lr ^ 1][x + k] = 1;
q.push((ele){s + 1, lr ^ 1, x + k});
}
if (w[lr][x + 1] == '-' && a[lr][x + 1] == 0) {
a[lr][x + 1] = 1;
q.push((ele){s + 1, lr, x + 1});
}
if (x - 1 >= 0 && w[lr][x - 1] == '-' && a[lr][x - 1] == 0 &&
s + 1 <= x - 1) {
a[lr][x - 1] = 1;
q.push((ele){s + 1, lr, x - 1});
}
q.pop();
}
}
void sutru() { puts(q.size() ? "YES" : "NO"); }
int main() {
suru();
truli();
sutru();
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct node {
int x, y, z;
};
node q[200001];
int n, m;
bool ok[2][100001];
char s[100005];
int main(void) {
scanf("%d %d", &n, &m);
for (int i = 0; i < 2; ++i) {
scanf("%s", s + 1);
for (int j = 1; j <= n; ++j)
if (s[j] == '-') ok[i][j] = true;
}
bool flag = false;
q[1].x = 0;
q[1].y = 1;
q[1].z = 0;
int h = 1, e = 1;
ok[0][1] = false;
while (h <= e) {
node p = q[h];
++h;
int i = p.x, j = p.y, k = p.z;
if (j <= k) continue;
if ((j + 1 > n) || (j + m > n)) {
flag = true;
break;
}
if (ok[i][j + 1]) {
++e;
q[e].x = i;
q[e].y = j + 1;
q[e].z = k + 1;
ok[i][j + 1] = false;
}
if (ok[i ^ 1][j + m]) {
++e;
q[e].x = i ^ 1;
q[e].y = j + m;
q[e].z = k + 1;
ok[i ^ 1][j + m] = false;
}
if (ok[i][j - 1]) {
++e;
q[e].x = i;
q[e].y = j - 1;
q[e].z = k + 1;
ok[i][j - 1] = false;
}
}
if (flag)
printf("YES\n");
else
printf("NO\n");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
int a[2][200005];
char ca[2][200005];
using namespace std;
struct ss {
int cnt;
int tag;
} t1, t2, t3;
queue<struct ss> qq;
int main() {
int n, i, j, k;
while (scanf("%d%d", &n, &k) != EOF) {
scanf(" %s", ca[0]);
scanf(" %s", ca[1]);
memset(a, -1, sizeof(a));
a[0][0] = 0;
while (!qq.empty()) qq.pop();
t1.cnt = 0;
t1.tag = 0;
qq.push(t1);
int res = 0;
while (!qq.empty()) {
t2 = qq.front();
qq.pop();
t3 = t2;
t3.cnt = t2.cnt + 1;
if (a[t3.tag][t3.cnt] == -1 && ca[t3.tag][t3.cnt] == '-' && t3.cnt < n) {
if (a[t2.tag][t2.cnt] + 1 <= t3.cnt) {
a[t3.tag][t3.cnt] = a[t2.tag][t2.cnt] + 1;
qq.push(t3);
}
}
t3 = t2;
t3.cnt = t2.cnt - 1;
if (a[t3.tag][t3.cnt] == -1 && ca[t3.tag][t3.cnt] == '-' && t3.cnt >= 0) {
if (a[t2.tag][t2.cnt] + 1 <= t3.cnt) {
a[t3.tag][t3.cnt] = a[t2.tag][t2.cnt] + 1;
qq.push(t3);
}
}
t3 = t2;
t3.tag = !t2.tag;
t3.cnt = t2.cnt + k;
if (a[t3.tag][t3.cnt] == -1 && ca[t3.tag][t3.cnt] == '-' && t3.cnt < n) {
if (a[t2.tag][t2.cnt] + 1 <= t3.cnt) {
a[t3.tag][t3.cnt] = a[t2.tag][t2.cnt] + 1;
qq.push(t3);
}
}
if (t3.cnt >= n) {
res = 1;
break;
}
}
if (a[0][n - 1] >= 0 || a[1][n - 1] >= 0) {
res = 1;
}
if (res)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n, k, d[5][100050], dd[5][100050];
char a[5][100050];
void bfs(int x, int y) {
queue<pair<long long, pair<long long, long long> > > qp;
int tg = 0;
qp.push(pair<long long, pair<long long, long long> >(
tg, pair<long long, long long>(x, y)));
dd[x][y] = 0;
while (qp.size() != 0) {
int u, v, u1, v1;
u = qp.front().second.first;
v = qp.front().second.second;
tg = qp.front().first;
qp.pop();
if (v + k > n || v + 1 > n) {
cout << "YES";
exit(0);
}
u1 = u;
v1 = v + 1;
if (a[u1][v1] != 'X') {
if (dd[u1][v1] > dd[u][v] + 1) {
dd[u1][v1] = dd[u][v] + 1;
qp.push(pair<long long, pair<long long, long long> >(
dd[u1][v1], pair<long long, long long>(u1, v1)));
}
}
if (u == 1) {
u1 = u + 1;
v1 = v + k;
if (a[u1][v1] != 'X') {
if (dd[u1][v1] > dd[u][v] + 1) {
dd[u1][v1] = dd[u][v] + 1;
qp.push(pair<long long, pair<long long, long long> >(
dd[u1][v1], pair<long long, long long>(u1, v1)));
}
}
}
if (u == 2) {
u1 = u - 1;
v1 = v + k;
if (a[u1][v1] != 'X') {
if (dd[u1][v1] > dd[u][v] + 1) {
dd[u1][v1] = dd[u][v] + 1;
qp.push(pair<long long, pair<long long, long long> >(
dd[u1][v1], pair<long long, long long>(u1, v1)));
}
}
}
if (tg != 0 && tg + 1 < v - 1 && a[u][v - 1] != 'X') {
u1 = u;
v1 = v - 1;
if (a[u1][v1] != 'X') {
if (dd[u1][v1] > dd[u][v] + 1) {
dd[u1][v1] = dd[u][v] + 1;
qp.push(pair<long long, pair<long long, long long> >(
dd[u1][v1], pair<long long, long long>(u1, v1)));
}
}
}
}
}
int main() {
cin >> n >> k;
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
memset(dd, 10, sizeof(dd));
bfs(1, 1);
cout << "NO";
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e3;
const long long MAXN = 1e5 + 100;
long long n, k, Dis[2][MAXN];
char a[2][MAXN];
vector<pair<long long, long long> > adj[2][MAXN];
bool val(long long x, long long y) {
return a[x][y] == '-' && x < 2 && x >= 0 && y < n && y >= 0;
}
void bfs(long long x, long long y) {
for (long long i = 0; i < 2; i++)
for (long long j = 0; j < n; j++) Dis[i][j] = INF;
Dis[x][y] = 0;
queue<pair<long long, long long> > q;
q.push({x, y});
while (!q.empty()) {
pair<long long, long long> pi;
pi = q.front();
q.pop();
long long u = pi.first, v = pi.second;
for (auto t : adj[u][v]) {
if (Dis[t.first][t.second] == INF) {
if (Dis[u][v] + 1 <= t.second)
Dis[t.first][t.second] = Dis[u][v] + 1, q.push({t.first, t.second});
}
}
}
}
void check_adj() {
for (long long i = 0; i < 2; i++) {
for (long long j = 0; j < n; j++) {
cout << "-------\n";
cout << i + 1 << " " << j + 1 << endl;
for (auto x : adj[i][j])
cout << x.first + 1 << " " << x.second + 1 << endl;
}
}
}
void check() {
for (long long i = 0; i < 2; i++) {
for (long long j = 0; j < n; j++) cout << Dis[i][j] << " ";
cout << endl;
}
}
int main() {
cin >> n >> k;
for (long long i = 0; i < 2; i++)
for (long long j = 0; j < n; j++) cin >> a[i][j];
for (long long i = 0; i < 2; i++) {
for (long long j = 0; j < n; j++) {
if (val(i, j - 1)) adj[i][j].push_back({i, j - 1});
if (val(i, j + 1)) adj[i][j].push_back({i, j + 1});
if (val(i + 1, j + k)) adj[i][j].push_back({i + 1, j + k});
if (val(i - 1, j + k)) adj[i][j].push_back({i - 1, j + k});
}
}
bfs(0, 0);
bool isAns = false;
for (long long i = 0; i < 2; i++) {
for (long long j = n - 1; j >= n - k; j--) {
if (Dis[i][j] != INF) isAns = true;
}
}
if (isAns)
cout << "YES";
else
cout << "NO";
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<string> s;
vector<vector<int> > dp;
long long n, k;
bool cal(int str, int ind) {
queue<pair<int, int> > q;
q.push({0, 0});
int water = 0;
while (q.size()) {
int len = q.size();
while (len--) {
str = q.front().first;
ind = q.front().second;
q.pop();
if (ind + k >= n) return true;
if (ind < water) {
continue;
}
if (dp[str][ind]) continue;
dp[str][ind] = 1;
if ((ind + 1 < n) && ((s[str][ind + 1] == '-'))) q.push({str, ind + 1});
if (ind - 1 >= 0 && s[str][ind - 1] == '-') q.push({str, ind - 1});
if (ind + k < n && s[(str == 1) ? 0 : 1][ind + k] == '-')
q.push({((str == 1) ? 0 : 1), ind + k});
}
++water;
}
return false;
}
int main() {
cin >> n >> k;
s.resize(2);
cin >> s[0] >> s[1];
dp.resize(2, (vector<int>(n, 0)));
if (cal(0, 0))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3F3F3F3F;
int n, k;
const int M = 100010;
char a[2][M];
int dist[2][M];
bool vis[2][M];
void bfs(int x, int y) {
memset(vis, false, sizeof(vis));
memset(dist, 0x3F, sizeof(dist));
queue<pair<int, int> > q;
dist[x][y] = 0;
q.push(make_pair(x, y));
while (!q.empty()) {
pair<int, int> f = q.front();
q.pop();
int x = f.first, y = f.second;
if (vis[x][y]) continue;
vis[x][y] = true;
if (dist[x][y] > y) continue;
if (y + 1 >= n or y + k >= n) {
printf("YES\n");
return;
}
if (y + 1 < n and a[x][y + 1] != 'X')
dist[x][y + 1] = min(dist[x][y + 1], dist[x][y] + 1),
q.push(make_pair(x, y + 1));
if (y - 1 >= 0 and a[x][y - 1] != 'X')
dist[x][y - 1] = min(dist[x][y - 1], dist[x][y] + 1),
q.push(make_pair(x, y - 1));
int x2 = (x + 1) % 2;
if (y + k < n and a[x2][y + k] != 'X')
dist[x2][y + k] = min(dist[x2][y + k], dist[x][y] + 1),
q.push(make_pair(x2, y + k));
}
printf("NO\n");
}
int main() {
scanf("%d%d", &n, &k);
scanf("%s%s", a[0], a[1]);
bfs(0, 0);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k, cnt;
vector<string> s(2);
vector<vector<bool>> a(2, vector<bool>(1e6));
void dfs(int x, int y) {
if (s[x][y] != '-' || a[x][y] || y < cnt) return;
if (y + k >= n) {
cout << "YES";
exit(0);
}
cnt++;
a[x][y] = 1;
dfs(1 - x, y + k);
dfs(x, y + 1);
dfs(x, y - 1);
cnt--;
}
int main() {
cin >> n >> k >> s[0] >> s[1];
dfs(0, 0);
cout << "NO";
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k, x, y, fi, fr, f[2][100100], qx[200200], qy[200200];
char s[2][100100];
int main() {
cin >> n >> k;
scanf("%s", s[0]);
scanf("%s", s[1]);
f[0][0] = 1;
for (fr = 1; fi < fr;) {
x = qx[fi];
y = qy[fi++];
if (y >= n - k) {
puts("YES");
return 0;
}
if (y > 0 && s[x][y - 1] == '-' && f[x][y - 1] == 0 && y > f[x][y]) {
f[x][y - 1] = f[x][y] + 1;
qx[fr] = x;
qy[fr++] = y - 1;
}
if (s[x][y + 1] == '-' && f[x][y + 1] == 0) {
f[x][y + 1] = f[x][y] + 1;
qx[fr] = x;
qy[fr++] = y + 1;
}
if (s[1 - x][y + k] == '-' && f[1 - x][y + k] == 0) {
f[1 - x][y + k] = f[x][y] + 1;
qx[fr] = 1 - x;
qy[fr++] = y + k;
}
}
puts("NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int V = 200100;
int q[V], dp[V], n, K, i, j, top, tail;
char s[2][V];
int main() {
while (~scanf("%d%d", &n, &K)) {
scanf("%s%s", s[0], s[1]);
s[0][n] = s[1][n] = '-';
n++;
memset(dp, -1, sizeof(dp));
dp[0] = 0;
top = 0;
tail = 1;
q[1] = 0;
while (top < tail) {
top++;
i = q[top];
if (i % n != 0) {
j = i - 1;
if (s[j / n][j % n] == '-' && dp[j] == -1 && j % n >= dp[i] + 1) {
dp[j] = dp[i] + 1;
tail++;
q[tail] = j;
}
}
if (i % n != n - 1) {
j = i + 1;
if (s[j / n][j % n] == '-' && dp[j] == -1 && j % n >= dp[i] + 1) {
dp[j] = dp[i] + 1;
tail++;
q[tail] = j;
}
}
j = ((i / n) ^ 1) * n + min(i % n + K, n - 1);
if (s[j / n][j % n] == '-' && dp[j] == -1 && j % n >= dp[i] + 1) {
dp[j] = dp[i] + 1;
tail++;
q[tail] = j;
}
}
if (dp[n - 1] != -1 || dp[2 * n - 1] != -1)
puts("YES");
else
puts("NO");
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int Inf = 1000000000;
const int Maxn = 100005;
int n, k;
string s[2];
int dist[2][Maxn];
bool Possible() {
fill((int*)dist, (int*)dist + 2 * Maxn, Inf);
dist[0][0] = 0;
queue<pair<int, int> > Q;
if (s[0][0] != 'X') Q.push(pair<int, int>(0, 0));
while (!Q.empty()) {
pair<int, int> v = Q.front();
int d = dist[v.first][v.second];
Q.pop();
if (s[v.first][v.second] == 'X' || d > v.second) continue;
pair<int, int> u = pair<int, int>(v.first, v.second - 1);
if (u.second >= 0 && d + 1 < dist[u.first][u.second])
dist[u.first][u.second] = d + 1, Q.push(u);
u = pair<int, int>(v.first, v.second + 1);
if (u.second >= n) return true;
if (d + 1 < dist[u.first][u.second])
dist[u.first][u.second] = d + 1, Q.push(u);
u = pair<int, int>(1 - v.first, v.second + k);
if (u.second >= n) return true;
if (d + 1 < dist[u.first][u.second])
dist[u.first][u.second] = d + 1, Q.push(u);
}
return false;
}
int main() {
std::ios_base::sync_with_stdio(0);
cin >> n >> k;
cin >> s[0] >> s[1];
printf("%s\n", Possible() ? "YES" : "NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
void swap(int &x, int &y) {
int t = x;
x = y;
y = t;
}
int main() {
int n;
cin >> n;
int *p = new int[n];
vector<int> k;
for (int i = 0; i < n; i++) {
cin >> p[i];
k.push_back(p[i]);
}
sort(k.begin(), k.end());
int count = 0;
for (int i = 0; i < n; i++)
if (p[i] != k[i]) count++;
if (count < 3)
cout << "YES";
else
cout << "NO";
delete[] p;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int n;
cin >> n;
vector<int> sorted(n);
vector<int> other(n);
for (int i = 0; i < n; i++) {
cin >> sorted[i];
other[i] = sorted[i];
}
sort(sorted.begin(), sorted.end());
bool can = 1;
for (int i = 0; i < n; i++) {
if (sorted[i] != other[i]) {
if (!can) {
cout << "NO\n";
return 0;
}
for (int j = n - 1; j > i; j--) {
if (other[j] == sorted[i]) {
swap(other[j], other[i]);
break;
}
}
can = 0;
}
}
cout << "YES\n";
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, ct = 0;
cin >> n;
vector<int> nums(n), nums_copy(n);
for (int i = 0; i < n; ++i) {
cin >> nums[i];
nums_copy[i] = nums[i];
}
sort(nums.begin(), nums.end());
for (int i = 0; i < n; ++i) {
if (nums_copy[i] != nums[i]) {
++ct;
}
}
if (ct <= 2) {
cout << "YES";
} else {
cout << "NO";
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b, c = 0;
cin >> n;
vector<int> v, vv;
for (int i = 0; i < n; i++) {
cin >> a;
v.push_back(a);
vv.push_back(a);
}
sort(v.begin(), v.end());
for (int i = 0; i < n; i++) {
if (v[i] != vv[i]) c++;
}
if (c > 2)
puts("NO");
else
puts("YES");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, a[1000007], b[1000007];
int main() {
int i, cnt = 0;
cin >> n;
for (i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n);
for (i = 0; i < n; i++) {
if (a[i] != b[i]) {
cnt++;
}
}
if (cnt == 0 || cnt == 2) {
cout << "YES\n";
} else {
cout << "NO\n";
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool debug = true;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
const int dy[] = {1, 0, -1, 0, -1, 1, 1, -1, 0};
template <typename X>
inline X square(const X& a) {
return a * a;
}
int scan_d() {
bool minus = false;
int result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
if (ch == '-')
minus = true;
else
result = ch - '0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = (result << 3) + (result << 1) + (ch - '0');
}
if (minus)
return -result;
else
return result;
}
long scan_ld() {
bool minus = false;
long result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
if (ch == '-')
minus = true;
else
result = ch - '0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = (result << 3) + (result << 1) + (ch - '0');
}
if (minus)
return -result;
else
return result;
}
long long scan_lld() {
bool minus = false;
long long result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
if (ch == '-')
minus = true;
else
result = ch - '0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = (result << 3) + (result << 1) + (ch - '0');
}
if (minus)
return -result;
else
return result;
}
long long unsigned scan_llu() {
long long unsigned result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
result = ch - '0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = (result << 3) + (result << 1) + (ch - '0');
}
return result;
}
void print_d(int n) {
if (n < 0) {
n = -n;
putchar('-');
}
int i = 10;
char output_buffer[10];
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 10);
}
void print_ld(long n) {
if (n < 0) {
n = -n;
putchar('-');
}
int i = 11;
char output_buffer[11];
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 11);
}
void print_lld(long long n) {
if (n < 0) {
n = -n;
putchar('-');
}
int i = 21;
char output_buffer[21];
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 21);
}
void print_llu(long long unsigned n) {
int i = 21;
char output_buffer[21];
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 21);
}
void println_d(int n) {
if (n < 0) {
n = -n;
putchar('-');
}
int i = 10;
char output_buffer[11];
output_buffer[10] = '\n';
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 11);
}
void println_ld(long n) {
if (n < 0) {
n = -n;
putchar('-');
}
int i = 11;
char output_buffer[12];
output_buffer[11] = '\n';
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 12);
}
void println_lld(long long n) {
if (n < 0) {
n = -n;
putchar('-');
}
int i = 21;
char output_buffer[22];
output_buffer[21] = '\n';
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 22);
}
void println_llu(long long unsigned n) {
int i = 21;
char output_buffer[22];
output_buffer[21] = '\n';
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 22);
}
char sp;
void printsp_d(int n) {
if (n < 0) {
n = -n;
putchar('-');
}
int i = 10;
char output_buffer[11];
output_buffer[10] = sp;
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 11);
}
void printsp_ld(long n) {
if (n < 0) {
n = -n;
putchar('-');
}
int i = 11;
char output_buffer[12];
output_buffer[11] = sp;
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 12);
}
void printsp_lld(long long n) {
if (n < 0) {
n = -n;
putchar('-');
}
int i = 21;
char output_buffer[22];
output_buffer[21] = sp;
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 22);
}
void printsp_llu(long long unsigned n) {
int i = 21;
char output_buffer[22];
output_buffer[21] = sp;
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 22);
}
int main() {
int n, i, x, ans;
vector<int> a, b;
n = scan_d();
for (i = 0; i < n; i++) {
x = scan_d();
a.push_back(x);
b.push_back(x);
}
ans = 0;
sort(b.begin(), b.end());
for (i = 0; i < n; i++)
if (a[i] != b[i]) ans++;
if (ans > 2)
puts("NO");
else
puts("YES");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
void Test() {
long long n;
cin >> n;
long long a[n], b[n];
for (long long i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n);
long long cnt = 0;
for (long long i = 0; i < n; i++) {
if (a[i] != b[i]) ++cnt;
}
if (!cnt || cnt == 2)
cout << "YES";
else
cout << "NO";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(10);
Test();
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, a[100100], b[100100];
int cnt = 0;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b, b + n);
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) cnt++;
if (cnt > 2) {
printf("NO\n");
return 0;
}
}
printf("YES\n");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, x[100200], y[100200];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &x[i]);
y[i] = x[i];
}
sort(x, x + n);
int ans = 0;
for (int i = 0; i < n; i++)
if (x[i] != y[i]) ans++;
if (ans < 3)
printf("YES\n");
else
printf("NO\n");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int k = 0;
int A[n], B[n];
while (k != n) {
cin >> A[k];
B[k] = A[k];
k++;
}
sort(A, A + k);
int count = 0;
for (int i = 0; i < n; i++) {
if (A[i] != B[i]) count++;
}
if (count > 2)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, c = 0, f = 0;
long long m;
vector<long long> a, b;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> m;
a.push_back(m);
b.push_back(m);
}
sort(b.begin(), b.end());
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) c++;
}
if (c <= 2)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int size = 100008;
int old[size], now[size];
int main() {
int i, n, sum;
while (scanf("%d", &n) != EOF) {
for (i = 0; i < n; i++) {
scanf("%d", &old[i]);
now[i] = old[i];
}
sort(now, now + n);
sum = 0;
for (i = 0; i < n; i++) {
if (old[i] != now[i]) sum++;
}
if (sum == 0 || sum == 2)
puts("YES");
else
puts("NO");
}
return 0;
}
| 9 | CPP |
n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
res = 0
for i in range(n):
if a[i] != b[i]:
res += 1
print('YES' if res <= 2 else 'NO') | 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int a[10000000], b[10010000];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
int p = 0;
sort(a, a + n);
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) p++;
}
if (p > 2)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n), b;
for (int i = 0; i < n; ++i) cin >> a[i];
b = a;
sort(a.begin(), a.end());
int cnt = 0;
for (int i = 0; i < n; ++i)
if (a[i] != b[i]) ++cnt;
if (cnt <= 2)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[100005], sa[100005];
int main() {
int n, i, cnt;
while (scanf("%d", &n) == 1) {
for (i = 0; i < n; ++i) scanf("%d", &a[i]);
for (i = 0; i < n; ++i) sa[i] = a[i];
sort(sa, sa + n);
for (cnt = i = 0; i < n; ++i)
if (a[i] != sa[i]) ++cnt;
if (cnt <= 2)
puts("YES");
else
puts("NO");
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
int a[n], b[n], t = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[t++] = a[i];
}
sort(a, a + n);
int s = 0;
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
s++;
}
}
if (s <= 2) {
cout << "YES";
} else
cout << "NO";
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int arr[100001] = {0};
int sortedArr[100001] = {0};
int main() {
int n, number = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i], sortedArr[i] = arr[i];
}
sort(sortedArr, sortedArr + n);
for (int i = 0; i < n; i++) {
if (arr[i] != sortedArr[i]) {
number++;
}
if (ceil(number / 2.0) > 1) {
break;
}
}
if (ceil(number / 2.0) > 1) {
cout << "NO";
return 0;
}
cout << "YES";
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int a[maxn], b[maxn];
int main() {
int n;
cin >> n;
int nums = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n);
for (int i = 0; i < n; i++) nums += (b[i] != a[i]);
if (nums % 2) nums++;
nums /= 2;
if (nums <= 1) {
cout << "YES" << endl;
} else
cout << "NO" << endl;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void scan(T &x) {
x = 0;
bool neg = 0;
register T c = getchar();
if (c == '-') neg = 1, c = getchar();
while ((c < 48) || (c > 57)) c = getchar();
for (; c < 48 || c > 57; c = getchar())
;
for (; c > 47 && c < 58; c = getchar()) x = (x << 3) + (x << 1) + (c & 15);
if (neg) x *= -1;
}
template <typename T>
void print(T n) {
bool neg = 0;
if (n < 0) n *= -1, neg = 1;
char snum[65];
long long int i = 0;
do {
snum[i++] = n % 10 + '0';
n /= 10;
} while (n);
--i;
if (neg) putchar('-');
while (i >= 0) putchar(snum[i--]);
printf(" ");
}
long long int readll() {
bool minus = false;
long long int result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
if (ch == '-')
minus = true;
else
result = ch - '0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = result * 10 + (ch - '0');
}
if (minus)
return -result;
else
return result;
}
long long int readint() {
char r;
bool start = false, neg = false;
long long int ret = 0;
while (true) {
r = getchar();
if ((r - '0' < 0 || r - '0' > 9) && r != '-' && !start) {
continue;
}
if ((r - '0' < 0 || r - '0' > 9) && r != '-' && start) {
break;
}
if (start) ret *= 10;
start = true;
if (r == '-')
neg = true;
else
ret += r - '0';
}
if (!neg)
return ret;
else
return -ret;
}
bool subsetSums(long long int arr[], long long int n, long long int k) {
long long total = 1 << n;
for (long long i = 0; i < total; i++) {
long long sum = 0;
for (long long int j = 0; j < n; j++)
if (i & (1 << j)) sum += arr[j];
if (sum == k) return true;
}
return false;
}
void solve() {
long long int k;
scan(k);
long long int n = k % 25;
long long int t = k / 25;
string str = "abcdefghijklmnopqrstuvwxyz";
if (n > 0) {
string b = str.substr(0, n + 1);
reverse(b.begin(), b.end());
cout << b;
}
reverse(str.begin(), str.end());
while (t--) {
cout << str;
}
cout << '\n';
}
long long int phi(long long int n) {
if (n == 1) return 2;
long long int i;
long long int result = n;
for (i = 2; i * i <= n; i++) {
if (n % i == 0) {
while (n % i == 0) n /= i;
result -= result / i;
}
}
if (n > 1) result -= result / n;
return result;
}
bool checkprime(long long int n) {
if (n == 1) return false;
if (n == 2 || n == 3) return true;
long long int i;
for (i = 2; i * i <= n; i++) {
if (n % i == 0) break;
}
if (i * i > n)
return true;
else
return false;
}
bool lexicographical_compare(string s1, string s2) {
for (long long int i = 0; i < min(s1.length(), s2.length()); i++) {
if (s1[i] < s2[i]) return true;
if (s1[i] > s2[i]) return false;
}
if (s1.length() <= s2.length()) return true;
}
long long int reverse(long long int n) {
long long int sum = 0;
long long int i = 0;
long long int temp = n;
long long int len = 0;
while (temp > 0) {
len++;
temp /= 10;
}
while (n > 0) {
sum += (n % 10) * pow(10, len - i - 1);
i++;
n /= 10;
}
return sum;
}
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int modularinverse(long long int a, long long int p) {
return power(a, p - 2, p);
}
bool prime[1000001];
void sieve() {
prime[1] = false;
int i, j;
for (i = 2; i < 1000000; ++i) {
prime[i] = true;
}
for (i = 2; i * i < 1000000; ++i) {
if (prime[i]) {
for (j = 2; j * i < 1000000; ++j) prime[i * j] = false;
}
}
}
int digits(int n) {
int count = 0;
while (n > 0) {
count++;
n /= 10;
}
return count;
}
bool check(int n) {
int count = 0;
for (int i = 2; i <= sqrt(n); ++i) {
if (n % i == 0 && checkprime(i)) {
++count;
} else if (n % i == 0) {
if (checkprime(n / i)) ++count;
}
}
if (count >= 3)
return true;
else
return false;
}
int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
return (a.second < b.second);
}
bool isLeapYear(int year) {
if (year % 400 == 0)
return true;
else if (year % 100 == 0)
return false;
else if (year % 4 == 0)
return true;
else
return false;
}
bool check(int x, int y) {
map<int, int> fuck;
map<int, int> fuckoff;
while (x > 0) {
fuck[x % 10]++;
x /= 10;
}
while (y > 0) {
fuckoff[y % 10]++;
y /= 10;
}
bool ok = false;
for (int i = 0; i < 10; i++) {
if (fuck[i] > 0 && fuckoff[i] > 0) {
ok = true;
break;
}
}
return ok;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int a[n];
int b[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n);
int count = 0;
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) ++count;
}
if (count == 0 || count == 2) {
puts("YES");
return 0;
}
puts("NO");
}
| 9 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
int a[110000], b[110000];
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < (int)(n); i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n);
int cnt = 0;
for (int i = 0; i < (int)(n); i++) {
if (a[i] != b[i]) ++cnt;
}
if (cnt <= 2)
cout << "YES";
else
cout << "NO";
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[100003], b[100003];
int main() {
int n, m, c = 0, r = 0;
cin >> n;
int a[n], b[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
std::sort(b, b + n);
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) r++;
}
if (r > 2)
cout << "NO";
else
cout << "YES";
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n], b[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(a, a + n);
int c = 0;
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
c++;
}
}
if (c > 2) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0);
std::ios::sync_with_stdio(false);
double x, y, z;
cin >> x;
vector<int> vec, v;
for (int i = 0; i < x; i++) {
cin >> y;
vec.push_back(y);
v.push_back(y);
}
long long L = 0, R = 0, n = 0;
sort(vec.begin(), vec.end());
for (int i = 0; i < x; i++) {
if (vec[i] != v[i]) {
n++;
}
}
if (n > 2) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000;
int a[maxn];
int main() {
int n;
scanf("%i", &n);
for (int i = 0; i < n; i++) scanf("%i", &a[i]);
int cnt = 0;
int l = 0, r = 0;
for (int i = 1; i < n; i++)
if (a[i - 1] > a[i]) {
if (cnt == 2) {
printf("NO\n");
return 0;
}
if (cnt == 0) {
l = i - 1;
cnt++;
} else {
r = i;
cnt++;
}
}
if (cnt == 0) {
printf("YES\n");
return 0;
}
if (cnt == 1) {
r = l + 1;
for (; l > 0; l--)
if (a[l - 1] != a[l]) break;
for (; r < n - 1; r++)
if (a[r + 1] != a[r]) break;
}
swap(a[l], a[r]);
for (int i = 1; i < n; i++) {
if (a[i - 1] > a[i]) {
printf("NO\n");
return 0;
}
}
printf("YES\n");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<bool> prime;
vector<long long> fact, inv, primes, factors;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
void factorize(long long a) {
fact.clear();
for (long long i = 1; i * i <= a; i++) {
if (i * i == a)
factors.push_back(i);
else if (a % i == 0) {
factors.push_back(i);
factors.push_back(a / i);
}
}
sort(factors.begin(), factors.end());
}
long long power(long long a, long long b) {
if (a == 1 || b == 0) return 1;
long long c = power(a, b / 2);
long long res = 1;
res = c * c;
if (b % 2) res *= a;
return res;
}
long long modInv(long long a) { return power(a, 1000000007 - 2); }
void factorial(long long n) {
fact.resize(n + 1);
fact[0] = 1;
for (long long i = 1; i <= n; i++) {
fact[i] = fact[i - 1] * i;
if (fact[i] >= 1000000007) fact[i] %= 1000000007;
}
}
void InvFactorial(long long n) {
inv.resize(n + 1);
inv[0] = 1;
for (long long i = 1; i <= n; i++) inv[i] = modInv(fact[i]);
}
long long ncr(long long n, long long r) {
if (n < r || n < 0 || r < 0) return 0;
long long b = inv[n - r];
long long c = inv[r];
long long a = fact[n] * b;
if (a >= 1000000007) a %= 1000000007;
a *= c;
if (a >= 1000000007) a %= 1000000007;
return a;
}
void remove_duplicates(vector<pair<long long, long long>> &v) {
sort(v.begin(), v.end());
long long _size = unique(v.begin(), v.end()) - v.begin();
v.resize(_size);
}
unsigned long long gcd(unsigned long long u, unsigned long long v) {
if (u == 0 || v == 0) return max(u, v);
unsigned long long shift = __builtin_ctz(u | v);
u >>= __builtin_ctz(u);
do {
v >>= __builtin_ctz(v);
if (u > v) swap(u, v);
v -= u;
} while (v != 0);
return u << shift;
}
void sieve(long long n) {
prime.assign(n + 1, 1);
prime[1] = false;
for (long long p = 2; p * p <= n; p++)
if (prime[p])
for (long long i = p * p; i <= n; i += p) prime[i] = false;
for (long long i = 2; i < n + 1; i++)
if (prime[i]) primes.push_back(i);
}
void solve() {
long long n;
cin >> n;
vector<long long> a(n), b(n);
for (long long i = 0; i < n; i++) cin >> a[i];
b = a;
sort(b.begin(), b.end());
long long count = 0;
for (long long i = 0; i < n; i++) {
if (a[i] != b[i]) count++;
}
if (count == 0 || count == 2)
cout << "Yes" << '\n';
else
cout << "No" << '\n';
}
signed main() {
fast();
long long T = 1;
for (long long i = 1; i <= T; i++) solve();
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int A[100008], B[100008];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> A[i];
B[i] = A[i];
}
sort(B, B + n);
int i = 0, c = 0;
while (i != n) {
if (A[i] != B[i]) c++;
i++;
}
if (c == 0 || c == 2)
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5;
int a[N], b[N];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b, b + n);
int cnt = 0;
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) cnt++;
}
printf("%s\n", (cnt > 2) ? "NO" : "YES");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[100000], b[100000];
int main() {
int n, s = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(a, a + n);
for (int i = 0; i < n; i++)
if (a[i] != b[i]) s++;
if (s > 2)
cout << "NO";
else
cout << "YES";
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[100000], b[100000];
int main() {
int n, s = 0;
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(a, a + n);
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) s++;
if (s > 2) {
cout << "NO";
return 0;
}
}
cout << "YES";
}
| 9 | CPP |
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
#vsInput()
n=Int()
a=array()
sorted_a=sorted(a)
disturbed=[]
for i in range(n):
if(a[i]!=sorted_a[i]):
disturbed.append(i)
if(len(disturbed)<=2): print("YES")
else: print("NO")
| 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 2 * (1e9);
int main() {
int N, cnt = 0;
cin >> N;
vector<int> A(N);
for (int &i : A) {
cin >> i;
}
vector<int> B = A;
sort(B.begin(), B.end());
for (int i = 0; i < N; i++) {
if (A[i] != B[i]) {
cnt++;
}
}
if (cnt > 2) {
cout << "NO";
} else {
cout << "YES";
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int n, c = 0;
cin >> n;
long long a[n], b[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n);
for (int i = 0; i < n; i++) {
if ((a[i] - b[i])) c++;
}
if (c == 2 || c == 0)
cout << "YES";
else
cout << "NO";
cout << "\n";
}
| 9 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:66777216")
using namespace std;
const int Max = 100000;
int a[Max + 10];
int b[Max + 10];
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n);
int k = 0;
for (int i = 0; i < n; i++)
if (a[i] != b[i]) k++;
if (k == 2 || k == 0)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n;
int a[100500];
int b[100500];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b + 1, b + n + 1);
int counter = 0;
for (int i = 1; i <= n; i++) {
int cur = a[i];
if (a[i] != b[i]) counter++;
}
if (counter > 2)
printf("NO");
else
printf("YES");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
int main() {
int n;
cin >> n;
std::vector<int> m(n);
std::vector<int> o(n);
for (int i = 0; i < n; ++i) {
cin >> m[i];
o[i] = m[i];
}
std::sort(m.begin(), m.end());
int diff = 0;
for (int i = 0; i < n; ++i) {
if (m[i] != o[i]) {
diff++;
}
}
if (diff == 0 || diff == 2) {
cout << "YES";
} else {
cout << "NO";
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, cnt = 0;
cin >> n;
vector<int> matr(n), ind;
for (int i = 0; i < n; i++) cin >> matr[i];
for (int i = 0; i < n - 1 && cnt <= 2; i++)
if (matr[i] > matr[i + 1]) cnt++, ind.push_back(i);
bool ent = false;
if (cnt == 2) {
swap(matr[ind[0]], matr[ind[1] + 1]);
for (int i = 0; i < n - 1; i++)
if (matr[i] > matr[i + 1]) {
ent = true;
break;
}
} else if (cnt == 1) {
int j = ind[0] + 1, st = j, k = st - 1;
for (; k >= 0 && matr[k] == matr[st - 1]; k--)
;
k++;
for (; j < n && matr[j] == matr[st]; j++)
;
j--;
swap(matr[k], matr[j]);
for (int i = 0; i < n - 1; i++)
if (matr[i] > matr[i + 1]) {
ent = true;
break;
}
}
cout << (cnt <= 2 && !ent ? "YES" : "NO") << endl;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> arr(n), sorted;
for (int &i : arr) cin >> i;
sorted = arr;
sort(sorted.begin(), sorted.end());
int diff = 0;
for (int i = 0; i < n && diff <= 2; ++i) {
if (arr[i] != sorted[i]) diff++;
}
if (diff > 2)
cout << "NO";
else
cout << "YES";
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using pll = pair<ll, ll>;
using vll = vector<pll>;
using MAP = map<ll, ll>;
using sl = set<ll>;
using sll = set<pll>;
using MAPs = map<ll, sl>;
using MAPv = map<ll, vl>;
const ll inf = LLONG_MAX;
const ll mod = 1e9 + 7;
const ll N = 2e5 + 5;
int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};
int n, a[N];
void MAIN() {
cin >> n;
vector<int> v;
for (int i = 0; i < n; i++) cin >> a[i], v.push_back(a[i]);
sort((v).begin(), (v).end());
int ind1 = 0, ind2 = 0, cnt = 0;
for (int i = 0; i < n; i++) {
if (a[i] != v[i]) {
cnt++;
if (!ind1)
ind1 = i;
else
ind2 = i;
}
}
if (cnt > 2) {
cout << "NO\n";
return;
}
swap(a[ind1], a[ind2]);
for (int i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
cout << "NO\n";
return;
}
}
cout << "YES\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t = 1;
while (t-- > 0) {
MAIN();
}
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<long long> v, t;
int main() {
long long n;
scanf("%I64d", &n);
;
v.resize(n);
for (int i = 0; i < n; ++i) scanf("%I64d", &v[i]);
;
t = v;
sort((t).begin(), (t).end());
long long cnt = 0;
for (int i = 0; i < n; ++i)
if (v[i] != t[i]) cnt++;
cout << (cnt > 2 ? "NO" : "YES");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long INF = 1e18L;
const double eps = 1e-9;
const long long mod = 1e9 + 7;
const double pi = 2.0 * acos(0.0);
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long n, cnt = 0;
cin >> n;
vector<long long> a(n), b(n);
for (long long i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort((b).begin(), (b).end());
for (long long i = 0; i < n; i++) {
if (a[i] != b[i]) cnt += 1;
}
if (cnt >= 3) {
cout << "NO\n";
} else {
cout << "YES\n";
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[100009];
int b[100009];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(a, a + n);
int counter = 0;
for (int i = 0; i < n; ++i) {
if (a[i] != b[i]) ++counter;
}
printf("%s\n", counter < 3 ? "YES" : "NO");
return 0;
}
| 9 | CPP |
n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
ans = 0
for i in range(n):
if not a[i] == b[i]:
ans += 1
if ans <= 2:
print("YES")
else:
print("NO") | 9 | PYTHON3 |
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
b=a.copy()
a.sort()
for i in range(n):
a[i]=abs(a[i]-b[i])
k=Counter(a)
if(k[0]>=n-2):
print("YES")
else:
print("NO") | 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void OpenFiles() {}
int x[100000 + 100], sortedx[100000 + 100];
int main() {
OpenFiles();
int i, N, num = 0;
scanf("%d", &N);
for (i = 1; i <= N; i++) {
scanf("%d", &x[i]);
sortedx[i] = x[i];
}
sort(sortedx + 1, sortedx + N + 1);
for (i = 1; i <= N; i++)
if (x[i] != sortedx[i]) num++;
if (num > 2)
printf("NO");
else
printf("YES");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<int> m;
vector<int> sortm;
int n;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int a;
scanf("%d", &a);
m.push_back(a);
sortm.push_back(a);
}
sort(sortm.begin(), sortm.end());
int count = 0;
for (int i = 0; i < n; i++) {
if (m[i] != sortm[i]) count++;
}
if (count == 0 || count == 2)
printf("YES\n");
else
printf("NO\n");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int N;
int A[100001];
pair<int, int> B[100001];
bool comp(pair<int, int> a, pair<int, int> b) {
if (a.first != b.first) return a.first < b.first;
return a.second < b.second;
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
scanf("%d", &A[i]);
B[i].first = A[i];
B[i].second = i;
}
sort(B + 1, B + N + 1, comp);
int cnt = 0;
for (int i = 1; i <= N; i++)
if (A[i] != B[i].first) cnt++;
if (cnt == 0 || cnt == 2)
printf("YES\n");
else
printf("NO\n");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n(0);
cin >> n;
vector<int> v(n), w(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
w[i] = v[i];
}
sort(v.begin(), v.end());
int count = 0;
for (int i = 0; i < n; ++i) {
if (v[i] != w[i]) ++count;
}
cout << (count == 0 || count == 2 ? "YES" : "NO") << endl;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
int in() {
int r = 0, c;
for (c = getchar(); c <= 32; c = getchar())
;
if (c == '-') return -in();
for (; c > 32; r = (r << 1) + (r << 3) + c - '0', c = getchar())
;
return r;
}
using namespace std;
const int INF = int(1e9 + 7);
int v[100010], v2[100010];
int n;
bool poss;
bool check() {
for (int i = 0; i < n - 1; i++) {
if (v[i] > v[i + 1]) return false;
}
return true;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> v[i];
}
set<pair<int, int> > s;
if (check()) {
cout << "YES" << endl;
return 0;
}
int a, b;
s.insert(make_pair(-v[0], 0));
for (int i = 1; i < n; i++) {
if (-((*s.begin()).first) > v[i]) {
a = ((*s.begin()).second);
b = i;
}
s.insert(make_pair(-v[i], i));
}
swap(v[a], v[b]);
if (check())
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
void Solve() {
long long int n;
cin >> n;
long long int a[n], copy[n];
for (long long int i = 0; i <= n - 1; i++) {
cin >> a[i];
copy[i] = a[i];
}
sort(copy, copy + n);
long long int error = 0;
for (long long int i = 0; i <= n - 1; i++)
if (a[i] != copy[i]) error++;
if (error == 0 || error == 2)
cout << "YES";
else
cout << "NO";
}
int main(int argc, char const *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
;
int T = 1;
while (T--) Solve();
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, a[100005], b[100005];
int main() {
int i, x;
cin >> n;
for (i = 0; i < n; i++) cin >> a[i], b[i] = a[i];
sort(a, a + n);
x = 0;
for (i = 0; i < n; i++)
if (a[i] != b[i]) x++;
if (x == 0 || x == 2)
printf("YES");
else
printf("NO");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, in1, in2;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
int flag = 0;
for (int i = 1; i < n; i++) {
if (arr[i] < arr[i - 1]) {
in1 = i - 1;
flag = 1;
break;
}
}
if (!flag)
cout << "YES";
else {
if (in1 != 0) {
while (arr[in1] == arr[in1 - 1]) {
if (in1 > 1)
in1--;
else {
in1--;
break;
}
}
}
int m = 1000000010;
for (int i = in1 + 1; i < n; i++) {
if (arr[i] <= m) {
m = arr[i];
in2 = i;
}
}
arr[in1] = arr[in1] + arr[in2] - (arr[in2] = arr[in1]);
flag = 0;
for (int i = 1; i < n; i++) {
if (arr[i] < arr[i - 1]) {
flag = 1;
break;
}
}
if (!flag)
cout << "YES";
else
cout << "NO";
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a[200000], b[200000];
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i], b[i] = a[i];
sort(b, b + n);
int c = 0;
vector<int> vs;
for (int i = 0; i < n; i++)
if (a[i] != b[i]) {
vs.push_back(i);
}
if (!vs.size())
cout << "YES\n";
else {
swap(a[vs[0]], a[vs[1]]);
for (int i = 0; i < n; i++) c += (a[i] != b[i]);
if (c)
cout << "NO\n";
else
cout << "YES\n";
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[100000], b[100000], n, cnt = 0;
while (cin >> n) {
for (int i = 0; i < n; ++i) {
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n);
for (int i = 0; i < n; ++i) {
if (a[i] != b[i]) cnt++;
}
if (cnt == 0 || cnt == 2) {
cout << "YES" << endl;
} else
cout << "NO" << endl;
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[300000];
int b[300000];
int cmp(const void *x, const void *y) { return *(int *)x - *(int *)y; }
int main() {
int n, i;
cin >> n;
int ok = 0;
for (i = 1; i <= n; i++) {
cin >> a[i];
b[i] = a[i];
if (a[i] < a[i - 1]) ok = 1;
}
if (n == 2 || !ok) {
cout << "YES" << endl;
return 0;
}
qsort(a, n + 1, sizeof(a[0]), cmp);
int c[3], t = 0;
for (i = 1; i <= n; i++) {
if (a[i] != b[i]) {
t++;
if (t > 2) break;
c[t] = i;
}
}
if (t > 2)
cout << "NO" << endl;
else {
if (a[c[1]] == b[c[2]] && b[c[1]] == a[c[2]])
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, arr[(int)(2e5 + 10)], s, brr[(int)(2e5 + 10)];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> arr[i], brr[i] = arr[i];
sort(arr + 1, arr + n + 1);
for (int i = 1; i <= n; i++)
if (arr[i] != brr[i]) s++;
if (s == 0 or s == 2)
puts("YES");
else
puts("NO");
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a[100010], b[100010];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(a, a + n);
int c;
for (int i = 0; i < n; i++)
if (a[i] != b[i]) c++;
printf("%s\n", c > 2 ? "NO" : "YES");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double EPS = 1e-7;
int n;
vector<pair<int, int> > a;
int b[100047];
int c[100047];
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> b[i];
c[i] = b[i];
}
sort(b, b + n);
for (int i = 0; i < n; ++i) {
if (c[i] != b[i]) {
a.push_back(make_pair(c[i], b[i]));
}
}
if (a.size() == 2) {
if (a[0].first == a[1].second && a[1].first == a[0].second) {
cout << "YES\n";
return 0;
}
} else if (!a.size()) {
cout << "YES\n";
return 0;
}
cout << "NO\n";
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, v[100005], aux[100005];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> v[i];
aux[i] = v[i];
}
sort(aux + 1, aux + n + 1);
int contor = 0;
for (int i = 1; i <= n; i++)
if (v[i] != aux[i]) contor++;
if (contor == 2 || !contor)
cout << "YES";
else
cout << "NO";
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> vec1;
for (int i = 0; i < (int)(n); ++i) {
int temp;
cin >> temp;
vec1.push_back(temp);
}
vector<int> vec2(vec1);
int c = 0;
sort(vec1.begin(), vec1.end());
for (int i = 0; i < (int)(n); ++i) {
if (vec1[i] != vec2[i]) c++;
}
if (c < 3)
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int A[100005], B[100005], n;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%d", &A[i]);
B[i] = A[i];
}
sort(B + 1, B + n + 1);
int res = 0;
for (int i = 1; i <= n; i++) {
if (A[i] != B[i]) res++;
}
cout << (res <= 2 ? "YES" : "NO") << endl;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[202011], b[200110], i, n;
int main() {
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
b[i] = a[i];
}
int ans = 0;
sort(a + 1, a + n + 1);
for (i = 1; i <= n; i++)
if (a[i] != b[i]) ans++;
if (ans == 2 || ans == 0) {
cout << "YES";
} else {
cout << "NO";
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, kol = 0;
cin >> n;
int m[n], a[n];
for (int i = 0; i < n; i++) {
cin >> m[i];
a[i] = m[i];
}
sort(a, a + n);
for (int i = 0; i < n; i++)
if (a[i] != m[i]) kol++;
cout << ((kol <= 2) ? "YES" : "NO");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<long long int> adj[200005];
bool vis[200005] = {false};
map<pair<long long int, long long int>, long long int> m;
int main() {
long long int n;
cin >> n;
long long int a[n], b[n];
for (long long int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n);
long long int count = 0;
for (long long int i = 0; i < n; i++) {
if (b[i] != a[i]) {
count++;
}
}
if (count == 2 || count == 0)
cout << "YES\n";
else
cout << "NO\n";
}
| 9 | CPP |
n = int(input())
l = list(map(int, input().split()))
l1 = []
for i in l: l1.append(i)
l1.sort()
cnt = 0
for i in range(n):
if l[i] != l1[i]: cnt -= -1
if cnt == 0 or cnt == 2: print('YES')
else: print('NO') | 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int a[100000], b[100000];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b, b + n);
int cnt = 0;
for (int i = 0; i < n; ++i) {
if (a[i] != b[i]) cnt++;
}
puts(cnt == 0 || cnt == 2 ? "YES" : "NO");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int A[n], B[n];
for (int i = 0; i < n; i++) {
cin >> A[i];
B[i] = A[i];
}
sort(B, B + n);
int i = 0, c = 0;
while (i < n) {
if (A[i] != B[i]) {
c++;
}
i++;
}
if (c == 0 || c == 2)
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n);
vector<int> v1(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
v1[i] = v[i];
}
sort(v.begin(), v.end());
int res = 0;
for (int i = 0; i < n; ++i) {
if (v1[i] != v[i]) {
++res;
}
}
if (res > 2) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int arr[100000], arr2[100000];
vector<int> idx;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
arr2[i] = arr[i];
}
sort(arr2, arr2 + n);
int nfalse = 0;
for (int i = 0; i < n; i++) {
if (arr[i] != arr2[i]) idx.push_back(i);
}
if ((idx.size() == 2 && arr[idx[0]] == arr2[idx[1]] &&
arr[idx[1]] == arr2[idx[0]]) ||
idx.size() == 0)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
const unsigned long long P = 239017, MaxN = 2100000, INF = 100000000;
using namespace std;
int n, arr[1200000], b[1200000];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &arr[i]), b[i] = arr[i];
}
sort(arr, arr + n);
int ans = 0;
for (int i = 0; i < n; ++i)
if (arr[i] != b[i]) ++ans;
if (ans == 2 || ans == 0)
printf("YES");
else
printf("NO");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, v, R, L, c;
vector<long long> vi, vs;
bool t;
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> v;
vi.push_back(v);
vs.push_back(vi[i]);
}
sort(vs.begin(), vs.end());
for (int i = 0; i < n; ++i) {
if (vi[i] != vs[i] && t == 0) {
R = i;
t = 1;
} else if (vi[i] != vs[i] && t == 1) {
L = i;
break;
}
}
swap(vi[R], vi[L]);
for (int i = 0; i < n; ++i) {
if (vi[i] == vs[i]) ++c;
}
if (c == n)
cout << "YES";
else
cout << "NO";
return 0;
}
| 9 | CPP |
def issort(l):
return all(l[i] <= l[i+1] for i in range(len(l)-1))
sa = int(input())
array = list(map(int, input().split(' ')))
array2 = array[:]
array2.sort()
count = 0
mislist=[]
for i in range(sa):
if array[i] != array2[i]:
count += 1
if count <= 2:
mislist.append(i)
if count > 2:
print("NO")
else:
if mislist == []:
print("YES")
elif len(mislist) == 1:
if issort(array):
print("YES")
else:
print("NO")
else:
array[mislist[0]], array[mislist[1]] = array[mislist[1]], array[mislist[0]]
if issort(array):
print("YES")
else:
print("NO")
| 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int a[maxn], b[maxn];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b + 1, b + n + 1);
int op = 0;
for (int i = n; i > 0; i--) {
if (a[i] != b[i]) {
op++;
for (int j = 1; j < i; j++) {
int t;
if (a[j] == b[i]) {
t = a[j];
a[j] = a[i];
a[i] = t;
break;
}
}
}
if (op > 1) break;
}
if (op > 1)
puts("NO");
else
puts("YES");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int arr[N], arr2[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
arr2[i] = arr[i];
}
sort(arr2 + 1, arr2 + n + 1);
vector<int> whatSwap;
for (int i = 1; i <= n; i++)
if (arr2[i] != arr[i]) whatSwap.push_back(i);
if (whatSwap.size() != 2 && whatSwap.size() != 0) {
cout << "NO\n";
return 0;
}
if (whatSwap.size() == 0) {
cout << "YES\n";
return 0;
}
swap(arr[whatSwap[0]], arr[whatSwap[1]]);
for (int i = 1; i <= n; i++) {
if (arr[i] != arr2[i]) {
cout << "NO\n";
return 0;
}
}
cout << "YES\n";
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<int> V1, V2;
int main() {
int n, i, s, a;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a);
V1.push_back(a);
V2.push_back(a);
}
sort(V2.begin(), V2.end());
s = 0;
for (i = 0; i < n; i++) s += (V1[i] != V2[i]);
if (s > 2)
printf("NO\n");
else
printf("YES\n");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[10000000], b[10010000];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
int p = 0;
sort(a, a + n);
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) p++;
}
if (p > 2)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
| 9 | CPP |
#include <bits/stdc++.h>
const long long int mod = 998244353;
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int i, n, cnt = 0;
cin >> n;
vector<long long int> a(n), b(n);
for (i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(a.begin(), a.end());
for (i = 0; i < n; i++) {
if (a[i] != b[i]) cnt++;
}
if ((cnt + 1) / 2 <= 1)
cout << "YES"
<< "\n";
else
cout << "NO"
<< "\n";
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
enum { MAXN = 100000, MINK = 1, MAXK = 1000000000 };
int a[MAXN + 2];
void solveTheProblem(void) {
int n;
scanf("%d", &n);
a[0] = MINK - 1;
a[n + 1] = MAXK + 1;
vector<int> bads;
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= n; ++i) {
if (!(a[i - 1] <= a[i] && a[i] <= a[i + 1])) {
bads.push_back(i);
}
}
if (bads.size() > 6) {
cout << "NO\n";
} else if (bads.size() == 0) {
cout << "YES\n";
} else {
for (int k = 0; k < bads.size(); ++k) {
for (int i = 1; i <= n; ++i) {
swap(a[i], a[bads[k]]);
bool ok = 1;
for (int j = 0; j < bads.size(); ++j) {
if (!(a[bads[j] - 1] <= a[bads[j]] && a[bads[j]] <= a[bads[j] + 1])) {
ok = 0;
break;
}
}
ok &= (a[i - 1] <= a[i] && a[i] <= a[i + 1]);
if (ok) {
cout << "YES\n";
return;
}
swap(a[i], a[bads[k]]);
}
}
cout << "NO\n";
}
}
int main(void) {
solveTheProblem();
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, cnt = 0;
cin >> n;
int arr1[n], arr2[n];
for (int i = 0; i < n; i++) cin >> arr1[i], arr2[i] = arr1[i];
sort(arr1, arr1 + n);
for (int i = 0; i < n; i++)
if (arr1[i] != arr2[i]) cnt++;
cnt > 2 ? cout << "NO" : cout << "YES";
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[100005] = {0};
int b[100005] = {0};
int main() {
long long n, maxx = 0, minn = 0, c = 0, l, x = 0, y = 0;
cin >> n;
for (int i = 0; i < (n); i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n);
for (int i = 0; i < (n); i++) {
if (a[i] != b[i]) maxx++;
if (maxx > 2) {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[100001], b[100001];
int t;
int main() {
int i, j, n, m;
while (scanf("%d", &n) != EOF) {
int flag = 0, cnt = 0;
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(a + 1, a + 1 + n);
for (i = 1; i <= n; i++) {
if (a[i] != b[i]) {
cnt++;
}
if (cnt >= 3) {
flag = 1;
break;
}
}
if (flag == 1)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int dr[] = {0, 0, 1, -1, -1, -1, 1, 1};
int dc[] = {1, -1, 0, 0, -1, 1, -1, 1};
int const MAX = 1e5 + 7;
int const MOD = 1e9 + 7;
int a[MAX], b[MAX];
int main() {
int n;
cin >> n;
int c = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n);
for (int i = 0; i < n; ++i)
if (a[i] != b[i]) c++;
if (c <= 2)
cout << "YES\n";
else
cout << "NO\n";
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
long a[100005], b[100005];
int main() {
long n, i;
cin >> n;
for (i = 0; i < n; i++) {
scanf("%ld", &a[i]);
b[i] = a[i];
}
sort(a, a + n);
long ans = 0;
for (i = 0; i < n; i++)
if (a[i] != b[i]) ++ans;
if (ans <= 2)
cout << "YES";
else
cout << "NO";
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[1000001], v[1000001];
int n;
int ans = 0, d;
int main() {
int i;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
v[i] = a[i];
}
sort(a + 1, a + 1 + n);
for (i = 1; i <= n; i++)
if (v[i] != a[i]) ans++;
if (ans <= 2)
printf("YES\n");
else
printf("NO\n");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, temp;
cin >> n;
std::vector<long long int> v, v1;
for (long long int i = 0; i < n; i++) {
cin >> temp;
v.push_back(temp);
v1.push_back(temp);
}
sort(v1.begin(), v1.end());
long long int count1 = 0;
bool flag1 = 0;
for (int i = 0; i < n; i++) {
if (v[i] != v1[i]) {
count1++;
if (count1 > 2) {
flag1 = 1;
break;
}
}
}
if (flag1 == 1) {
cout << "NO";
} else {
cout << "YES";
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n;
int a[maxn], x[maxn];
bool all_nodec() {
for (int i = 2; i <= n; ++i) {
if (x[i - 1] > x[i]) {
return false;
}
}
return true;
}
int k = 0;
bool ok() {
if (k == 1) {
return true;
} else if (k == 2) {
int p;
for (int i = 2; i <= n; ++i) {
if (x[i - 1] > x[i]) {
p = i;
break;
}
}
while (p + 1 <= n && x[p + 1] == x[p]) {
++p;
}
for (int i = 1; i <= n; ++i) {
if (x[i] > x[p]) {
swap(x[i], x[p]);
return all_nodec();
}
}
} else if (k == 3) {
int p1, p2;
for (int i = 1; i <= n; ++i) {
if (x[i] > x[i + 1]) {
p1 = i;
break;
}
}
for (int i = n; i >= 1; --i) {
if (x[i - 1] > x[i]) {
p2 = i;
break;
}
}
swap(x[p1], x[p2]);
return all_nodec();
}
return false;
}
int main() {
scanf("%d", &n);
a[0] = ~0u >> 1;
for (int i = 1; i <= n; ++i) {
scanf("%d", &x[i]);
if (a[k] > x[i]) {
a[++k] = x[i];
} else {
*upper_bound(&a[1], &a[k + 1], x[i], greater<int>()) = x[i];
}
}
printf("%s\n", ok() ? "YES" : "NO");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int mark[111111];
int a[111111];
int b[111111];
int n;
int noEle, cnt;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n + 1);
for (int i = 1; i <= n; i++) {
if (b[i] != a[i]) {
noEle++;
if (noEle % 2 != 0) cnt++;
if (cnt > 1) {
cout << "NO";
return 0;
}
}
}
cout << "YES";
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n], brr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
brr[i] = arr[i];
}
sort(brr, brr + n);
int c = 0;
for (int i = 0; i < n; i++) {
if (arr[i] != brr[i]) c++;
}
if (c <= 2)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, num, i, c = 0;
cin >> n;
vector<long long int> a, b;
for (i = 0; i < n; i++) {
cin >> num;
a.push_back(num);
b.push_back(num);
}
sort(a.begin(), a.end());
for (i = 0; i < n; i++) {
if (a[i] != b[i]) c++;
}
(c <= 2 ? cout << "YES" : cout << "NO");
}
| 9 | CPP |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.