답안 #930054

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
930054 2024-02-18T11:32:39 Z Craftless JJOOII 2 (JOI20_ho_t2) C++17
0 / 100
2 ms 604 KB
#include <bits/stdc++.h>

#define int long long int
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define INF 1e13

using namespace std;

int N, K, ans = INF;
string S;

vector<int> js, os, is;

int32_t main() {

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	cin >> N >> K >> S;

	for (int i = 0; i < N; i++) {
		if (S[i] == 'J') js.pb(i);
		if (S[i] == 'O') os.pb(i);
		if (S[i] == 'I') is.pb(i);
	}

	if (K > js.size()) {
		cout << -1 << "\n";
		return 0;
	}

	for (int i = 0; i <= js.size() - K; i++) {
		int total = 0;
		int curI = js[i];
		for (int j = 0; j < K - 1; j++) {
			total += js[i + j + 1] - js[i + j] - 1;
			curI = js[i + j + 1];
		}
		int off = upper_bound(os.begin(), os.end(), curI) - os.begin();
		if (off >= os.size() or os.size() - off < K) continue;
		// cout << curI << "." << os[off] << " ";
		total += os[off] - curI - 1;
		// cout << (os.size() - off + 1) << endl;
		curI = os[off];
		for (int j = 0; j < K - 1; j++) {
			total += os[off + j + 1] - os[off + j] - 1;
			curI = os[off + j + 1];
		}
		int off2 = upper_bound(is.begin(), is.end(), curI) - is.begin();
		if (off2 >= is.size() or is.size() - off2 < K) continue;
		// cout << curI << "." << is[off2] << " ";
		total += is[off2] - curI - 1;
		// cout << "A" << total << endl;
		curI = is[off2];
		for (int j = 0; j < K - 1; j++) {
			total += is[off2 + j + 1] - is[off2 + j] - 1;
			curI = is[off2 + j + 1];
		}
		ans = min(ans, total);
	}


	cout << (ans == INF ? -1 : ans) << "\n";

	return 0;	
}

Compilation message

ho_t2.cpp: In function 'int32_t main()':
ho_t2.cpp:35:8: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |  if (K > js.size()) {
      |      ~~^~~~~~~~~~~
ho_t2.cpp:40:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'long long unsigned int' [-Wsign-compare]
   40 |  for (int i = 0; i <= js.size() - K; i++) {
      |                  ~~^~~~~~~~~~~~~~~~
ho_t2.cpp:48:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |   if (off >= os.size() or os.size() - off < K) continue;
      |       ~~~~^~~~~~~~~~~~
ho_t2.cpp:48:43: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'long long int' [-Wsign-compare]
   48 |   if (off >= os.size() or os.size() - off < K) continue;
      |                           ~~~~~~~~~~~~~~~~^~~
ho_t2.cpp:58:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |   if (off2 >= is.size() or is.size() - off2 < K) continue;
      |       ~~~~~^~~~~~~~~~~~
ho_t2.cpp:58:45: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'long long int' [-Wsign-compare]
   58 |   if (off2 >= is.size() or is.size() - off2 < K) continue;
      |                            ~~~~~~~~~~~~~~~~~^~~
ho_t2.cpp:21:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |  freopen("input.txt", "r", stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:22:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |  freopen("output.txt", "w", stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -