QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#381915#6299. Binary StringTalaodiWA 113ms9996kbC++237.1kb2024-04-07 21:47:542024-04-07 21:47:55

Judging History

你现在查看的是最新测评结果

  • [2024-04-07 21:47:55]
  • 评测
  • 测评结果:WA
  • 用时:113ms
  • 内存:9996kb
  • [2024-04-07 21:47:54]
  • 提交

answer

#include <bits/stdc++.h>

namespace IO {
	template <class Stream>
	void write_int128(Stream& out, __int128 v) {
		if (v >= 10) {
			write_int128(out, v / 10);
		}
		out << (int) (v % 10);
	}

	template <class Stream>
	Stream& fmtbase(Stream& out, const char* format) {
		for (; *format; format++) {
			if (*format == '{' && *(format + 1) == '}') {
				throw std::invalid_argument("Error Number of Parameters!");
			}
			
			out << *format;
		}
		
		return out;
	}
	
	template <class Stream, class... Nxt>
	Stream& fmtbase(Stream& out, const char* format, const __int128& value, const Nxt&... nxt) {
		for (; *format; format++) {
			if (*format == '{' && *(format + 1) == '}') {
				write_int128(out, value);
				return fmtbase(out, format + 2, nxt...);
			} 
			
			out << *format;
		}
		
		throw std::invalid_argument("Error Number of Parameters!");
	}

	template <class Stream, class Fst, class... Nxt>
	Stream& fmtbase(Stream& out, const char* format, const Fst& value, const Nxt&... nxt) {
		for (; *format; format++) {
			if (*format == '{' && *(format + 1) == '}') {
				out << value;
				return fmtbase(out, format + 2, nxt...);
			} 
			
			out << *format;
		}
		
		throw std::invalid_argument("Error Number of Parameters!");
	}
	
	template <class... Ps>
	std::string to_string(const char* format, const Ps&... ps) {
		std::stringstream ss;
		fmtbase(ss, format, ps...);
		return ss.str();
	}

	template <class... Ps>
	std::ostream& fmtout(const char* format, const Ps&... ps) {
		return fmtbase(std::cout, format, ps...);
	}
	
	template <class... Ps>
	std::ostream& fmterr(const char* format, const Ps&... ps) {
		return fmtbase(std::cerr, format, ps...);
	}
	
	std::istream& allin() {
		return std::cin;
	}
	
	template <class Fst, class ... Nxt>
	std::istream& allin(Fst& fst, Nxt&... nxt) {
		std::cin >> fst;
		return allin(nxt...);
	}
	
	template <class Iter>
	std::istream& rangin(Iter begin, Iter end) {
		while (begin != end) {
			std::cin >> *begin;
			begin++;
		}
		
		return std::cin;
	}
	
	namespace Fast {
		bool sync = false;
		
		char buf[1 << 23];
		char *p1 = buf, *p2 = buf;
		
		void sync_with_ios(bool s) {
			sync = s;
		}
		
		inline char getchar() {
			if (sync) {
				return (char) std::cin.get();
			}
			else {
				return (p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, 1 << 22, stdin)), p1 == p2 ? EOF : *p1++;
			}
		}
		
		void read() { }
		
		template <class T, class... U>
		void read(T& x, U&... us) {
			x = 0;
			T pos = 1;
			
			char c = getchar();
			while (!isdigit(c)) {
				if (c == '-') {
					pos = -1;
				}
				
				c = getchar();
			}
			
			while (isdigit(c)) {
				x = 10 * x + c - '0';
				c = getchar();
			}
			
			x *= pos;
			read(us...);
		}
		
		template <class T>
		void write(const T& t) {
			if (t > 10) {
				write(t / 10);
			}
			
			std::cout << (int) (t % 10);
		}
	}
}

namespace Solve {
	using namespace IO;

	using ll = long long;
	using ul = unsigned long long;
	using db = double;
	using ld = long double;
	using ui = unsigned;
	using ib = __int128;
	using ub = __uint128_t;

	int const INF = std::numeric_limits<int>::max();
	int const NINF = std::numeric_limits<int>::min();
	ll const LINF = std::numeric_limits<ll>::max();
	ll const LNINF = std::numeric_limits<ll>::min();
	ld const EPS = 1e-8;

	std::mt19937_64 mt;

	ll rnd(ll l, ll r) {
		return std::uniform_int_distribution<ll>(l, r)(mt);
	}

	template <class T>
	inline int isz(const T& v) {
		return v.size();
	}

	template <class T>
	inline T& ckmx(T& a, T b) {
		return a < b ? (a = b) : a;
	}

	template <class T>
	inline T& ckmi(T& a, T b) {
		return b < a ? (a = b) : a;
	}

	int const N = 1e7 + 10;

	struct Line {
		int l, r;
	};

	int cnt[2];
	int s[N + 1];
	int t[N + 1];
	int tot;
	int nxt[N + 1];
	int n;

	void read() {
		std::string t;
		std::cin >> t;
		n = isz(t);
		for (int i = 0; i < n; i++) {
			s[i] = t[i] - '0';
		}
	}

