题解:CF2259E Treasure Map Destruction (Constructive Version)
Charged_Charge · · 题解
思路:
首先,如果位置
AC 代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 10;
int t, n, a[N], s[N], ls[N], rs[N];
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> t;
while(t--){
cin >> n;
for(int i = 0; i <= n; i++) s[i] = 0, ls[i] = 1e9, rs[i] = 1e9;
for(int i = 1; i <= n; i++) cin >> a[i];
bool ok = true;
for(int i = 1; i <= n; i++){
if(a[i] == -1 || a[i] == 0) continue;
if(a[max(i - a[i], 1)] != -1 && a[min(i + a[i], n)] != -1){
if(a[max(i - a[i], 1)] != 0 && a[min(i + a[i], n)] != 0){
ok = false;
break;
}
}
s[max(i - a[i] + 1, 1)]++;
s[min(i + a[i], n + 1)]--;
}
if(!ok){
cout << -1 << '\n';
continue;
}
for(int i = 1; i <= n; i++) s[i] = s[i - 1] + s[i];
for(int i = 1; i <= n; i++){
if(a[i] == 0 && s[i] == 1){
ok = false;
break;
}
}
if(!ok){
cout << -1 << '\n';
continue;
}
for(int i = 1; i <= n; i++) if(s[i] == 0 && a[i] == -1) a[i] = 0;
int lt = -1e9;
for(int i = 1; i <= n; i++){
if(a[i] == 0) lt = i;
ls[i] = i - lt;
}
int nx = 1e9;
for(int i = n; i >= 1; i--){
if(a[i] == 0) nx = i;
rs[i] = nx - i;
}
for(int i = 1; i <= n; i++){
if(a[i] == -1) continue;
if(min(ls[i], rs[i]) != a[i]){
ok = false;
break;
}
}
if(!ok) cout << -1;
else for(int i = 1; i <= n; i++) cout << (a[i] == 0 ? 1 : 0);
cout << '\n';
}
return 0;
}