답안 #571892

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
571892 2022-06-03T05:52:33 Z dantoh000 JJOOII 2 (JOI20_ho_t2) C++14
0 / 100
1 ms 1748 KB
#include <bits/stdc++.h>
using namespace std;
int n,k;
string s;
int nx[200005];
int jmp[200005];
vector<int> J, O, I;
int main(){
    scanf("%d%d",&n,&k);
    cin >> s;
    for (int i = 0; i < n; i++){
        if (s[i] == 'J') J.push_back(i);
        if (s[i] == 'O') O.push_back(i);
        if (s[i] == 'I') I.push_back(i);
    }
    int lastI = -1, lastO = -1;
    memset(nx,-1,sizeof(nx));
    for (int i = n-1; i >= 0; i--){
        if (s[i] == 'I') lastI = i;
        if (s[i] == 'O') {
            nx[i] = lastI;
            lastO = i;
        }
        if (s[i] == 'J'){
            nx[i] = lastO;
        }
    }
    memset(jmp,-1,sizeof(jmp));
    for (int i = 0; i < J.size()-k+1; i++){
        jmp[J[i]] = J[i+k-1];
    }
    for (int i = 0; i < O.size()-k+1; i++){
        jmp[O[i]] = O[i+k-1];
    }
    for (int i = 0; i < I.size()-k+1; i++){
        jmp[I[i]] = I[i+k-1];
    }
    int ans = -1;
    for (int i = 0; i < J.size()-2; i++){
        int lastJ = jmp[J[i]];
        int firstO = nx[lastJ]; if (firstO == -1) continue;
        int lastO = jmp[firstO]; if (lastO == -1) continue;
        int firstI = nx[lastO]; if (firstI == -1) continue;
        int lastI = jmp[firstI]; if (lastI == -1) continue;
        int cost = lastI - J[i] + 1 - 3*k;
        if (ans == -1 || ans > cost) ans = cost;
    }
    printf("%d\n",ans);

}

Compilation message

ho_t2.cpp: In function 'int main()':
ho_t2.cpp:29:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for (int i = 0; i < J.size()-k+1; i++){
      |                     ~~^~~~~~~~~~~~~~
ho_t2.cpp:32:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |     for (int i = 0; i < O.size()-k+1; i++){
      |                     ~~^~~~~~~~~~~~~~
ho_t2.cpp:35:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |     for (int i = 0; i < I.size()-k+1; i++){
      |                     ~~^~~~~~~~~~~~~~
ho_t2.cpp:39:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |     for (int i = 0; i < J.size()-2; i++){
      |                     ~~^~~~~~~~~~~~
ho_t2.cpp:9:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 |     scanf("%d%d",&n,&k);
      |     ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1748 KB Output is correct
2 Incorrect 1 ms 1748 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1748 KB Output is correct
2 Incorrect 1 ms 1748 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1748 KB Output is correct
2 Incorrect 1 ms 1748 KB Output isn't correct
3 Halted 0 ms 0 KB -