text
stringlengths 49
983k
|
|---|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int n;
cin >> n;
string s, t;
cin >> s >> t;
long long int cnt = 0;
long long int ans = 0;
long long int mn = 0, mx = 0;
for (long long int i = 0; i < (long long int)(n); i++) {
if (s[i] == '0' && t[i] == '1')
cnt++;
else if (s[i] == '1' && t[i] == '0')
cnt--;
ans = max(ans, abs(cnt));
ans = max({ans, abs(cnt - mx), abs(cnt - mn)});
mn = min(mn, cnt);
mx = max(mx, cnt);
}
if (cnt != 0) {
cout << -1 << '\n';
return;
}
cout << ans << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t = 1;
for (long long int i = 0; i < (long long int)(t); i++) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int n = 0, f = 1, ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
n = n * 10 + ch - '0';
ch = getchar();
}
return n * f;
}
char a[2000005], b[2000005];
int n;
int c[2000005];
int get(int x) {
int cur = 0, res = 0;
for (int i = 1; i <= n; i++) {
cur += x * c[i];
res = max(res, cur);
if (cur < 0) cur = 0;
}
return res;
}
int main() {
int s1 = 0, s2 = 0;
n = read();
scanf("%s%s", a + 1, b + 1);
for (int i = 1; i <= n; i++) {
if (a[i] != b[i] && a[i] == '0')
c[i] = -1, s1++;
else if (a[i] != b[i] && a[i] == '1')
c[i] = 1, s2++;
}
if (s1 != s2) {
printf("-1\n");
return 0;
}
printf("%d\n", max(get(1), get(-1)));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (long long i = 0; i < v.size(); ++i) {
os << v[i] << "";
}
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
for (long long i = 0; i < v.size(); ++i) {
for (long long j = 0; j < v[i].size(); j++) os << v[i][j] << "";
cout << "\n";
}
return os;
}
long long cnt(string t) {
long long sum = 0;
for (long long i = 0; i < t.length(); i++) {
if (t[i] == '1') sum++;
}
return sum;
}
void solve() {
long long n;
cin >> n;
string s1, s2;
cin >> s1 >> s2;
long long x = 0, y = 0;
if (s1.length() != s2.length()) {
cout << -1 << "\n";
return;
} else if (s1 == s2) {
cout << 0 << "\n";
return;
} else if (cnt(s1) != cnt(s2)) {
cout << -1 << "\n";
return;
} else {
for (long long i = 0; i < n; i++) {
if (s1[i] == '1' && s2[i] == '0') {
x++;
if (y) y--;
} else if (s1[i] == '0' && s2[i] == '1') {
y++;
if (x) x--;
}
}
cout << x + y << "\n";
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t = 1;
for (long long x = 0; x < t; x++) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10 + 1e6;
const long long MOD = 998244353;
int n, k;
long long dp[MAXN], b[MAXN][2];
pair<long long, long long> a[MAXN];
string s, t;
void Inout() {
freopen(
"ABC"
".inp",
"r", stdin);
freopen(
"ABC"
".out",
"w", stdout);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
cin >> s;
cin >> t;
int x = 0, mi = n, ma = -1;
for (int i = 0; i < n; ++i) {
x += (s[i] - t[i]);
mi = min(mi, x);
ma = max(ma, x);
}
(x == 0) ? cout << ma - mi : cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:16777216")
using namespace std;
const int INF = 2000000000;
const long long LINF = 2000000000000000000;
template <typename T>
void print(vector<T>& a) {
for (int i = 0; i < a.size(); i++) cout << a[i] << ' ';
cout << '\n';
}
template <typename T>
void print(deque<T>& a) {
for (int i = 0; i < a.size(); i++) cout << a[i] << ' ';
cout << '\n';
}
template <typename T>
void print(vector<vector<T>>& a) {
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[i].size(); j++) cout << a[i][j] << ' ';
cout << '\n';
}
}
template <typename T>
void input(vector<T>& a) {
for (int i = 0; i < a.size(); i++) cin >> a[i];
}
template <typename T>
void input(deque<T>& a) {
for (int i = 0; i < a.size(); i++) cin >> a[i];
}
template <typename T>
void input(vector<vector<T>>& a) {
for (int i = 0; i < a.size(); i++)
for (int j = 0; j < a[i].size(); j++) cin >> a[i][j];
}
void solve() {
int n;
cin >> n;
string s, t;
cin >> s >> t;
string vasya = s, senya = t;
sort((vasya).begin(), (vasya).end());
sort((senya).begin(), (senya).end());
if (vasya != senya) {
cout << -1;
return;
}
int ans = 0;
int h1 = 0, h2 = 0;
for (int i = 0; i < n; i++)
if (s[i] != t[i])
if (s[i] == '1' && t[i] == '0') {
if (!h2)
ans++;
else
h2--;
h1++;
} else {
if (!h1)
ans++;
else
h1--;
h2++;
}
cout << ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tst = 1;
while (tst--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n;
cin >> n;
string s, t;
cin >> s >> t;
long long ones = 0;
for (long long i = 0; i < n; i++) {
if (s[i] - '0') {
ones++;
}
if (t[i] - '0') {
ones--;
}
}
if (ones) {
cout << -1;
return 0;
}
long long cnt10 = 0, ans10 = 0;
for (long long i = 0; i < n; i++) {
if (s[i] == t[i]) continue;
if (s[i] == '1') {
cnt10++;
} else {
cnt10--;
}
ans10 = max(ans10, cnt10);
}
long long cnt01 = 0, ans01 = 0;
for (long long i = 0; i < n; i++) {
if (s[i] == t[i]) continue;
if (s[i] == '0') {
cnt01++;
} else {
cnt01--;
}
ans01 = max(ans01, cnt01);
}
cout << ans10 + ans01;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
string s, t;
cin >> s >> t;
vector<int> a;
for (int i = 0; i < n; i++)
if (s[i] != t[i]) a.push_back(s[i] - '0');
int cnt0 = 0, cnt1 = 0;
for (int i = 0; i < (int)a.size(); i++)
if (a[i] == 0)
cnt0++;
else
cnt1++;
if (cnt0 != cnt1) {
printf("-1");
return 0;
}
set<int> t0, t1;
for (int i = 0; i < (int)a.size(); i++) {
if (a[i] == 0) {
if (t1.empty()) {
t0.insert(i);
continue;
}
t1.erase(*t1.begin());
t0.insert(i);
} else {
if (t0.empty()) {
t1.insert(i);
continue;
}
t0.erase(*t0.begin());
t1.insert(i);
}
}
printf("%d", (int)t0.size() + (int)t1.size());
}
|
#include <bits/stdc++.h>
using namespace std;
long long int caseno, n;
char s[1000006];
char t[1000006];
set<long long int> store[2];
set<long long int>::iterator it, it1;
void Input() {
scanf("%lld", &n);
scanf("%s", &s);
scanf("%s", &t);
}
void Calculation() {
for (int i = 0; i < n; i++) {
if (s[i] != t[i]) {
store[t[i] - '0'].insert(i);
}
}
if (store[0].size() != store[1].size()) {
puts("-1");
return;
}
long long int res = 0;
while (store[0].size()) {
long long int pos0 = *store[0].begin();
long long int pos1 = *store[1].begin();
long long int indx = 0, last = 0;
if (pos0 > pos1) indx = 1;
res++;
while (1) {
it = store[indx].lower_bound(last);
if (it == store[indx].end()) break;
long long int posNow0 = *it;
it1 = store[1 - indx].lower_bound(posNow0);
if (it1 == store[1 - indx].end()) break;
last = *it1;
store[indx].erase(it);
store[1 - indx].erase(it1);
}
}
printf("%lld\n", res);
}
void Solve() {
Input();
Calculation();
}
int main() {
int q;
q = 1;
caseno = 0;
while (q--) {
caseno++;
Solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios ::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
string s, t;
cin >> n >> s >> t;
int cnt = 0;
for (int i = 0; i < n; ++i) {
cnt += s[i] - '0';
cnt -= t[i] - '0';
}
if (cnt != 0) {
cout << -1 << '\n';
return 0;
}
int cnt0 = 0, cnt1 = 0;
for (int i = 0; i < n; ++i) {
if (s[i] != t[i]) {
if (s[i] == '0') {
if (cnt1) {
cnt1--;
cnt0++;
} else {
cnt0++;
}
} else {
if (cnt0) {
cnt0--;
cnt1++;
} else {
cnt1++;
}
}
}
}
cout << cnt0 + cnt1 << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
void Output(const vector<T>& v) {
for (int i = 0; i < v.size(); ++i) {
cout << v[i];
if (i < v.size() - 1)
cout << " ";
else
cout << endl;
}
}
template <class T>
void Input(vector<T>& v, int n) {
v.resize(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
}
}
bool check(const vector<int>& a, int k, int v, bool first) {
int r = 0;
int total = 0;
bool need_buddy = !first;
for (int i = 0; i < a.size() && total < k; ++i) {
if (need_buddy) {
need_buddy = false;
++total;
continue;
}
if (a[i] <= v) {
++r;
++total;
need_buddy = true;
}
}
return total >= k;
}
int main() {
int n;
cin >> n;
string a;
string b;
cin >> a;
cin >> b;
set<int> one_to_zero;
set<int> zero_to_one;
for (int i = 0; i < n; ++i) {
if (a[i] == '1' && b[i] == '0') {
one_to_zero.insert(i);
} else {
if (a[i] == '0' && b[i] == '1') {
zero_to_one.insert(i);
}
}
}
if (one_to_zero.size() != zero_to_one.size()) {
cout << "-1" << endl;
return 0;
}
int ans = 0;
while (one_to_zero.size() > 0 && zero_to_one.size() > 0) {
int a = *one_to_zero.begin();
int b = *zero_to_one.begin();
set<int>*current, *next;
int v;
if (a < b) {
current = &one_to_zero;
next = &zero_to_one;
v = a;
} else {
current = &zero_to_one;
next = &one_to_zero;
v = b;
}
current->erase(current->begin());
int last;
int ct = 1;
while (true) {
auto it = next->lower_bound(v);
if (it == next->end()) break;
v = *it;
next->erase(it);
last = v;
++ct;
swap(current, next);
}
if (ct % 2 == 1) {
current->insert(last);
}
++ans;
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
string s, t;
cin >> s >> t;
if (accumulate(begin(s), end(s), 0) != accumulate(begin(t), end(t), 0)) {
cout << -1 << '\n';
return 0;
}
{
string ns, nt;
for (int i = 0; i < n; i++) {
if (s[i] != t[i]) {
ns.push_back(s[i]);
nt.push_back(t[i]);
}
}
s = ns;
t = nt;
n = s.size();
}
if (n == 0) {
cout << "0\n";
return 0;
}
vector<int> arr = {1};
for (int i = 1; i < n; i++) {
if (s[i] == s[i - 1])
arr.back()++;
else
arr.push_back(1);
}
if (arr.size() > 2 && s[0] == s.back()) {
arr[0] += arr.back();
arr.pop_back();
}
int ans = 0;
while (arr.size()) {
ans++;
n = arr.size();
for (int& val : arr) val--;
vector<int> nxt;
for (int i = 0; i < n; i++) {
if (i + 1 == n) {
if (arr[i]) {
nxt.push_back(arr[i]);
}
} else {
if (arr[i] == 0 && arr[i + 1] == 0) {
i++;
} else if (arr[i] > 0 && arr[i + 1] > 0) {
nxt.push_back(arr[i]);
nxt.push_back(arr[i + 1]);
i++;
} else if (arr[i] == 0) {
if (nxt.size()) {
nxt.back() += arr[i + 1];
} else {
nxt.push_back(arr[i + 1]);
}
i++;
} else {
nxt.push_back(arr[i]);
}
}
}
if (nxt.size() & 1) {
nxt[0] += nxt.back();
nxt.pop_back();
}
arr = nxt;
}
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
string s, t;
int sz = 0, tz = 0;
cin >> s >> t;
for (long long int i = 0; i <= s.size() - 1; i++)
if (s[i] == '0') sz++;
for (long long int i = 0; i <= t.size() - 1; i++)
if (t[i] == '0') tz++;
if (sz != tz) {
cout << -1 << "\n";
return 0;
}
vector<long long int> sd;
for (long long int i = 0; i <= s.size() - 1; i++)
if (s[i] != t[i]) sd.push_back((s[i] == '0' ? -1 : 1));
if (sd.empty()) {
cout << 0 << "\n";
return 0;
}
long long int ma = 0, cnt = 0;
for (long long int i = 0; i <= sd.size() - 1; i++) {
cnt += sd[i];
ma = max(ma, cnt);
if (cnt < 0) cnt = 0;
}
cnt = 0;
for (long long int i = 0; i <= sd.size() - 1; i++) {
cnt += -sd[i];
ma = max(ma, cnt);
if (cnt < 0) cnt = 0;
}
cout << ma << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 200100;
long long n, cnt, ans, pos, r, x, y;
string s, t;
set<long long> a, b;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> s >> t;
for (long long i = 0; i < n; i++) {
cnt += s[i] - t[i];
if (s[i] != t[i] && s[i] == '0')
a.insert(i);
else if (s[i] != t[i])
b.insert(i);
}
if (cnt) {
cout << -1;
return 0;
}
for (; !a.empty();) {
ans++;
x = *a.begin(), y = *b.begin();
a.erase(a.begin());
b.erase(b.begin());
pos = min(x, y);
if (x < y)
r = 0;
else
r = 1;
for (;;) {
if (!r) {
auto le = a.lower_bound(y);
if (le == a.end()) break;
x = *le;
auto ri = b.lower_bound(x);
if (ri == b.end()) break;
y = *ri;
} else {
auto ri = b.lower_bound(x);
if (ri == b.end()) break;
y = *ri;
auto le = a.lower_bound(y);
if (le == a.end()) break;
x = *le;
}
a.erase(x);
b.erase(y);
}
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n;
vector<int> v;
string a, b;
int go(int x) {
int maxv = 0, curr = 0;
for (int i = 0; i < (int)v.size(); i++) {
curr += x * v[i];
curr = max(curr, 0);
maxv = max(maxv, curr);
}
return maxv;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> a >> b;
int s = 0;
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
if (a[i] == '1') {
v.push_back(1);
s += 1;
} else {
v.push_back(-1);
s += -1;
}
}
}
if (s != 0) {
cout << -1 << "\n";
return 0;
}
int ans = max(go(1), go(-1));
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
string s[2];
int n;
cin >> n >> s[0] >> s[1];
int cnt[2][2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++) cnt[i][j] = 0;
for (int i = 0; i < 2; i++)
for (int j = 0; j < n; j++) cnt[i][s[i][j] - '0']++;
if ((cnt[0][0] != cnt[1][0]) || (cnt[1][1] != cnt[0][1]))
return cout << "-1\n", 0;
vector<int> vec;
for (int i = 0; i < n; i++)
if (s[0][i] == '1' && s[1][i] == '0')
vec.push_back(1);
else if (s[0][i] == '0' && s[1][i] == '1')
vec.push_back(-1);
n = vec.size();
int ans = 0, sum = 0, mn = 0;
for (int j = 0; j < 2; j++) {
sum = 0, mn = 0;
for (int i = 0; i < n; i++) {
sum += vec[i];
mn = min(mn, sum);
ans = max(ans, sum - mn);
vec[i] *= -1;
}
}
cout << ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, i;
cin >> n;
string s, t;
cin >> s >> t;
long long c1, c2, c3, c4;
c1 = c2 = c3 = c4 = 0;
if (s.size() != t.size()) {
cout << -1;
} else {
string ans;
for (i = 0; i < s.size(); i++) {
if (s[i] == '1')
c1++;
else
c2++;
if (t[i] == '1')
c3++;
else
c4++;
if (s[i] != t[i]) {
ans.push_back(s[i]);
}
}
if (c1 != c3 or c2 != c4) {
cout << -1;
} else {
long long maxtill = 0;
long long a0 = 0, a1 = 0;
for (i = 0; i < ans.size(); i++) {
if (!a1 and !a0) {
if (ans[i] == '0')
a1++;
else
a0++;
} else {
if (ans[i] == '0') {
if (a0 > 0)
a0--, a1++;
else
a1++;
} else if (ans[i] == '1') {
if (a1 > 0)
a1--, a0++;
else
a0++;
}
}
}
cout << a0 + a1 << '\n';
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int SZ = 2e5 + 4;
const double EPS = 1e-7;
const int mod = 998244353;
const int hashpow = 2;
const int inf = 2e9;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
int n;
string s, t;
inline void read() { cin >> n >> s >> t; }
inline void solve() {
string tmp;
int cnt = 0;
for (int i = 0; i < n; ++i) {
if (s[i] != t[i]) {
tmp += s[i];
cnt += s[i] - '0';
}
}
if (cnt * 2 != tmp.size()) {
cout << -1 << "\n";
return;
}
int zero = 0, one = 0;
for (char c : tmp) {
if (c == '1') {
if (zero) {
--zero;
}
++one;
} else {
if (one) {
--one;
}
++zero;
}
}
cout << zero + one << "\n";
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
read();
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool mmax(T &m, const T q) {
if (m < q) {
m = q;
return true;
} else
return false;
}
template <typename T>
bool mmin(T &m, const T q) {
if (m > q) {
m = q;
return true;
} else
return false;
}
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 3;
const int INF = 1e9;
int solve() {
int n;
cin >> n;
string s, t;
cin >> s >> t;
int s0 = 0, t0 = 0;
for (int i = 0; i < (int)(n); ++i) {
s0 += s[i] == '0';
t0 += t[i] == '0';
}
if (s0 != t0) do {
cout << (-1) << '\n';
return 0;
} while (0);
deque<int> waiting_0, waiting_1;
deque<pair<int, int> > q01, q10;
for (int i = 0; i < (int)(n); ++i)
if (s[i] != t[i]) {
int c = s[i] - '0';
if (c == 0) {
if (waiting_0.size()) {
int idx = waiting_0.front();
waiting_0.pop_front();
if (q10.size()) {
auto p = q10.front();
if (p.second < idx) q10.pop_front();
}
q10.push_back({idx, i});
} else {
waiting_1.push_back(i);
}
} else {
if (waiting_1.size()) {
int idx = waiting_1.front();
waiting_1.pop_front();
if (q01.size()) {
auto p = q01.front();
if (p.second < idx) q01.pop_front();
}
q01.push_back({idx, i});
} else {
waiting_0.push_back(i);
}
}
}
assert(waiting_0.size() == 0);
assert(waiting_1.size() == 0);
cout << q01.size() + q10.size() << '\n';
}
int main(int argc, char **argv) {
ios::sync_with_stdio(false);
cin.tie(0);
int ntest = 1;
while (ntest--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
int n, i;
string s;
string t;
cin >> n >> s >> t;
int sum_s = 0;
int sum_t = 0;
int curr;
int l = 0;
int maxx;
int ones_sum;
int zeros_sum;
set<int> sett;
vector<int> v;
for (i = 0; i < n; i++) {
sum_s += s[i] - '0';
sum_t += t[i] - '0';
if (s[i] != t[i]) {
if (s[i] == '0') {
v.emplace_back(-1);
} else {
v.emplace_back(1);
}
l++;
}
}
if (sum_s != sum_t) {
cout << -1 << "\n";
} else {
if (!v.empty()) {
curr = 0;
maxx = 0;
for (i = 0; i < l; i++) {
curr += v[i];
if (curr > maxx) {
maxx = curr;
}
if (curr < 0) {
curr = 0;
}
}
ones_sum = maxx;
curr = 0;
maxx = 0;
for (i = 0; i < l; i++) {
curr += (-1 * v[i]);
if (curr > maxx) {
maxx = curr;
}
if (curr < 0) {
curr = 0;
}
}
zeros_sum = maxx;
cout << max(ones_sum, zeros_sum) << "\n";
} else {
cout << 0 << "\n";
}
}
}
|
#include <bits/stdc++.h>
namespace predefined {
constexpr bool kIsDebug = false;
enum class LogType { INFO, DEBUG };
template <LogType Type, bool... Bools>
struct Log;
template <>
struct Log<LogType::INFO> {
Log() {}
~Log() { std::cout << '\n'; }
template <typename T>
Log &operator<<(const T &t) {
std::cout << t;
return *this;
}
};
template <>
struct Log<LogType::DEBUG, true> {
Log() {}
~Log() { std::cout << '\n'; }
void Open() { freopen("input.txt", "r", stdin); }
template <typename T>
Log &operator<<(const T &t) {
std::cout << t;
return *this;
}
};
template <>
struct Log<LogType::DEBUG, false> {
Log() {}
~Log() {}
void Open() {}
template <typename T>
Log &operator<<(const T &t) {
return *this;
}
};
namespace solver {
using SolveFunc = std::function<void()>;
template <typename MultiRunner>
void Run(SolveFunc solve_func);
template <>
void Run<std::true_type>(SolveFunc solve_func) {
int test_case;
std::cin >> test_case;
while (test_case--) {
solve_func();
}
}
template <>
void Run<std::false_type>(SolveFunc solve_func) {
solve_func();
}
} // namespace solver
template <bool MultiTest>
struct Solver {
constexpr static bool condition = MultiTest | kIsDebug;
typedef typename std::conditional<condition, std::true_type,
std::false_type>::type run_type;
explicit Solver(solver::SolveFunc solve_func) {
solver::Run<run_type>(solve_func);
}
};
template <typename T>
struct BoolMap {
explicit BoolMap(const T &_true_value, const T &_false_value)
: true_value(_true_value), false_value(_false_value) {}
const T true_value;
const T false_value;
const T inline GetValue(const bool bool_key) const {
return bool_key ? true_value : false_value;
};
};
} // namespace predefined
namespace {
using namespace std;
constexpr bool kMultiTestCase = false;
using Solver = predefined::Solver<kMultiTestCase>;
} // namespace
void solve() {
int n;
string s, t;
cin >> n >> s >> t;
int s_count = 0, t_count = 0;
int zero_first = 0, one_first = 0;
for (int i = 0; i < n; ++i) {
if (s[i] == '0') ++s_count;
if (t[i] == '0') ++t_count;
if (s[i] != t[i]) {
if (s[i] == '0') {
++zero_first;
one_first -= (one_first != 0);
} else {
++one_first;
zero_first -= (zero_first != 0);
}
}
}
if (s_count != t_count) {
predefined::Log<predefined::LogType::INFO>() << -1;
return;
}
predefined::Log<predefined::LogType::INFO>() << zero_first + one_first;
}
int main(int argc, char **argv) {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
predefined::Log<predefined::LogType::DEBUG, predefined::kIsDebug>().Open();
;
Solver(std::bind(solve));
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
using namespace std;
struct debugger {
template <typename T>
debugger& operator,(const T& v) {
cerr << v << " ";
return *this;
}
} dbg;
bool sortbysec(const pair<long long, long long>& a,
const pair<long long, long long>& b) {
if (a.second == b.second) return a.first < b.first;
return (a.second < b.second);
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long isPrime(long long n) {
if (n < 2) return 0;
if (n < 4) return 1;
if (n % 2 == 0 or n % 3 == 0) return 0;
for (long long i = 5; i * i <= n; i += 6)
if (n % i == 0 or n % (i + 2) == 0) return 0;
return 1;
}
long long modexpo(long long x, long long p) {
long long res = 1;
x = x % 1000000007;
while (p) {
if (p % 2) res = res * x;
p >>= 1;
x = x * x % 1000000007;
res %= 1000000007;
}
return res;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) {
long long n;
cin >> n;
string s1, s2;
cin >> s1 >> s2;
long long ot = 0, os = 0;
for (long long i = 0; i < n; ++i) {
if (s1[i] == '1') os++;
if (s2[i] == '1') ot++;
}
if (ot != os) {
cout << -1;
return 0;
}
vector<long long> v;
for (long long i = 0; i < n; ++i) {
if (s1[i] == s2[i])
v.push_back(0);
else if (s1[i] == '1') {
v.push_back(1);
} else
v.push_back(-1);
}
long long ans = 0, m1 = 0;
for (long long i = 0; i < n; ++i) {
m1 += v[i];
if (m1 < 0) m1 = 0;
ans = (ans > m1 ? ans : m1);
}
long long m2 = 0;
for (long long i = 0; i < n; ++i) {
m2 += v[i];
if (m2 > 0) m2 = 0;
ans = (ans > -m2 ? ans : -m2);
}
cout << ans;
cout << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int ll_MAX = 1e18 + 5;
const long long int MOD = 1e9 + 7;
long long int fast_expo(long long int x, long long int p) {
if (p == 0)
return 1;
else if (p % 2 == 0) {
long long int t = fast_expo(x, p / 2) % MOD;
return (t * t) % MOD;
} else
return (x * (fast_expo(x, p - 1)) % MOD) % MOD;
}
long long int gcd(long long int a, long long int b) {
if (a == 0 || b == 0)
return a + b;
else
return gcd(b % a, a);
}
bool check(long long int arr[], long long int s, long long int e,
long long int val, long long int num) {
long long int cnt = 0;
for (long long int i = s; i <= e; ++i) {
if (arr[i] <= val) {
cnt++;
i++;
}
}
if (cnt >= num) return true;
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long int t_c;
t_c = 1;
for (long long int t_i = 0; t_i < t_c; ++t_i) {
long long int n;
cin >> n;
string s, t;
cin >> s >> t;
long long int cnt = 0;
for (auto i : s) {
if (i == '0') cnt++;
}
for (auto i : t) {
if (i == '0') cnt--;
}
if (cnt != 0) {
cout << -1;
return 0;
}
string str;
for (long long int i = 0; i < n; ++i) {
if (s[i] != t[i]) {
str.push_back(s[i]);
}
}
if (str.size() == 0) {
cout << 0;
return 0;
}
long long int curr0 = 0, curr1 = 0;
vector<long long int> vect;
for (auto i : str) {
if (i == '0') {
curr0++;
if (curr1 != 0) vect.push_back(curr1);
curr1 = 0;
} else {
curr1++;
if (curr0 != 0) vect.push_back(-curr0);
curr0 = 0;
}
}
if (curr1) vect.push_back(curr1);
if (curr0) vect.push_back(-curr0);
if (vect.size() == 0) {
cout << 0 << "\n";
return 0;
}
long long int ans = 0;
vector<long long int> vect2;
while (1) {
ans++;
if (vect[vect.size() - 1] * vect[0] < 0) {
for (long long int i = 0; i < vect.size(); ++i) {
if (vect[i] > 1) {
if (vect2.size() > 0 && vect2[vect2.size() - 1] > 0) {
long long int val = vect2[vect2.size() - 1];
vect2.pop_back();
vect2.push_back(val + vect[i] - 1);
} else
vect2.push_back(vect[i] - 1);
} else if (vect[i] < -1) {
if (vect2.size() > 0 && vect2[vect2.size() - 1] < 0) {
long long int val = vect2[vect2.size() - 1];
vect2.pop_back();
vect2.push_back(val + vect[i] + 1);
} else
vect2.push_back(vect[i] + 1);
}
}
} else {
for (long long int i = 0; i < vect.size() - 1; ++i) {
if (vect[i] > 1) {
if (vect2.size() > 0 && vect2[vect2.size() - 1] > 0) {
long long int val = vect2[vect2.size() - 1];
vect2.pop_back();
vect2.push_back(val + vect[i] - 1);
} else
vect2.push_back(vect[i] - 1);
} else if (vect[i] < -1) {
if (vect2.size() > 0 && vect2[vect2.size() - 1] < 0) {
long long int val = vect2[vect2.size() - 1];
vect2.pop_back();
vect2.push_back(val + vect[i] + 1);
} else
vect2.push_back(vect[i] + 1);
}
}
if (vect[vect.size() - 1] > 0) {
if (vect2.size() > 0 && vect2[vect2.size() - 1] > 0) {
long long int val = vect2[vect2.size() - 1];
vect2.pop_back();
vect2.push_back(val + vect[vect.size() - 1]);
} else
vect2.push_back(vect[vect.size() - 1]);
} else if (vect[vect.size() - 1] < 0) {
if (vect2.size() > 0 && vect2[vect2.size() - 1] < 0) {
long long int val = vect2[vect2.size() - 1];
vect2.pop_back();
vect2.push_back(val + vect[vect.size() - 1]);
} else
vect2.push_back(vect[vect.size() - 1]);
}
}
if (vect2.size() == 0) break;
vect = vect2;
vect2.clear();
}
cout << ans;
}
return 0;
}
|
#include <bits/stdc++.h>
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int main() {
static char aa[1000000 + 1], bb[1000000 + 1];
int n, i, dmin, dmax, dmin_, dmax_, dsum;
scanf("%d%s%s", &n, aa, bb);
dsum = dmin_ = dmax_ = dmin = dmax = 0;
for (i = 0; i < n; i++) {
int d = (aa[i] == '0' ? 1 : 0) - (bb[i] == '0' ? 1 : 0);
dsum += d;
dmin_ = min(dmin_, dmin = min(dmin + d, 0));
dmax_ = max(dmax_, dmax = max(dmax + d, 0));
}
printf("%d\n", dsum == 0 ? max(dmax_, -dmin_) : -1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long double PI = acos(-1);
const long double eps = 1e-9;
const long long inf = LLONG_MAX / 10;
void ECHO(string _s) { cout << _s; }
void ECHO(bool _s) {
if (_s)
cout << "true";
else
cout << "false";
}
void ECHO(char _s) { cout << _s; }
void ECHO(long unsigned _s) { cout << _s; }
void ECHO(int _s) { cout << _s; }
void ECHO(long long _s) {
if (_s == inf)
cout << "inf";
else
cout << _s;
}
void ECHO(double _s) { cout << _s; }
void ECHO(long double _s) { cout << _s; }
void ECHO(bitset<8> b) { cout << b; }
template <typename T>
void ECHO(set<T> _s) {
for (auto el : _s) {
ECHO(el);
cout << "_";
}
}
template <typename T1, typename T2>
void ECHO(map<T1, T2> _s) {
for (auto el : _s) {
ECHO(el);
cout << "_";
}
}
template <typename T1, typename T2>
void ECHO(pair<T1, T2> _s) {
cout << "(";
ECHO(_s.first);
cout << ",";
ECHO(_s.second);
cout << ")";
}
template <typename T>
void ECHO(vector<T> v) {
for (auto el : v) {
ECHO(el);
cout << "_";
}
}
template <typename T, typename... Args>
void ECHO(string _s, T x, Args... args) {
int _i, c = 0;
for (_i = 0; _i < (int)_s.size(); _i++) {
if (c == 0 && _s[_i] == ',') break;
if (_s[_i] == '(') c++;
if (_s[_i] == ')') c--;
}
cout << " (" << _s.substr(0, _i) << "): ";
ECHO(x);
while (_i < (int)_s.size() && (_s[_i] == ',' || _s[_i] == ' ')) _i++;
ECHO(_s.substr(_i), args...);
}
class SegmentTreeIndex {
vector<int> tree;
int n;
void update(int k, int l, int r, int pos, int dx) {
if (l == r) {
tree[k] = dx;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid)
update(k << 1, l, mid, pos, dx);
else
update(k << 1 | 1, mid + 1, r, pos, dx);
tree[k] = tree[k << 1] + tree[k << 1 | 1];
}
int queryR(int k, int l, int r, int pos) {
if (tree[k] == 0) return -1;
if (l == r) return l;
int mid = (l + r) >> 1;
int ans = -1;
if (pos <= mid) ans = queryR(k << 1, l, mid, pos);
if (ans != -1) return ans;
return queryR(k << 1 | 1, mid + 1, r, pos);
}
int queryL(int k, int l, int r, int pos) {
if (tree[k] == 0) return -1;
if (l == r) return l;
int mid = (l + r) >> 1;
int ans = -1;
if (pos > mid) ans = queryL(k << 1 | 1, mid + 1, r, pos);
if (ans != -1) return ans;
return queryL(k << 1, l, mid, pos);
}
int k_ele(int k, int l, int r, int kth) {
if (l == r) return l;
int mid = (l + r) >> 1;
if (tree[k << 1] >= kth) return k_ele(k << 1, l, mid, kth);
return k_ele(k << 1 | 1, mid + 1, r, kth - tree[k << 1]);
}
public:
SegmentTreeIndex(int _n) {
n = _n;
tree.assign(4 * n, 0);
}
void update(int pos, int dx) { update(1, 0, n - 1, pos, dx); }
int queryL(int pos) { return queryL(1, 0, n - 1, pos); }
int queryR(int pos) { return queryR(1, 0, n - 1, pos); }
int k_ele(int k) { return k_ele(1, 0, n - 1, k); }
};
long long n;
string v, w;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n;
cin >> v >> w;
SegmentTreeIndex a(2 * n), b(2 * n);
long long i, difa = 0, difb = 0;
for (i = 0; i < n; i++) {
if (v[i] == '1' && w[i] == '0') {
a.update(i, 1);
a.update(n + i, 1);
difa++;
}
if (v[i] == '0' && w[i] == '1') {
b.update(i, 1);
b.update(n + i, 1);
difb++;
}
}
if (difa != difb) {
cout << "-1\n";
return 0;
}
long long cont = 0;
long long numa, numb, numl;
while (true) {
numl = a.queryR(0);
if (numl == -1) break;
++cont;
numa = numl;
bool ok = true;
while (numa != -1 && numa < n && ok) {
numb = b.queryR(numa);
if (numb > numl + n) break;
a.update(numa, 0);
a.update(numa + n, 0);
if (numb >= n) {
numb -= n;
ok = false;
}
b.update(numb, 0);
b.update(numb + n, 0);
numa = a.queryR(numb);
}
}
cout << cont << "\n";
return 0;
}
|
#include <bits/stdc++.h>
char x[1000010], y[1000010];
std::set<int> S1, S2;
void func1(int k) {
std::set<int>::iterator it1 = S1.lower_bound(k);
if (it1 == S1.end()) return;
std::set<int>::iterator it2 = S2.lower_bound((*it1));
if (it2 == S2.end()) return;
int C = (*it2);
S1.erase(it1);
S2.erase(it2);
func1(C);
}
void func2(int k) {
std::set<int>::iterator it2 = S2.lower_bound(k);
if (it2 == S2.end()) return;
std::set<int>::iterator it1 = S1.lower_bound((*it2));
if (it1 == S1.end()) return;
int C = (*it1);
S2.erase(it2);
S1.erase(it1);
func2(C);
}
int main() {
int a;
scanf("%d", &a);
scanf("%s%s", x + 1, y + 1);
for (int i = 1; i <= a; i++) {
if (x[i] == '0' && y[i] == '1') S1.insert(i);
if (x[i] == '1' && y[i] == '0') S2.insert(i);
}
if (S1.size() != S2.size()) {
printf("-1");
return 0;
}
int count = 0;
while (!S1.empty() || !S2.empty()) {
std::set<int>::iterator it1 = S1.begin();
std::set<int>::iterator it2 = S2.begin();
if ((*it1) < (*it2))
func1(0);
else
func2(0);
count++;
}
printf("%d", count);
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
long long int const N = 1e8;
set<long long int> arr[2];
long long int solve() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int n;
cin >> n;
string second, t;
cin >> second >> t;
set<long long int> v1, v2;
long long int a = 0, b = 0;
for (long long int i = 0; i < n; i++) {
if (second[i] == '0' && t[i] == '1') {
a++;
arr[0].insert(i);
} else if (second[i] == '1' && t[i] == '0') {
arr[1].insert(i);
b++;
}
}
if (a != b) {
cout << -1;
return 0;
}
long long int ans = 0;
bool flag;
long long int first, h;
while (!arr[0].empty() && !arr[1].empty()) {
ans++;
auto it1 = (arr[0].begin());
auto it2 = (arr[1].begin());
first = 0;
if ((*it1) > *(it2)) {
first = 1;
}
it1 = arr[first].begin();
while (1) {
it2 = arr[first ^ 1].upper_bound(*it1);
if (it2 == arr[first ^ 1].end()) {
break;
}
arr[first].erase(it1);
h = (*(it2));
arr[first ^ 1].erase(it2);
it1 = arr[first].upper_bound(h);
if (it1 == arr[first].end()) {
break;
}
}
}
cout << ans;
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int t;
t = 1;
while (t) {
solve();
t--;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using vi = vector<int>;
using vpi = vector<pi>;
const int INF = 1e9;
const ll LINF = 1e18;
template <class T>
bool ckmax(T &a, T b) {
return a < b ? a = b, true : false;
}
template <class T>
bool ckmin(T &a, T b) {
return a > b ? a = b, true : false;
}
int solve() {
int n, count_nz = 0;
cin >> n;
string tmp1, tmp2, s;
cin >> tmp1 >> tmp2;
for (int i = 0; i < n; ++i)
if (tmp1[i] != tmp2[i]) s.push_back(tmp1[i]), count_nz += tmp1[i] - '0';
if (s.size() != 2 * count_nz) return -1;
vector<set<int>> second(2);
for (int i = 0; i < s.size(); ++i)
if (s[i] == '0')
second[0].insert(i);
else
second[1].insert(i);
int ans = 0;
for (; !second[0].empty(); ++ans) {
int dig = 0, pos = -1337;
vi del;
if (*(second[0].begin()) > *(second[1].begin())) dig = 1;
for (;;) {
auto it = second[dig].lower_bound(pos);
if (it == second[dig].end()) break;
del.push_back(pos = *it);
dig ^= 1;
}
if (del.size() % 2) del.pop_back();
for (int i : del) second[0].erase(i), second[1].erase(i);
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << solve() << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> v;
long long maxSubArraySum(long long sym) {
long long max_so_far = INT_MIN, max_ending_here = 0;
for (long long i = 0; i < v.size(); i++) {
max_ending_here = max_ending_here + sym * v[i];
if (max_so_far < max_ending_here) max_so_far = max_ending_here;
if (max_ending_here < 0) max_ending_here = 0;
}
return max_so_far;
}
int main() {
long long n;
cin >> n;
string s1, s2;
cin >> s1 >> s2;
long long all_zero = 1, all_one = 1;
for (long long i = 0; i < n; i++) {
if (s1[i] == s2[i]) continue;
if (s1[i] == '1') {
v.push_back(1);
all_zero = 0;
} else {
v.push_back(-1);
all_one = 0;
}
}
sort(s1.begin(), s1.end());
sort(s2.begin(), s2.end());
if (s1 == s2 && (all_one || all_zero))
cout << 0 << '\n';
else if (s1 == s2)
cout << max(maxSubArraySum(1), maxSubArraySum(-1)) << '\n';
else
cout << -1 << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long double PI = 4 * atan((long double)1);
const long long INF = 1e18;
const long long NINF = -1e18;
long long get_hash(string s) {
long long N = 1000001;
long long base[N], A = 11, MD = 1110111110111;
base[0] = 1;
for (long long i = (1); i < (N); ++i) base[i] = (base[i - 1] * A) % MD;
long long hs = 0;
for (long long i = (0); i < (s.size()); ++i) {
hs += (s[i] * base[i]);
hs %= MD;
}
return hs;
}
long long power(long long a, long long n) {
long long res = 1;
while (n) {
if (n % 2) res *= a;
a *= a;
n /= 2;
}
return res;
}
vector<string> vec_splitter(string s) {
s += ',';
vector<string> res;
while (!s.empty()) {
res.push_back(s.substr(0, s.find(',')));
s = s.substr(s.find(',') + 1);
}
return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
__attribute__((unused)) long long idx,
__attribute__((unused)) long long LINE_NUM) {
cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, long long idx, long long LINE_NUM, Head H,
Tail... T) {
if (idx > 0)
cerr << ", ";
else
cerr << "Line(" << LINE_NUM << ") ";
stringstream ss;
ss << H;
cerr << args[idx] << " = " << ss.str();
debug_out(args, idx + 1, LINE_NUM, T...);
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n;
cin >> n;
string a, b;
cin >> a >> b;
long long one = 0, ze = 0;
vector<long long> cnt;
for (long long i = (0); i < (n); ++i) {
if (a[i] == b[i])
continue;
else {
if (a[i] == '0') {
ze++;
cnt.push_back(-1);
} else {
one++;
cnt.push_back(1);
}
}
}
if (ze != one) {
cout << -1;
return 0;
}
long long maxa = 0;
long long ans = 0, ans1 = 0;
if (!cnt.empty())
for (long long i = (0); i < (cnt.size()); ++i) {
ans += cnt[i];
ans1 += -1 * cnt[i];
if (ans1 > maxa) maxa = ans1;
if (ans > maxa) maxa = ans;
if (ans1 < 0) ans1 = 0;
if (ans < 0) ans = 0;
}
cout << maxa;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
vector<string> vec_splitter(string s) {
s += ',';
vector<string> res;
while (!s.empty()) {
res.push_back(s.substr(0, s.find(',')));
s = s.substr(s.find(',') + 1);
}
return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
__attribute__((unused)) int idx,
__attribute__((unused)) int LINE_NUM) {
cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
if (idx > 0)
cerr << ", ";
else
cerr << "Line(" << LINE_NUM << ") ";
stringstream ss;
ss << H;
cerr << args[idx] << " = " << ss.str();
debug_out(args, idx + 1, LINE_NUM, T...);
}
double get_time() { return 1.0 * clock() / CLOCKS_PER_SEC; }
int main() {
fast();
int n;
string s, t;
cin >> n >> s >> t;
vector<int> v;
for (int i = 0; i < n; i++) {
if (s[i] == t[i]) continue;
if (s[i] == '1')
v.push_back(1);
else
v.push_back(-1);
}
n = v.size();
int best = 0;
vector<int> dp(n + 1);
for (int i = 0; i < (int)v.size(); i++) {
dp[i + 1] = max(dp[i] + v[i], 0);
best = max(best, dp[i + 1]);
}
for (int i = 0; i < (int)v.size(); i++) {
dp[i + 1] = max(dp[i] - v[i], 0);
best = max(best, dp[i + 1]);
}
sort((s).begin(), (s).end());
sort((t).begin(), (t).end());
if (s != t) best = -1;
cout << best << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> v;
long long int x[1000000], y[1000000], pot[30], czy = 0, w, n, m;
char s1[10000000], s2[10000000];
int main() {
long long int a, b, c, d, k, i, j, q, t, p;
{
scanf("%lld%s%s", &n, s1, s2);
w = 0;
for (i = 0; i < n; i++) {
if (s1[i] != s2[i]) {
v.push_back((long long)(s1[i] - '0'));
w += v[v.size() - 1];
}
}
if (v.size() == 0) {
printf("0\n");
return (0);
}
if (v.size() != w * 2)
printf("-1\n");
else {
w = 0;
c = 0;
a = 0;
for (i = 0; i < v.size(); i++) {
if (v[i] == 1)
a++;
else
a--;
w = max(a, w);
c = max(-a, c);
}
printf("%lld\n", w + c);
}
}
return (0);
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int l = 0, o = 0, lo = 0, ol = 0;
int n;
cin >> n;
string s, t;
cin >> s >> t;
for (int i = 0; i < n; ++i) {
if (s[i] == t[i]) continue;
if (s[i] == '0') {
if (l > 0) {
l--;
lo++;
} else {
if (ol > 0) {
ol--;
o++;
} else {
o++;
}
}
} else {
if (o > 0) {
o--;
ol++;
} else {
if (lo > 0) {
lo--;
l++;
} else {
l++;
}
}
}
}
if (l > 0 || o > 0) {
cout << -1 << "\n";
} else {
cout << lo + ol << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int main() {
int t = 1;
while (t--) {
int n;
cin >> n;
string s, r;
cin >> s >> r;
set<int> s1, s0;
int ans = 0;
for (int i = 0; i < n; i++)
if (s[i] != r[i]) {
if (s[i] == '1')
s1.insert(i);
else
s0.insert(i);
}
if (s1.size() != s0.size())
cout << -1 << endl;
else {
while (s1.size() != 0 && s0.size() != 0) {
ans++;
int d, val;
auto it = s1.begin();
auto ip = s0.begin();
int flag = 0;
if (*it > *ip) {
val = *ip;
d = 0;
s0.erase(ip);
} else {
flag = 1;
val = *it;
d = 1;
s1.erase(it);
}
while (1) {
if (d % 2 == 0) {
auto ig = s1.lower_bound(val);
if (ig == s1.end()) {
if (flag == 0) s0.insert(val);
break;
}
val = *ig;
s1.erase(ig);
} else {
auto ig = s0.lower_bound(val);
if (ig == s0.end()) {
if (flag == 1) s1.insert(val);
break;
}
val = *ig;
s0.erase(ig);
}
d ^= 1;
}
}
cout << ans;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
const int N = 2e5;
long long po(int, int);
void solve() {
int n;
cin >> n;
string s, t;
cin >> s >> t;
int m1 = 0, m2 = 0, bal = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1' && t[i] == '0') bal++;
if (s[i] == '0' && t[i] == '1') bal--;
m1 = max(m1, bal);
m2 = min(m2, bal);
}
if (bal != 0)
cout << "-1\n";
else
cout << m1 - m2 << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
while (t--) {
solve();
}
return 0;
}
long long po(int a, int b) {
if (b == 0) return 1;
long long ans = 1;
if (b % 2 == 0) {
ans = po(a, b / 2) % M;
ans = (ans % M * ans % M) % M;
} else {
ans = po(a, (b - 1) / 2) % M;
ans = (ans % M * ans % M * a) % M;
}
return ans % M;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n;
char a[N], b[N];
set<int> l, r;
int ans;
int main() {
ios::sync_with_stdio(false);
cin >> n >> a + 1 >> b + 1;
for (int i = 1; i <= n; i++) {
if (a[i] != b[i]) {
if (a[i] == '0')
l.insert(i);
else
r.insert(i);
}
}
if (!l.size() && !r.size()) return cout << 0, 0;
while (1) {
if (!l.size() && !r.size()) break;
if (l.size() == 0) {
return cout << -1, 0;
}
if (r.size() == 0) {
return cout << -1, 0;
break;
}
auto L = l.begin();
int dx = *L;
auto R = r.begin();
int dy = *R;
int last;
if (dx < dy) {
l.erase(L);
r.erase(R);
int st = 1;
while (1) {
if (st) {
auto x = l.lower_bound(dy);
if (x == l.end()) break;
dx = *x;
last = *x;
l.erase(x);
} else {
auto x = r.lower_bound(dx);
if (x == r.end()) break;
dy = *x;
r.erase(x);
}
st ^= 1;
}
if (!st) l.insert(last);
} else {
l.erase(L);
r.erase(R);
int st = 0;
while (1) {
if (st) {
auto x = l.lower_bound(dy);
if (x == l.end()) break;
dx = *x;
l.erase(x);
} else {
auto x = r.lower_bound(dx);
if (x == r.end()) break;
dy = *x;
last = *x;
r.erase(x);
}
st ^= 1;
}
if (st) r.insert(last);
}
ans++;
}
cout << ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
long long power(long long x, long long p) {
if (p == 0) return 1;
if (p == 1) return x;
if (p % 2) return x * power(x, p - 1);
long long y = power(x, p / 2);
return y * y;
}
void solve() {
int n;
cin >> n;
string s, t, res;
cin >> s >> t;
int one1 = 0, one2 = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1') one1++;
if (t[i] == '1') one2++;
if (s[i] != t[i]) res.push_back(s[i]);
}
if (one1 != one2) {
cout << "-1" << endl;
return;
}
int sum1 = 0, best1 = 0, sum2 = 0, best2 = 0;
for (int i = 0; i < res.size(); i++) {
sum1 = max(sum1 + (res[i] == '0' ? 1 : -1), (res[i] == '0' ? 1 : -1));
sum2 = max(sum2 + (res[i] == '1' ? 1 : -1), (res[i] == '1' ? 1 : -1));
best1 = max(best1, sum1);
best2 = max(best2, sum2);
}
cout << max(best1, best2) << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
long long const mod = 1e9 + 7;
long long const inf = 2e18;
inline long long add(long long a, long long b) {
return (a % mod + b % mod + mod) % mod;
}
inline long long mul(long long a, long long b) {
return (a % mod * b % mod + mod) % mod;
}
inline long long sub(long long a, long long b) {
return (a % mod - b % mod + mod) % mod;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long modexp(long long x, long long n) {
if (n == 0) {
return 1;
} else if (n & 1) {
return (modexp((x * x) % mod, n / 2) % mod * x % mod) % mod;
} else {
return (modexp((x * x) % mod, n / 2) % mod);
}
}
long long modinv(long long n) { return modexp(n, mod - 2); }
void solve() {
long long n;
cin >> n;
string second, t;
cin >> second >> t;
set<long long> s1[2];
for (long long i = 0; i < (n); i++) {
if (second[i] != t[i]) {
s1[t[i] - '0'].insert(i);
}
}
if (s1[0].size() != s1[1].size()) {
cout << "-1";
return;
}
long long ans = 0;
auto x = s1[0].begin();
auto y = s1[1].begin();
while (!s1[0].empty()) {
x = s1[0].begin();
y = s1[1].begin();
long long last = -1;
ans++;
if (*x < *y) {
while (1) {
x = s1[0].lower_bound(last);
y = s1[1].lower_bound(*x);
if (x == s1[0].end() || y == s1[1].end()) break;
last = *y;
s1[0].erase(x);
s1[1].erase(y);
}
} else {
while (1) {
y = s1[1].lower_bound(last);
x = s1[0].lower_bound(*y);
if (x == s1[0].end() || y == s1[1].end()) break;
last = *x;
s1[0].erase(x);
s1[1].erase(y);
}
}
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long T = 1;
for (int t = 1; t <= T; t++) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline void mine(T1 &x, T2 y) {
if (x > y) x = y;
}
template <typename T1, typename T2>
inline void maxe(T1 &x, T2 y) {
if (x < y) x = y;
}
ostream &operator<<(ostream &a, const vector<int> &b) {
for (auto k : b) cout << k << ' ';
return a;
}
const int MOD = 1000000007;
const int INF = 1000000050;
const long long BIG = (long long)2e18 + 50;
const int MX = 200010;
const double EPS = 1e-9;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
string s1, s2;
cin >> s1 >> s2;
set<int> p01;
set<int> p10;
for (int i = 0; i < n; i++) {
if (s1[i] == '0' && s2[i] == '1')
p01.insert(i);
else if (s1[i] == '1' && s2[i] == '0')
p10.insert(i);
}
if (p01.size() != p10.size()) {
cout << "-1\n";
return 0;
}
int ans = 0;
while (!p01.empty()) {
int cur;
cur = min(*p01.begin(), *p10.begin());
vector<int> done;
while (true) {
done.push_back(cur);
if (p01.find(cur) != p01.end()) {
auto it = p10.upper_bound(cur);
if (it == p10.end()) break;
cur = *it;
} else {
auto it = p01.upper_bound(cur);
if (it == p01.end()) break;
cur = *it;
}
}
if ((int)done.size() % 2) done.pop_back();
for (auto &x : done) {
p01.erase(x);
p10.erase(x);
}
++ans;
}
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2000005;
int n, x, y, z, m, k, A, B;
char a[N], b[N];
int arr[N], H[N], s, t;
int G[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
A += (a[i] == '1');
}
vector<int> v1, v2;
for (int i = 1; i <= n; i++) {
cin >> b[i];
B += (b[i] == '1');
if (a[i] != b[i]) {
if (a[i] == '1') {
v1.push_back(i);
} else {
v2.push_back(i);
}
}
}
if (A != B) {
cout << -1 << endl;
return 0;
}
int t1 = 0;
int t2 = 0;
for (int i = 1; i <= n; i++) {
if (t1 < v1.size() and i == v1[t1]) {
G[i] = 1;
G[v2[t2]] = 2;
t2++;
t1++;
} else if (t2 < v2.size() and i == v2[t2]) {
G[i] = -1;
G[v1[t1]] = -2;
t2++;
t1++;
}
}
int mx1 = 0, mx2 = 0;
int s2 = 0, s1 = 0;
for (int i = 1; i <= n; i++) {
if (G[i] == 1) {
s1++;
} else if (G[i] == 2) {
s1--;
} else if (G[i] == -1) {
s2++;
} else if (G[i] == -2) {
s2--;
}
mx1 = max(mx1, s1);
mx2 = max(mx2, s2);
}
cout << mx1 + mx2 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long min(long long a, long long b) { return (a < b) ? a : b; }
long long max(long long a, long long b) { return (a > b) ? a : b; }
long long fp(long long a, long long b) {
if (b == 0) return 1;
long long x = fp(a, b / 2);
x = (x * x) % mod;
if (b & 1) x = (x * a) % mod;
return x;
}
const long long N = 1e6 + 6;
char a[N], b[N];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
string s1, s2;
cin >> s1;
cin >> s2;
long long c1 = 0, c2 = 0;
vector<long long> v;
set<array<long long, 2>> st;
vector<vector<long long>> mp(3);
for (long long i = 0; i < n; i++) {
a[i + 1] = s1[i] - '0';
b[i + 1] = s2[i] - '0';
if (s1[i] == '1')
c1++;
else
c1--;
if (s2[i] == '1')
c2++;
else
c2--;
if (s1[i] != s2[i]) {
v.push_back(s1[i] - '0');
st.insert({i, s1[i] - '0'});
mp[s1[i] - '0'].push_back(i);
}
}
if (c1 != c2) {
cout << -1;
return 0;
}
if (s1 == s2) {
cout << 0;
return 0;
}
long long ans = 0, a = 0, b = 0;
for (auto i : v) {
if (i == 0) {
if (b > 0) {
b--;
}
a++;
} else {
if (a > 0) {
a--;
}
b++;
}
ans = max(ans, a + b);
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
return (a.first > b.first);
}
const int MAXN = 1000005;
long long int fac[MAXN];
const int mod = 1e9 + 7;
long long int expo(long long int a, long long int b) {
if (b == 0)
return 1;
else if (b % 2 == 0) {
long long int y = expo(a, b / 2);
return (y * y) % mod;
} else
return (a * expo(a, b - 1)) % mod;
}
long long int modinv(long long int n) { return expo(n, mod - 2); }
long long int C(int n, int r) {
long long int ans =
(fac[n] * ((modinv(fac[r]) * modinv(fac[n - r])) % mod)) % mod;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
string s, t;
cin >> n >> s >> t;
vector<int> vec;
int cnt0 = 0, cnt1 = 0;
for (int i = 0; i < n; i++) {
if (s[i] != t[i]) {
vec.push_back(s[i] - '0');
if (s[i] == '1')
++cnt1;
else
++cnt0;
}
}
if (cnt0 != cnt1) {
cout << "-1\n";
return 0;
}
int x = 0, y = 0;
for (int i = 0; i < vec.size(); i++) {
if (vec[i]) {
if (x) --x;
++y;
} else {
if (y) --y;
++x;
}
}
cout << x + y << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
long long int r, i;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
long long int power(long long int x, long long int y, long long int mod) {
long long int temp, ty, my;
if (y == 0) return 1;
temp = power(x, y / 2, mod);
ty = (temp % mod) * (temp % mod);
if (y % 2 == 0) {
return ty % mod;
} else {
my = (x % mod) * (ty % mod);
return my % mod;
}
}
void fastscan(int &number) {
bool negative = false;
register int c;
number = 0;
c = getchar();
if (c == '-') {
negative = true;
c = getchar();
}
for (; (c > 47 && c < 58); c = getchar()) number = number * 10 + c - 48;
if (negative) number *= -1;
}
const long long int N = 2000010;
vector<long long int> graph[N];
long long int ar[N];
long long int br[N];
long long int visit[N];
double eps = 2.22045e-010;
long long int dpf[N];
long long int dpb[N];
long long int solve(long long int arr[], long long int n) {
long long int i, j, k;
long long int mx = 0;
long long int temp = 0;
for (i = (0); i < (n); i++) {
temp += arr[i];
temp = max(temp, 0LL);
mx = max(mx, temp);
}
return mx;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, m, i, t, j, k, q;
long long int a, b, c, h, d;
cin >> n;
string a1, a2;
cin >> a1;
cin >> a2;
long long int c1 = 0, c2 = 0;
for (i = (0); i < (n); i++) {
if (a1[i] == a2[i]) {
ar[i] = 0;
br[i] = 0;
} else if (a1[i] == '1') {
ar[i] = 1;
br[i] = -1;
c1++;
} else {
ar[i] = -1;
br[i] = 1;
c2++;
}
}
if (c1 != c2) {
cout << "-1\n";
return 0;
}
long long int ans = max(solve(ar, n), solve(br, n));
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s1, s2;
cin >> s1 >> s2;
int a[n];
int x = 0;
for (int i = 0; i < n; i++) {
if (s1[i] == s2[i])
a[i] = 0;
else if (s1[i] == '1') {
a[i] = 1;
x++;
} else {
a[i] = -1;
x--;
}
}
if (x != 0) {
cout << -1 << endl;
return 0;
}
int sum = 0, best = 0;
for (int i = 0; i < n; i++) {
sum = max(a[i], sum + a[i]);
best = max(best, sum);
a[i] *= -1;
}
sum = 0;
for (int i = 0; i < n; i++) {
sum = max(a[i], sum + a[i]);
best = max(best, sum);
}
cout << best << endl;
}
|
#include <bits/stdc++.h>
const int N = 1000010;
char s[N], t[N], tmp[N];
int cnt[N];
int main() {
int n;
scanf("%d%s%s", &n, s, t);
for (int i = 0; i < n; ++i) {
++cnt[s[i]];
--cnt[t[i]];
}
if (cnt['0'] || cnt['1']) {
puts("-1");
return 0;
}
int len = 0;
for (int i = 0; i < n; ++i) {
if (s[i] != t[i]) {
tmp[len++] = s[i];
}
}
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < len; ++i) {
if (tmp[i] == '0') {
if (cnt2 > 0) {
--cnt2;
}
++cnt1;
} else {
if (cnt1 > 0) {
--cnt1;
}
++cnt2;
}
}
printf("%d\n", cnt1 + cnt2);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long binpow(long long a, long long b) {
if (b == 0) return 1;
long long res = binpow(a, b / 2);
res *= res;
if (b % 2) return res * a;
return res;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
string to_upper(string a) {
for (int i = 0; i < (int)a.size(); ++i)
if (a[i] >= 'a' && a[i] <= 'z') a[i] -= 'a' - 'A';
return a;
}
string to_lower(string a) {
for (int i = 0; i < (int)a.size(); ++i)
if (a[i] >= 'A' && a[i] <= 'Z') a[i] += 'a' - 'A';
return a;
}
void solve() {
int n;
cin >> n;
cout << n / 2 + 1 << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
while (n--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int tc = 1;
cin >> tc;
int n;
while (tc--) {
cin >> n;
cout << n / 2 + 1 << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int MAX = (long long int)1e5 + 5;
void solve() {
long long int n;
cin >> n;
long long int ans = n / 2 + 1;
cout << ans << endl;
}
int32_t main() {
ios_base::sync_with_stdio();
cin.tie(0);
cout.tie(0);
;
long long int tc;
cin >> tc;
while (tc--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
cout << (n / 2) + 1 << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int factorial(long long int n) {
return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long int lcm(long long int a, long long int b) {
return (a * b) / gcd(a, b);
}
bool sortByVal(const pair<double, double> &a, const pair<double, double> &b) {
if (a.first == b.first) {
return (a.second > b.second);
} else
return (a.first < b.first);
}
int main() {
long long int t = 1;
cin >> t;
while (t--) {
long long int n, m, i, j, k, l, r, sum = 0, b, c = 0, d = 0, ans = 0;
cin >> n;
cout << n / 2 + 1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const long long mod = 1e9 + 7;
inline long long read() {
long long X = 0;
bool flag = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') flag = 0;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
X = (X << 1) + (X << 3) + ch - '0';
ch = getchar();
}
if (flag) return X;
return ~(X - 1);
}
long long n, m, k;
long long ar[N], br[N];
long long ans;
void solve() {
n = read();
cout << n / 2 + 1 << endl;
}
int main() {
int T;
T = read();
while (T--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int inf = 1000000000000000000;
const long double pi = 3.14159265358979323846264338;
const long long mod = 1000000007;
long long int pow(long long int x, unsigned long long int y, long long int p) {
long long int res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int invmod(long long int a, long long int m) {
return pow(a, m - 2, m);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
cout << n / 2 + 1 << "\n";
}
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline")
#pragma GCC optimization("O3")
using namespace std;
const unsigned long long mod = 1000000007;
const long long inf = ((1ll << 31ll) - 1ll);
const long long INF = (((1ll << 59ll) - 1ll) * 2ll) + 1ll;
const long double pi = acos(-1);
const long long MAXN = 1000000;
vector<long long> Criba(MAXN + 1);
void iniciar_criba() {
for (long long i = 2; i <= MAXN; i += 2) Criba[i] = 2;
for (long long i = 3; i <= MAXN; i += 2) {
if (!Criba[i]) {
Criba[i] = i;
for (long long j = i * i; j <= MAXN; j += (2 * i))
if (!Criba[j]) Criba[j] = i;
}
}
}
bool isprime(long long tupac) { return (Criba[tupac] == tupac); }
long long GCD(long long q, long long r) {
if (r == 0) return q;
return GCD(r, q % r);
}
long long LCM(long long q, long long r) { return q * (r / GCD(q, r)); }
long long qpow(long long b, long long e) {
if (!e) return 1;
if (e & 1) return qpow(b, e - 1) * b % mod;
long long pwur = qpow(b, e >> 1);
return pwur * pwur % mod;
}
long long InverM(long long a, long long b) {
long long eso = a - (a / b) * b;
if (eso == 0) return 0;
long long ans = (1 - b * InverM(b, eso)) / eso;
if (ans < 0) ans += b;
return ans;
}
long long t, n, m, k, lB, rB;
void problem() {
cin >> n;
cout << n / 2 + 1;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.setf(ios::fixed);
cout.precision(0);
srand(time(NULL));
cin >> t;
while (t--) {
problem();
cout << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
cout << n / 2 + 1 << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
cout << n / 2 + 1 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long int bigmod(long long int a, long long int p, long long int m) {
long long int result = 1;
long long int x = a;
while (p) {
if (p & 1) {
result = (result * x) % m;
}
x = (x * x) % m;
p = p >> 1;
}
return result;
}
int main() {
long long int t, nab, nbc, nca, n, m, i, j;
long long int d, a, b, c;
scanf("%I64d", &t);
while (t--) {
cin >> n;
if (n == 1)
cout << "1";
else
cout << (n / 2) + 1;
printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
if (n == 1)
cout << 1 << endl;
else if (n == 2) {
cout << 2 << endl;
} else {
int m = 2 + (n / 2 - 1);
cout << m << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
long long power(long long base, long long exp);
void solve() {
long long n;
cin >> n;
cout << n - (n - 1) / 2 << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
long long t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
long long power(long long base, long long exp) {
base %= 1000000007;
long long result = 1;
while (exp > 0) {
if (exp & 1) result = ((long long)result * base) % 1000000007;
base = ((long long)base * base) % 1000000007;
exp >>= 1;
}
return result;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t, n, i, m;
cin >> t;
while (t--) {
cin >> n;
cout << n / 2 + 1 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
int n;
while (t--) cin >> n, cout << n / 2 + 1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
void subham_cc99() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
int32_t main() {
subham_cc99();
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int ans = (n / 2);
ans++;
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
int t;
int main() {
cin >> t;
while (t--) {
int n;
cin >> n;
if (n == 1)
cout << 1 << endl;
else
cout << n / 2 + 1 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long int t;
cin >> t;
for (long long int f = 0; f < t; f++) {
long long int n;
cin >> n;
if (n % 2 == 0) {
cout << n / 2 + 1 << endl;
} else {
cout << (n + 1) / 2 << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for (int _ = 0; _ < t; _++) {
int n;
cin >> n;
cout << n / 2 + 1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n;
cin >> t;
while (t--) {
cin >> n;
cout << (n / 2) + 1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int T, N;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> T;
for (int i = 1; i <= T; ++i) {
cin >> N;
cout << N / 2 + 1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
cout << n / 2 + 1 << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
void solve(int n) {
int count = 0;
if (n % 2 == 0) {
count = n / 2 + 1;
cout << count << endl;
} else {
count = (n + 1) / 2;
cout << count << endl;
}
}
int main() {
int t;
cin >> t;
while (t) {
int n;
cin >> n;
solve(n);
t--;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solution() {
long long int n;
cin >> n;
cout << n / 2 + 1 << "\n";
}
int32_t main() {
long long int t;
t = 1;
cin >> t;
for (long long int i = 0; i < t; i++) solution();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int i, t;
cin >> t;
while (t--) {
long long n;
cin >> n;
if (n == 1) {
cout << 1 << endl;
} else
cout << n / 2 + 1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n;
cin >> t;
while (t--) {
cin >> n;
cout << n / 2 + 1 << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e9 + 5;
int t, n, x;
int main() {
cin >> t;
while (t--) {
cin >> x;
printf("%d\n", x / 2 + 1);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MID = 1e5 + 3;
const int INF = 1e9 + 5;
const int MOD = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int q;
cin >> q;
while (q--) {
int n;
cin >> n;
if (n % 2 == 0) {
cout << n / 2 + 1 << "\n";
} else {
cout << (n + 1) / 2 << "\n";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(7);
long long tt;
cin >> tt;
while (tt--) {
long long x;
cin >> x;
if (x % 2 == 0)
cout << x / 2 + 1 << "\n";
else
cout << (x + 1) / 2 << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int ans = (n / 2);
ans += 1;
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int T, p;
cin >> T;
for (p = 1; p <= T; p++) {
long long int N, a;
cin >> N;
a = ((long long int)(N / 2)) + 1;
cout << a << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const char nl = '\n';
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
;
long long t, n, x;
cin >> t;
while (t--) {
cin >> n;
x = n / 2;
if (n & 1)
cout << n - x << nl;
else {
cout << n - x + 1 << nl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
int n;
for (int i = 0; i < t; i++) {
cin >> n;
if (n % 2 == 0) {
cout << (n / 2) + 1 << endl;
} else if (n % 2 != 0) {
cout << (n + 1) / 2 << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
cout << (int)(n / 2) + 1 << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
int x;
while (t--) {
cin >> x;
cout << x / 2 + 1 << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int INF = 2140000000;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(time(NULL));
int t;
cin >> t;
while (t-- > 0) {
int n;
cin >> n;
cout << n / 2 + 1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long int t;
register long int n;
cin >> t;
while (t--) {
cin >> n;
cout << ((n >> 1) + 1) << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
scanf("%d", &T);
while (T--) {
int n;
scanf("%d", &n);
if (n & 1)
printf("%d\n", (n + 1) / 2);
else
printf("%d\n", (n + 2) / 2);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
long long t;
cin >> t;
for (long long w = 0; w < t; w++) {
long long x;
cin >> x;
long long y = x / 2;
cout << y + 1 << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
if (n % 2 == 1) {
cout << n / 2 + 1 << "\n";
} else {
cout << (n + 2) / 2 << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
int main() {
int t, i;
long long int n;
scanf("%d", &t);
for (i = 0; i < t; i++) {
scanf("%lld", &n);
printf("%lld\n", n / 2 + 1);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int tt = 0;
scanf("%d", &tt);
while (tt--) {
int n, ans = 0;
scanf("%d", &n);
printf("%d\n", n / 2 + 1);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
long long n;
cin >> n;
cout << n / 2 + 1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
cout << n / 2 + 1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.14159265358979323;
const long long mod1 = 1e9 + 7;
const long long mod2 = 1e18;
const long long mod3 = 9e18;
const long long N = 1e9 + 7;
long long power(long long x, long long n) {
if (n == 0) return 1;
long long temp = power(x, n / 2);
if (n % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
long long gcd(long long a, long long b) {
if (a < b) return gcd(b, a);
if (b == 0) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a * b) / (gcd(a, b)); }
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
bool f(long long x, long long y) { return x > y; }
long long MAX(long long a, long long b) { return a > b ? a : b; }
long long MIN(long long x, long long y) { return x > y ? y : x; }
long long pmod(long long a, long long b, long long mod) {
long long ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % mod;
b /= 2;
a = (a * a) % mod;
}
return ans;
}
void solve() {
long long n;
cin >> n;
cout << n / 2 + 1 << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using lli = long long int;
const int MOD = 1000000007;
const int MOD1 = 998244353;
const int maxn = 100010;
const int lim = (int)1e9;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 0, n = 0;
cin >> t;
while (t--) {
cin >> n;
cout << (n / 2) + 1 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
if (n == 1)
cout << 1 << endl;
else if (n == 3 || n == 2)
cout << 2 << endl;
else if (n % 2 == 0) {
cout << n / 2 + 1 << endl;
} else {
cout << (n + 1) / 2 << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
cout << n / 2 + 1 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin >> t;
while (t--) {
long long n, x;
cin >> n;
x = (n / 2) + 1;
cout << x << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
cout << n / 2 + 1 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int nxt() {
int a;
cin >> a;
return a;
}
long long int nxtll() {
long long int a;
cin >> a;
return a;
}
void solve() {
int n = nxt();
if (n <= 2)
cout << n << endl;
else
cout << 1 + n / 2 << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
bool multi_run = 1;
if (multi_run) {
int t;
cin >> t;
while (t--) solve();
} else {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
double d;
int n;
cin >> n;
n /= 2;
n++;
cout << n << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int a[N];
void solve() {
int n;
cin >> n;
cout << n / 2 + 1 << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void setIO(string str, bool dbg) {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
cout.tie(nullptr);
if (!dbg) {
freopen((str + ".in").c_str(), "r", stdin);
freopen((str + ".out").c_str(), "w", stdout);
}
}
const long long MAX = 1e6 + 5;
const long long LEN = 1e6 + 5;
const long long MOD = 1e9 + 7;
const long long INF = 9e17;
long long xyz = 1;
long long n;
void run() {
cin >> n;
cout << n / 2 + 1 << "\n";
}
signed main() {
setIO("", 1);
cin >> xyz;
while (xyz--) run();
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
const long long MOD = 1e9 + 7;
using namespace std;
bool isPrime(long long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long i = 5; i * i <= n; i = i + 2)
if (n % i == 0) return false;
return true;
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long lcm(long long a, long long b) {
long long g = gcd(a, b);
long long ans = (a * b) / g;
return ans;
}
bool palindrome(string s) {
for (long long i = 0; i < s.length() / 2; i++) {
if (s[i] != s[s.length() - i - 1]) return 0;
}
return 1;
}
void printDivisors(int n) {
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i)
printf("%d ", i);
else
printf("%d %d ", i, n / i);
}
}
}
int convert_number(string s) {
int n;
stringstream(s) >> n;
return n;
}
vector<long long> prime;
void primes(long long m) {
bool isprime[m];
for (int i = 0; i <= m; i++) isprime[i] = true;
for (int i = 3; i * i <= m; i++) {
if (isprime[i]) {
for (int j = i * i; j <= m; j += i) isprime[j] = false;
}
}
prime.push_back(2);
for (int i = 3; i <= m; i += 2)
if (isprime[i]) prime.push_back(i);
}
bool sign(int n) {
if (n > 0)
return true;
else
return false;
}
int findsum(int n) {
int sum = 0;
if (n != 0)
return sum = n + findsum(n - 1);
else
return 0;
}
int main(int argc, char *argv[]) {
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
cout << (n / 2) + 1 << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int t, n, i, r;
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
printf("%d\n", n / 2 + 1);
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.