#include "hieroglyphs.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define fi first
#define se second
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif
vi VEC;
bool check_one(vi a, vi b){
int n = sz(a);
int m = sz(b);
int cntA[2] = {0, 0};
int cntB[2] = {0, 0};
for(auto ele : a){
cntA[ele]++;
}
for(auto ele : b){
cntB[ele]++;
}
VEC = {};
if(cntA[0] > cntB[0] || (cntA[0] == cntB[0] && cntA[1] > cntB[1])){
swap(a, b);
swap(n, m);
swap(cntA[0], cntB[0]);
swap(cntA[1], cntB[1]);
}
if(cntA[1] <= cntB[1]){
int wsk = 0;
for(auto ele : b){
if(wsk == n){
return true;
}
if(ele == a[wsk]){
wsk++;
}
}
if(wsk == n){
return true;
}
return false;
}
// debug(a);
// debug(b);
int wska = 0;
int wskb = 0;
int cnt = 0;
int target = cntA[0] + cntB[1];
int t[2] = {cntA[0], cntB[1]};
while(cnt < target){
//cerr << "===\n";
//cerr << wska << ' ' << wskb << ' ' << cnt << '\n';
//cerr << min(cntA[a[wska]], cntB[a[wska]]) << ' ' << t[a[wska]] << '\n';
// cerr << a[wska] << ' ' << b[wskb] << '\n';
if(wska == n || wskb == m){
return false;
}
if(a[wska] == b[wskb]){
VEC.pb(a[wska]);
cnt++;
cntA[a[wska]]--;
cntB[b[wskb]]--;
t[a[wska]]--;
wska++;
wskb++;
continue;
}
if(cntA[a[wska]] == t[a[wska]]){
cntB[b[wskb]]--;
wskb++;
}else{
cntA[a[wska]]--;
wska++;
}
}
return true;
}
vi ucs(vi A, vi B){
int n = sz(A);
int m = sz(B);
vi ret = {-1};
// for(int b = 0; b < 19; b++){
// vi na = {};
// vi nb = {};
// for(auto ele : A){
// if(ele & (1 << b)){
// na.pb(1);
// }else{
// na.pb(0);
// }
// }
// for(auto ele : B){
// if(ele & (1 << b)){
// nb.pb(1);
// }else{
// nb.pb(0);
// }
// }
// //debug(b);
// //debug(na);
// // debug(nb);
// if(!check_one(na, nb)){
// return ret;
// }
// }
int maks = 0;
for(auto ele : A){
maks = max(maks, ele);
}
for(auto ele : B){
maks = max(maks, ele);
}
if(maks <= 1){
check_one(A, B);
return VEC;
}
ret = {};
//znajdź wynik
set<int> ind = {};
map<int, vi> posA;
map<int, vi> posB;
map<int, int> cntA;
map<int, int> cntB;
for(int i = n - 1; i >= 0; i--){
posA[A[i]].pb(i);
cntA[A[i]]++;
}
for(int i = m - 1; i >= 0; i--){
posB[B[i]].pb(i);
cntB[B[i]]++;
}
int mx = -1;
for(int i = 0; i < n; i++){
if(cntA[A[i]] > cntB[A[i]]){
continue;
}
while(sz(posB[A[i]]) && posB[A[i]].back() <= mx){
posB[A[i]].pop_back();
}
ind.insert(i);
mx = posB[A[i]].back();
posB[A[i]].pop_back();
}
mx = -1;
for(int i = 0; i < m; i++){
if(cntB[B[i]] > cntA[B[i]]){
continue;
}
while(sz(posA[B[i]]) && posA[B[i]].back() <= mx){
posA[B[i]].pop_back();
}
ind.insert(posA[B[i]].back());
mx = posA[B[i]].back();
posA[B[i]].pop_back();
}
for(auto ele : ind){
ret.pb(A[ele]);
}
return ret;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |