Submission #1544747
Source Code Expand
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <utility>
#include <cstdlib>
#include <memory>
#include <queue>
#include <cassert>
#include <cmath>
#include <ctime>
#include <complex>
#include <bitset>
#include <fstream>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
using namespace std;
#define ws ws_____________________
#define y1 y1_____________________
#define y0 y0_____________________
#define left left_________________
#define right right_______________
#define next next_________________
#define prev prev_________________
#define hash hash_________________
#define pb push_back
#define fst first
#define snd second
#define mp make_pair
#define sz(C) ((int) (C).size())
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define ford(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define all(C) begin(C), end(C)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef long double ld;
typedef complex<double> cd;
#ifdef LOCAL
#define eprintf(args...) fprintf(stderr, args), fflush(stderr)
#else
#define eprintf(...) ;
#endif
#define FILE_NAME "a"
const int MAXN = 2e3 + 10;
#define TEST 0
int n;
vi a;
vi b;
bool read() {
#if !TEST
static char buf[MAXN];
scanf("%s\n", buf);
for (int i = 0; buf[i]; ++i) {
a.pb(buf[i] - '0');
}
scanf("%s\n", buf);
for (int i = 0; buf[i]; ++i) {
b.pb(buf[i] - '0');
}
n = sz(a);
assert(sz(b) == n);
#else
n = 1 + rand() % 10;
a.resize(n);
b.resize(n);
forn(i, n) {
a[i] = rand() & 1;
b[i] = rand() % 10 < 3;
}
#endif
return 1;
}
vi pref;
int sum_b(int l, int r) {
assert(l <= r);
assert(0 <= l);
assert(r < n);
return pref[r + 1] - pref[l];
}
bool check_left(int i, int l) {
assert(l < n);
if (i - l < 0) {
if (sum_b(0, i) > 0) {
return 1;
}
if (sum_b(n - (l - i), n - 1) > 0) {
return 1;
}
return 0;
}
return sum_b(i - l, i) > 0;
}
bool check_right(int i, int r) {
assert(r < n);
if (i + r >= n) {
if (sum_b(i, n - 1) > 0) {
return 1;
}
if (sum_b(0, r - (n - 1 - i) - 1) > 0) {
return 1;
}
return 0;
}
return sum_b(i, i + r) > 0;
}
int solve() {
int Sum_b = accumulate(all(b), 0);
if (Sum_b == 0) {
int Sum_a = accumulate(all(a), 0);
if (Sum_a == 0) {
return 0;
}
return -1;
}
// forn(i, n) {
// printf("i=%d, min_l=%d, min_r=%d\n", i, min_l[i], min_r[i]);
// }
int ans = 10 * n;
forn(iter, 2) {
pref.assign(n + 1, 0);
forn(i, n) {
pref[i + 1] = pref[i] + b[i];
}
vi min_l(n);
vi min_r(n);
forn(i, n) {
bool was = 0;
for (int l = 0; l < n; ++l) {
if (check_left(i, l)) {
min_l[i] = l;
was = 1;
break;
}
}
assert(was);
was = 0;
for (int r = 0; r < n; ++r) {
if (check_right(i, r)) {
min_r[i] = r;
was = 1;
break;
}
}
assert(was);
}
forn(r, n) {
int flipped = 0;
set<pii> S;
vvi to_erase(n);
forn(i, n) {
int j = (i + r) % n;
if (a[i] != b[j]) {
++flipped;
if (!check_right(i, r)) {
to_erase[min_r[i]].pb(i);
S.insert(mp(min_l[i], i));
}
}
}
for (int nr = r; nr < n; ++nr) {
for (int i : to_erase[nr]) {
S.erase(mp(min_l[i], i));
}
int l = 0;
if (!S.empty()) {
l = S.rbegin()->fst;
}
int cur = flipped + l * 2 + r + (nr - r) * 2;
ans = min(ans, cur);
}
}
reverse(all(a));
reverse(all(b));
}
return ans;
}
bool brut_check_left(int i, int l) {
forn(it, l + 1) {
int j = (i - it % n + n) % n;
if (b[j]) {
return 1;
}
}
return 0;
}
bool brut_check_right(int i, int r) {
forn(it, r + 1) {
int j = (i + it % n) % n;
if (b[j]) {
return 1;
}
}
return 0;
}
int brut() {
int best_l = -1;
int best_r = -1;
int ans = 10 * n;
forn(l, n) forn(r, n) {
int cur = 0;
bool ok = 1;
forn(i, n) {
int j = (i + (r - l + n) % n) % n;
if (a[i] != b[j]) {
++cur;
if (!brut_check_left(i, l) && !brut_check_right(i, r)) {
ok = 0;
break;
}
}
}
if (!ok) {
continue;
}
cur += l + r;
if (ans > cur) {
best_l = l;
best_r = r;
ans = cur;
}
}
printf("best_l = %d, best_r = %d\n", best_l, best_r);
return ans >= 5 * n ? -1 : ans;
}
int main() {
#ifdef LOCAL
freopen(FILE_NAME ".in", "r", stdin);
// freopen(FILE_NAME ".out", "w", stdout);
#endif
#if TEST
while (1) {
read();
int my = solve();
int br = brut();
if (my == br) {
printf("OK %d == %d\n", my, br);
continue;
}
printf("my = %d, br = %d\n", my, br);
printf("n = %d\n", n);
forn(i, n) {
printf("%d ", a[i]);
}
puts("");
forn(i, n) {
printf("%d ", b[i]);
}
puts("");
return 0;
}
#endif
while (read()) {
printf("%d\n", solve());
break;
}
#ifdef LOCAL
cerr.precision(5);
cerr << "Time: " << fixed << (double) clock() / CLOCKS_PER_SEC << endl;
#endif
return 0;
}
Submission Info
Submission Time |
|
Task |
D - Shift and Flip |
User |
pavelsavchenkov |
Language |
C++14 (GCC 5.4.1) |
Score |
1000 |
Code Size |
5441 Byte |
Status |
AC |
Exec Time |
844 ms |
Memory |
512 KB |
Compile Error
./Main.cpp: In function ‘bool read()’:
./Main.cpp:74:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%s\n", buf);
^
./Main.cpp:79:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%s\n", buf);
^
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
1000 / 1000 |
Status |
|
|
Set Name |
Test Cases |
Sample |
sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt |
All |
sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, subtask_1_01.txt, subtask_1_02.txt, subtask_1_03.txt, subtask_1_04.txt, subtask_1_05.txt, subtask_1_06.txt, subtask_1_07.txt, subtask_1_08.txt, subtask_1_09.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt, subtask_1_14.txt, subtask_1_15.txt, subtask_1_16.txt, subtask_1_17.txt, subtask_1_18.txt, subtask_1_19.txt, subtask_1_20.txt, subtask_1_21.txt, subtask_1_22.txt, subtask_1_23.txt, subtask_1_24.txt, subtask_1_25.txt, subtask_1_26.txt, subtask_1_27.txt, subtask_1_28.txt, subtask_1_29.txt, subtask_1_30.txt, subtask_1_31.txt, subtask_1_32.txt, subtask_1_33.txt, subtask_1_34.txt, subtask_1_35.txt, subtask_1_36.txt, subtask_1_37.txt, subtask_1_38.txt, subtask_1_39.txt, subtask_1_40.txt, subtask_1_41.txt, subtask_1_42.txt, subtask_1_43.txt, subtask_1_44.txt, subtask_1_45.txt, subtask_1_46.txt |
Case Name |
Status |
Exec Time |
Memory |
sample_01.txt |
AC |
1 ms |
256 KB |
sample_02.txt |
AC |
1 ms |
256 KB |
sample_03.txt |
AC |
1 ms |
256 KB |
sample_04.txt |
AC |
1 ms |
256 KB |
subtask_1_01.txt |
AC |
1 ms |
256 KB |
subtask_1_02.txt |
AC |
1 ms |
256 KB |
subtask_1_03.txt |
AC |
1 ms |
256 KB |
subtask_1_04.txt |
AC |
1 ms |
256 KB |
subtask_1_05.txt |
AC |
96 ms |
384 KB |
subtask_1_06.txt |
AC |
1 ms |
256 KB |
subtask_1_07.txt |
AC |
60 ms |
384 KB |
subtask_1_08.txt |
AC |
1 ms |
256 KB |
subtask_1_09.txt |
AC |
1 ms |
256 KB |
subtask_1_10.txt |
AC |
2 ms |
256 KB |
subtask_1_11.txt |
AC |
143 ms |
384 KB |
subtask_1_12.txt |
AC |
140 ms |
384 KB |
subtask_1_13.txt |
AC |
143 ms |
384 KB |
subtask_1_14.txt |
AC |
552 ms |
384 KB |
subtask_1_15.txt |
AC |
381 ms |
384 KB |
subtask_1_16.txt |
AC |
242 ms |
384 KB |
subtask_1_17.txt |
AC |
222 ms |
384 KB |
subtask_1_18.txt |
AC |
150 ms |
384 KB |
subtask_1_19.txt |
AC |
140 ms |
384 KB |
subtask_1_20.txt |
AC |
136 ms |
384 KB |
subtask_1_21.txt |
AC |
477 ms |
384 KB |
subtask_1_22.txt |
AC |
476 ms |
384 KB |
subtask_1_23.txt |
AC |
839 ms |
512 KB |
subtask_1_24.txt |
AC |
844 ms |
512 KB |
subtask_1_25.txt |
AC |
839 ms |
512 KB |
subtask_1_26.txt |
AC |
2 ms |
256 KB |
subtask_1_27.txt |
AC |
2 ms |
256 KB |
subtask_1_28.txt |
AC |
8 ms |
256 KB |
subtask_1_29.txt |
AC |
30 ms |
256 KB |
subtask_1_30.txt |
AC |
125 ms |
384 KB |
subtask_1_31.txt |
AC |
1 ms |
256 KB |
subtask_1_32.txt |
AC |
2 ms |
256 KB |
subtask_1_33.txt |
AC |
3 ms |
256 KB |
subtask_1_34.txt |
AC |
9 ms |
256 KB |
subtask_1_35.txt |
AC |
34 ms |
256 KB |
subtask_1_36.txt |
AC |
136 ms |
384 KB |
subtask_1_37.txt |
AC |
2 ms |
256 KB |
subtask_1_38.txt |
AC |
2 ms |
256 KB |
subtask_1_39.txt |
AC |
9 ms |
256 KB |
subtask_1_40.txt |
AC |
32 ms |
256 KB |
subtask_1_41.txt |
AC |
130 ms |
384 KB |
subtask_1_42.txt |
AC |
127 ms |
384 KB |
subtask_1_43.txt |
AC |
136 ms |
384 KB |
subtask_1_44.txt |
AC |
141 ms |
384 KB |
subtask_1_45.txt |
AC |
135 ms |
384 KB |
subtask_1_46.txt |
AC |
140 ms |
384 KB |