Submission #241336

#TimeUsernameProblemLanguageResultExecution timeMemory
241336kal013JJOOII 2 (JOI20_ho_t2)C++17
100 / 100
10 ms2148 KiB
/*/ Author: kal013 /*/
// #pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.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> ;

template<class key, class value, class cmp = std::less<key>>
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;

struct custom_hash { // Credits: https://codeforces.com/blog/entry/62393
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

template<class T> ostream& operator<<(ostream &os, vector<T> V) {
    os << "[ ";
    for(auto v : V) os << v << " ";
    return os << "]";
}
template<class T> ostream& operator<<(ostream &os, set<T> S){
    os << "{ ";
    for(auto s:S) os<<s<<" ";
    return os<<"}";
}
template<class T> ostream& operator<<(ostream &os, unordered_set<T> S){
    os << "{ ";
    for(auto s:S) os<<s<<" ";
    return os<<"}";
}
template<class T> ostream& operator<<(ostream &os, ordered_set<T> S){
    os << "{ ";
    for(auto s:S) os<<s<<" ";
    return os<<"}";
}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
    return os << "(" << P.first << "," << P.second << ")";
}
template<class L, class R> ostream& operator<<(ostream &os, map<L,R> M) {
    os << "{ ";
    for(auto m:M) os<<"("<<m.first<<":"<<m.second<<") ";
    return os<<"}";
}
template<class L, class R> ostream& operator<<(ostream &os, unordered_map<L,R> M) {
    os << "{ ";
    for(auto m:M) os<<"("<<m.first<<":"<<m.second<<") ";
    return os<<"}";
}
template<class L, class R, class chash = std::hash<R> > ostream& operator<<(ostream &os, gp_hash_table<L,R,chash> M) {
    os << "{ ";
    for(auto m:M) os<<"("<<m.first<<":"<<m.second<<") ";
    return os<<"}";
}

#define TRACE
#ifdef TRACE
    #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cerr << name << " : " << arg1 << endl;
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ',');
        cerr.write(names, comma - names) << " : " << arg1<<" | ";
        __f(comma+1, args...);
    }
#else
    #define trace(...) 1
#endif

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

inline int64_t random_long(long long l,long long r){
    uniform_int_distribution<int64_t> generator(l,r);
    return generator(rng);
}
inline int64_t random_long(){
    uniform_int_distribution<int64_t> generator(LLONG_MIN,LLONG_MAX);
    return generator(rng);
}


/*/---------------------------Defines----------------------/*/
typedef vector<int> vi;
typedef pair<int,int> pii;

#define ll long long
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(v) (v).begin(),(v).end()
/*/-----------------------Modular Arithmetic---------------/*/

const int mod=1e9+7;
inline int add(int x,int y){
    x+=y;
    if (x>=mod) return x-mod;
    return x;
}
inline int sub(int x,int y){
    x-=y;
    if (x<0) return x+mod;
    return x;
}
inline int mul(int x,int y){
    return (x*1ll*y)%mod;
}
inline int power(int x,int y){
    int ans=1;
    while(y){
        if (y&1) ans=mul(ans,x);
        x=mul(x,x);
        y>>=1;
    }
    return ans;
}
inline int inv(int x){
    return power(x,mod-2);
}
/*/-----------------------------Code begins----------------------------------/*/

void solve(){
    int n,k;
    cin>>n>>k;
    string s;
    cin>>s;

    vector<int> j,o,i;

    for(int x = 0; x< n; ++x){
    	if (s[x] =='J') j.push_back(x);
    	else if (s[x] == 'O') o.push_back(x);
    	else i.push_back(x);
    }

    int iter_o = 0, iter_i = 0;

    if (j.size() < k || o.size() < k || i.size() < k){
    	cout<<-1<<endl;
    	return;
    }

   	int ans = n+1;
    for(int z = k - 1; z < j.size(); ++z){

    	int l = j[z - k + 1];

    	while(iter_o < o.size() && o[iter_o] <= j[z]) ++iter_o;
    	if (iter_o + k >= o.size() + 1) break;
    	while(iter_i < i.size() && i[iter_i] <= o[iter_o + k -1]) ++iter_i;
    	if (iter_i + k >= i.size() + 1) break;
    	ans = min(ans, i[iter_i + k - 1] - l + 1 - 3*k );
    }

    if (ans > n - 3*k){
    	cout<<-1<<endl;
    }
    else{
    	cout<<ans<<endl;
    }
}
int main(){
    // Use "set_name".max_load_factor(0.25);"set_name".reserve(512); with unordered set
    // Or use gp_hash_table<X,null_type>
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    cout<<fixed<<setprecision(25);
    cerr<<fixed<<setprecision(10);
    auto start = std::chrono::high_resolution_clock::now();
    int t=1;
    // cin>>t;
    while(t--) {
        solve();
    }
    auto stop = std::chrono::high_resolution_clock::now(); 
    auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start); 
    
    // cerr << "Time taken : " << ((long double)duration.count())/((long double) 1e9) <<"s "<< endl;     
}




Compilation message (stderr)

ho_t2.cpp: In function 'void solve()':
ho_t2.cpp:156:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if (j.size() < k || o.size() < k || i.size() < k){
         ~~~~~~~~~^~~
ho_t2.cpp:156:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if (j.size() < k || o.size() < k || i.size() < k){
                         ~~~~~~~~~^~~
ho_t2.cpp:156:50: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if (j.size() < k || o.size() < k || i.size() < k){
                                         ~~~~~~~~~^~~
ho_t2.cpp:162:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int z = k - 1; z < j.size(); ++z){
                        ~~^~~~~~~~~~
ho_t2.cpp:166:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      while(iter_o < o.size() && o[iter_o] <= j[z]) ++iter_o;
            ~~~~~~~^~~~~~~~~~
ho_t2.cpp:167:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      if (iter_o + k >= o.size() + 1) break;
          ~~~~~~~~~~~^~~~~~~~~~~~~~~
ho_t2.cpp:168:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      while(iter_i < i.size() && i[iter_i] <= o[iter_o + k -1]) ++iter_i;
            ~~~~~~~^~~~~~~~~~
ho_t2.cpp:169:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      if (iter_i + k >= i.size() + 1) break;
          ~~~~~~~~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...