// incorrect/author-solexist.cpp
#include "hieroglyphs.h"
#include<bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
//erases non-common elements
void clean(vi& a, vi& b) {
vi ap;
vi bp;
set<int> as;
set<int> bs;
for (int x : a) as.insert(x);
for (int x : b) bs.insert(x);
for (int x : a) if (bs.count(x)) ap.push_back(x);
for (int x : b) if (as.count(x)) bp.push_back(x);
swap(a, ap);
swap(b, bp);
}
map<int, int> coordinate_compress(vi& a, vi& b) {
int cc = 0;
map<int, int> mp;
map<int, int> rmp;
for (int& x : a) {
if (!mp.count(x)) {
mp[x] = cc++;
rmp[mp[x]] = x;
}
x = mp[x];
}
for (int& x : b) {
if (!mp.count(x)) {
mp[x] = cc++;
rmp[mp[x]] = x;
}
x = mp[x];
}
return rmp;
}
bool is_subsequence(const vi& a, const vi& b) {
int j = 0;
for (int x : a) {
if (j < (int)b.size() && b[j] == x) {
j++;
}
}
return j == (int)b.size();
}
vector<int> get_candidate(vector<int> a, vector<int> b) {
int n = a.size();
int m = b.size();
vi occ_a(max(n, m)+1, 0);
vi occ_b(max(n, m)+1, 0);
for (int i=0; i < n; ++i) {
occ_a[a[i]]++;
}
for (int i=0; i < m; ++i) {
occ_b[b[i]]++;
}
vi c;
queue<int> qa;
queue<int> qb;
for (int i=0; i < n; ++i) {
if (occ_a[a[i]] <= occ_b[a[i]]) {
qa.push(i);
}
}
for (int i=0; i < m; ++i) {
if (occ_a[b[i]] > occ_b[b[i]]) {
qb.push(i);
}
}
int i_a_curr = 0;
int i_b_curr = 0;
int i_a_next = 0;
int i_b_next = 0;
vi occ_a_curr = vi(occ_a);
vi occ_a_next = vi(occ_a);
vi occ_b_curr = vi(occ_b);
vi occ_b_next = vi(occ_b);
while(!qa.empty() && !qb.empty()) {
while(i_a_next < qa.front()) {
occ_a_next[a[i_a_next]]--;
i_a_next++;
}
while(i_b_next < qb.front()) {
occ_b_next[b[i_b_next]]--;
i_b_next++;
}
int x = a[i_a_next];
int y = b[i_b_next];
int occ_x = occ_a_next[x];
int occ_y = occ_b_next[y];
bool a_good = (occ_a_next[y] >= occ_y && occ_b_curr[x] > occ_b_next[x]);
bool b_good = (occ_b_next[x] >= occ_x && occ_a_curr[y] > occ_a_next[y]);
if (a_good && b_good) return vi();
if (!a_good && !b_good) return vi();
if(a_good) {
c.push_back(x);
qa.pop();
while(i_a_curr <= i_a_next) {
occ_a_curr[a[i_a_curr]]--;
i_a_curr++;
}
while(b[i_b_curr] != x) {
occ_b_curr[b[i_b_curr]]--;
i_b_curr++;
}
occ_b_curr[b[i_b_curr]]--;
i_b_curr++;
}
else {
c.push_back(y);
qb.pop();
while(i_b_curr <= i_b_next) {
occ_b_curr[b[i_b_curr]]--;
i_b_curr++;
}
while(a[i_a_curr] != y) {
occ_a_curr[a[i_a_curr]]--;
i_a_curr++;
}
occ_a_curr[a[i_a_curr]]--;
i_a_curr++;
}
}
while(!qa.empty()) {
c.push_back(a[qa.front()]);
qa.pop();
}
while(!qb.empty()) {
c.push_back(b[qb.front()]);
qb.pop();
}
return ((is_subsequence(a, c) && is_subsequence(b, c)) ? c : vi());
}
//returns v of size |b|
//v[i] = smallest index so that there exists a common subsequence of a[0..v[i]] and b[0..i] that is not
// a common subsequence of a[0..v[i]] and b[0..i-1]. if such index does not exist, v[i] = |a|
vector<int> index_vector(const vi& a, const vi& b) {
int n = a.size();
int m = b.size();
vi v(m);
int max_v = 0;
for (int x : a) max_v = max(x, max_v);
for (int x : b) max_v = max(x, max_v);
vi prev_occ_b(max_v+1, -1);
vvi a_occ(max_v+1);
for (int i=0; i < n; ++i) {
a_occ[a[i]].push_back(i);
}
for (int i=0; i <= max_v; ++i) {
a_occ[i].push_back(n);
}
vector<pair<int, int>> min_stack;
for (int i=0; i < m; ++i) {
if (prev_occ_b[b[i]] == -1) {
v[i] = a_occ[b[i]][0];
}
else {
int min_val = lower_bound(min_stack.begin(), min_stack.end(), pair<int, int>(prev_occ_b[b[i]], -1))->second;
if (min_val < n) v[i] = *lower_bound(a_occ[b[i]].begin(), a_occ[b[i]].end(), min_val+1);
else v[i] = n;
}
while(!min_stack.empty() && min_stack.back().second >= v[i]) {
min_stack.pop_back();
}
min_stack.emplace_back(i, v[i]);
prev_occ_b[b[i]] = i;
}
return v;
}
// RMQ template from KACTL
template<class T>
struct RMQ {
vector<vector<T>> jmp;
RMQ(const vector<T>& V) : jmp(1, V) {
for (int pw = 1, k = 1; pw * 2 <= (int)V.size(); pw *= 2, ++k) {
jmp.emplace_back((int)V.size() - pw * 2 + 1);
for (int j=0; j < (int)jmp[k].size(); ++j)
jmp[k][j] = min(jmp[k - 1][j], jmp[k - 1][j + pw]);
}
}
T query(int a, int b) {
int dep = 31 - __builtin_clz(b - a);
return min(jmp[dep][a], jmp[dep][b - (1 << dep)]);
}
};
bool no_single_crossing(const vi& a, const vi& b, const vi& c) {
int n = a.size();
int m = b.size();
int l = c.size();
vi rb = vi(b);
reverse(rb.begin(), rb.end());
vi rc = vi(c);
reverse(rc.begin(), rc.end());
vi v_ab = index_vector(a, b);
vi v_rbc = index_vector(rb, rc);
RMQ<int> v_rbc_rmq = RMQ<int>(v_rbc);
vi v_max_rc_i(n+1);
v_max_rc_i[n] = 0;
for (int i=n-1; i >= 0; --i) {
v_max_rc_i[i] = v_max_rc_i[i+1];
if (a[i] == rc[v_max_rc_i[i+1]]) {
v_max_rc_i[i]++;
}
if (v_max_rc_i[i] > l) v_max_rc_i[i] = l;
}
vi v_min_rc_i(m+1);
v_min_rc_i[0] = l-1;
for (int i=0; i < m; ++i) {
v_min_rc_i[i+1] = v_min_rc_i[i];
if (b[i] == rc[v_min_rc_i[i]]) {
v_min_rc_i[i+1]--;
}
if (v_min_rc_i[i] < -1) v_min_rc_i[i] = -1;
}
for (int j=0; j < m; ++j) {
int ai = v_ab[j];
if (ai == n) continue;
int min_rc_i = v_min_rc_i[j+1];
int max_rc_i = v_max_rc_i[ai+1];
if (min_rc_i+1 < max_rc_i && v_rbc_rmq.query(min_rc_i+1, max_rc_i) + j < m-1) return false;
}
return true;
}
bool verify(const vi& a, const vi& b, const vi& c) {
if (c.empty()) return false;
return no_single_crossing(a, b, c) && no_single_crossing(b, a, c);
}
vector<int> ucs(vector<int> a, vector<int> b) {
clean(a, b);
if (a.empty() || b.empty()) {
return vector<int>();
}
map<int, int> mp = coordinate_compress(a, b);
vi c = get_candidate(a, b);
for (int& x : c) x = mp[x];
return c;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
6328 KB |
Output is correct |
2 |
Correct |
37 ms |
6336 KB |
Output is correct |
3 |
Correct |
24 ms |
5736 KB |
Output is correct |
4 |
Correct |
26 ms |
5476 KB |
Output is correct |
5 |
Correct |
49 ms |
6240 KB |
Output is correct |
6 |
Correct |
14 ms |
4236 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
6328 KB |
Output is correct |
2 |
Correct |
37 ms |
6336 KB |
Output is correct |
3 |
Correct |
24 ms |
5736 KB |
Output is correct |
4 |
Correct |
26 ms |
5476 KB |
Output is correct |
5 |
Correct |
49 ms |
6240 KB |
Output is correct |
6 |
Correct |
14 ms |
4236 KB |
Output is correct |
7 |
Correct |
190 ms |
10816 KB |
Output is correct |
8 |
Correct |
173 ms |
10848 KB |
Output is correct |
9 |
Correct |
163 ms |
10836 KB |
Output is correct |
10 |
Correct |
171 ms |
10780 KB |
Output is correct |
11 |
Correct |
168 ms |
10936 KB |
Output is correct |
12 |
Correct |
159 ms |
10988 KB |
Output is correct |
13 |
Correct |
166 ms |
10788 KB |
Output is correct |
14 |
Correct |
153 ms |
10912 KB |
Output is correct |
15 |
Correct |
147 ms |
10768 KB |
Output is correct |
16 |
Correct |
156 ms |
10928 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
98 ms |
6136 KB |
Output is correct |
22 |
Correct |
129 ms |
10244 KB |
Output is correct |
23 |
Correct |
121 ms |
10284 KB |
Output is correct |
24 |
Correct |
153 ms |
10384 KB |
Output is correct |
25 |
Correct |
76 ms |
6588 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
604 KB |
Output is correct |
34 |
Correct |
7 ms |
1844 KB |
Output is correct |
35 |
Correct |
28 ms |
6004 KB |
Output is correct |
36 |
Correct |
46 ms |
6988 KB |
Output is correct |
37 |
Correct |
41 ms |
6460 KB |
Output is correct |
38 |
Correct |
54 ms |
6396 KB |
Output is correct |
39 |
Correct |
30 ms |
6324 KB |
Output is correct |
40 |
Correct |
126 ms |
9624 KB |
Output is correct |
41 |
Correct |
82 ms |
7320 KB |
Output is correct |
42 |
Correct |
45 ms |
4924 KB |
Output is correct |
43 |
Correct |
28 ms |
5112 KB |
Output is correct |
44 |
Correct |
25 ms |
4956 KB |
Output is correct |
45 |
Correct |
23 ms |
4656 KB |
Output is correct |
46 |
Correct |
127 ms |
8456 KB |
Output is correct |
47 |
Correct |
111 ms |
8408 KB |
Output is correct |
48 |
Correct |
135 ms |
8364 KB |
Output is correct |
49 |
Correct |
132 ms |
8288 KB |
Output is correct |
50 |
Correct |
108 ms |
8284 KB |
Output is correct |
51 |
Correct |
138 ms |
8360 KB |
Output is correct |
52 |
Correct |
118 ms |
7816 KB |
Output is correct |
53 |
Correct |
109 ms |
7736 KB |
Output is correct |
54 |
Correct |
98 ms |
7604 KB |
Output is correct |
55 |
Correct |
15 ms |
2384 KB |
Output is correct |
56 |
Correct |
63 ms |
6360 KB |
Output is correct |
57 |
Correct |
37 ms |
5940 KB |
Output is correct |
58 |
Correct |
43 ms |
6432 KB |
Output is correct |
59 |
Correct |
83 ms |
6676 KB |
Output is correct |
60 |
Correct |
106 ms |
7340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |