传送门: https://ac.nowcoder.com/acm/contest/9982/F
题意
给一个序列,让你找一个k,k代表区间,从左到右可以将区间翻转,问可以不可以将整个序列升序排列。
思路
赛中的这道题,大多数都是暴力过的,可能是数据太弱了。
一开始就能想到的就是splay区间翻转了。
Code
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
const int N = 1e5 + 10;
int a[N];
int n, cnt, root;
struct Node {
int ch[2];
int val, fa;
int siz, tag;
} t[N];
void update(int x) {
t[x].siz = t[t[x].ch[0]].siz + t[t[x].ch[1]].siz + 1;
}
void push_down(int x) {
if (x && t[x].tag) {
t[t[x].ch[0]].tag ^= 1;
t[t[x].ch[1]].tag ^= 1;
swap(t[x].ch[0], t[x].ch[1]);
t[x].tag = 0;
}
}
int id(int x) {
return x == t[t[x].fa].ch[1];
}
void connect(int fa, int x, int d) {
t[x].fa = fa;
t[fa].ch[d] = x;
}
void rotate(int x) {
int f = t[x].fa;
int ff = t[f].fa;
push_down(x);
push_down(f);
int fson = id(x);
int ffson = id(f);
int son = t[x].ch[fson ^ 1];
connect(f, son, fson);
connect(x, f, fson ^ 1);
connect(ff, x, ffson);
update(f), update(x);
}
void splay(int x, int to) { // 将 x 转到 to 的子节点位置
while (t[x].fa != to) {
int f = t[x].fa;
if (t[f].fa != to) {
rotate(id(x) == id(f) ? f : x);
}
rotate(x);
}
if (!to) { // 在 splay(l, 0) 时起作用
root = x;
}
}
int build(int l, int r, int fa) {
if (l > r) {
return 0;
}
int mid = (l + r) >> 1;
int x = ++cnt;
t[x].fa = fa;
t[x].siz = 1;
t[x].val = a[mid];
t[x].ch[0] = build(l, mid - 1, x);
t[x].ch[1] = build(mid + 1, r, x);
update(x);
return x;
}
int find(int rank) {
int x = root;
while (1) {
push_down(x);
if (rank <= t[t[x].ch[0]].siz) {
x = t[x].ch[0];
} else {
rank -= t[t[x].ch[0]].siz + 1;
if (!rank) {
return x;
}
x = t[x].ch[1];
}
}
}
void reverse(int l, int r) {
l = find(l);
r = find(r);
splay(l, 0);
splay(r, l);
int pos = t[t[root].ch[1]].ch[0];
t[pos].tag ^= 1;
}
void print(int x) {
if (!x) {
return;
}
push_down(x);
print(t[x].ch[0]);
if (t[x].val != INF && t[x].val != -INF) {
printf("%d ", t[x].val);
}
print(t[x].ch[1]);
}
void solve(){
int k = 0;
cin >> n;
for (int i = 1;i <= n; ++i) {
cin >> a[i + 1];
}
a[1] = -INF; a[n + 2] = INF;
for (int i = 1; i <= n; ++i) {
if (i != a[i + 1]) {
for (int j = i + 1; j <= n; ++j) {
if (i == a[j + 1]) {
k = j - i + 1;
break;
}
}
break;
}
}
//cout<<k<<endl;
if (k == 0) {
cout<< "yes" << endl << 1 << endl;
}
else{
root=build(1, n + 2, 0);
bool flag = false;
for(int i = 1;i <= n - k + 1; i++) {
if(t[find(i + 1)].val != i) {
if(t[find(i + k)].val != i) {
cout << "no" << endl;
return ;
}
reverse(i, i + k + 1);
}
}
cout << "yes" << endl << k << endl;
}
}
signed main() {
solve();
}