Submission #203122

#TimeUsernameProblemLanguageResultExecution timeMemory
203122ZwariowanyMarcinJJOOII 2 (JOI20_ho_t2)C++14
100 / 100
13 ms3436 KiB
#include <bits/stdc++.h>
#define LL long long
#define pb push_back
#define mp make_pair
#define ss(x) (int) x.size()
#define fi first
#define se second
#define cat(x) cerr << #x << " = " << x << endl
#define rep(i, j, n) for (int i = j; i <= n; ++i)
#define per(i, j, n) for (int i = n; j <= i; --i)

using namespace std;

const int nax = 2e5 + 111;
const int INF = 1e9 + 11;

int n, k;
char s[nax];

vector <int> v[3];
int J[nax];
int I[nax];

int main() {
	scanf ("%d%d", &n, &k);
	scanf ("%s", s + 1);
	int a = 0, b = 0;
	rep(i, 1, n) {
		if (s[i] == 'J') v[0].pb(i), ++a;
		if (s[i] == 'O') v[1].pb(i);
		if (s[i] == 'I') v[2].pb(i), ++b;
		J[i] = a;
		I[i] = b;
	}
	
	int best = INF;
	for (int i = 0; i + k - 1 < ss(v[1]); ++i) {
		int x = v[1][i];
		int y = v[1][i + k - 1];
		int l = (J[x] - k >= 0 ? v[0][J[x] - k] : -1);
		int r = (I[y] + k - 1 < ss(v[2]) ? v[2][I[y] + k - 1] : -1);
		if (l == -1 || r == -1) continue;
		//printf ("%d %d %d %d\n", i, l, r, x);
		best = min(best, r - l + 1);
	}
	if (best == INF) printf ("-1\n");
	else printf ("%d\n", best - 3 * k);
		
	
	return 0;
}

Compilation message (stderr)

ho_t2.cpp: In function 'int main()':
ho_t2.cpp:25:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf ("%d%d", &n, &k);
  ~~~~~~^~~~~~~~~~~~~~~~
ho_t2.cpp:26:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf ("%s", s + 1);
  ~~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...