# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1135886 | nathan4690 | Building 4 (JOI20_building4) | C++20 | 146 ms | 49520 KiB |
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define f1(i,n) for(int i=1;i<=n;i++)
#define __file_name ""
using namespace std;
const ll maxn = 1e6+5, inf=1e18;
ll n, a[2][maxn];
pair<ll,ll> f[2][maxn];
string s;
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
if(fopen(__file_name ".inp", "r")){
freopen(__file_name ".inp","r",stdin);
freopen(__file_name ".out","w",stdout);
}
// code here
cin >> n;
n *= 2;
f1(i,n) cin >> a[0][i];
f1(i,n) cin >> a[1][i];
f1(i,n){
// For position i
// f[0][i]: choose A
// f[1][i]: choose B
f[0][i] = f[1][i] = {inf, -inf};
for(int x=0;x<=1;x++) for(int y=0;y<=1;y++){
if(a[x][i-1] <= a[y][i]){
f[y][i].first = min(f[y][i].first, (x == y || f[x][i-1].first == inf ? f[x][i-1].first : i - 1 - f[x][i-1].second) + 1);
f[y][i].second = max(f[y][i].second, (x == y || f[x][i-1].second == -inf ? f[x][i-1].second : i - 1 - f[x][i-1].first) + 1);
}
}
// cout << f[0][i].first << ' ' << f[0][i].second << " - " << f[1][i].first << ' ' << f[1][i].second << '\n';
}
int y = 0;
a[0][n+1] = inf;
ll c[2] = {n/2, n/2};
for(int i=n;i>=1;i--){
bool flag = false;
for(int x=0;x<=1;x++){
// cout << i << ' ' << x << ' ' << f[x][i].first << ' ' << f[x][i].second << ' ' << c[0] << ' ' << c[1] << '\n';
if(a[x][i] <= a[y][i+1]){
if(f[x][i].first <= c[x] && c[x] <= f[x][i].second){
y = x;
flag = true;
c[x]--;
// if(x == y) c[x]--;
// else c[y]--;
break;
}
// f[y][i].first = min(f[y][i].first, f[x][i-1].first + (x == y));
// f[y][i].second = max(f[y][i].second, f[x][i-1].second + (x == y));
}
}
// cout << i << ' ' << s << endl;
if(!flag){
cout << -1;
return 0;
}
s += 'A' + y;
}
reverse(s.begin(), s.end());
cout << s;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |