text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
int n, r;
long long c[1 << 19];
int main() {
while (scanf("%d%d", &n, &r) != EOF) {
int nn = 1 << n;
long long sum = 0;
for (int i = 0; i < nn; ++i) {
scanf("%lld", &c[i]);
sum += c[i];
}
printf("%.8f\n", 1.0 * sum / nn);
while (r--) {
int x;
long long y;
scanf("%d%lld", &x, &y);
sum += (y - c[x]);
printf("%.8f\n", 1.0 * sum / nn);
c[x] = y;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, r;
int a[1 << 18];
double an = 0;
int main() {
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(12);
cin >> n >> r;
for (int i = 0; i < (1 << n); i++) {
cin >> a[i];
an += a[i];
}
cout << an * pow(2, -n) << '\n';
for (int i = 0; i < r; i++) {
int pos, val;
cin >> pos >> val;
an -= a[pos];
a[pos] = val;
an += a[pos];
cout << an * pow(2, -n) << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int r, n, c[300000], i, j, k, z, g, sum = 0;
cin >> n >> r;
for (i = 0; i < (1 << n); i++) {
cin >> c[i];
sum = sum + c[i];
}
n = 1 << n;
double ans1, ans2;
ans1 = (double)sum / n;
ans2 = ans1;
cout << ans2 << setprecision(10) << "\n";
while (r--) {
cin >> z >> g;
ans2 = ans1 + (double)(g - c[z]) / n;
c[z] = g;
cout << ans2 << setprecision(10) << "\n";
ans1 = ans2;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, no_of_changes;
scanf("%d %d", &n, &no_of_changes);
int no_of_elements = (1LL << n);
long long sum = 0;
vector<int> A(no_of_elements + 1);
for (int i = 0; i < no_of_elements; i++) {
scanf("%d", &A[i]);
sum += A[i];
}
double average = (sum * 1.0) / no_of_elements;
printf("%.12f\n", average);
for (int i = 1; i <= no_of_changes; i++) {
int position, new_value;
scanf("%d %d", &position, &new_value);
sum = sum - A[position] + new_value;
A[position] = new_value;
average = (sum * 1.0) / (no_of_elements);
printf("%.12f\n", average);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n, r;
cin >> n >> r;
n = 1 << n;
long long c[n], sum = 0;
for (long long i = 0; i < n; i++) {
cin >> c[i];
sum += c[i];
}
cout << fixed << setprecision(10) << endl;
cout << (double)sum / n << endl;
for (long long i = 0; i < r; i++) {
long long idx, val;
cin >> idx >> val;
sum -= c[idx];
c[idx] = val;
sum += c[idx];
cout << (double)sum / n << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = (1 << 18);
long long n, r, all, c[N];
void solve() {
cin >> n >> r;
for (int i = 0; i < (1 << n); ++i) {
cin >> c[i];
all += c[i];
}
cout << setprecision(6) << fixed << all / (long double)(1 << n) << "\n";
for (int i = 0; i < r; ++i) {
long long z, g;
cin >> z >> g;
all += g - c[z];
c[z] = g;
cout << setprecision(6) << fixed << all / (long double)(1 << n) << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
solve();
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); }
const long long mod = 1e9 + 7;
const int maxn = 1e6 + 10;
const int inf = 0x3f3f3f3f;
int T;
int n, c[maxn], q;
int main() {
while (~scanf("%d%d", &n, &q)) {
long long sum = 0;
n = pow(2, n);
for (int i = 0; i <= n - 1; i++) {
scanf("%d", &c[i]);
sum += c[i];
}
printf("%.15lf\n", 1.0 * sum / n);
int a, b;
while (q--) {
scanf("%d%d", &a, &b);
sum = sum - c[a] + b;
c[a] = b;
printf("%.15lf\n", 1.0 * sum / n);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char **argv) {
int n, r;
cin >> n >> r;
vector<long long> vec(1 << n);
long long sum = 0;
for (auto &v : vec) {
cin >> v;
sum += v;
}
cout << sum / double(1 << n) << '\n';
int a, b;
for (size_t i = 0; i < r; ++i) {
cin >> a >> b;
sum += (b - vec[a]);
vec[a] = b;
cout << fixed << setprecision(10) << sum / double(1 << n) << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
ll x[1 << 18];
int main() {
ios::sync_with_stdio(false), cin.tie(0);
ll n, r;
cin >> n >> r;
ll m = 1 << n;
ll sum = 0;
for (int i = 0; i < m; i++) cin >> x[i], sum += x[i];
cout << fixed << setprecision(7);
cout << (ld)sum / (ld)m << endl;
for (int i = 0; i < r; i++) {
ll a, b;
cin >> a >> b;
sum -= x[a];
x[a] = b;
sum += x[a];
cout << (ld)sum / (ld)m << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = (1 << 18) + 5;
int n, r;
int c[N];
int main() {
scanf("%d%d", &n, &r);
for (int i = 0; i < (1 << n); i++) scanf("%d", &c[i]);
long long sum = 0;
for (int i = 0; i < (1 << n); i++) sum += c[i];
printf("%.7lf\n", (double)sum / (1 << n));
while (r--) {
int z, g;
scanf("%d%d", &z, &g);
sum -= c[z];
c[z] = g;
sum += c[z];
printf("%.7lf\n", (double)sum / (1 << n));
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long nums[270000], sum;
int n, r;
void ans(long long sum, int len) {
long long head = sum / len;
long long tail = sum % len * 1000000;
tail /= len;
printf("%lld.%lld\n", head, tail);
}
int main() {
while (cin >> n >> r) {
sum = 0;
for (int i = 0; i < (1 << n); i++) {
cin >> nums[i];
sum += nums[i];
}
ans(sum, 1 << n);
for (int i = 0; i < r; i++) {
int a, b;
cin >> a >> b;
sum -= nums[a];
nums[a] = b;
sum += nums[a];
ans(sum, 1 << n);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, r, c[262144], z, g;
double ans;
int main() {
scanf("%d%d", &n, &r), n = 1 << n;
for (int i = 0; i < n; i++) scanf("%d", &c[i]), ans += 1.0 * c[i] / n;
printf("%.6f\n", ans);
for (int i = 0; i < r; i++)
scanf("%d%d", &z, &g), ans += 1.0 * (g - c[z]) / n, c[z] = g,
printf("%.6f\n", ans);
}
|
#include <bits/stdc++.h>
#pragma GCC optimize "Ofast"
#pragma GCC optimize "unroll-loops"
#pragma GCC target "sse,sse2,sse3,sse4,abm,avx,mmx,popcnt,tune=native"
char _;
using namespace std;
int n, r, z, g;
long long tot;
int c[300000];
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> r;
for (int i = 0; i < (1 << n); i++) {
cin >> c[i];
tot += c[i];
}
cout << fixed << setprecision(10) << (1.0 * tot / (1 << n)) << "\n";
for (int i = 0; i < r; i++) {
cin >> z >> g;
tot += g - c[z];
c[z] = g;
cout << fixed << setprecision(10) << (1.0 * tot / (1 << n)) << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll llinf = (1ll << 61) - 1;
const int inf = 1000000007;
int TC = 1, CN, n, r, c[1 << 18 | 5];
signed main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
cout.precision(11), cout.setf(ios::fixed);
auto kase = [&]() -> void {
cin >> n >> r;
double sum = 0;
for (int i = 0; i < 1 << n; i++) {
cin >> c[i], sum += c[i];
}
for (int i = 0; i < r; i++) {
cout << sum / (1 << n) << '\n';
int z, g;
cin >> z >> g;
sum -= c[z], c[z] = g, sum += c[z];
}
cout << sum / (1 << n) << '\n';
};
while (CN++ != TC) kase();
}
|
#include <bits/stdc++.h>
using namespace std;
double const EPS = 1.0E-9;
int const MOD = (int)1e9 + 7;
inline int read(char* s) { return scanf("%s", s); }
inline int read(int& x) { return scanf("%d", &(x)); }
inline int read(long long& x) { return scanf("%lld", &x); }
inline int read(double& x) { return scanf("%lf", &x); }
inline int writeln(int x) { return printf("%d\n", x); }
inline int writeln(long long x) { return printf("%lld\n", x); }
inline int writeln(double x) { return printf("%.12f\n", x); }
template <typename... T>
int read(T&... args) {
using swallow = int[];
swallow w = {0, (read(args))...};
return sizeof(w) / sizeof(w[0]) - 1;
}
template <typename T>
int read(std::vector<T>& v) {
for (T& t : v) read(t);
return (int)v.size();
}
inline long long gcd(long long x, long long y) {
return y == 0 ? x : gcd(y, x % y);
}
inline long long lcm(long long x, long long y) {
return x == 0 && y == 0 ? 0 : x / gcd(x, y) * y;
}
inline long long powmod(long long x, long long n, long long m = MOD) {
long long r = 1;
while (n) {
if (n & 1) r = (r * x) % m;
x = (x * x) % m;
n /= 2;
}
return r;
}
static int leap(int y) { return y % 4 == 0 && y % 100 != 0 || y % 400 == 0; }
static int const month[2][12] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
static const int maxn = 1 << 19;
static const int maxm = 1 << 20;
int n, q;
int a[maxn];
long long sum;
static int solve() {
read(n, q);
for (int i = 0; i < (1 << n); ++i) {
read(a[i]);
sum += a[i];
}
writeln((double)sum / (1 << n));
for (int i = (0), vzq_b = (q); i < vzq_b; ++i) {
int z, g;
read(z, g);
sum += g - a[z];
a[z] = g;
writeln((double)sum / (1 << n));
}
return 0;
}
int main(int argc, char* argv[]) {
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using LD = long double;
const int MX = 1 << 18 | 3;
struct Istream {
static const int MXL = 2000000;
char bf[MXL], *p1, *p2;
FILE* F;
Istream(FILE* F = stdin) : p1(bf), p2(bf), F(F) {}
char getchar() {
return p1 == p2 && (p2 = (p1 = bf) + fread(bf, 1, MXL, F)) == bf ? EOF
: *p1++;
}
Istream& operator>>(int& x) {
int c = 0;
x = 0;
while (!isdigit(c)) c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
return *this;
}
} __JUMPMELON_DA_ZUI_ZI__;
int N, a[MX], Q;
LL sum;
int main() {
ios::sync_with_stdio(0);
cout << fixed << setprecision(10);
__JUMPMELON_DA_ZUI_ZI__ >> N >> Q;
for (int i = 0; i < (1 << N); i++)
__JUMPMELON_DA_ZUI_ZI__ >> a[i], sum += a[i];
cout << (LD)sum / (1 << N) << "\n";
while (Q--) {
int u, x;
__JUMPMELON_DA_ZUI_ZI__ >> u >> x;
sum = sum - a[u] + x;
a[u] = x;
cout << (LD)sum / (1 << N) << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 6e5 + 51;
int n, r, pos, c;
long double res;
int x[MAXN];
inline int read() {
register int num = 0, neg = 1;
register char ch = getchar();
while (!isdigit(ch) && ch != '-') {
ch = getchar();
}
if (ch == '-') {
neg = -1;
ch = getchar();
}
while (isdigit(ch)) {
num = (num << 3) + (num << 1) + (ch - '0');
ch = getchar();
}
return num * neg;
}
int main() {
n = 1 << read(), r = read();
for (register int i = 0; i < n; i++) {
x[i] = read(), res += x[i];
}
cout << fixed << setprecision(8) << res / n << endl;
for (register int i = 1; i <= r; i++) {
pos = read(), c = read(), res -= x[pos], res += (x[pos] = c);
cout << fixed << setprecision(8) << res / n << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
double mc[32];
vector<double> vc;
int main() {
mc[0] = 1;
for (int i = 1; i <= 30; i++) mc[i] = mc[i - 1] * 2;
long long n, r;
double q;
double sum, ans, x1, x2;
while (scanf("%lld%lld", &n, &r) != EOF) {
sum = 0;
for (long long i = 0; i < mc[n]; i++) {
scanf("%lf", &q);
sum += q;
vc.push_back(q);
}
printf("%.6f\n", sum / mc[n]);
for (int i = 0; i < r; i++) {
scanf("%lf%lf", &x1, &x2);
sum -= vc[x1];
vc[x1] = x2;
sum += x2;
ans = sum / mc[n];
printf("%.6f\n", ans);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using dd = double;
using ldd = long double;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n, r;
cin >> n >> r;
vector<ll> arr((ll(1)) << n);
ll sum = 0;
n = ((ll)1) << n;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
sum += arr[i];
}
cout.precision(10);
cout << ((dd)sum) / (dd(n)) << '\n';
for (int i = 0; i < r; ++i) {
ll a, b;
cin >> a >> b;
sum -= arr[a];
sum += b;
arr[a] = b;
cout << ((dd)sum) / (dd(n)) << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long double PI = acos(-1);
const long double eps = 0.0000000001;
const long long INF = 0x3fffffffffffffff;
long long n, r, a, b, f[1 << 18], ans;
long double s;
signed main() {
ios::sync_with_stdio(false);
cout << fixed << setprecision(6);
cin >> n >> r;
s = 1 << n;
for (long long i = 0; i < 1 << n; ++i) {
cin >> f[i];
ans += f[i];
}
cout << ans / s << endl;
for (long long i = 1; i <= r; ++i) {
cin >> a >> b;
ans -= f[a];
f[a] = b;
ans += f[a];
cout << ans / s << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double a[2000005], sum = 0;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << setprecision(12);
long long n, r, i, j, idx, x, tot;
cin >> n >> r;
tot = (1 << n);
for (i = 0; i < (1 << n); i++) {
cin >> a[i];
sum += a[i];
}
cout << (sum / (1.0l * tot)) << "\n";
while (r--) {
cin >> idx >> x;
sum += (x - a[idx]);
a[idx] = x;
cout << (sum / (1.0l * tot)) << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
long long n, q;
double res = 0.0;
cin >> n >> q;
vector<long long> a(1 << n);
for (long long i = 0; i < (1 << n); i++) cin >> a[i];
for (long long i = 0; i < a.size(); i++) res += a[i];
cout << fixed << setprecision(10);
cout << (res / (1 << n)) << '\n';
while (q--) {
long long x, val;
cin >> x >> val;
res += val - a[x];
a[x] = val;
cout << (res / (1 << n)) << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long expo(long long base, long long pow) {
long long ans = 1;
while (pow != 0) {
if (pow & 1 == 1) {
ans = ans * base;
ans = ans % mod;
}
base *= base;
base %= mod;
pow /= 2;
}
return ans;
}
long long inv(long long x) { return expo(x, mod - 2); }
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int M = 2e6 + 5;
long long arr[M];
int main() {
int n;
long long r;
cin >> n >> r;
long long summ = 0;
long long tot = (1LL << n);
for (int i = 0; i < (1 << n); i++) {
scanf("%lld", &(arr[i]));
summ += arr[i];
}
double ex = (summ * 1.0) / tot;
printf("%.10f\n", ex);
for (int i = 1; i <= r; i++) {
int here;
long long val;
scanf("%d", &(here));
scanf("%lld", &(val));
summ -= arr[here];
summ += val;
arr[here] = val;
ex = (summ * 1.0) / tot;
printf("%.10f\n", ex);
}
}
|
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using LD = long double;
const int MX = 1 << 18 | 3;
int N, a[MX], Q;
LL sum;
int main() {
ios::sync_with_stdio(0);
cout << fixed << setprecision(10);
cin >> N >> Q;
for (int i = 0; i < (1 << N); i++) cin >> a[i], sum += a[i];
cout << (LD)sum / (1 << N) << "\n";
while (Q--) {
int u, x;
cin >> u >> x;
sum = sum - a[u] + x;
a[u] = x;
cout << (LD)sum / (1 << N) << "\n";
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
const int N=1e6+7,INF=0x3f3f3f3f,sgn[]={-1,1};
char a[N];
int n,s[N],nxt[N];
int solve(int x,int cnt=0){
int w=0;
FOR(i,1,n){
if(a[i]=='?'&&cnt+2+nxt[i]<=x)cnt+=2;
w=min(w,s[i]+cnt);
}
return x-w;
}
int main(){
scanf("%s",a+1),n=strlen(a+1),nxt[n+1]=-INF;
FOR(i,1,n)s[i]=s[i-1]+sgn[a[i]=='1'];
ROF(i,n,0)nxt[i]=max(nxt[i+1],s[i]);
cout<<min(solve(nxt[0]),solve(nxt[0]+1));
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int) (x).size())
typedef long long ll;
typedef pair<int,int> pii;
template<class T> void print(T & x){ cout << x; }
template<class T,class U> void print(pair<T,U> & p){cout << "("; print(p.first); cout << ", "; print(p.second); cout << ")"; }
template<class T> void print(vector<T> & v) {
cout << "{";
if (sz(v)) print(v[0]);
for (int i = 1; i < sz(v); i++) cout << ", ", print(v[i]);
cout << "}\n";
}
string S;
int solve(int m){
vector<int> sm(sz(S));
int cnt = 0;
for(int i=0; i<sz(S); i++){
if(S[i] == '1') cnt++;
else cnt--;
sm[i] = cnt;
}
for(int i=sz(S)-2; i>=0; i--)
sm[i] = max(sm[i], sm[i+1]);
int add = 0;
int minim = 0;
cnt = 0;
for(int i=0; i<sz(S); i++){
if(S[i] != '?')
cnt += (S[i]-'0')*2-1;
else{
if(sm[i]+add+2 <= m)
cnt++, add += 2;
else
cnt--;
}
minim = min(minim, cnt);
}
return m-minim;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> S;
int z = 0;
int cnt = 0;
for(int i=0; i<sz(S); i++){
if(S[i] == '1') cnt++;
else cnt--;
z = max(z, cnt);
}
cout << min(solve(z), solve(z+1)) << endl;
}
|
//E
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[1000010];
int sum[1000010];
int mxn[1000010];
int mx,mi,ans,n;
void check(int mx){
mi=0;
int cnt=0;
for(int i=1;i<=n;i++){
if(s[i]=='0') cnt--;
if(s[i]=='1') cnt++;
if(s[i]=='?'){
if(mxn[i]-sum[i-1]+cnt+2<=mx) cnt++;
else cnt--;
}
mi=min(mi,cnt);
}
}
int main()
{
scanf("%s",s+1);
n=strlen(s+1);
for(int i=1;i<=n;i++){
if(s[i]=='0'||s[i]=='?') sum[i]=sum[i-1]-1;
if(s[i]=='1') sum[i]=sum[i-1]+1;
mx=max(mx,sum[i]);
}
mxn[n]=sum[n];
for(int i=n-1;i>=1;i--)
mxn[i]=max(sum[i],mxn[i+1]);
check(mx);
//cout<<mx<<mi<<endl;
ans=mx-mi;
mx++;
check(mx);
//cout<<mx<<mi<<endl;
ans=min(ans,mx-mi);
printf("%d",ans);
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1000005,p=998244353,inf=0x3f3f3f3f;
int read(){
int f=1,g=0;
char ch=getchar();
for (;!isdigit(ch);ch=getchar()) if (ch=='-') f=-1;
for (;isdigit(ch);ch=getchar()) g=g*10+ch-'0';
return f*g;
}
int n,g[N],ans;
char ch[N];
int work(int R){
int ta=0,tb=0,cur=0;
for (int i=1;i<=n;i++){
if (ch[i]!='?') cur+=(ch[i]=='1') ? 1 : -1;
else cur+=(cur+1+g[i+1]<=R) ? 1 : -1;
ta=max(ta,cur);tb=min(tb,cur);
}
return ta-tb;
}
int main(){
// freopen("a.txt","r",stdin);
scanf("%s",ch+1);
n=strlen(ch+1);
for (int i=n;i;i--) g[i]=max(0,g[i+1]+((ch[i]=='1') ? 1 : -1));
ans=min(work(g[1]),work(g[1]+1));
printf("%d\n",ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 1000000007;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
string S;
cin >> S;
int N = S.size();
vector<int> X(N + 1, 0);
vector<int> pref_mn(N + 1, 0);
vector<int> suff_mn(N + 1, 0);
vector<int> pref_mx(N + 1, 0);
vector<int> suff_mx(N + 1, 0);
for (int i = 0; i < N; i++) {
X[i + 1] = X[i] + (S[i] == '0' ? -1 : 1);
pref_mn[i + 1] = min(X[i + 1], pref_mn[i]);
pref_mx[i + 1] = max(X[i + 1], pref_mx[i]);
//cerr << X[i] << endl;
}
suff_mn[N] = X[N];
suff_mx[N] = X[N];
for (int i = N - 1; i >= 0; i--) {
suff_mn[i] = min(suff_mn[i + 1], X[i]);
suff_mx[i] = max(suff_mx[i + 1], X[i]);
}
vector<int> L(N + 1, -1);
for (int i = N; i >= 0; i--) {
if (X[i] >= 0) {
L[X[i]] = i;
}
}
int res = pref_mx.back() - pref_mn.back();
for (int j = 0; j < 2; j++) {
int cur = N;
int mx = pref_mx.back();
//cerr << "mx = " << mx << endl;
int INF = (int)1 << 60;
int smx = -INF;
int smn = INF;
while (mx >= 0) {
int i = mx;
if (i % 2 != j) {
i--;
}
if (i >= 0 && L[i] != -1) {
//cerr << cur << " " << L[i] - 1 << endl;
int ncur = min(cur - 1, L[i] - 1);
int tmx = -INF;
int tmn = INF;
while (cur > ncur || (cur >= 0 && S[cur] != '?')) {
if (cur >= 0) {
tmx = max(tmx, X[cur] - 2);
tmn = min(tmn, X[cur] - 2);
}
cur--;
}
smx = max(smx - 2, tmx);
smn = min(smn - 2, tmn);
if (cur >= 0) {
int mn = min(pref_mn[cur], smn);
mx = max(pref_mx[cur], smx);
//cerr << mx << " " << mn << endl;
res = min(res, mx - mn);
} else {
break;
}
}else{
break;
}
}
}
cout << res << endl;
} |
#include <stdio.h>
char S[1001001];
int main()
{
scanf ("%s", S);
int l = 0, r = 0;
while (S[r]) r++;
while (l + 1 < r){
int m = (l + r) / 2;
int p = 0, q = m, d = 1; bool g = 1;
for (int i = 0; S[i]; i++){
if (S[i] == '0') p++, q++;
if (S[i] == '1') p--, q--;
if (S[i] == '?') p--, q++;
if (p < 0){
p = 0;
if (d == 2 && p % 2 != q % 2) p++;
}
if (q > m){
q = m;
if (d == 2 && p % 2 != q % 2) q--;
}
if (p > q){
g = 0; break;
}
if (p == q) d = 2;
}
if (g) r = m;
else l = m;
}
printf ("%d\n", r);
return 0;
} |
#include "iostream"
#include "algorithm"
#include "cstring"
#include "cstdio"
#include "cmath"
#include "vector"
#include "map"
#include "set"
#include "queue"
using namespace std;
#define MAXN 1000006
//#define int long long
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define all(x) (x).begin() , (x).end()
#define mem( a ) memset( a , 0 , sizeof a )
typedef long long ll;
int n , m;
char ch[MAXN];
int A[MAXN] , a[MAXN] , B[MAXN] , mn[MAXN] , mmn = 0x3f3f3f3f;
bool us[MAXN];
void solve() {
scanf("%s",ch + 1);
n = strlen( ch + 1 );
int mx = 0 , ps;
rep( i , 1 , n ) {
if( ch[i] == '?' || ch[i] == '0' ) A[i] = A[i - 1] - 1;
else A[i] = A[i - 1] + 1;
a[i] = A[i];
mx = max( mx , A[i] );
}
rep( i , 0 , n ) if( A[i] == mx ) ps = i;
B[n] = A[n];
per( i , n - 1 , 1 ) B[i] = max( A[i] , B[i + 1] );
int t = 0;
rep( i , ps , n ) {
if( ch[i] == '?' && B[i] + t <= mx - 2 ) t += 2 , us[i] = 1;
A[i] += t;
}
mmn = 0;
rep( i , 1 , n ) mmn = min( mmn , A[i] );
int as = mx - mmn , chp = 0;
mem( us );
rep( i , 1 , n ) A[i] = a[i];
t = 0;
rep( i , ps , n ) {
if( ch[i] == '?' && B[i] + t <= mx - 1 ) t += 2 , us[i] = 1;
A[i] += t;
}
mmn = 0;
rep( i , 1 , n ) mmn = min( mmn , A[i] );
int as1 = mx + 1 - mmn , chp1 = 0;
printf("%d\n",min( as1 , as ));
}
signed main() {
// int T;cin >> T;while( T-- ) solve();
solve();
} |
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b);
#define chmin(a, b) a = min(a, b);
using namespace std;
using ll = long long;
using P = pair<int,int>;
int main() {
string s;
cin >> s;
vector<int> d;
d.push_back(0);
int now = 0;
rep(i, s.size()) {
now += (s[i] == '1' ? -1 : 1);
d.push_back(now);
}
int mx = 0, mn = 0;
for(int di: d) {
chmax(mx, di);
chmin(mn, di);
}
int mxind;
rep(i, d.size()) {
if (d[i] == mx) {
mxind = i;
break;
}
} // smallest index
int mnind;
rep(i, d.size()) {
if (d[i] == mn) {
mnind = i;
}
} // largest index
if (mxind < mnind) {
cout << mx - mn << endl;
return 0;
}
int mx2 = mn;
rep(i, mnind) chmax(mx2, d[i]);
int ans = 1001001001;
rep(_, 2) {
priority_queue<P> q;
for(int i = mnind + 1; i < d.size(); i++) {
q.emplace(-d[i], i);
}
int cnt = 0;
int tmx = mn;
for(int i = mnind; i < mxind; i++) {
while(q.top().second <= i) q.pop();
if (s[i] == '?' && -q.top().first - (cnt + 1) * 2 >= mn) {
cnt += 1;
}
chmax(tmx, d[i+1] - cnt * 2);
}
chmin(ans, max(tmx, mx2) - mn);
mn--;
}
cout << ans << endl;
}
|
#include <iostream>
#include <cstdio>
#include <cstring>
char s[1010000]; int n;
bool check(int len)
{
int l = -len, r = 0;
int type = 1;
for(int i = n; i >= 1; i--)
{
if(s[i] == '0')
{
if(r + len == 0) return 0;
else
{
r--;
if(l + len == 0) l += type; l--;
}
}
if(s[i] == '1')
{
if(l == 0) return 0;
else
{
l++;
if(r == 0) r -= type; r++;
}
}
if(s[i] == '?')
{
if(l == r)
{
if(l == 0) l--, r--;
else if(l + len == 0) l++, r++;
else
{
type = 2;
l--; r++;
}
}
else
{
if(l + len == 0) l += type; l--;
if(r == 0) r -= type; r++;
}
}
}
return 1;
}
int main()
{
scanf("%s", s + 1);
n = strlen(s + 1);
int l = 1, r = n + 1;
while(l < r)
{
int mid = l + r >> 1;
if(check(mid)) r = mid;
else l = mid + 1;
}
printf("%d\n", r);
} |
#include <iostream>
#include <limits>
using namespace std;
int main() {
string s;
cin >> s;
int maxPrefix = 0, prefix[s.size() + 1];
prefix[0] = 0;
for (int i = 1; i <= s.size(); ++i) {
if (s[i - 1] == '1') {
prefix[i] = prefix[i - 1] + 1;
} else {
prefix[i] = prefix[i - 1] - 1;
}
maxPrefix = max(maxPrefix, prefix[i]);
}
auto solve = [&](int maxPrefix) {
int capacity[s.size()];
for (int i = s.size(), j = numeric_limits<int>::max(); i > 0; --i) {
j = min(j, maxPrefix - prefix[i]);
capacity[i - 1] = j / 2;
}
int sum = 0, result = 0, count = 0;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '1') {
++sum;
} else if (s[i] == '0') {
--sum;
} else if (capacity[i] > count) {
++sum;
++count;
} else {
--sum;
}
result = min(result, sum);
}
return result;
};
cout << min(maxPrefix - solve(maxPrefix), maxPrefix + 1 - solve(maxPrefix + 1));
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
int n, suf[maxn];
char s[maxn];
int main() {
scanf("%s", s + 1), n = strlen(s + 1);
int mx = 0;
for (int i = 1, cur = 0; i <= n; i++) {
if (s[i] ^ '1') cur++;
else mx = min(mx, --cur);
}
auto chk = [&](int x) {
for (int i = n; i; i--) {
suf[i] = min(0, suf[i + 1] + (s[i] ^ '1' ? 1 : -1));
}
int y = 0;
for (int i = 1, cur = 0; i <= n; i++) {
if (s[i] == '0') cur++;
else if (s[i] == '1') cur--;
else if (cur - 1 + suf[i + 1] >= x) cur--;
else cur++;
y = max(y, cur);
}
return y - x;
};
printf("%d\n", min(chk(mx), chk(mx - 1)));
return 0;
} |
#include<bits/stdc++.h>
#define ll long long
#define lld long double
using namespace std;
template<typename tn> void read(tn &a){
tn x=0,f=1; char c=' ';
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
a=x*f;
}
const int N = 1001000;
char c[N];
int n,s[N],mx[N];
int solve(int lim){
for(int i=1;i<=n;i++){
s[i]=s[i-1]+(c[i]=='1'?1:-1);
if(s[i]>lim) return -1;
}
mx[n]=s[n];
for(int i=n-1;i;i--)
mx[i]=max(mx[i+1],s[i]);
int tag=0,mn=0;
for(int i=1;i<=n;i++){
if(c[i]!='?'){mn=min(mn,tag+s[i]);continue;}
if(mx[i]+tag+2<=lim) tag+=2;
mn=min(mn,tag+s[i]);
}
return lim-mn;
}
int main(){
ios::sync_with_stdio(0);
cin>>c+1;
n=strlen(c+1);
int P=0,now=0;
for(int i=1;i<=n;i++)
now+=(c[i]=='1'?1:-1),P=max(P,now);
int ans=1e9;
for(int i=P;i<=P+1;i++)
ans=min(ans,solve(i));
cout<<ans<<'\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)
mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template <typename T>
inline void chkmax(T &x, T y) { if (x < y) x = y; }
template <typename T>
inline void chkmin(T &x, T y) { if (x > y) x = y; }
inline int read() {
#define nc getchar()
int x = 0;
char c = nc;
while (c < 48) c = nc;
while (c > 47) x = x * 10 + (c ^ 48), c = nc;
return x;
#undef nc
}
typedef long long ll;
const int maxn = 1e6 + 10;
char str[maxn];
int n, A[maxn], B[maxn], tmp[maxn];
void solve() {
scanf("%s", str + 1);
n = strlen(str + 1);
int mx = 0, pos = 0;
rep(i, 1, n) {
A[i] = A[i - 1] + (str[i] == '1' ? 1 : -1);
if (mx <= A[i]) mx = A[i], pos = i;
}
memcpy(tmp, A, sizeof A);
B[n] = A[n];
per(i, n - 1, 1) B[i] = max(B[i + 1], A[i]);
int s = 0;
rep(i, pos, n) {
if (str[i] == '?' && B[i] + s <= mx - 2) s += 2;
A[i] += s;
}
int mn = 0;
rep(i, 1, n) chkmin(mn, A[i]);
int res1 = mx - mn;
memcpy(A, tmp, sizeof A);
s = 0;
rep(i, pos, n) {
if (str[i] == '?' && B[i] + s <= mx - 1) s += 2;
A[i] += s;
}
mn = 0;
rep(i, 1, n) chkmin(mn, A[i]);
int res2 = mx - mn + 1;
cout << min(res1, res2) << endl;
}
int main() {
// int T = read();
// while (T--) solve();
solve();
return 0;
} |
#include <bits/stdc++.h>
#define pb push_back
#define pll pair <ll, ll>
#define mp make_pair
#define pyshnapyshnakaa ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define x first
#define y second
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define plll pair <pair <ll, ll>, ll>
#define pllll pair <pair <ll, ll>, pair <ll, ll> >
#define psl pair <string, ll>
#define all(a) a.begin(), a.end()
#define vvl vector <vector <ll> >
typedef long long ll;
typedef long double ld;
using namespace std;
const ll maxn = 1e6;
ll n, m, k, t;
string s;
vector <ll> S;
ll getf(ll mx) {
// cout << "MX " << mx << endl;
ll mn = 0;
ll q;
ll d = 0;
ll cur = 0;
for (q = 0; q < s.length(); q++) {
// cout << q << " " << S[q] << endl;
if (s[q] == '0') {
// cout << "cur " << cur << endl << endl;
cur--;
mn = min(mn, cur);
continue;
}
if (s[q] == '1') {
cur++;
// cout << "cur " << cur << endl << endl;
mn = min(mn, cur);
continue;
}
if (S[q] + d + 2 <= mx) {
// cout << q << " UP" << endl;
d += 2;
cur++;
}
else {
cur--;
}
// cout << "cur " << cur << endl << endl;
mn = min(mn, cur);
}
// cout << mx - mn << " GETF" << endl << endl;
return mx - mn;
}
int main() {
pyshnapyshnakaa;
ll q, w, e, a, b, c;
cin >> s;
n = s.length();
S.resize(n);
ll z = 0;
ll cur = 0;
for (q = 0; q < s.length(); q++) {
if (s[q] == '1') {
cur++;
}
else {
cur--;
}
S[q] = cur;
z = max(z, cur);
}
// cout << "Z " << z << endl;
for (q = n - 2; q >= 0; q--) {
S[q] = max(S[q], S[q + 1]);
}
ll ans = min(getf(z), getf(z + 1));
cout << ans;
return 0;
}
|
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
using namespace std;
const int N=1000005;
int n;char s[N];
bool chk(int mid){
rep(_,0,1){
int k1=_,k2=mid;
if((k2&1)!=(k1&1))--k2;
rep(i,1,n){
if(s[i]=='0')--k1,--k2;else
if(s[i]=='1')++k1,++k2;else
--k1,++k2;
if(k1<0)k1+=2;
if(k2>mid)k2-=2;
if(k1>k2)goto gg;
}
return 1;
gg:;
}
return 0;
}
int main(){
scanf("%s",s+1),n=strlen(s+1);
if(n==1){puts("1");return 0;}
int l=0,r=1e6,ans=1e6;
while(l<=r){
int mid=(l+r)>>1;
if(chk(mid))ans=mid,r=mid-1;
else l=mid+1;
}
printf("%d\n",ans);
return 0;
} |
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int main(void){
string S;
cin >> S;
int Zbuf=0;
int Z=0;
multiset<int, greater<int>> csum;
for(int i=0; i<S.size(); i++){
if(S[i] == '1'){
Zbuf++;
}else Zbuf--;
Z=max(Z, Zbuf);
csum.insert(Zbuf);
}
Zbuf = 0;
int Zbuf0 = 0;
int Zbuf1 = 0;
int fZ0 = 0;
int fZ1 = 0;
int Zbuf0diff = 0;
int Zbuf1diff = 0;
for(int i=0; i<S.size(); i++){
switch(S[i]){
case '0':
Zbuf--;
Zbuf0--;
Zbuf1--;
break;
case '1':
Zbuf++;
Zbuf0++;
Zbuf1++;
break;
default:
Zbuf--;
int MaxRight = *(csum.begin());
if(MaxRight+Zbuf0diff+2 <= Z){
Zbuf0++;
Zbuf0diff += 2;
}else{
Zbuf0--;
}
if(MaxRight+Zbuf1diff+2 <= Z+1){
Zbuf1++;
Zbuf1diff += 2;
}else{
Zbuf1--;
}
}
fZ0=min(fZ0, Zbuf0);
fZ1=min(fZ1, Zbuf1);
csum.erase(csum.find(Zbuf));
}
cout << min(Z-fZ0, Z+1-fZ1) << endl;
return 0;
} |
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
string S;
cin>>S;
int N=S.size();
int dep=0;
vector<int> d(N+1,0);
for(int i=0;i<N;i++){
if(S[i]=='0'||S[i]=='?') dep--;
else dep++;
d[i+1]=dep;
}
for(int i=N-1;i>=0;i--) chmax(d[i],d[i+1]);
int Z=d[0];
int mxs=Z;
int ans=N;
rep(k,2){
int mis=0;
int dep=0;
int pls=0;
for(int i=0;i<N;i++){
if(S[i]=='0') dep--;
else if(S[i]=='1') dep++;
else{
if(d[i+1]+2*pls+2<=mxs){
dep++;
pls++;
}else{
dep--;
}
}
chmin(mis,dep);
}
chmin(ans,mxs-mis);
mxs++;
}
cout<<ans<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 6;
char s[N], t[N];
int f[N], rig[N];
int n;
int calc(int maxi) {
int add = 0;
for (int i = 0; i < n; ++i) {
if (s[i] != '?') continue;
if (rig[i+1] + (add + 1) * 2 <= maxi) {
++add;
s[i] = '1';
}
else {
s[i] = '0';
}
}
int now = 0, low = 0, hig = 0;
// printf("%s\n", s);
for (int i = 0; i < n; ++i) {
now += (s[i] == '1' ? 1 : -1);
low = min(low, now);
hig = max(hig, now);
}
return abs(hig - low);
}
int main() {
scanf("%s", s);
n = strlen(s);
f[0] = 0;
int maxi = 0;
for (int i = 0; i < n; ++i) {
f[i+1] = f[i] + (s[i] == '1') * 2 - 1;
maxi = max(maxi, f[i+1]);
}
rig[n] = f[n];
for (int i = n-1; i >= 0; --i) {
rig[i] = max(rig[i+1], f[i]);
}
for (int i = 0; i < n; ++i)
t[i] = s[i];
int ans = calc(maxi);
for (int i = 0; i < n; ++i)
s[i] = t[i];
ans = min(ans, calc(maxi+1));
for (int i = 0; i < n; ++i)
s[i] = t[i];
ans = min(ans, calc(maxi+2));
printf("%d\n", ans);
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define db double
#define pint pair<int,int>
#define mk(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define eb(x,y) emplace_back(x,y)
#define fi first
#define se second
#define Rep(x,y,z) for(int x=y;x<=z;x++)
#define Red(x,y,z) for(int x=y;x>=z;x--)
using namespace std;
const int MAXN=1e6+5;
char buf[1<<12],*pp1=buf,*pp2=buf,nc;int ny;
//inline char gc() {return pp1==pp2&&(pp2=(pp1=buf)+fread(buf,1,1<<12,stdin),pp1==pp2)?EOF:*pp1++;}
inline char gc(){return getchar();}
inline int read(){
int x=0;for(ny=1;nc=gc(),(nc<48||nc>57)&&nc!=EOF;)if(nc==45)ny=-1;if(nc<0)return nc;
for(x=nc-48;nc=gc(),47<nc&&nc<58&&nc!=EOF;x=(x<<3)+(x<<1)+(nc^48));return x*ny;
}
char s[MAXN];int n,a[MAXN],Min[MAXN];
inline int Solve(int lim){
static int b[MAXN];int cnt=0;
Rep(i,1,n){
if(s[i]=='?'&&Min[i]-2*cnt-2>=lim)cnt++;
b[i]=a[i]-2*cnt;
}int Max=0;Rep(i,1,n)Max=max(Max,b[i]);
return Max-lim;
}
int main(){
// freopen("std.in","r",stdin);
// freopen("std.out","w",stdout);
scanf("%s",s+1),n=strlen(s+1);
Rep(i,1,n)if(s[i]=='0')a[i]=a[i-1]-1;else a[i]=a[i-1]+1;
Min[n+1]=n+5;Red(i,n,0)Min[i]=min(Min[i+1],a[i]);
cout<<min(Solve(Min[0]),Solve(Min[0]-1));
return 0;
}
|
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1000005;
const int INF=1061109567;
int n;
char s[N];
int a[N];
int sum[N];
int suf[N];
int calc(int Min)
{
static int b[N];
int add=0;
for(int i=1;i<=n;i++)
{
if(s[i]=='?'&&suf[i]-add-2>=Min) add+=2;
b[i]=sum[i]-add;
}
int Max=max(*max_element(b+1,b+n+1),0);
return Max-Min;
}
int main()
{
scanf("%s",s+1);
n=strlen(s+1);
for(int i=1;i<=n;i++)
if(s[i]=='1'||s[i]=='?') a[i]=1;
else if(s[i]=='0') a[i]=-1;
for(int i=1;i<=n;i++)
sum[i]=sum[i-1]+a[i];
suf[n+1]=INF;
for(int i=n;i>=1;i--)
suf[i]=min(suf[i+1],sum[i]);
int m=min(*min_element(sum+1,sum+n+1),0);
printf("%d",min(calc(m),calc(m-1)));
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1001001;
char s[N];
int n;
int main(){
ios::sync_with_stdio(false);
cin>>s;
n=strlen(s);
int l=0,r=0;
int l0=0,r0=0;
int l1=l-2;
int r1=r+2;
for(int i=0;i<n;i++){
char c=s[i];
if(c=='1'){
l0++;
r0++;
if(l1!=l-2)l1++;
if(r1!=r+2)r1++;
}
else if(c=='0'){
l0--;
r0--;
if(l1!=l-2)l1--;
if(r1!=r+2)r1--;
}
else{
l0--;
r0++;
if(l1!=l-2)l1--;
if(r1!=r+2)r1++;
}
int d=(r0-l0)%2==0?2:1;
if(l0<l){
l1=l0;
l0+=d;
}
if(r0>r){
r1=r0;
r0-=d;
}
if(l0>r0){
if(l1!=l-2&&r1!=r+2){
l0=l1;
r0=r1;
if(l1<l){
l--;
r0--;
}
else{
r++;
l0++;
}
l1=l-2;
r1=r+2;
}
else if(l1!=l-2){
l--;
l0=r0=l1;
l1=l-2;
}
else{
r++;
l0=r0=r1;
r1=r+2;
}
}
// cerr<<l<<" "<<r<<" "<<l0<<" "<<r0<<" "<<l1<<" "<<r1<<endl;
}
int ans=r-l;
cout<<ans<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e6 + 10;
int n, m;
int sum[maxN + 1], mx[maxN + 1];
char s[maxN + 1];
inline int solve(int rule)
{
int res = 0, mn = 0;
for(int i = 1; i <= n; i++)
{
if(s[i] == '?' && mx[i] + res + 2 <= rule) res += 2;
mn = min(mn, sum[i] + res);
}
return mn;
}
int main()
{
scanf("%s", s + 1);
n = strlen(s + 1);
for(int i = 1; i <= n; i++)
if(s[i] == '1') sum[i] = sum[i - 1] + 1;
else sum[i] = sum[i - 1] - 1;
mx[n] = sum[n];
for(int i = n - 1; i >= 1; i--) mx[i] = max(mx[i + 1], sum[i]);
m = max(0, mx[1]);
printf("%d\n", min(m - solve(m), m + 1 - solve(m + 1)));
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
char s[N];
int sum[N],mx[N],n;
int f(int M){
for(int i=1,cnt=0;i<=n;++i){
if(s[i]=='?'){
if((cnt+1)*2+mx[i]<=M) ++cnt,sum[i]=1;
else sum[i]=-1;
}
else sum[i]=(s[i]=='0'?-1:1);
sum[i]+=sum[i-1];
}
return *min_element(sum,sum+n+1);
}
int main(){
cin>>(s+1);n=strlen(s+1);
for(int i=1;i<=n;++i){
if(s[i]=='?') sum[i]=-1;
if(s[i]=='1') sum[i]=1;
if(s[i]=='0') sum[i]=-1;
sum[i]+=sum[i-1];
}
for(int i=n;i>=0;--i)
if(i+1<=n)mx[i]=max(sum[i],mx[i+1]);
else mx[i]=sum[i];
//for(int i=0;i<=n;++i)cout<<mx[i]<<" ";cout<<endl;
int Z=mx[0];
cout<<min(Z-f(Z),Z+1-f(Z+1));
return 0;
} |
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MAXN = 1000010;
int suffix_one[MAXN];
int suffix_zero[MAXN];
int main() {
string s;
cin >> s;
int suffix_0 = 0;
int suffix_1 = 0;
for (int i = s.size(); i > 0; i--) {
suffix_one[i] = suffix_1;
suffix_zero[i] = suffix_0;
if (s[i - 1] == '1') {
suffix_1++;
suffix_0--;
}
else if (s[i - 1] == '0') {
suffix_1--;
suffix_0++;
}
else {
suffix_1--;
suffix_0--;
}
suffix_1 = max(suffix_1, 0);
suffix_0 = max(suffix_0, 0);
}
int left = 0, right = s.size();
while (left < right) {
bool flag = true;
int mid = (left + right) / 2;
int allowed_1 = mid;
int allowed_0 = mid;
for (int i = 0; flag && i < s.size(); i++) {
if (s[i] == '1') {
if (allowed_1 <= 0) {
flag = false;
}
allowed_1--;
allowed_0++;
}
else if (s[i] == '0') {
if (allowed_0 <= 0) {
flag = false;
}
allowed_1++;
allowed_0--;
}
else {
int capacity_1 = allowed_1 - suffix_one[i + 1];
int capacity_0 = allowed_0 - suffix_zero[i + 1];
bool favor_1 = capacity_1 > capacity_0;
if (capacity_1 == capacity_0 && allowed_1 > allowed_0) {
favor_1 = true;
}
if (allowed_1 > 0 && allowed_1 - 1 >= suffix_one[i + 1] && favor_1) {
allowed_1--;
allowed_0++;
}
else if (allowed_0 > 0 && allowed_0 - 1 >= suffix_zero[i + 1]) {
allowed_1++;
allowed_0--;
}
else {
flag = false;
break;
}
}
allowed_1 = min(allowed_1, mid);
allowed_0 = min(allowed_0, mid);
}
if (flag) {
right = mid;
}
else {
left = mid + 1;
}
}
cout << left << endl;
} |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n;
char s[N];
int sum[N], pmx[N], pmi[N];
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
sum[0] = 0;
pmx[0] = 0;
pmi[0] = 0;
for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + 1 - 2 * (s[i] == '0');
pmx[i] = max(pmx[i - 1], sum[i]);
pmi[i] = min(pmi[i - 1], sum[i]);
}
int suf_mi = sum[n], suf_mx = sum[n];
int ans = pmx[n] - pmi[n];
for (int i = n; i; i--) {
suf_mi = min(suf_mi, sum[i]);
suf_mx = max(suf_mx, sum[i]);
if (s[i] == '?') {
if (pmx[i - 1] < suf_mx) {
ans = min(ans, max(pmx[i - 1], suf_mx - 2) - min(pmi[i - 1], suf_mi - 2));
}
if (pmx[i - 1] + 1 < suf_mx) {
suf_mx -= 2;
suf_mi -= 2;
ans = min(ans, max(pmx[i - 1], suf_mx) - min(pmi[i - 1], suf_mi));
}
}
}
suf_mi = sum[n], suf_mx = sum[n];
bool found = false;
for (int i = n; i; i--) {
suf_mi = min(suf_mi, sum[i]);
suf_mx = max(suf_mx, sum[i]);
if (s[i] == '?') {
if (pmx[i - 1] + 1 == suf_mx) {
ans = min(ans, max(pmx[i - 1], suf_mx - 2) - min(pmi[i - 1], suf_mi - 2));
if (!found) {
suf_mi -= 2;
suf_mx -= 2;
found = true;
}
} else if (pmx[i - 1] + 1 < suf_mx) {
suf_mx -= 2;
suf_mi -= 2;
ans = min(ans, max(pmx[i - 1], suf_mx) - min(pmi[i - 1], suf_mi));
}
}
}
printf("%d\n", ans);
return 0;
}
|
//Awwawa! Dis cold yis ratten buy tEMMIE!
#include <bits/stdc++.h>
#define ll long long
#define maxn 1000005 /*rem*/
#define mod 998244353
#define db double
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define fi first
#define se second
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,true:false;}
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,true:false;}
using namespace std;
ll ksm(ll a, ll b) {
if (!b) return 1;
ll ns = ksm(a, b >> 1);
ns = ns * ns % mod;
if (b & 1) ns = ns * a % mod;
return ns;
}
char s[maxn];
int n;
bool check(int x) {
pi r[2] = {mp(0, x), mp(0, x)};
for (int i = 1; i <= n; i++) {
pi cur[2] = {mp(maxn * 10, -maxn * 10), mp(maxn * 10, -maxn * 10)};
pi fr[2] = {r[0], r[1]};
for (int i = 0; i < 2; i++) {
if (fr[i].se % 2 != i) fr[i].se--;
if (fr[i].fi % 2 != i) fr[i].fi++;
}
for (int j = 0; j < 2; j++) {
int tp = j * 2 - 1;
if (s[i] != '0' + j) {
for (int s = 0; s < 2; s++) {
pi ed = mp(fr[s].fi + tp, fr[s].se + tp);
chkmax(ed.fi, 0),
chkmin(ed.se, x);
if (ed.fi > ed.se) continue;
chkmin(cur[s ^ 1].fi, ed.fi);
chkmax(cur[s ^ 1].se, ed.se);
}
}
}
// cout << i << ' ' << r[0].fi << ' ' << r[1].fi << ' ' << r[0].se << ' ' << r[1].se << endl;
r[0] = cur[0];
r[1] = cur[1];
for (int i = 0; i < 2; i++) {
if (r[i].se % 2 != i) r[i].se--;
if (r[i].fi % 2 != i) r[i].fi++;
}
}
if (r[0].fi <= r[0].se || (r[1].fi <= r[1].se)) return 1;
return 0;
}
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
int l = 0, r = n;
check(1);
// while (1);
while (l < r) {
int mid = (l + r) >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
cout << l << endl;
return 0;
}
|
#include<algorithm>
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=2e6+5;
const int inf=1e9;
char s[N];
int n,a[N],b[N];
int go(int z){
int res=0,ans=0;
for(int i=0;i<n;i++){
if(s[i]=='?'&&a[i]+res+2<=z) res+=2;
ans=min(ans,b[i]+res);
}
return z-ans;
}
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
int sum=0;
scanf("%s",s),n=strlen(s);
for(int i=0;i<n;i++){
if(s[i]=='0') ++sum;
else --sum;
a[i]=b[i]=sum;
}
for(int i=n-2;~i;i--) a[i]=max(a[i],a[i+1]);
printf("%d\n",min(go(max(0,a[0])),go(max(0,a[0])+1)));
return 0;
}
|
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
const int N = 1e6+7;
char s[N];
int len;
int sum[N],maxn[N],n;
int get(int p){
int ans = 0,tot = 0;
for(int i = 1; i <= n; i++){
if(s[i] == '?' && tot + 2 + maxn[i] <= p) tot += 2;
ans = min(sum[i] + tot,ans);
}
return ans;
}
int main(){
cin >> (s+1);
n = strlen(s+1);
for(int i = 1; i <= n; i++)
if(s[i] == '1') sum[i] = sum[i-1] + 1;
else sum[i] = sum[i-1] - 1;
maxn[n] = sum[n];
for(int i = n-1; i > 0; i--) maxn[i] = max(maxn[i+1],sum[i]);
int m = max(0,maxn[1]);
cout << min(m-get(m),m+1-get(m+1));
} |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int _w;
char str[N];
int n , l , r , mid , res , L , R;
int main( void ) {
_w = scanf("%s",str+1);
n = strlen( str + 1 );
l = 0 , r = n , res = n;
bool flag , _f;
while( l <= r ) {
mid = ( l + r ) >> 1;
flag = 0;
for( int _ = 0 ; _ < 2 ; ++_ ) {
L = _ , R = mid , _f = 1;
if( (_^R) & 1 ) --R;
if( L > R ) {
_f = 0;
} else
for( int i = 1 ; i <= n ; ++i ) {
if( str[i] == '1' ) ++L , ++R;
if( str[i] == '0' ) --L , --R;
if( str[i] == '?' ) --L , ++R;
if( L < 0 ) L += 2;
if( R > mid ) R -= 2;
if( L > R ) {
_f = 0;
break;
}
}
flag |= _f;
}
if( flag ) res = mid , r = mid - 1;
else l = mid + 1;
}
cout << res;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll,ll> pll;
typedef vector<bool> vb;
const ll oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define FOR(i,a,b) for (ll i = (a); i < (b); i++)
#define FORD(i,a,b) for (ll i = (b)-1; i >= (a); i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define TR(X) ({ if(1) cerr << "TR: " << (#X) << " = " << (X) << endl; })
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s; cin >> s;
ll n = sz(s);
ll kmin = 0, kmax = n+1;
while (kmax-kmin > 1) {
ll k = (kmin+kmax)/2;
bool ok2 = false;
FOR(m,0,2) {
bool ok = true;
ll lo = m, hi = k;
while (hi%2 != m) hi--;
if (lo > hi) ok = false;
for (char c: s) {
if (c == '0') lo++;
else if (lo > 0) lo--;
else lo++;
if (c == '1') hi--;
else if (hi < k) hi++;
else hi--;
if (lo > hi) ok = false;
}
if (ok) ok2 = true;
}
if (ok2) kmax = k; else kmin = k;
}
cout << kmax << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define N 1000100
char s[N];
int n, z, p[N], zp[N], zm[N];
int calc(int m) {
int mn = 0;
for (int i = 1; i <= n; i ++) {
p[i] = p[i-1];
if(s[i] == '0') p[i] --;
else if(s[i] == '1') p[i] ++;
else if(p[i] + zm[i] - zp[i] < m) p[i] ++;
else p[i] --;
mn = min(mn, p[i]);
}
return m - mn;
}
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
for(int i = 1; i <= n; i ++) {
zp[i] = zp[i-1];
if(s[i] == '1') zp[i] ++;
else zp[i] --;
z = max(z, zp[i]);
}
zm[n+1] = -1e9;
for(int i = n; i >= 1; i --) zm[i] = max(zm[i+1], zp[i]);
printf("%d\n", min(calc(z), calc(z + 1)));
}
|
#include<stdio.h>
#include<string.h>
#define it register int
#define ct const int
#define il inline
using namespace std;
typedef long long ll;
#define rll register ll
#define cll const ll
const int N=1000005;
int n,T,s[N],mx[N];
char S[N];
il int Min(ct p,ct q){return p<q?p:q;}
int main(){
scanf("%s",S+1),n=strlen(S+1);it i;
for(i=1;i<=n;++i) s[i]=s[i-1]+(S[i]=='1'?1:-1);
auto cal = [&] (ct x){
for(it i=1,p=0;i<=n;++i){
if(S[i]!='?') s[i]=s[i-1]+(S[i]=='1'?1:-1);
if(S[i]=='?') mx[i]+(p+1<<1)<=x?s[i]=s[i-1]+1,++p:s[i]=s[i-1]-1;
}
it mn=s[0];
for(it i=1;i<=n;++i) mn=(s[i]<mn?s[i]:mn);
return mn;
};
for(i=n-1,mx[n]=s[n];~i;--i) mx[i]=(mx[i+1]>s[i]?mx[i+1]:s[i]);
printf("%d\n",Min(mx[0]-cal(mx[0]),mx[0]+1-cal(mx[0]+1)));
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define db long double
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
using namespace std;
bool check(string s, int x) {
int l = 0, r = x, step = 1;
for (int i = 0; i < s.size(); ++i) {
if (l == r) step = 1;
if (s[i] == '0') {
l--, r--;
if (l < 0) {
l += step;
}
if (l > r) {
return false;
}
}
else if (s[i] == '1') {
l++, r++;
if (r > x) {
r -= step;
}
if (l > r) {
return false;
}
}
else {
if (step == 2 || l != r) {
l--, r++;
if (l < 0) {
l += step;
}
if (r > x) {
r -= step;
}
if (l > r) {
return false;
}
}
else {
l--, r++;
step = 2;
if (l < 0) {
l += step;
}
if (r > x) {
r -= step;
}
if (l > r) {
return false;
}
}
}
}
return true;
}
int main(){
#ifdef LOCAL
freopen("H_input.txt", "r", stdin);
//freopen("H_output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
int L = 0, R = 1e7;
while (R-L>1) {
int M = (L+R)/2;
if (check(s, M)) R = M;
else L = M;
}
cout << R;
}
|
/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author cherrypi59
*/
#pragma GCC optimize ("O2")
#pragma GCC optimize ("tree-vectorize")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define FFOR(i, a, b) FOR(i, a, b+1)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FOR(i, 1, n+1)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define LEN(x) (int)(x).size()
#define DUMP(x) cerr<<__LINE__<<' '<<#x<<"="<<(x)<<endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair
using namespace std;
using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vector<T>>;
template <typename T> inline bool chmax(T &a, T b){if(a<b){a = b; return true;} return false;}
template <typename T> inline bool chmin(T &a, T b){if(a>b){a = b; return true;} return false;}
constexpr lint ten(int n) {return n==0 ? 1 : ten(n-1)*10;}
class B01Unbalanced {
public:
int N;
string S;
vc<int> sums, suff;
int f(int mx){
int d = 0, mn = 0;
REP(i, N){
if(S[i]=='?' and suff[i+1]+d+2<=mx) d += 2;
chmin(mn, sums[i+1]+d);
}
return mx - mn;
}
void solve(std::istream& in, std::ostream& out){
ios_base::sync_with_stdio(false);
in.tie(nullptr), out.tie(nullptr);
in>>S;
N = LEN(S);
sums.resize(N+1, 0);
suff.resize(N+1, 0);
REP(i, N){
sums[i+1] = sums[i];
if(S[i]=='0' or S[i]=='?') sums[i+1]--;
else sums[i+1]++;
}
suff[N] = sums[N];
for(int i=N-1;0<=i;--i) suff[i] = max(suff[i+1], sums[i]);
int z = *max_element(ALL(sums));
out<<min(f(z), f(z+1))<<'\n';
}
};
int main() {
B01Unbalanced solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const int N = 2e6+5;
int n;
char s[N];
int g[N];
int v[N], h[N];
int main()
{
scanf("%s",s+1);n=strlen(s+1);
int mx=0,ss=0;
for(int i=1;i<=n;i++){
if(s[i]=='1')ss++;
else ss--;
v[i]=h[i]=ss;
mx=max(mx,ss);
}
int mn=0;
for(int i=n-1;i;i--)v[i]=max(v[i],v[i+1]);
// cerr << mx << "?" << endl;
ss=0;
for(int i=1;i<=n;i++){
if(s[i]=='1')ss++;
else if(s[i]=='0')ss--;
else if(s[i]=='?'){
// cerr << ss+1+g[i+1] << " " << mx << endl;
if(ss+v[i]+2-h[i-1]<=mx)ss++;
else ss--;
}
// assert(ss<=mx);
// cerr << ss << ",";
// cerr << g[i+1] << "?" << endl;
mn=min(mn,ss);
}
int ans=mx-mn;
mn=ss=0;
++mx;
for(int i=1;i<=n;i++){
if(s[i]=='1')ss++;
else if(s[i]=='0')ss--;
else if(s[i]=='?'){
// cerr << ss+1+g[i+1] << " " << mx << endl;
if(ss+v[i]+2-h[i-1]<=mx)ss++;
else ss--;
}
// assert(ss<=mx);
// cerr << ss << ",";
// cerr << g[i+1] << "?" << endl;
mn=min(mn,ss);
}
ans=min(ans,mx-mn);
printf("%d\n",ans);
}
|
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define ll long long
#define maxn 1000005
#define inf 1e9
#define pb push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
inline int read()
{
int x=0,w=1; char c=getchar();
while(c<'0'||c>'9') {if(c=='-') w=-1; c=getchar();}
while(c<='9'&&c>='0') {x=(x<<1)+(x<<3)+c-'0'; c=getchar();}
return w==1?x:-x;
}
char s[maxn];
int len,suf[maxn],pre[maxn],mn,ans;
int main()
{
scanf("%s",s+1); len=strlen(s+1); mn=0; ans=inf;
rep(i,1,len) pre[i]=pre[i-1]+(s[i]=='0'?-1:1),mn=min(mn,pre[i]);
suf[len]=pre[len]; per(i,len-1,1) suf[i]=min(suf[i+1],pre[i]);
rep(j,1,2)
{
int mx=0,nw=0,p=0;
rep(i,1,len)
{
if(s[i]!='?') nw+=(s[i]=='0'?-1:1);
else
{
if(suf[i]-pre[i-1]+nw-2>=mn) nw--;
else nw++;
}
p=max(p,nw);
}
ans=min(ans,p-mn); mn--;
}
cout<<ans<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr);
string s;
cin >> s;
int n = (int)s.size();
vector<int> left0(n+1, 0), left1(n+1, 0);
{
int min0 = 0;
int min1 = 0;
int cur0 = 0;
int cur1 = 0;
for(int i = 0; i < n; i++){
if(s[i] == '0') cur0++;
if(s[i] != '0') cur0--;
if(s[i] == '1') cur1++;
if(s[i] != '1') cur1--;
min0 = min(min0, cur0);
min1 = min(min1, cur1);
left0[i+1] = cur0 - min0;
left1[i+1] = cur1 - min1;
}
}
int odd0 = 0, odd1 = 0, even0 = 0, even1 = 0;
for(int i = 0; i <= n; i++){
if(i & 1){
odd0 = max(odd0, left0[i]);
odd1 = max(odd1, left1[i]);
} else {
even0 = max(even0, left0[i]);
even1 = max(even1, left1[i]);
}
}
int ans = 0;
ans = max(ans, odd0);
ans = max(ans, odd1);
ans = max(ans, even0);
ans = max(ans, even1);
if(odd0 == even0) ans = max(ans, odd0+1);
if(odd1 == even1) ans = max(ans, odd1+1);
cout << ans << '\n';
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
string S;
cin >> S;
int N = int(S.size());
int low = 1, high = N;
while (low < high) {
int mid = (low + high) >> 1;
bool good = false;
for (int z = 0; z < 2; ++z) {
int l = z, r = mid;
if ((r - l) & 1) --r;
bool fail = false;
for (int i = 0; i < N; ++i) {
if (S[i] == '1') {
++l, ++r;
} else if (S[i] == '0') {
--l, --r;
} else {
--l, ++r;
}
if (l < 0) l += 2;
if (r > mid) r -= 2;
if (l > r) {
fail = true;
break;
}
}
if (!fail) good = true;
}
if (good) high = mid;
else low = mid + 1;
}
cout << low << "\n";
return 0;
}
|
#include<bits/stdc++.h>
#define llong long long
#define mkpr make_pair
#define x first
#define y second
#define iter iterator
#define riter reversed_iterator
#define y1 Lorem_ipsum_
#define tm dolor_sit_amet_
using namespace std;
inline int read()
{
int x = 0,f = 1; char ch = getchar();
for(;!isdigit(ch);ch=getchar()) {if(ch=='-') f = -1;}
for(; isdigit(ch);ch=getchar()) {x = x*10+ch-48;}
return x*f;
}
const int mxN = 1e6;
char a[mxN+3];
int b[mxN+3];
int s[mxN+3];
int mn[mxN+3];
int n;
int main()
{
scanf("%s",a+1); n = strlen(a+1);
int cur = 0,cur2 = 0,lb = 0;
for(int i=1; i<=n; i++) {b[i] = (a[i]=='1'?-1:1); s[i] = s[i-1]+b[i]; lb = min(lb,s[i]);}
mn[n] = s[n]; for(int i=n-1; i>=0; i--) {mn[i] = min(s[i],mn[i+1]);}
int ans = 0;
for(int i=1; i<=n; i++)
{
if(a[i]=='?')
{
if(mn[i]-cur*2-2>=lb) {cur++; b[i] = -1;}
}
}
for(int i=1; i<=n; i++) {s[i] = s[i-1]+b[i]; ans = max(ans,s[i]);}
ans -= lb;
for(int i=1; i<=n; i++) {b[i] = (a[i]=='1'?-1:1); s[i] = s[i-1]+b[i];}
mn[n] = s[n]; for(int i=n-1; i>=0; i--) {mn[i] = min(s[i],mn[i+1]);}
int ans2 = 0; lb--; cur = 0;
for(int i=1; i<=n; i++)
{
if(a[i]=='?')
{
if(mn[i]-cur*2-2>=lb) {cur++; b[i] = -1;}
}
}
for(int i=1; i<=n; i++) {s[i] = s[i-1]+b[i]; ans2 = max(ans2,s[i]);}
ans2 -= lb;
printf("%d\n",min(ans,ans2));
return 0;
}
// printf("ans=%d\n",ans);
// printf("b: "); for(int i=1; i<=n; i++) printf("%d ",b[i]); puts("");
// printf("s: "); for(int i=1; i<=n; i++) printf("%d ",s[i]); puts(""); |
#include <bits/stdc++.h>
#define endl '\n'
#define fi first
#define se second
#define MOD(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,n) for (int i = 0; i < n; i++)
#define forr(i,a,b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
const int MX = 1000005;
int st[4 * MX], lz[4 * MX];
void push (int i, int j, int pos) {
if (i < j) {
lz[pos * 2] += lz[pos];
lz[pos * 2 + 1] += lz[pos];
}
st[pos] += lz[pos];
lz[pos] = 0;
}
void update (int i, int j, int pos, int a, int b, int k) {
push(i, j, pos);
if (j < a || b < i) return;
if (a <= i && j <= b) {
lz[pos] = k;
push(i, j, pos);
return;
}
int m = (i + j) / 2;
update(i, m, pos * 2, a, b, k);
update(m + 1, j, pos * 2 + 1, a, b, k);
st[pos] = max(st[pos * 2], st[pos * 2 + 1]);
}
int n;
string s;
int solve (int mx) {
memset(st, 0, sizeof(st));
memset(lz, 0, sizeof(lz));
int acu = 0, mn = 0;
forn (i, n) {
char c = s[i];
if (c == '1') acu++;
else acu--;
update(1, n, 1, i + 1, i + 1, acu);
}
acu = 0;
forn (i, n) {
char c = s[i];
if (c == '1') acu++;
else if (c == '0') acu--;
else {
update(1, n, 1, i + 1, n, 2);
if (st[1] <= mx) {
acu++;
} else {
update(1, n, 1, i + 1, n, -2);
acu--;
}
}
mn = min(mn, acu);
}
return mn;
}
int main () {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> s;
n = s.size();
int acu = 0, mx = 0;
for (int c : s) {
if (c == '1') acu++;
else acu--;
mx = max(mx, acu);
}
cout << min(mx - solve(mx), mx + 1 - solve(mx + 1)) << endl;
return 0;
}
|
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair < int , int >
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout<<x<<'\n', 0;
using namespace std;
const int N=1e6+100;
ll dp[N];
ll a[N];
int32_t main(){
string s;
cin >> s;
ll n=s.size();
for (int i=1;i<=n;i++){
if (s[i-1]=='1') a[i]=1;
else a[i]=-1;
}
ll sig=0,ma=0;
for (int i=1;i<=n;i++){
sig+=a[i];
ma=max(ma,sig);
}
for (int i=n;i;i--) dp[i]=max(dp[i],dp[i+1]+a[i]);
sig=0;
ll mi=0;
//cout << ma << endl;
ll ans=1e9;
for (int j=1;j<=2;j++){
sig=0,mi=0;
for (int i=1;i<=n;i++){
if (s[i-1]=='?'){
if (sig+1+dp[i+1]>ma){
sig--;
mi=min(mi,sig);
}
else{
sig++;
mi=min(sig,mi);
}
}
else{
sig+=a[i];
mi=min(mi,sig);
}
}
ans=min(ans,ma-mi);
ma++;
}
cout <<ans << endl;
}
|
// B.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e6 + 200;
int n, pre[MAX_N], suf[MAX_N], ans = 0x7fffffff;
char str[MAX_N];
void prepare()
{
for (int i = 1; i <= n; i++)
pre[i] = pre[i - 1] + (str[i] == '0' ? -1 : 1);
suf[n + 1] = MAX_N;
for (int i = n; i >= 0; i--)
suf[i] = min(suf[i + 1], pre[i]);
}
void solve(int lbound)
{
int bias = 0, upper = 0;
for (int i = 1; i <= n; i++)
{
if (str[i] == '?' && suf[i] - 2 - bias >= lbound)
bias += 2;
pre[i] -= bias, upper = max(upper, pre[i]);
}
ans = min(ans, upper - lbound);
}
int main()
{
scanf("%s", str + 1), n = strlen(str + 1);
prepare(), solve(suf[0]);
prepare(), solve(suf[0] - 1);
printf("%d\n", ans);
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert
using namespace std;
const int INF=1e9;
//-------------------------------------------------
int main(void)
{
cin.tie(0);
ios::sync_with_stdio(false);
string S; cin>>S;
int N = S.size();
vi sum(N+1);
rep(i,N){
if (S[i]=='0') sum[i+1]=sum[i]+1;
else sum[i+1]=sum[i]-1;
}
vi maxi(N+1,-INF);
RFOR(i,N-1,0){
maxi[i] = max(maxi[i+1],sum[i+1]);
}
auto getDiff = [&](int x){
int res=0;
int c=0;
rep(i,N){
if (S[i]=='?' && maxi[i]+c+2<=x) c+=2;
chmin(res,sum[i+1]+c);
}
return x-res;
};
int minMax = max(0,maxi[0]);
int ans=min(getDiff(minMax), getDiff(minMax+1));
cout<<ans<<"\n";
return 0;
}
|
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1000005;
int n,a[maxn],b[maxn],ans=1e9;
char s[maxn];
inline void work(int x){
int cnt=0,now=n;
for (int i=0;i<=n;i++){
if(s[i]=='?'&&b[i]+cnt+2<=x) cnt+=2;
now=min(now,cnt+a[i]);
}
ans=min(ans,x-now);
}
int main(){
scanf("%s",s+1);n=strlen(s+1);
for (int i=1;i<=n;i++) b[i]=a[i]=a[i-1]+(s[i]=='1'?1:-1);
for (int i=n;i;i--) b[i-1]=max(b[i-1],b[i]);
work(b[0]),work(b[0]+1);
printf("%d\n",ans);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
char s[1001000];int n,P,suf[1001000];
int main()
{
scanf("%s",s+1);n=strlen(s+1);
for(int i=1,now=0;i<=n;i++)
{
if(s[i]=='1') now++;else now--;
P=max(P,now);
}
for(int i=n;i;i--)
{
suf[i]=suf[i+1];
if(s[i]=='1') suf[i]++;else suf[i]--;
suf[i]=max(suf[i],0);
}
int ans=n;
for(int wzp=P;wzp<=P+1;wzp++)
{
int res=0,now=0;
for(int i=1;i<=n;i++)
{
if(s[i]=='1') now++;
if(s[i]=='0') now--;
if(s[i]=='?')
{
if(suf[i+1]+now+1<=wzp) now++;
else now--;
}
res=min(res,now);
}
ans=min(ans,wzp-res);
}
cout<<ans<<endl;
} |
#include<bits/stdc++.h>
using namespace std;
#define reg register
typedef long long ll;
#define INF 0X3F3F3F3F
const int MAXS=1000000+5;
int n;
char str[MAXS];
int a[MAXS],sum[MAXS];
int sMin[MAXS];
inline int Solve(reg int Min){
reg int base=0;
static int tmp[MAXS];
for(reg int i=1;i<=n;++i)
if(str[i]=='?')
if(sMin[i]-base-2>=Min){
base+=2;
tmp[i]=-1;
}
else
tmp[i]=1;
else
tmp[i]=(str[i]=='0')?(-1):(1);
int Max=0,sum=0;
for(reg int i=1;i<=n;++i){
sum+=tmp[i];
Max=max(Max,sum);
}
return Max-Min;
}
int main(void){
scanf("%s",str+1);
n=strlen(str+1);
for(reg int i=1;i<=n;++i)
a[i]=(str[i]=='0')?(-1):(1);
for(reg int i=1;i<=n;++i)
sum[i]=sum[i-1]+a[i];
sMin[n]=sum[n];
for(reg int i=n-1;i>=0;--i)
sMin[i]=min(sMin[i+1],sum[i]);
printf("%d\n",min(Solve(sMin[0]),Solve(sMin[0]-1)));
return 0;
} |
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
int left = 0, right = s.size();
while (left < right) {
int mid = (left + right) / 2;
// test mid
int even_low = 0;
int even_high = mid;
bool even_ok = even_high >= even_low;
if (mid % 2) {
even_high--;
}
int odd_low = 1;
int odd_high = mid;
bool odd_ok = odd_high >= odd_low;
if (mid % 2 == 0) {
odd_high--;
}
for (int i = 0; i < s.size(); i++) {
if (s[i] == '1') {
even_low++;
even_high++;
odd_low++;
odd_high++;
}
else if (s[i] == '0') {
even_low--;
even_high--;
odd_low--;
odd_high--;
}
else {
even_low--;
even_high++;
odd_low--;
odd_high++;
}
swap(even_low, odd_low);
swap(even_high, odd_high);
swap(even_ok, odd_ok);
while (even_low < 0) {
even_low += 2;
}
while (even_high > mid) {
even_high -= 2;
}
while (odd_low < 0) {
odd_low += 2;
}
while (odd_high > mid) {
odd_high -= 2;
}
if (even_high < even_low) {
even_ok = false;
}
if (odd_high < odd_low) {
odd_ok = false;
}
}
if (odd_ok || even_ok) {
right = mid;
}
else {
left = mid + 1;
}
}
cout << left << endl;
} |
#include <iostream>
#include<set>
#include<vector>
#include<string>
using namespace std;
const int mod=1000000007;
int add(int a, int b)
{
int c=a+b;
if(c>=mod)c-=mod;
return c;
}
int dif(int a, int b)
{
int c=a-b;
if(c<0)c+=mod;
return c;
}
int mlt(int a, int b)
{
long long c=a*1LL*b;
return c%mod;
}
int bp(int a, int b)
{
if(b==0)return 1;
if(b%2==0)
{
int val=bp(a, b/2);
return mlt(val, val);
}
if(b%2==1)
{
int val=bp(a, b-1);
return mlt(val, a);
}
}
int inv(int a)
{
return bp(a, mod-2);
}
void outp(vector<vector<int>>&v)
{
for(int i=0; i<v.size(); i++)
{
for(int j=0; j<v[i].size(); j++)cout<<v[i][j]<<' ';
cout<<endl;
}
cout<<endl;
}
bool tst(string s, int k, int a, int b)
{
int cura=a, curb=b;
for(int j=0; j<s.length(); j++)
{
if(s[j]=='0')
{
if(curb==k)
curb--;
else curb++;
cura++;
if(cura>curb)return false;
if(cura<0)return false;
}
if(s[j]=='1')
{
curb--;
cura--;
if(cura<0)cura+=2;
if(cura>curb)return false;
}
if(s[j]=='?')
{
cura--;
if(cura<0)cura=1;
curb++;
if(curb>k)curb-=2;
if(cura>curb)return false;
}
}
return true;
}
bool check(string s, int k)
{
if(k%2==0)
{
if(tst(s, k, 0, k))return true;
if(tst(s, k, 1, k-1))return true;
return false;
}
else
{
if(tst(s, k, 0, k-1))return true;
if(tst(s, k, 1, k))return true;
return false;
}
}
int main()
{
string s;
cin>>s;
int n=s.length();
vector<int>l0(n+1), l1(n+1);
int maxval=0;
for(int i=1; i<=n; i++)
{
if(s[i-1]=='0')
{
l0[i]=l0[i-1]+1;
}
else{
l0[i]=max(l0[i-1]-1, 0);
}
if(s[i-1]=='1')
{
l1[i]=l1[i-1]+1;
}
else{
l1[i]=max(l1[i-1]-1, 0);
}
maxval=max(maxval, l0[i]);
maxval=max(maxval, l1[i]);
}
if(check(s, maxval))cout<<max(maxval,1)<<endl;
else cout<<maxval+1<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using VI = vector<int>;
#define fst first
#define snd second
const int INF = 1e9;
bool valid(const string& s, int lim) {
// cerr << "> lim = " << lim << endl;
const int olim = lim - (1 - lim%2), elim = lim - (lim%2);
int olo = 1, ohi = olim;
int elo = 0, ehi = elim;
for (char c: s) {
swap(olo, elo);
swap(ohi, ehi);
if (c == '0' || c == '?') {
olo = max(1, olo - 1);
elo = max(0, elo - 1);
} else {
olo++;
if (olo > lim) olo = INF;
elo++;
if (elo > lim) elo = INF;
}
if (c == '1' || c == '?') {
ohi = min(olim, ohi + 1);
ehi = min(elim, ehi + 1);
} else {
ohi--;
if (ohi < 0) ohi = -INF;
ehi--;
if (ehi < 0) ehi = -INF;
}
// fprintf(stderr, ">> odd: [%d, %d], even: [%d, %d]\n", olo, ohi, elo, ehi);
}
return olo <= ohi || elo <= ehi;
}
int main() {
#ifdef LOCAL_EXEC
// freopen("sample.in", "r", stdin);
// freopen("sample.out", "w", stdout);
#else
ios_base::sync_with_stdio(false); cin.tie(NULL);
#endif
string s; cin >> s;
int lo = 1, hi = s.size();
while (lo < hi) {
int mid = (lo + hi) / 2;
if (valid(s, mid)) {
hi = mid;
} else {
lo = mid + 1;
}
}
cout << lo << endl;
return 0;
}
|
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cstring>
#include <functional>
#include <iostream>
#include <istream>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <array>
#include <complex>
#define endl '\n'
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
vector<int> pre(s.length());
int x = 0, m = 0;
for (int i = 0; i < s.length(); ++i)
{
x += s[i] == '0' ? +1 : -1;
m = max(x, m);
pre[i] = x;
}
vector<int> pre_rev = pre;
for (int i = (int)s.length() - 2; i >= 0; --i)
pre_rev[i] = max(pre_rev[i], pre_rev[i + 1]);
pre_rev.push_back(-1000000000);
int res = s.length();
for (int j = 0; j < 2; ++j)
{
int z = m + j, l = 0, x = 0, k = 0;
for (int i = 0; i < s.length(); ++i)
{
if (s[i] == '?')
{
if (max(x + 1, pre_rev[i + 1] + 2 * (k + 1)) <= z)
{
x += 1;
k += 1;
}
else
{
x--;
}
}
else if (s[i] == '0')
{
x++;
}
else
{
x--;
}
l = min(l, x);
}
res = min(res, z - l);
}
cout << res << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
const int N=1000005,M=1000000007;
int n,i,j,ans,l,r,al,ar,sp;
char c[N];
void rt(int m,int l,int r)
{
for(i=strlen(c+1);i>=1;--i)
{
if(c[i]=='0')
{
--l;
--r;
if(l<0)
l+=2;
}
if(c[i]=='1')
{
++l,++r;
if(r>m)
r-=2;
}
if(c[i]=='?')
{
--l;
if(l<0)
l+=2;
++r;
if(r>m)
r-=2;
}
if(l>r||l>m||r<0)
{
al=1;
ar=0;
return;
}
}
al=l,ar=r;
}
bool check(int m)
{
int i;
int a[2][2],b[2][2];
rt(m,0,m/2*2);
a[0][0]=al,a[0][1]=ar;
rt(m,1,(m&1?m:m-1));
a[1][0]=al,a[1][1]=ar;
for(i=1;c[i];++i)
if(c[i]=='0')
c[i]='1';
else
if(c[i]=='1')
c[i]='0';
rt(m,0,m/2*2);
b[0][0]=m-ar,b[0][1]=m-al;
rt(m,1,(m&1?m:m-1));
b[1][0]=m-ar,b[1][1]=m-al;
if(m&1)
{
swap(b[0][0],b[1][0]);
swap(b[0][1],b[1][1]);
}
for(i=1;c[i];++i)
if(c[i]=='0')
c[i]='1';
else
if(c[i]=='1')
c[i]='0';
if(min(a[0][1],b[0][1])>=max(a[0][0],b[0][0])||min(a[1][1],b[1][1])>=max(a[1][0],b[1][0]))
return true;
else
return false;
}
int main()
{
scanf("%s",c+1);
int l=1,r=strlen(c+1);
while(l<r)
{
int mid=l+r>>1;
if(check(mid))
r=mid;
else
l=mid+1;
}
cout<<l;
}
|
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
template<typename T>
void fin(T const& t){ std::cout << t << std::endl; exit(0); }
int main() {
std::cin.tie(0); std::ios::sync_with_stdio(false);
std::string s; std::cin >> s;
int a0 = 0, a1 = 1, b0 = 0, b1 = -1;
int c0 = a0-b0, c1 = a0-b1, c2 = a1-b0, c3 = a1-b1;
for(auto c: s) {
if(c=='0') { --a0; --a1; --b0; --b1; }
else if(c=='1') { ++a0; ++a1; ++b0; ++b1; }
else { --a0; --a1; ++b0; ++b1; }
if(a0 < 0) a0 += 2;
if(a1 < 0) a1 += 2;
if(b0 > 0) b0 -= 2;
if(b1 > 0) b1 -= 2;
c0 = std::max(c0, a0-b0);
c1 = std::max(c1, a0-b1);
c2 = std::max(c2, a1-b0);
c3 = std::max(c3, a1-b1);
}
fin(std::min(std::min(c0, c1), std::min(c2, c3)));
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
bool p[1000009];
int a[1000009], n, mni, mxi, ul;
bool ps(int dl)
{
int l = 0, r = 0;
for (int i = 0; i <= n; i++) {
int u = (ul - a[i]) / 2, d = (dl - a[i] + 1) / 2;
if (p[i]) r++;
l = max(l, d); r = min(r, u);
if (l > r) return false;
}
return true;
}
int bs(int l, int r)
{
int md = (l + r) / 2;
if (r == l + 1 && md == l) md = r;
if (l >= r) return md;
if (ps(md)) return bs(md, r);
else return bs(l, md - 1);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
string s; cin >> s;
n = s.length();
for (int i = 0; i < n; i++) {
if (s[i] == '?') {
s[i] = '0';
p[i + 1] = true;
}
if (s[i] == '0')
a[i + 1] = a[i] - 1;
else
a[i + 1] = a[i] + 1;
}
mxi = 0, mni = 0;
for (int i = 1; i <= n; i++) {
if (a[mxi] <= a[i])
mxi = i;
if (a[mni] > a[i])
mni = i;
}
int ans = a[mxi] - a[mni];
if (mni <= mxi) {
cout << ans << '\n';
return 0;
}
ul = a[mxi];
ans = min(ans, ul - bs(a[mni], a[mxi]));
ul = a[mxi] + 1;
ans = min(ans, ul - bs(a[mni], a[mxi]));
cout << ans << '\n';
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
using vint=vector<int>;
using pint=pair<int,int>;
using vpint=vector<pint>;
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}
template<class A,class B>
ostream& operator<<(ostream& ost,const pair<A,B>&p){
ost<<"{"<<p.first<<","<<p.second<<"}";
return ost;
}
template<class T>
ostream& operator<<(ostream& ost,const vector<T>&v){
ost<<"{";
for(int i=0;i<v.size();i++){
if(i)ost<<",";
ost<<v[i];
}
ost<<"}";
return ost;
}
inline int topbit(unsigned long long x){
return x?63-__builtin_clzll(x):-1;
}
inline int popcount(unsigned long long x){
return __builtin_popcountll(x);
}
inline int parity(unsigned long long x){
return __builtin_parity(x);
}
int N;
string S;
int calc(int M){
vint A(N+1);
rep(i,N){
A[i+1]=A[i];
if(S[i]=='1')A[i+1]++;
else A[i+1]--;
}
vint suf=A;
for(int i=N-1;i>=0;i--)chmax(suf[i],suf[i+1]);
int offset=0;
int mi=0;
rep(i,N){
if(S[i]=='?'&&suf[i+1]+offset+2<=M)offset+=2;
chmin(mi,A[i+1]+offset);
}
return M-mi;
}
signed main(){
cin>>S;
N=S.size();
int cur=0;
int m=0;
rep(i,N){
if(S[i]=='1')cur++;
else cur--;
chmax(m,cur);
}
cout<<min(calc(m),calc(m+1))<<endl;
return 0;
} |
#include<bits/stdc++.h>
#define re register
#define inc(i,j,k) for(re int i=j;i<=k;i++)
#define dec(i,j,k) for(re int i=j;i>=k;i--)
using namespace std;
const int N=1e6+5;
int n,sum[N],ans,minn,maxx,cur,pos,suf[N],now;
char s[N];
void get(int x){
now=minn=maxx=0;
inc(i,1,n){
if(s[i]=='1') sum[i]=sum[i-1]+1;
if(s[i]=='0') sum[i]=sum[i-1]-1;
if(s[i]=='?'){
if(suf[i]+now+2<=x) sum[i]=sum[i-1]+1,now+=2;
else sum[i]=sum[i-1]-1;
}
minn=min(minn,sum[i]);
maxx=max(maxx,sum[i]);
}
ans=min(ans,maxx-minn);
}
int main(){
scanf("%s",s+1);
n=strlen(s+1);
minn=maxx=0;
inc(i,1,n){
if(s[i]=='1') sum[i]=sum[i-1]+1;
else sum[i]=sum[i-1]-1;
maxx=max(maxx,sum[i]);
}
suf[n+1]=-n;
dec(i,n,1) suf[i]=max(suf[i+1],sum[i]);
int z=maxx;ans=n;
get(z),get(z+1);
printf("%d",ans);
} |
#include <bits/stdc++.h>
using namespace std;
int solve (string const& s, int h) {
int n = s.size();
vector<pair<int, int>> dp(n + 1, {0, h});
for (int i = n - 1, par = -1; i >= 0; i--) {
if (par != -1) par ^= 1;
if (s[i] == '1') {
dp[i] = {dp[i + 1].first - 1, dp[i + 1].second - 1};
} else if (s[i] == '0') {
dp[i] = {dp[i + 1].first + 1, dp[i + 1].second + 1};
} else {
if (dp[i + 1].first == dp[i + 1].second && par == -1) {
par = (dp[i + 1].first + 1) & 1;
}
dp[i] = {dp[i + 1].first - 1, dp[i + 1].second + 1};
}
dp[i].first = max(dp[i].first, 0);
dp[i].second = min(dp[i].second, h);
if ((dp[i].first > h) || (dp[i].second < 0) || (par != -1 && dp[i].first == dp[i].second && (dp[i].first & 1) != par)) {
return 0;
}
}
return 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> s;
int n = s.size();
int l = 1, r = n;
while (l != r) {
int mid = (l + r) / 2;
int ok = solve(s, mid);
if (ok) {
r = mid;
} else l = mid + 1;
}
cout << l << '\n';
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 1<<20, mod = 1e9 + 7;
string s;
int n, ans = 1<<30;
int p[maxn], sm[maxn], tt[maxn];
//0 => -1 && 1 => 1
int solve() {//initialize to 1
memset(p, 0, sizeof p);//prefix sum
memset(sm, 0, sizeof sm);//suffix min of prefix sums
int g_min = 0, g_max = 0;
for(int i = 1; i <= n; i++) {
p[i] = p[i-1] + (s[i] == '0' ? -1 : 1);
sm[i] = p[i];
}
for(int i = n; i--;) sm[i] = min(sm[i], sm[i+1]);
g_min = sm[0];
for(int run = 0, i = 0; i <= n; i++) {
sm[i] += run;
p[i] += run;
if(s[i] == '?' && sm[i] - 2 >= g_min) {
sm[i] -= 2;
p[i] -= 2;
run -= 2;
}
g_max = max(g_max, p[i]);
}
return g_max - g_min;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> s;
n = s.size();
s = "." + s;
int ans = 1<<30;
for(int i = 0; i < 2; i++) {
ans = min(ans, solve());
for(auto &c : s)
if(c == '0') c = '1';
else if(c == '1') c = '0';
ans = min(ans, solve());
reverse(1 + s.begin(), s.end());
}
cout << ans;
}
|
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
const int mxn = 1000000;
int n;
string s;
int a[mxn], b[mxn];
int sol(int x){
int r1 = 0, r2 = 0;
for(int i = 0; i < n; i++){
r2 += 2 * (s[i] == '?' && b[i] + r2 < x - 1);
r1 = min(r1, a[i] + r2);
}
return x - r1;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> s;
n = s.size();
for(int i = 0; i < n; i++){
a[i] = 2 * (s[i] == '1') - 1 + (i ? a[i - 1] : 0);
}
for(int i = n - 1; ~i; i--){
b[i] = max(a[i], (i < n - 1 ? b[i + 1] : a[i]));
}
int v = max(b[0], 0);
cout << min(sol(v), sol(v + 1)) << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
const char newl = '\n';
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=int>
vector<T> read(size_t n){
vector<T> ts(n);
for(size_t i=0;i<n;i++) cin>>ts[i];
return ts;
}
//INSERT ABOVE HERE
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
string s;
cin>>s;
int n=s.size();
vector<int> sm(n+1,0);
for(int i=0;i<n;i++)
sm[i+1]=sm[i]+(s[i]=='1'?1:-1);
vector<int> mx(n+1);
for(int i=n;i>=0;i--){
mx[i]=sm[i];
if(i+1<=n) chmax(mx[i],mx[i+1]);
}
int ans=n;
for(int k=0;k<2;k++){
int lim=mx[0]+k;
int cur=0;
int num=0;
int low=0,hgh=0;
for(int i=0;i<n;i++){
if(s[i]=='1'){
cur+=1;
chmin(low,cur);
chmax(hgh,cur);
continue;
}
if(s[i]=='0'){
cur-=1;
chmin(low,cur);
chmax(hgh,cur);
continue;
}
assert(s[i]=='?');
if(mx[i+1]+(num+1)*2<=lim){
num++;
cur+=1;
}else{
cur-=1;
}
chmin(low,cur);
chmax(hgh,cur);
}
chmin(ans,hgh-low);
}
cout<<ans<<newl;
return 0;
}
|
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
int left = 0, right = s.size();
while (left < right) {
int mid = (left + right) / 2;
bool flag = false;
for (int base = 0; !flag && base <= 1; base++) {
int low = base;
int high = mid;
if ((high - low) & 1) {
high--;
}
bool ok = high >= low;
for (int i = 0; ok && i < s.size(); i++) {
if (s[i] == '1') {
low++;
high++;
}
else if (s[i] == '0') {
low--;
high--;
}
else {
low--;
high++;
}
if (low < 0) {
low += 2;
}
if (high > mid) {
high -= 2;
}
ok = high >= low;
}
flag = ok;
}
if (flag) {
right = mid;
}
else {
left = mid + 1;
}
}
cout << left << endl;
} |
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define PRIM 3
#define INF (1 << 29)
#define LINF (1LL << 60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;
string s;
bool ok(int x) {
for (int p = 0; p < 2; p++) {
bool ng = false;
int bottom = p, top = x;
if (top % 2 != p)
top--;
for (char c : s) {
if (c == '0')
bottom--, top--;
if (c == '1')
bottom++, top++;
if (c == '?')
bottom--, top++;
if (bottom < 0)
bottom += 2;
if (top > x)
top -= 2;
if (top < bottom)
ng = true;
}
if (!ng)
return true;
}
return false;
}
int main() {
cin >> s;
int bottom = 0, top = s.size();
while (top - bottom > 1) {
int mid = (top + bottom) / 2;
if (ok(mid))
top = mid;
else
bottom = mid;
}
cout << top << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()
const int N = 1e6 + 5;
string s;
int n;
int mx_suf[N];
int f(int z) {
int mn = 0, suf = 0;
for (int i = 0; i < n; i++) {
for (char c = '1'; c >= '0'; c--) {
int d = (c == '1' ? 1 : -1);
if ((c == s[i] || s[i] == '?') &&
suf + d + mx_suf[i + 1] <= z) {
suf += d;
break;
}
}
mn = min(mn, suf);
}
return z - mn;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> s;
n = s.size();
for (int i = n - 1; i >= 0; i--) {
mx_suf[i] = max(mx_suf[i + 1] + (s[i] == '1' ? 1 : -1), 0);
}
cout << min(f(mx_suf[0]), f(mx_suf[0] + 1));
return 0;
} |
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<ctime>
#include<map>
#include<bitset>
#include<set>
#include<assert.h>
#include<chrono>
#include<random>
#include<iostream>
#define LL long long
#define mp(x,y) make_pair(x,y)
#define pll pair<long long,long long>
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
#define VI vector<int>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline LL read()
{
LL f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int stack[20];
template<typename T>inline void write(T x)
{
if(x<0){putchar('-');x=-x;}
if(!x){putchar('0');return;}
int top=0;
while(x)stack[++top]=x%10,x/=10;
while(top)putchar(stack[top--]+'0');
}
template<typename T>inline void pr1(T x){write(x);putchar(' ');}
template<typename T>inline void pr2(T x){write(x);putchar('\n');}
template<typename T>inline void chkmin(T &x,T y){x=x<y?x:y;}
template<typename T>inline void chkmax(T &x,T y){x=x>y?x:y;}
const int MAXN=1000005;
char ch[MAXN];int len;
int val[MAXN],rmx[MAXN];
int calculate(int lim)
{
for(int i=1;i<=len;i++)
{
if(ch[i]=='1')val[i]=1;else val[i]=-1;
val[i]+=val[i-1];
}rmx[len+1]=-1e9;
for(int i=len;i>=1;i--)rmx[i]=val[i],chkmax(rmx[i],rmx[i+1]);
int ret=0;
for(int i=1,sum=0,S=0;i<=len;i++)
{
if(ch[i]=='1')++S;
else if(ch[i]=='0')--S;
else
{
if(rmx[i]+sum+2<=lim)sum+=2,++S;
else --S;
}chkmin(ret,S);
}return ret;
}
int main()
{
#ifdef Rose
double BeginJudgeTime=clock();
#endif
scanf("%s",ch+1);len=strlen(ch+1);
int mxp=0;
for(int i=1,S=0;i<=len;i++)
{
if(ch[i]=='1')++S;
else --S;chkmax(mxp,S);
}
int z1=calculate(mxp),z2=calculate(mxp+1);
pr2(min(mxp-z1,mxp+1-z2));
#ifdef Rose
double EndJudgeTime=clock();
cerr<<"JudgeTime is"<<" ";
cerr<<(EndJudgeTime-BeginJudgeTime)/CLOCKS_PER_SEC<<endl;
#endif
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000005;
int n;
char s[MAXN];
bool ok(int lim)
{
bool flag[2] = {1,1};
for (int par = 0;par <= 1;par++)
{
int l = par,r = ((lim & 1) == par ? lim : lim - 1);
if (l > r)
flag[par] = 0;
for (int i = 1;i <= n;i++)
{
if (s[i] == '?')
{
l--;
r++;
}
else
{
l += (s[i] == '0' ? -1 : 1);
r += (s[i] == '0' ? -1 : 1);
}
if (l < 0)
l += 2;
if (r > lim)
r -= 2;
if (l > r)
flag[par] = 0;
}
}
return flag[0] || flag[1];
}
int main()
{
scanf("%s",s + 1);
n = strlen(s + 1);
int low = 1,high = n;
while (low < high)
{
int mid = low + high >> 1;
if (ok(mid))
high = mid;
else
low = mid + 1;
}
printf("%d\n",low);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1000000;
char s[N+9];
int n;
void into(){
scanf("%s",s+1);
n=strlen(s+1);
}
int pre[N+9],suf[N+9];
void Get_suf(){
for (int i=1;i<=n;++i) pre[i]=pre[i-1]+(s[i]=='0'?-1:1);
suf[n]=pre[n];
for (int i=n-1;i>=0;--i) suf[i]=min(pre[i],suf[i+1]);
}
int Get_ans(int lim){
int res=0,now=0;
for (int i=1;i<=n;++i){
if (s[i]=='?'&&suf[i]+now>=lim+2) now-=2;
res=max(res,pre[i]+now);
}
return res-lim;
}
int ans;
void Get_ans(){
ans=min(Get_ans(suf[0]),Get_ans(suf[0]-1));
}
void work(){
Get_suf();
Get_ans();
}
void outo(){
printf("%d\n",ans);
}
int main(){
into();
work();
outo();
return 0;
} |
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) x.begin(),x.end()
#define chmin(a,b) a=min(a,b)
#define chmax(a,b) a=max(a,b)
int solve(string s){
int cs=0,mi=0,ans=0;
set<int> S;
for(int i=0;i<s.size();i++){
if(s[i]=='1')cs++;
else cs--;
chmin(mi,cs);
if(ans<cs-mi){
S.clear();
ans=cs-mi;
}
if(ans==cs-mi){
S.insert(i%2);
}
}
if(S.size()==2)ans++;
return ans;
}
int main(){
string s;
cin>>s;
if(s.size()==1){
cout<<1<<endl;
return 0;
}
int a=solve(s);
for(int i=0;i<s.size();i++){
if(s[i]=='0')s[i]='1';
else if(s[i]=='1')s[i]='0';
}
int b=solve(s);
//cout<<a<<" "<<b<<endl;
cout<<max(a,b)<<endl;
}
|
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
template<typename T>
void fin(T const& t){ std::cout << t << std::endl; exit(0); }
int main() {
std::cin.tie(0); std::ios::sync_with_stdio(false);
std::string s; std::cin >> s;
int a0 = 0, a1 = 1, b0 = 0, b1 = -1;
int c0 = a0-b0, c1 = a0-b1, c2 = a1-b0, c3 = a1-b1;
for(int i = 0; i < s.size(); i += 2) {
char c = s[i];
if(c=='0') { --a0; --a1; --b0; --b1; }
else if(c=='1') { ++a0; ++a1; ++b0; ++b1; }
else { --a0; --a1; ++b0; ++b1; }
if(a0 < 0) a0 += 2;
if(b0 > 0) b0 -= 2;
c0 = std::max(c0, a0-b0);
c1 = std::max(c1, a0-b1);
c2 = std::max(c2, a1-b0);
c3 = std::max(c3, a1-b1);
c = s[i+1];
if(c=='0') { --a0; --a1; --b0; --b1; }
else if(c=='1') { ++a0; ++a1; ++b0; ++b1; }
else { --a0; --a1; ++b0; ++b1; }
if(a1 < 0) a1 += 2;
if(b1 > 0) b1 -= 2;
c0 = std::max(c0, a0-b0);
c1 = std::max(c1, a0-b1);
c2 = std::max(c2, a1-b0);
c3 = std::max(c3, a1-b1);
}
if(s.size()%2==1) {
char c = s.back();
if(c=='0') { --a0; --a1; --b0; --b1; }
else if(c=='1') { ++a0; ++a1; ++b0; ++b1; }
else { --a0; --a1; ++b0; ++b1; }
if(a0 < 0) a0 += 2;
if(b0 > 0) b0 -= 2;
c0 = std::max(c0, a0-b0);
c1 = std::max(c1, a0-b1);
c2 = std::max(c2, a1-b0);
c3 = std::max(c3, a1-b1);
}
fin(std::min(std::min(c0, c1), std::min(c2, c3)));
return 0;
}
|
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n, M, a[1000010];
char s[1000010];
inline int Solve (int x) {
int res = 0;
for (int i = 1, t = 1, su = 0; i <= n; ++i) {
if (s[i] == '?')
if (a[i] + t * 2 <= x) ++t, ++su;
else --su;
else su += s[i] == '1' ? 1 : -1;
res = min (res, su);
}
return res;
}
int main () {
scanf ("%s", s + 1);
n = strlen (s + 1);
for (int i = 1; i <= n; ++i) {
a[i] = a[i - 1] + (s[i] == '1' ? 1 : -1);
M = max (M, a[i]);
}
for (int i = n - 1; i >= 1; --i) a[i] = max (a[i], a[i + 1]);
printf ("%d\n", min (M - Solve (M), M + 1 - Solve (M + 1)));
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll Z=0;
string s;
cin>>s;
ll rui=0;
ll a=0;
ll f=0;
ll s1=s.size();
ll b[s1+1];
ll c[s1+1];
for (ll i=0;i<s1;i++){
if(s.at(i)=='1'){
a++;}
else{
a--;}
b[i]=a;
c[i]=a;
Z=max(Z,a);}
for (ll i=s1-2;i>=0;i--){
b[i]=max(b[i],b[i+1]);}
for (ll i=0;i<s1;i++){
if(s.at(i)=='1'){
rui++;}
else if(s.at(i)=='0'){
rui--;}
else{
if(rui-c[i-1]+b[i]+2<=Z){
rui++;}
else{
rui--;}}
f=min(f,rui);}
ll Z1=Z+1;
ll f1=0;
rui=0;
for (ll i=0;i<s1;i++){
if(s.at(i)=='1'){
rui++;}
else if(s.at(i)=='0'){
rui--;}
else{
if(rui+b[i]-c[i-1]+2<=Z1){
rui++;}
else{
rui--;}}
f1=min(f1,rui);}
ll ans=min(Z-f,Z1-f1);
if(s.at(0)=='?'&&s1==1){
ans=1;}
cout<<ans<<endl;}
|
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MAXN = 1000010;
int suffix_one[MAXN];
int suffix_zero[MAXN];
int main() {
string s;
cin >> s;
int suffix_0 = 0;
int suffix_1 = 0;
for (int i = s.size(); i > 0; i--) {
suffix_one[i] = suffix_1;
suffix_zero[i] = suffix_0;
if (s[i - 1] == '1') {
suffix_1++;
suffix_0--;
}
else if (s[i - 1] == '0') {
suffix_1--;
suffix_0++;
}
else {
suffix_1--;
suffix_0--;
}
suffix_1 = max(suffix_1, 0);
suffix_0 = max(suffix_0, 0);
}
int left = 0, right = s.size();
while (left < right) {
bool flag = true;
int mid = (left + right) / 2;
int allowed_1 = mid;
int allowed_0 = mid;
for (int i = 0; flag && i < s.size(); i++) {
if (s[i] == '1') {
if (allowed_1 <= 0) {
flag = false;
}
allowed_1--;
allowed_0++;
}
else if (s[i] == '0') {
if (allowed_0 <= 0) {
flag = false;
}
allowed_1++;
allowed_0--;
}
else {
int capacity_1 = allowed_1 - suffix_one[i + 1];
int capacity_0 = allowed_0 - suffix_zero[i + 1];
if (allowed_1 > 0 && allowed_1 - 1 >= suffix_one[i + 1] && (capacity_1 > capacity_0 || capacity_1 == capacity_0 && allowed_1 > allowed_0)) {
allowed_1--;
allowed_0++;
}
else if (allowed_0 > 0 && allowed_0 - 1 >= suffix_zero[i + 1]) {
allowed_1++;
allowed_0--;
}
else {
flag = false;
break;
}
}
allowed_1 = min(allowed_1, mid);
allowed_0 = min(allowed_0, mid);
}
if (flag) {
right = mid;
}
else {
left = mid + 1;
}
}
cout << left << endl;
} |
#include <cstdio>
#include <cstring>
#include <algorithm>
const int MN = 1000005;
int N;
char S[MN];
int p[MN];
inline int Solve(int lb) {
p[N] = 0;
for (int i = N; i >= 1; --i)
p[i - 1] = std::max(p[i] + (S[i] == '1' ? 1 : -1), 0);
int maxval = 0;
for (int i = 1, now = 0; i <= N; maxval = std::max(maxval, now), ++i)
if (S[i] == '?') now += now - 1 - p[i] >= lb ? -1 : 1;
else now += S[i] == '0' ? 1 : -1;
return maxval;
}
int main() {
scanf("%s", S + 1), N = strlen(S + 1);
int minval = 0;
for (int i = 1, now = 0; i <= N; ++i)
if (S[i] == '1') minval = std::min(minval, --now);
else ++now;
printf("%d\n", std::min(Solve(minval) - minval, Solve(minval - 1) - minval + 1));
return 0;
} |
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
int left = 0, right = s.size();
while (left < right) {
int mid = (left + right) / 2;
// test mid
int even_low = 0;
int even_high = mid;
bool even_ok = even_high >= even_low;
if (mid % 2) {
even_high--;
}
int odd_low = 1;
int odd_high = mid;
bool odd_ok = odd_high >= odd_low;
if (mid % 2 == 0) {
odd_high--;
}
for (int i = 0; i < s.size(); i++) {
if (s[i] == '1') {
even_low++;
even_high++;
odd_low++;
odd_high++;
}
else if (s[i] == '0') {
even_low--;
even_high--;
odd_low--;
odd_high--;
}
else {
even_low--;
even_high++;
odd_low--;
odd_high++;
}
while (even_low < 0) {
even_low += 2;
}
while (even_high > mid) {
even_high -= 2;
}
while (odd_low < 0) {
odd_low += 2;
}
while (odd_high > mid) {
odd_high -= 2;
}
if (even_high < even_low) {
even_ok = false;
}
if (odd_high < odd_low) {
odd_ok = false;
}
}
if (odd_ok || even_ok) {
right = mid;
}
else {
left = mid + 1;
}
}
cout << left << endl;
} |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int _w;
char str[N];
int n , mn , f[N] , mx , ans , g[N];
bool mark[N];
int main( void ) {
_w = scanf("%s",str+1);
n = strlen( str + 1 ); mx = 0;
for( int i = 1 , s = 0 ; i <= n ; ++i ) {
if( str[i] == '?' ) mark[i] = 1 , str[i] = '0';
if( str[i] == '0' ) --s;
else ++s;
f[i] = s;
mx = max( mx , f[i] );
}
ans = n;
g[n] = f[n];
for( int i = n - 1 ; i ; --i )
g[i] = max( g[i + 1] , f[i] );
for( int _ = 0 ; _ < 2 ; ++_ , ++mx ) {
mn = 0;
for( int i = 1 , s = 0 ; i <= n ; ++i ) {
if( mark[i] ) {
if( s + g[i] - f[i - 1] + 2 <= mx )
++s;
else --s;
} else {
if( str[i] == '0' ) --s;
else ++s;
}
mn = min( mn , s );
}
ans = min( ans , mx - mn );
}
printf("%d",ans);
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
char s[N];
int n,a[N],b[N];
int solve(int mx)
{
int add=0;
for(int i=1;i<=n;i++)
if(s[i]!='?') b[i]=s[i]=='1'?1:-1;
else
{
if(a[i]+add+2<=mx)
b[i]=1,add+=2;
else b[i]=-1;
}
for(int i=1;i<=n;i++) b[i]+=b[i-1];
int mn=0;mx=0;
for(int i=1;i<=n;i++)
mn=min(mn,b[i]),mx=max(mx,b[i]);
return mx-mn;
}
int main()
{
scanf("%s",s+1);
n=strlen(s+1);
for(int i=1;i<=n;i++) a[i]=a[i-1]+(s[i]=='1'?1:-1);
for(int i=n-1;i>=1;i--) a[i]=max(a[i],a[i+1]);
int ans=min(solve(a[1]),solve(a[1]+1));
ans=min(ans,solve(a[1]+2));
printf("%d\n",ans);
}
|
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
int main()
{
string s; cin>>s;
int n=s.size();
int ans=1e9;
for(int p=0; p<2; p++){
for(int q=0; q<2; q++){
int l=0, r=0, sl=0, sr=0;
int d=0, L=-p, R=q;
for(int i=0; i<n; i++){
if(s[i]=='0'){
l--; r--;
sl--; sr--;
if(~(i+p)&1){
sl=max(sl, 1);
}
}else if(s[i]=='1'){
l++; r++;
sl++; sr++;
if(~(i+q)&1){
sr=min(sr, -1);
}
}else{
l--; r++;
sl--; sr++;
if(~(i+p)&1){
sl=max(sl, 1);
}
if(~(i+q)&1){
sr=min(sr, -1);
}
}
d=max(d, sl-sr);
L=min(L, r-sl);
R=max(R, l-sr);
}
ans=min(ans, max(d, R-L));
}
}
cout<<ans<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int) ((x).size()))
typedef long long ll;
typedef long double ld;
int n;
string s;
int isPos(int u, int sv) {
int l = sv, h = sv, il = 0, ih = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '0')
l--, h--;
else if (s[i] == '1')
l++, h++;
else
l--, h++;
if (l < 0)
l += 2, il++;
if (h > u)
h -= 2, ih++;
if (l > h)
return il >= ih ? 1 : 2;
}
return 0;
}
bool isPos(int u) {
int l = -1, r = u;
while (l + 1 < r) {
int m = (l + r) / 2;
int a = isPos(u, m);
if (a == 0)
return true;
if (a == 1)
l = m;
else
r = m;
}
return isPos(u, r) == 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(10);
cin >> s;
n = sz(s);
int l = -1, r = n;
while (l + 1 < r) {
int m = (l + r) / 2;
if (isPos(m))
r = m;
else
l = m;
}
cout << r << "\n";
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.