// incorrect/felix-one-alternation-heuristic.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());
}
bool verify_greedy(vector<int> a, vector<int> b, vector<int> c) {
int n = a.size();
int m = b.size();
int l = c.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 cv(l, n);
int bi = 0;
int ci = 0;
for (int i=0; i < n; ++i) {
if (occ_a[a[i]] >= occ_b[a[i]]) {
while(bi < m && b[bi] != a[i]) bi++;
if (bi == m) break;
bi++;
while (ci < l && c[ci] != a[i]) ci++;
cv[ci] = i;
ci++;
}
}
int aj = n-1;
int mina = n;
int cj = l-1;
for (int j=m-1; j > -1; --j) {
if (occ_b[b[j]] >= occ_a[b[j]]) {
while (cj > -1 && c[cj] != b[j]) {
mina = min(mina, cv[cj]);
cj--;
}
if (cj < 0) break;
cj--;
while (aj > -1 && a[aj] != b[j]) {
aj--;
}
if (aj < 0) break;
if (aj > mina) return false;
aj--;
}
else {
while (cj > -1 && c[cj] != b[j]) cj--;
if (cj < 0) break;
cj--;
}
}
return true;
}
bool verify(vector<int> a, vector<int> b, vector<int> c) {
if (c.empty()) return false;
return verify_greedy(a, b, c) && verify_greedy(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);
if (verify(a, b, c)) {
for (int& x : c) x = mp[x];
return c;
}
return {-1};
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
360 KB |
Output is correct |
5 |
Correct |
258 ms |
16816 KB |
Output is correct |
6 |
Correct |
292 ms |
15604 KB |
Output is correct |
7 |
Correct |
233 ms |
15596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
360 KB |
Output is correct |
5 |
Correct |
258 ms |
16816 KB |
Output is correct |
6 |
Correct |
292 ms |
15604 KB |
Output is correct |
7 |
Correct |
233 ms |
15596 KB |
Output is correct |
8 |
Correct |
171 ms |
11668 KB |
Output is correct |
9 |
Correct |
169 ms |
11452 KB |
Output is correct |
10 |
Correct |
157 ms |
11656 KB |
Output is correct |
11 |
Correct |
170 ms |
11672 KB |
Output is correct |
12 |
Correct |
199 ms |
11628 KB |
Output is correct |
13 |
Correct |
192 ms |
11672 KB |
Output is correct |
14 |
Correct |
163 ms |
11672 KB |
Output is correct |
15 |
Correct |
154 ms |
11672 KB |
Output is correct |
16 |
Correct |
162 ms |
11448 KB |
Output is correct |
17 |
Correct |
155 ms |
11472 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
68 ms |
6532 KB |
Output is correct |
23 |
Correct |
134 ms |
10876 KB |
Output is correct |
24 |
Correct |
130 ms |
10812 KB |
Output is correct |
25 |
Correct |
128 ms |
10816 KB |
Output is correct |
26 |
Correct |
79 ms |
6540 KB |
Output is correct |
27 |
Correct |
157 ms |
11472 KB |
Output is correct |
28 |
Correct |
153 ms |
11668 KB |
Output is correct |
29 |
Incorrect |
171 ms |
11668 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
7096 KB |
Output is correct |
2 |
Correct |
32 ms |
7360 KB |
Output is correct |
3 |
Correct |
39 ms |
6320 KB |
Output is correct |
4 |
Correct |
23 ms |
5996 KB |
Output is correct |
5 |
Correct |
31 ms |
6732 KB |
Output is correct |
6 |
Correct |
12 ms |
4236 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Incorrect |
20 ms |
6328 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
171 ms |
11668 KB |
Output is correct |
2 |
Correct |
169 ms |
11452 KB |
Output is correct |
3 |
Correct |
157 ms |
11656 KB |
Output is correct |
4 |
Correct |
170 ms |
11672 KB |
Output is correct |
5 |
Correct |
199 ms |
11628 KB |
Output is correct |
6 |
Correct |
192 ms |
11672 KB |
Output is correct |
7 |
Correct |
163 ms |
11672 KB |
Output is correct |
8 |
Correct |
154 ms |
11672 KB |
Output is correct |
9 |
Correct |
162 ms |
11448 KB |
Output is correct |
10 |
Correct |
155 ms |
11472 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
68 ms |
6532 KB |
Output is correct |
16 |
Correct |
134 ms |
10876 KB |
Output is correct |
17 |
Correct |
130 ms |
10812 KB |
Output is correct |
18 |
Correct |
128 ms |
10816 KB |
Output is correct |
19 |
Correct |
79 ms |
6540 KB |
Output is correct |
20 |
Correct |
31 ms |
7096 KB |
Output is correct |
21 |
Correct |
32 ms |
7360 KB |
Output is correct |
22 |
Correct |
39 ms |
6320 KB |
Output is correct |
23 |
Correct |
23 ms |
5996 KB |
Output is correct |
24 |
Correct |
31 ms |
6732 KB |
Output is correct |
25 |
Correct |
12 ms |
4236 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 |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
2 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
604 KB |
Output is correct |
34 |
Correct |
9 ms |
2060 KB |
Output is correct |
35 |
Correct |
34 ms |
6668 KB |
Output is correct |
36 |
Correct |
47 ms |
7860 KB |
Output is correct |
37 |
Correct |
49 ms |
7476 KB |
Output is correct |
38 |
Correct |
32 ms |
6848 KB |
Output is correct |
39 |
Correct |
31 ms |
7480 KB |
Output is correct |
40 |
Correct |
149 ms |
9896 KB |
Output is correct |
41 |
Correct |
98 ms |
7884 KB |
Output is correct |
42 |
Correct |
35 ms |
5072 KB |
Output is correct |
43 |
Correct |
24 ms |
5112 KB |
Output is correct |
44 |
Correct |
26 ms |
5124 KB |
Output is correct |
45 |
Correct |
23 ms |
4564 KB |
Output is correct |
46 |
Correct |
127 ms |
8644 KB |
Output is correct |
47 |
Correct |
137 ms |
8900 KB |
Output is correct |
48 |
Correct |
122 ms |
8876 KB |
Output is correct |
49 |
Correct |
146 ms |
8632 KB |
Output is correct |
50 |
Correct |
120 ms |
8612 KB |
Output is correct |
51 |
Correct |
112 ms |
8792 KB |
Output is correct |
52 |
Correct |
118 ms |
8112 KB |
Output is correct |
53 |
Correct |
109 ms |
8112 KB |
Output is correct |
54 |
Correct |
100 ms |
8112 KB |
Output is correct |
55 |
Correct |
15 ms |
2476 KB |
Output is correct |
56 |
Correct |
40 ms |
7240 KB |
Output is correct |
57 |
Correct |
38 ms |
6720 KB |
Output is correct |
58 |
Correct |
53 ms |
7468 KB |
Output is correct |
59 |
Correct |
76 ms |
7676 KB |
Output is correct |
60 |
Correct |
104 ms |
7812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
4 ms |
604 KB |
Output is correct |
22 |
Correct |
3 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
360 KB |
Output is correct |
8 |
Correct |
258 ms |
16816 KB |
Output is correct |
9 |
Correct |
292 ms |
15604 KB |
Output is correct |
10 |
Correct |
233 ms |
15596 KB |
Output is correct |
11 |
Correct |
171 ms |
11668 KB |
Output is correct |
12 |
Correct |
169 ms |
11452 KB |
Output is correct |
13 |
Correct |
157 ms |
11656 KB |
Output is correct |
14 |
Correct |
170 ms |
11672 KB |
Output is correct |
15 |
Correct |
199 ms |
11628 KB |
Output is correct |
16 |
Correct |
192 ms |
11672 KB |
Output is correct |
17 |
Correct |
163 ms |
11672 KB |
Output is correct |
18 |
Correct |
154 ms |
11672 KB |
Output is correct |
19 |
Correct |
162 ms |
11448 KB |
Output is correct |
20 |
Correct |
155 ms |
11472 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
68 ms |
6532 KB |
Output is correct |
26 |
Correct |
134 ms |
10876 KB |
Output is correct |
27 |
Correct |
130 ms |
10812 KB |
Output is correct |
28 |
Correct |
128 ms |
10816 KB |
Output is correct |
29 |
Correct |
79 ms |
6540 KB |
Output is correct |
30 |
Correct |
157 ms |
11472 KB |
Output is correct |
31 |
Correct |
153 ms |
11668 KB |
Output is correct |
32 |
Incorrect |
171 ms |
11668 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |