Description

20-3-24-1

LOJ3271

Solution

首先不难有一个O(n2)O(n^2)的DP: 设f(i,0/1,j)f(i, 0/1, j)表示考虑到第ii个数,第ii个数是Ai/BiA_i/B_i,且总共选了jj个A元素,是否可行

通过简单猜测+打表仔细分析,不难发现jj这一维为1的位置是连续的一段,于是只需要记录值为11的位置的左端点和右端点即可

Code

考场代码,比较丑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <climits>
#include <queue>
#include <set>
#include <cmath>
#include <map>
#include <bitset>
#include <fstream>
#include <tr1/unordered_map>
#include <assert.h>

#define x first
#define y second
#define y0 Y0
#define y1 Y1
#define mp make_pair
#define pb push_back
#define DEBUG(x) cout << #x << " = " << x << endl;

using namespace std;

typedef long long LL;
typedef pair <int, int> pii;

template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }
template <typename T> inline T read ()
{
T sum = 0, fl = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

inline void proc_status ()
{
ifstream t ("/proc/self/status");
cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) << endl;
}

const int MAXN = 1e6;

int N, A[MAXN + 5], B[MAXN + 5];

int L[MAXN + 5][2], R[MAXN + 5][2];
vector <pii> lst[MAXN + 5][2][2];

inline void Solve ()
{
L[1][0] = 1, R[1][0] = 1;
L[1][1] = 0, R[1][1] = 0;

for (int i = 2; i <= N; ++i)
{
L[i][0] = L[i][1] = N + 1, R[i][0] = R[i][1] = -1;

if (A[i] >= A[i - 1] && L[i - 1][0] != -1)
{
Chkmin (L[i][0], L[i - 1][0] + 1);
Chkmax (R[i][0], R[i - 1][0] + 1);
lst[i][0][0].pb (mp (L[i - 1][0] + 1, R[i - 1][0] + 1));
}

if (A[i] >= B[i - 1] && L[i - 1][1] != -1)
{
Chkmin (L[i][0], L[i - 1][1] + 1);
Chkmax (R[i][0], R[i - 1][1] + 1);
lst[i][0][1].pb (mp (L[i - 1][1] + 1, R[i - 1][1] + 1));
}

if (B[i] >= A[i - 1] && L[i - 1][0] != -1)
{
Chkmin (L[i][1], L[i - 1][0]);
Chkmax (R[i][1], R[i - 1][0]);
lst[i][1][0].pb (mp (L[i - 1][0], R[i - 1][0]));
}

if (B[i] >= B[i - 1] && L[i - 1][1] != -1)
{
Chkmin (L[i][1], L[i - 1][1]);
Chkmax (R[i][1], R[i - 1][1]);
lst[i][1][1].pb (mp (L[i - 1][1], R[i - 1][1]));
}
}

if ((L[N][0] <= N / 2 && N / 2 <= R[N][0]) || (L[N][1] <= N / 2 && N / 2 <= R[N][1]))
{
vector <int> ans;

int i = N, j = N / 2, p = (L[N][0] <= N / 2 && N / 2 <= R[N][0]) ? 0 : 1;

while (i >= 1)
{
ans.pb (p);
if (i == 1)
{
break;
}
if (!p)
{
for (pii t : lst[i][0][0])
{
if (t.x <= j && j <= t.y)
{
p = 0;
--j, --i;
goto NXT;
}
}
for (pii t : lst[i][0][1])
{
if (t.x <= j && j <= t.y)
{
p = 1;
--j, --i;
goto NXT;
}
}
}
else
{
for (pii t : lst[i][1][0])
{
if (t.x <= j && j <= t.y)
{
p = 0;
--i;
goto NXT;
}
}
for (pii t : lst[i][1][1])
{
if (t.x <= j && j <= t.y)
{
p = 1;
--i;
goto NXT;
}
}

}

NXT:;
}

reverse (ans.begin(), ans.end());

for (int i = 0; i < ans.size(); ++i) printf("%c", ans[i] ? 'B' : 'A');
}
else puts("-1");
}

inline void Input ()
{
N = read<int>() * 2;
for (int i = 1; i <= N; ++i) A[i] = read<int>();
for (int i = 1; i <= N; ++i) B[i] = read<int>();
}

int main ()
{

#ifdef hk_cnyali
freopen("building.in", "r", stdin);
freopen("building.out", "w", stdout);
#endif

Input ();
Solve ();

return 0;
}