이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9+10;
int n, k;
vector<int> pos[3];
int get_next(int ind, int p)
{
auto it = lower_bound(pos[ind].begin(), pos[ind].end(), p);
if (it == pos[ind].end()) return -1;
int x = (int)(it-pos[ind].begin());
if (x+k-1 >= pos[ind].size()) return -1;
return pos[ind][x+k-1];
}
bool ok(int i)
{
if (i+k-1 >= pos[0].size()) return false;
int p = pos[0][i+k-1];
p = get_next(1, p+1);
if (p == -1) return false;
p = get_next(2, p+1);
if (p == -1) return false;
return true;
}
int main(void)
{
string s;
cin >> n >> k >> s;
int ans = inf;
for (int i = 0; i < n; i++)
{
if (s[i] == 'J') pos[0].push_back(i);
else if (s[i] == 'O') pos[1].push_back(i);
else pos[2].push_back(i);
if (s[i] != 'I' || pos[0].size() < k) continue;
int ini = 0, fim = pos[0].size()-1, opt = -1;
while (ini <= fim)
{
int mid = (ini+fim)>>1;
if (ok(mid)) opt = mid, ini = mid+1;
else fim = mid-1;
}
if (opt != -1)
ans = min(ans, i-pos[0][opt]+1-3*k);
}
if (ans != inf) cout << ans << "\n";
else cout << "-1\n";
}
컴파일 시 표준 에러 (stderr) 메시지
ho_t2.cpp: In function 'int get_next(int, int)':
ho_t2.cpp:17:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | if (x+k-1 >= pos[ind].size()) return -1;
| ~~~~~~^~~~~~~~~~~~~~~~~~
ho_t2.cpp: In function 'bool ok(int)':
ho_t2.cpp:24:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | if (i+k-1 >= pos[0].size()) return false;
| ~~~~~~^~~~~~~~~~~~~~~~
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:51:36: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
51 | if (s[i] != 'I' || pos[0].size() < k) continue;
| ~~~~~~~~~~~~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |