답안 #793667

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
793667 2023-07-26T05:23:50 Z 박상훈(#10058) Flip it and Stick it (CCO23_day2problem1) C++17
25 / 25
5 ms 1868 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

constexpr int INF = 1e9 + 100;

char s[1001000], t[10], b[1001000];
int n, m;

int f(char *s){
	for (int i=1;i<=m;i++) if (s[i-1] != t[i]) return 0;
	return 1;
}

int solve(char x, char y){
	int ret = 0;

	for (int i=1;i<=n;i++){
		b[i] = x;
		if (i>=m && f(b + i-m+1)==1){
			b[i] = y;
		}

		ret += (b[i]=='1');
	}

	return ret;
}

int solve(){
	// scanf("%s", s+1);
	// scanf("%s", t+1);

	n = strlen(s+1);
	m = strlen(t+1);

	int mn = solve('0', '1'), mx = solve('1', '0');
	int cnt = 0, cnt1 = 0;
	vector<pair<int, int>> B;

	for (int i=1;i<=n-m+1;i++){
		cnt += f(s+i);

		if (m==3 && t[1]==t[2] && t[2]==t[3] && f(s+i)){
			if (B.empty() || B.back().second+1 < i) B.emplace_back(i, i);
			else B.back().second++;
		}
	} 

	int cntcnt = 0;
	for (int i=1;i<=n;i++) cnt1 += (s[i]=='1');
	for (int i=1;i<=n;i++) cntcnt += (s[i]!=t[1]);

	vector<pair<int, int>> B2;
	for (int i=1;i<=n;i++) if (s[i]!=t[1]){
		if (B2.empty() || B2.back().second+1 < i) B2.emplace_back(i, i);
		else B2.back().second++;
	}

	if (m==3){
		if (t[1]==t[3] && t[1]!=t[2]) cnt = (cnt+1) / 2;
		
		else if (t[1]==t[2] && t[2]==t[3]){
			int cnt2 = 0;
			for (auto &[l, r]:B) cnt2 += (r-l) / 2 + 1;

			int fuck = B2.size();
			// printf("%d ", fuck);
			if (!B2.empty() && B2[0].first==1) fuck--;
			// printf("%d ", fuck);
			if (!B2.empty() && B2.back().second==n) fuck--;
			// printf("%d ", fuck);
			if (!B2.empty() && B2[0].first==1 && B2[0].second==n) fuck++;
			// printf("%d\n", fuck);

			int ok = cntcnt - fuck;
			// if (fuck > 0)printf("fuck = %d / %d %d / %d\n", fuck, B2[0].first, B2[0].second, (int)B2.size());
			cnt = max(cnt2, cnt - ok);
		}
	}

	

	if (mn <= cnt1 && cnt1 <= mx) return cnt;
	return -1;
}

mt19937 seed(1557);
uniform_int_distribution<int> rng(0, 2147483647);
int getrand(int l, int r){return rng(seed) % (r-l+1) + l;}

int gen(){
	int n = getrand(1, 12);
	int k = getrand(0, 5);

	for (int i=1;i<=3;i++) t[i] = '0';
	t[4] = 0;
	
	for (int i=1;i<=n;i++){
		s[i] = getrand('0', '1');
		if (i>=3 && f(s+i-2)) s[i] = '1';
	} 
	s[n+1] = 0;

	for (int i=1;i<=k;i++){
		int l = getrand(1, n), r = getrand(1, n);
		if (l>r) swap(l, r);

		reverse(s+l, s+r+1);
	}

	return k;


}

int rans;
void dfs(int c, int mx){
	int flag = 0;
	for (int i=1;i<=n-m+1;i++) if (f(s+i)) {flag = 1; break;}

	if (!flag){
		rans = min(rans, c);
		return;
	}

	if (c >= min(mx, rans)) return;

	for (int i=1;i<=n;i++){
		for (int j=i+1;j<=n;j++){
			reverse(s+i, s+j+1);
			dfs(c+1, mx);
			reverse(s+i, s+j+1);
		}
	}
}

int naive(int mx){
	n = strlen(s+1);
	m = strlen(t+1);
	rans = INF;
	dfs(0, mx);
	return rans;
}

void stress(int tc){
	// printf("------------------------\n");
	// printf("Stress #%d\n", tc);

	int ans = gen();
	// printf("Input:\n");
	// printf("%s\n%s\n", s+1, t+1);

	int out = solve();

	ans = naive(ans);
	assert(ans < INF);

	// printf("Answer: %d\n", ans);
	// printf("Output: %d\n", out);

	if (!(out==ans && out!=-1)){
		printf("------------------------\n");
		printf("Stress #%d\n", tc);

		printf("Input:\n");
		printf("%s\n%s\n", s+1, t+1);

		printf("Answer: %d\n", ans);
		printf("Output: %d\n", out);
	}

	assert(out == ans && out != -1);

	if (tc%10000==0) printf("ok %d done\n", tc);
}

int main(){
	// for (int i=1;;i++) stress(i);

	scanf("%s", s+1);
	scanf("%s", t+1);

	printf("%d\n", solve());
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:182:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  182 |  scanf("%s", s+1);
      |  ~~~~~^~~~~~~~~~~
Main.cpp:183:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  183 |  scanf("%s", t+1);
      |  ~~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 3 ms 596 KB Output is correct
4 Correct 3 ms 596 KB Output is correct
5 Correct 5 ms 1328 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 5 ms 1868 KB Output is correct
9 Correct 4 ms 1232 KB Output is correct
10 Correct 4 ms 1232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 5 ms 1868 KB Output is correct
9 Correct 4 ms 1232 KB Output is correct
10 Correct 4 ms 1232 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 3 ms 596 KB Output is correct
21 Correct 3 ms 596 KB Output is correct
22 Correct 3 ms 1868 KB Output is correct
23 Correct 4 ms 1232 KB Output is correct
24 Correct 4 ms 1232 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 3 ms 596 KB Output is correct
27 Correct 5 ms 1360 KB Output is correct
28 Correct 4 ms 1360 KB Output is correct
29 Correct 3 ms 1360 KB Output is correct
30 Correct 3 ms 596 KB Output is correct
31 Correct 4 ms 1232 KB Output is correct
32 Correct 4 ms 1232 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 3 ms 596 KB Output is correct
9 Correct 5 ms 1292 KB Output is correct
10 Correct 3 ms 1360 KB Output is correct
11 Correct 4 ms 1360 KB Output is correct
12 Correct 3 ms 1868 KB Output is correct
13 Correct 3 ms 1868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 3 ms 676 KB Output is correct
11 Correct 3 ms 692 KB Output is correct
12 Correct 4 ms 1360 KB Output is correct
13 Correct 3 ms 1360 KB Output is correct
14 Correct 3 ms 1360 KB Output is correct
15 Correct 4 ms 1868 KB Output is correct
16 Correct 3 ms 1868 KB Output is correct
17 Correct 5 ms 1360 KB Output is correct
18 Correct 3 ms 1868 KB Output is correct
19 Correct 4 ms 1232 KB Output is correct
20 Correct 3 ms 596 KB Output is correct
21 Correct 4 ms 1868 KB Output is correct
22 Correct 3 ms 1868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 3 ms 596 KB Output is correct
9 Correct 5 ms 1292 KB Output is correct
10 Correct 3 ms 1360 KB Output is correct
11 Correct 4 ms 1360 KB Output is correct
12 Correct 3 ms 1868 KB Output is correct
13 Correct 3 ms 1868 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 3 ms 676 KB Output is correct
24 Correct 3 ms 692 KB Output is correct
25 Correct 4 ms 1360 KB Output is correct
26 Correct 3 ms 1360 KB Output is correct
27 Correct 3 ms 1360 KB Output is correct
28 Correct 4 ms 1868 KB Output is correct
29 Correct 3 ms 1868 KB Output is correct
30 Correct 5 ms 1360 KB Output is correct
31 Correct 3 ms 1868 KB Output is correct
32 Correct 4 ms 1232 KB Output is correct
33 Correct 3 ms 596 KB Output is correct
34 Correct 4 ms 1868 KB Output is correct
35 Correct 3 ms 1868 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 3 ms 596 KB Output is correct
39 Correct 5 ms 1492 KB Output is correct
40 Correct 5 ms 1492 KB Output is correct
41 Correct 3 ms 596 KB Output is correct
42 Correct 3 ms 1868 KB Output is correct
43 Correct 4 ms 1360 KB Output is correct
44 Correct 3 ms 596 KB Output is correct
45 Correct 4 ms 1108 KB Output is correct
46 Correct 4 ms 1740 KB Output is correct
47 Correct 4 ms 1492 KB Output is correct
48 Correct 4 ms 1868 KB Output is correct
49 Correct 1 ms 212 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 1 ms 212 KB Output is correct
52 Correct 0 ms 212 KB Output is correct
53 Correct 0 ms 212 KB Output is correct
54 Correct 3 ms 700 KB Output is correct
55 Correct 3 ms 596 KB Output is correct
56 Correct 5 ms 1360 KB Output is correct
57 Correct 3 ms 1232 KB Output is correct
58 Correct 3 ms 1360 KB Output is correct
59 Correct 4 ms 1868 KB Output is correct
60 Correct 3 ms 1868 KB Output is correct
61 Correct 0 ms 212 KB Output is correct
62 Correct 0 ms 212 KB Output is correct
63 Correct 0 ms 212 KB Output is correct
64 Correct 0 ms 212 KB Output is correct
65 Correct 3 ms 1360 KB Output is correct
66 Correct 4 ms 1360 KB Output is correct
67 Correct 4 ms 1868 KB Output is correct
68 Correct 4 ms 1232 KB Output is correct
69 Correct 3 ms 596 KB Output is correct
70 Correct 3 ms 1868 KB Output is correct
71 Correct 3 ms 1868 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 0 ms 212 KB Output is correct