Submission #364631

#TimeUsernameProblemLanguageResultExecution timeMemory
364631Killer2501JJOOII 2 (JOI20_ho_t2)C++14
100 / 100
11 ms9516 KiB
#include <bits/stdc++.h>
#define pb push_back
#define task "sequence"
#define pll pair<ll, ll>
#define pii pair<vector<ll>, ll>
#define fi first
#define se second

using ll = long long;
const long long mod = 1e15+7;
const ll mod1 = 1e9+1;
const ll N = 2e5+5;
const int base = 350;
using ull = unsigned long long;
using namespace std;
string s, ss;
ll n, m, t, k, T, cnt, tong, a[N], c[N], b[N], ans, d[N], l[N], r[N];
vector<ll> adj[N], kq;
void sol()
{
    cin >> n >> k >> s;
    s = " " + s;
    l[0] = r[n+1] = mod;
    for(int i = 1; i <= n; i ++)
    {
        l[i] = l[i-1] + 1;
        if(s[i] == 'J')
        {
            kq.pb(i);
            if(kq.size() >= k)
            {
                l[i] = i - kq[kq.size()-k] + 1 - k;
            }
        }
    }
    kq.clear();
    for(int i = n; i > 0; i --)
    {
        r[i] = r[i+1] + 1;
        if(s[i] == 'I')
        {
            kq.pb(i);
            if(kq.size() >= k)
            {
                r[i] = kq[kq.size()-k] - i + 1 - k;
            }
        }
    }
    kq.clear();
    ans = mod;
    for(int i = 1; i <= n; i ++)
    {
        if(s[i] == 'O')
        {
            kq.pb(i);
            if(kq.size() >= k)
            {
                ans = min(ans, i - kq[kq.size()-k] + 1 - k + r[i+1] + l[kq[kq.size()-k]-1]);
            }
        }
    }
    if(ans == mod)ans = -1;
    cout << ans;
}

int main()
{
    if(fopen(task".in", "r")){
       freopen(task".in", "r", stdin);
       freopen(task".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int ntest;
    ntest =  1;
    //cin >> ntest;
    //cout << (1<<30) << '\n';
    while(ntest -- > 0)
    sol();

}

Compilation message (stderr)

ho_t2.cpp: In function 'void sol()':
ho_t2.cpp:30:26: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   30 |             if(kq.size() >= k)
      |                ~~~~~~~~~~^~~~
ho_t2.cpp:43:26: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   43 |             if(kq.size() >= k)
      |                ~~~~~~~~~~^~~~
ho_t2.cpp:56:26: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   56 |             if(kq.size() >= k)
      |                ~~~~~~~~~~^~~~
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:69:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   69 |        freopen(task".in", "r", stdin);
      |        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:70:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   70 |        freopen(task".out", "w", stdout);
      |        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...