	void main() {
		read();

		for (int i = 0; i < n; i++) {
			cnt[s[i]]++;
		}

		if (cnt[0] > cnt[1]) {
			std::reverse(s, s + n);
			for (int i = 0; i < n; i++) {
				s[i] ^= 1;
			}
			std::swap(cnt[0], cnt[1]);
		}

		int pos = -1;
		int mi = 0;
		int cur = 0;
		for (int i = 0; i < n; i++) {
			cur += s[i] ? 1 : -1;
			if (cur < mi) {
				mi = cur;
				pos = i;
			}
		}

		std::rotate(s, s + pos + 1, s + n);

		// for (int i = 0; i < n; i++) {
		// 	fmterr("{} ", s[i]);
		// }
		// fmterr("\n");

		int ans = 0;
		std::vector<Line> ls;
		for (int i = 0; i < n; i++) {
			if (s[i] == 1) {
				int j = i;
				while (j + 1 < n && s[j + 1]) {
					j++;
				}
				ls.push_back({ i, j });
				i = j;
			}
			else {
				int j = i;
				while (j + 1 < n && !s[j + 1]) {
					j++;
				}

				if (i == j) {
					continue;
				}
				else {
					int len = j - i + 1;
					int p = i;
					while (len > 1) {
						assert(isz(ls));
						auto& t = ls.back();
						int mid = p - t.r;
						t.r += mid / 2;
						p -= mid / 2;
						ans += mid / 2;

						t.l++;
						len--;
						ans++;
						// fmterr("en? {} {} {} {}\n", t.l, t.r, p, t.r);
						if (isz(ls) > 1) {
							ls[isz(ls) - 2].r++;
						}
						if (t.l == t.r) {
							ls.pop_back();
						}
					}
				}

				i = j;
			}

			// for (auto& v : ls) {
			// 	fmterr("({} {}) ", v.l, v.r);
			// }
			// fmterr("\n");
		}

		// for (auto& v : ls) {
		// 	fmterr("({} {}) ", v.l, v.r);
		// }
		// fmterr("\n");

		for (int i = 0; i < isz(ls); i++) {
			for (int j = ls[i].l; j <= ls[i].r; j++) {
				t[j] = 1;
			}
		}
		for (int i = ls[0].l - 1; i >= 0; i--) {
			t[i] = t[i + 1] ^ 1;
		}
		for (int i = ls.back().r + 1; i < n; i++) {
			t[i] = t[i - 1] ^ 1;
		}
		for (int i = 1; i < isz(ls); i++) {
			for (int j = ls[i - 1].r + 1; j < ls[i].l; j++) {
				t[j] = t[j - 1] ^ 1;
			}
		}

		// for (int i = 0; i < n; i++) {
		// 	fmterr("{} ", t[i]);
		// }
		// fmterr("\n");

		int j = -1;
		nxt[0] = -1;
		for (int i = 1; i < n; i++) {
			while (j != -1 && t[j + 1] != t[i]) {
				j = nxt[j];
			}
			if (t[j + 1] == t[i]) {
				j++;
			}
			nxt[i] = j;
		}

		int len = nxt[n - 1] + 1;
		if (len >= (n + 1) / 2 && n % (n - len) == 0) {
			ans += n - len;
		}
		else {
			ans += n;
		}

		fmtout("{}\n", ans);
	}

	void init() {

	}

	void clear() {
		for (int i = 0; i <= n; i++) {
			s[i] = t[i] = nxt[i] = 0;
		}
		cnt[0] = cnt[1] = 0;
		tot = 0;
	}
}

signed main() {
#ifndef ONLINE_JUDGE
	auto input_file = freopen("d.in", "r", stdin);
	auto output_file = freopen("d.out", "w", stdout);
#endif

	auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();

	IO::fmterr("seed: {}\n", seed);
	Solve::mt.seed(seed);

	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);

	int t = 1;
	std::cin >> t;

	Solve::init();

	for (int id = 1; id <= t; id++) {
		Solve::main();
		Solve::clear();
	}

#ifndef ONLINE_JUDGE
	std::cout.flush();
	fclose(input_file);
	fclose(output_file);
#endif

	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 9996kb

input:

3
1
001001
0001111

output:

1
3
9

result:

ok 3 number(s): "1 3 9"

Test #2:

score: -100
Wrong Answer
time: 113ms
memory: 9768kb

input:

262144
000000000000000000
100000000000000000
010000000000000000
110000000000000000
001000000000000000
101000000000000000
011000000000000000
111000000000000000
000100000000000000
100100000000000000
010100000000000000
110100000000000000
001100000000000000
101100000000000000
011100000000000000
11110000...

output:

1
18
18
19
18
18
19
20
18
18
18
19
19
19
20
21
18
18
18
19
18
18
19
20
19
19
19
20
20
20
21
22
18
18
18
19
18
18
19
21
18
18
18
19
19
19
20
21
19
19
19
20
19
19
20
21
20
20
20
21
21
21
22
23
18
18
18
19
18
18
19
20
18
18
18
19
19
19
21
22
18
18
18
19
18
18
19
20
19
19
19
20
20
20
21
22
19
19
19
20
1...

result:

wrong answer 12th numbers differ - expected: '20', found: '19'