Submission #230067

#TimeUsernameProblemLanguageResultExecution timeMemory
230067AMO5JJOOII 2 (JOI20_ho_t2)C++98
1 / 100
2084 ms384 KiB
// READ & UNDERSTAND
// ll, int overflow, array bounds, memset(0)
// special cases (n=1?), n+1 (1-index)
// do smth instead of nothing & stay organized
// WRITE STUFF DOWN

#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end() 
#define MOD 1000000007

typedef long long ll;
typedef pair <int, int> ii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;

ll INF=LLONG_MAX;

int const mxn=2e5+5;
vi dp[3]; //JOI,pos

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
	int n,k; string s;
	cin >> n >> k >> s;
	dp[0].emplace_back(0);
	dp[1].emplace_back(0);
	dp[2].emplace_back(0);
	for(int i=0; i<n; i++){
		dp[0].emplace_back(dp[0].back()+(s[i]=='J'?1:0));
		dp[1].emplace_back(dp[1].back()+(s[i]=='O'?1:0));
		dp[2].emplace_back(dp[2].back()+(s[i]=='I'?1:0));
	}
	int ans=1e9;
	int JJ = dp[0][n], OO = dp[1][n], II = dp[2][n];
	for(int j=k; j<=JJ; j++){
		int posj = lower_bound(all(dp[0]),j)-dp[0].begin();
		int cnto = dp[1][posj];
		for(int o=cnto+k; o<=OO; o++){
			int poso = lower_bound(all(dp[1]),o)-dp[1].begin();
			int cnti = dp[2][poso];
			for(int i=cnti+k; i<=II; i++){
				int posi = lower_bound(all(dp[2]),i)-dp[2].begin();
				int st = dp[0][posj]-k+1;
				int posini = lower_bound(all(dp[0]),st)-dp[0].begin();
				int len = posi-posini+1;
				int cnt = len-k*3;
				ans = min(ans,cnt);
			}
		}
	}
	if(ans==1e9)ans=-1;
	cout << ans << endl;
}	
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...