Submission #571893

#TimeUsernameProblemLanguageResultExecution timeMemory
571893dantoh000JJOOII 2 (JOI20_ho_t2)C++14
100 / 100
11 ms3364 KiB
#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()-k+1; 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 (stderr)

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()-k+1; 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);
      |     ~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...