Codeforces Round #771 (Div. 2)
A - Reverse
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 510;
int n;
int a[N];
void print()
{
for (int i = 1; i <= n; i ++ ) printf("%d ", a[i]);
puts("");
}
int main()
{
int T;
scanf("%d", &T);
while (T -- )
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
bool flag = true;
int l = 0, r = 0;
for (int i = 1; i <= n; i ++ )
if (i != a[i])
{
flag = false;
l = i;
break;
}
if (flag) print();
else
{
for (int i = l + 1; i <= n; i ++ )
if (a[i] == l)
r = i;
for (int i = l, j = r; i < j; i ++ , j -- ) swap(a[i], a[j]);
print();
}
}
return 0;
}
B - Odd Swap Sort
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int n;
int a[N];
int odd[N], even[N];
int cnt1, cnt2;
int main()
{
int T;
scanf("%d", &T);
while (T -- )
{
scanf("%d", &n);
cnt1 = cnt2 = 0;
for (int i = 0; i < n; i ++ )
{
scanf("%d", &a[i]);
if (a[i] % 2) odd[cnt1 ++ ] = a[i];
else even[cnt2 ++ ] = a[i];
}
// 分别扫描奇数组和偶数组是否有序
bool flag = true;
for (int i = 0; i < cnt1 - 1; i ++ )
if (odd[i] > odd[i + 1])
{
flag = false;
break;
}
for (int i = 0; i < cnt2 - 1 && flag; i ++ )
if (even[i] > even[i + 1])
{
flag = false;
break;
}
if (!flag) puts("No");
else puts("Yes");
}
return 0;
}
C - Inversion Graph
自己的代码,最终TLE了
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int n, idx;
int q[N], p[N], tmp[N];
int find(int x)
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
void merge_sort(int l, int r)
{
if (l >= r) return;
int mid = l + r >> 1;
merge_sort(l, mid), merge_sort(mid + 1, r);
int k = 0, i = l, j = mid + 1;
while (i <= mid && j <= r)
if (q[i] <= q[j]) tmp[k ++ ] = q[i ++ ];
else
{
int pj = find(q[j]);
for (int t = i; t <= mid; t ++ )
{
int pt = find(q[t]);
if (pt != pj) p[pt] = pj;
}
tmp[k ++ ] = q[j ++ ];
}
while (i <= mid) tmp[k ++ ] = q[i ++ ];
while (j <= r) tmp[k ++] = q[j ++ ];
for (int i = l, j = 0; i <= r; i ++ , j ++ ) q[i] = tmp[j];
}
int main()
{
int T;
scanf("%d", &T);
while (T -- )
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++ )
{
scanf("%d", &q[i]);
p[i] = i;
}
merge_sort(1, n);
int cnt = 1;
for (int i = 1; i < n; i ++ )
if (find(i) != find(i + 1)) cnt ++ ;
printf("%d\n", cnt);
}
return 0;
}
AC代码
#include<iostream>
using namespace std;
int main(){
int t;
cin >> t;
while (t--) {
int n, x, y=0,ans = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x;
y = max(y, x);
if (y == i) ans++, cout << i << endl;
}
cout << ans << endl;
}
return 0;
}