Submission #1129859

#TimeUsernameProblemLanguageResultExecution timeMemory
1129859agrim_09JJOOII 2 (JOI20_ho_t2)C++20
0 / 100
0 ms328 KiB
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define endl '\n';
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define py cout << "YES" << endl;
#define pn cout << "NO" << endl;
#define pb push_back
#define int long long
typedef long double lld;
#define double lld
typedef long long ll;
typedef unsigned long long ull;
const ll inf = 1e18;
const ll mod = 1e9+7;
const int N = 2e5;
vector<int>primes = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};

// Check for queue, priorioty_queue, stack, ordered_set solutions
// stack => LIFO (whatever goes in last comes out last)
// queue => FIFO (whatever goes in first comes out first)
// priority_queue => Dynamic queries of minimum/maximum
// ordered_set => set in vector form
//[order_of_key(k) gives number of elements less than k, while find_by_order(i) gives i^th element]

// To comment multiple lines : ctrl + /
// To find and replace : ctrl+H

void judge(){
    srand(time(NULL));
    #ifndef ONLINE_JUDGE
    freopen("inputA.txt","r",stdin);
    freopen("outputA.txt","w",stdout);
    freopen("Error.txt", "w", stderr);
    #define debug(x...) cerr << #x <<" = "; _print(x); cerr << endl;
    #else
    #define debug(x...)
    #endif
}

void usaco(string s) {
    freopen((s + ".in").c_str(),"r",stdin);
    freopen((s + ".out").c_str(),"w",stdout);
}

signed main(){
    fastio; //judge();
    int n,k; cin >> n >> k;
    string s; cin >> s;
    vector<int>posj, posi, poso;
    vector<int>prefo(n,0),prefi(n,0);
    for(int i = 0;i<n;i++){
        if(s[i]=='J')posj.pb(i);
        if(s[i]=='O')poso.pb(i);
        if(s[i]=='I')posi.pb(i);
    }
    prefo[0] = (s[0]=='O'), prefi[0] = (s[0]=='I'); 
    for(int i = 1;i<n;i++){
        prefo[i] = prefo[i-1] + (s[i]=='O');
        prefi[i] = prefi[i-1] + (s[i]=='I');
    }
    if(posj.size()<k or poso.size()<k or posi.size()<k){
        cout << -1; return 0;
    }
    int best = inf;

    for(int i = k-1;i<posj.size();i++){
        int moves = posj[i] - posj[i-(k-1)]+1-k;
        int pos = posj[i]+1;
        int low = pos, high = n-1, ans = -1;
        while(low<=high){
            int mid = (low+high)/2;
            if(prefo[mid]-prefo[pos-1]>=k){
                ans = mid, high = mid-1;
            }
            else{
                low = mid+1;
            }
        }
        if(ans==-1){
            break;
        }
        moves += ans - pos + 1 - k;
        pos = ans+1;
        low = pos, high = n-1, ans = -1;
        while(low<=high){
            int mid = (low+high)/2;
            if(prefi[mid]-prefi[pos-1]>=k){
                ans = mid, high = mid-1;
            }
            else{
                low = mid+1;
            }
        }
        moves += ans - pos + 1 - k;
        best = min(best,moves);
    }
    if(best==inf){
        cout << -1;
    }
    else{
        cout << best;
    }
}

Compilation message (stderr)

ho_t2.cpp: In function 'void judge()':
ho_t2.cpp:37:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     freopen("inputA.txt","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:38:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |     freopen("outputA.txt","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:39:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |     freopen("Error.txt", "w", stderr);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp: In function 'void usaco(std::string)':
ho_t2.cpp:47:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |     freopen((s + ".in").c_str(),"r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:48:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |     freopen((s + ".out").c_str(),"w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...