結果

問題 No.3266 岩井星人は見ずにはいられない
ユーザー shobonvip
提出日時 2025-09-07 09:57:27
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 2,119 bytes
コンパイル時間 4,426 ms
コンパイル使用メモリ 252,020 KB
実行使用メモリ 8,192 KB
最終ジャッジ日時 2025-09-07 09:57:33
合計ジャッジ時間 5,863 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
	author:  shobonvip
	created: 2025.09.07 09:11:53
**/

#include<bits/stdc++.h>
using namespace std;

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)
#define all(v) v.begin(), v.end()

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}

struct S {
	ll sum, mx;
};

S op(S a, S b) {
	return S{a.sum + b.sum, max(a.mx, a.sum + b.mx)};
}

S e() {
	return S{0, 0};
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	ll n, a; cin >> n >> a;
	string s; cin >> s;
	vector<S> rui(n + 1);
	vector<ll> ruinum(n + 1);
	rui[0] = e();
	rep(i,0,n) {
		if (s[i] == '0') {
			rui[i+1] = op(rui[i], S{-1, 0});
		} else {
			rui[i+1] = op(rui[i], S{1, 1});
			ruinum[i+1] += 1;
		}
		ruinum[i+1] += ruinum[i];
	}

	auto check = [&](ll t) -> ll {
		ll p = t / n;
		ll q = t % n;
		S v = e();
		ll nums = 0;
		{
			S w = rui[n];
			ll nu = ruinum[n];
			while (p > 0) {
				if (p & 1) {
					v = op(v, w);
					nums += nu;
				}
				w = op(w, w);
				nu = nu + nu;
				p >>= 1;
			}
		}
		v = op(v, rui[q]);
		nums += ruinum[q];
		return nums - v.mx;
	};

	ll ub = 2e18; // OK
	ll lb = 0; // NG
	while (ub - lb > 1) {
		ll t = (ub + lb) / 2;
		if (check(t) >= a) {
			ub = t;
		}else {
			lb = t;
		}
	}
	cout << ub << endl;
}

0