Submission #207816

#TimeUsernameProblemLanguageResultExecution timeMemory
207816bensonlzlJJOOII 2 (JOI20_ho_t2)C++14
100 / 100
21 ms3944 KiB
#include <bits/stdc++.h>

using namespace std;

int N, K, nextJ[200005], nextO[200005], nextI[200005];
vector<int> posJ, posO, posI;
char x[200005];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cin >> N >> K;
	for (int i = 1; i <= N; ++i){
		cin >> x[i];
		if (x[i] == 'J') posJ.push_back(i);
		if (x[i] == 'O') posO.push_back(i);
		if (x[i] == 'I') posI.push_back(i);
	}
	int lastJ = N+1, lastO = N+1, lastI = N+1;
	for (int i = N; i >= 1; --i){
		nextJ[i] = lastJ;
		nextO[i] = lastO;
		nextI[i] = lastI;
		if (x[i] == 'J') lastJ = i;
		if (x[i] == 'O') lastO = i;
		if (x[i] == 'I') lastI = i;
	}
	int mini = 1e9;
	for (int i = 0; i < posJ.size() - K + 1; ++i){
		int lJ = posJ[i], rJ = posJ[i+K-1];
		int lO = nextO[rJ];
		auto itO = lower_bound(posO.begin(),posO.end(),lO);
		if (itO - posO.begin() > posO.size() - K) break;
		int rO = *(itO + K - 1);
		int lI = nextI[rO];
		auto itI = lower_bound(posI.begin(),posI.end(),lI);
		if (itI - posI.begin() > posI.size() - K) break;
		int rI = *(itI + K - 1);
		mini = min(mini,rI - lJ + 1 - 3 * K);
	}
	if (mini == 1e9){
		cout << -1 << '\n';
	}
	else cout << mini << '\n';
}

Compilation message (stderr)

ho_t2.cpp: In function 'int main()':
ho_t2.cpp:29:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < posJ.size() - K + 1; ++i){
                  ~~^~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:33:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (itO - posO.begin() > posO.size() - K) break;
       ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
ho_t2.cpp:37:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (itI - posI.begin() > posI.size() - K) break;
       ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...