답안 #982662

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
982662 2024-05-14T15:36:02 Z SmuggingSpun JJOOII 2 (JOI20_ho_t2) C++14
100 / 100
162 ms 4384 KB
#include<bits/stdc++.h>
#define taskname "B"
using namespace std;
const int lim = 2e5 + 5;
template<class T>void minimize(T& a, T b){
	if(a > b){
		a = b;
	}
}
int n, k;
string s;
namespace sub1{
	void solve(){
		int m = 1 << n, ans = INT_MAX;
		string pat = string(k, 'J') + string(k, 'O') + string(k, 'I');
		for(int mask = 0; mask < m; mask++){
			string current = "";
			for(int i = 0; i < n; i++){
				if(1 << i & mask){
					current += s[i];
				}
			}
			if(current == pat){
				int l = 0, r = n - 1;
				while((1 << l & mask) == 0){
					l++;
				}
				while((1 << r & mask) == 0){
					r--;
				}
				int cnt = 0;
				for(int i = l; i <= r; i++){
					if((1 << i & mask) == 0){
						cnt++;
					}
				}
				minimize(ans, cnt);
			}
		}
		cout << (ans == INT_MAX ? -1 : ans);
	}
}
namespace sub23{
	vector<int>p[3];
	int nxt[lim][3];
	void solve(){
		for(int i = 0; i < n; i++){
			if(s[i] == 'J'){
				p[0].emplace_back(i);
			}
			else if(s[i] == 'O'){
				p[1].emplace_back(i);
			}
			else{
				p[2].emplace_back(i);
			}
		}
		memset(nxt, -1, sizeof(nxt));
		for(int i = 0; i < 3; i++){
			for(int j = 0, pos = 0; j < n && pos + k - 1 < p[i].size(); j++){
				nxt[j][i] = p[i][pos + k - 1];
				if(p[i][pos] == j){
					pos++;
				}
			}
		}
		int ans = INT_MAX;
		for(int i = 0; i + 3 * k <= n; i++){
			int current = i;
			for(int j = 0; j < 3; j++){
				if((current = nxt[current][j]) == -1){
					break;
				}
			}
			if(current != -1){
				minimize(ans, current - i - 3 * k + 1);
			}
		}
		cout << (ans == INT_MAX ? -1 : ans);
	}
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	if(fopen(taskname".inp", "r")){
		freopen(taskname".inp", "r", stdin);
	}
	cin >> n >> k >> s;
	if(n <= 21){
		sub1::solve();
	}
	else{
		sub23::solve();
	}
}

Compilation message

ho_t2.cpp: In function 'void sub23::solve()':
ho_t2.cpp:60:49: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |    for(int j = 0, pos = 0; j < n && pos + k - 1 < p[i].size(); j++){
      |                                     ~~~~~~~~~~~~^~~~~~~~~~~~~
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:85:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |   freopen(taskname".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 162 ms 348 KB Output is correct
4 Correct 152 ms 432 KB Output is correct
5 Correct 152 ms 432 KB Output is correct
6 Correct 156 ms 436 KB Output is correct
7 Correct 152 ms 436 KB Output is correct
8 Correct 149 ms 436 KB Output is correct
9 Correct 146 ms 344 KB Output is correct
10 Correct 152 ms 444 KB Output is correct
11 Correct 151 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 156 ms 348 KB Output is correct
14 Correct 159 ms 544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 162 ms 348 KB Output is correct
4 Correct 152 ms 432 KB Output is correct
5 Correct 152 ms 432 KB Output is correct
6 Correct 156 ms 436 KB Output is correct
7 Correct 152 ms 436 KB Output is correct
8 Correct 149 ms 436 KB Output is correct
9 Correct 146 ms 344 KB Output is correct
10 Correct 152 ms 444 KB Output is correct
11 Correct 151 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 156 ms 348 KB Output is correct
14 Correct 159 ms 544 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2768 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2652 KB Output is correct
33 Correct 1 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 162 ms 348 KB Output is correct
4 Correct 152 ms 432 KB Output is correct
5 Correct 152 ms 432 KB Output is correct
6 Correct 156 ms 436 KB Output is correct
7 Correct 152 ms 436 KB Output is correct
8 Correct 149 ms 436 KB Output is correct
9 Correct 146 ms 344 KB Output is correct
10 Correct 152 ms 444 KB Output is correct
11 Correct 151 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 156 ms 348 KB Output is correct
14 Correct 159 ms 544 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2768 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2652 KB Output is correct
33 Correct 1 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 6 ms 4056 KB Output is correct
37 Correct 6 ms 4384 KB Output is correct
38 Correct 6 ms 4024 KB Output is correct
39 Correct 6 ms 4288 KB Output is correct
40 Correct 5 ms 4052 KB Output is correct
41 Correct 5 ms 3992 KB Output is correct
42 Correct 6 ms 4384 KB Output is correct
43 Correct 5 ms 3576 KB Output is correct
44 Correct 4 ms 3808 KB Output is correct
45 Correct 5 ms 4052 KB Output is correct
46 Correct 5 ms 4044 KB Output is correct
47 Correct 5 ms 4052 KB Output is correct
48 Correct 5 ms 4052 KB Output is correct
49 Correct 3 ms 3772 KB Output is correct
50 Correct 5 ms 4040 KB Output is correct
51 Correct 5 ms 4048 KB Output is correct
52 Correct 4 ms 3936 KB Output is correct
53 Correct 4 ms 4208 KB Output is correct
54 Correct 3 ms 3944 KB Output is correct
55 Correct 3 ms 3944 KB Output is correct
56 Correct 2 ms 3944 KB Output is correct