This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define mp(x, y) make_pair(x, y)
#define sz(v) ((int) (v).size())
#define all(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define pb push_back
#define max_rng *max_element
#define min_rng *min_element
#define rep(i, n) for(int i = 1, _n = (n); i <= _n; ++i)
#define forn(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define ford(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
template <class X, class Y>
inline bool maximize(X &x, Y y) {
return (x < y ? x = y, true : false);
}
template <class X, class Y>
inline bool minimize(X &x, Y y) {
return (x > y ? x = y, true : false);
}
template <class X>
inline void compress(vector<X> &a) {
sort(all(a));
a.resize(unique(all(a)) - a.begin());
}
int ctz(ll x) { return __builtin_ctzll(x); }
int lg(ll x) { return 63 - __builtin_clzll(x); }
int popcount(ll x) { return __builtin_popcount(x); }
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) {
return l + abs((ll) rng()) % (r - l + 1);
}
const ll oo = (ll) 1e17;
const int inf = (int) 1e9, mod = (int) 1e9 + 7;
const int mxn = 1e6 + 5, mxm = 2e2 + 5, base = 307, LOG = 20;
void add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
void sub(int &x, int y) { x -= y; if (x < 0) x += mod; }
int n;
string A, B;
vector<vector<int>> op;
int trans[16][2];
int f[16][16], g[10][10];
int dp[mxn][16];
void prepare() {
vector<int> p(3);
iota(all(p), 0);
do {
vector<int> cur;
for (int v : p) {
cur.pb(v);
op.pb(cur);
}
} while (next_permutation(all(p)));
op.pb(vector<int>());
compress(op);
memset(f, 0x3f, sizeof f);
forn(i, 0, sz(op) - 1) forn(j, 0, sz(op) - 1) {
const vector<int> &a = op[i];
const vector<int> &b = op[j];
forn(x, 1, sz(a)) forn(y, 1, sz(b)) {
g[x][y] = max(g[x][y-1], g[x-1][y]);
if (a[x - 1] == b[y - 1])
maximize(g[x][y], g[x - 1][y - 1] + 1);
}
f[i][j] = sz(b) - g[sz(a)][sz(b)];
}
forn(i, 0, sz(op) - 1) forn(j, 0, 1) {
int bit = j;
for (int o : op[i]) {
if (!o) bit = 0;
else if (o == 1) bit = 1;
else bit ^= 1;
}
trans[i][j] = bit;
}
return;
}
int main(void) {
// think what before starting to code
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define TASK "partner"
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
cin >> n >> A >> B;
prepare();
memset(dp, 0x3f, sizeof dp);
dp[0][0] = 0;
rep(i, n) {
int d1 = A[i-1] - '0', d2 = B[i-1] - '0';
forn(cur, 0, 15) if (trans[cur][d1] == d2) {
forn(pre, 0, 15) {
minimize(dp[i][cur], dp[i - 1][pre] + f[pre][cur]);
}
}
}
cout << min_rng(dp[n], dp[n] + 16);
return 0;
}
Compilation message (stderr)
lamp.cpp: In function 'int main()':
lamp.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
lamp.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |