Submission #1298588

#TimeUsernameProblemLanguageResultExecution timeMemory
1298588quan606303JJOOII 2 (JOI20_ho_t2)C++20
100 / 100
10 ms5396 KiB
/*
 * @Author: RMQuan 
 * @Date: 2025-12-03 00:25:48 
 * @Last Modified by: RMQuan
 * @Last Modified time: 2025-12-03 00:36:07
 */
/*idea : 



*/
#include <bits/stdc++.h>
bool M1;
#define int long long
#define ll long long
#define INTMAX INT_MAX
#define INTMIN INT_MIN
#define LONGMAX LLONG_MAX
#define LONGMIN LLONG_MIN
#define fi first
#define se second
#define memfull(a,b) memset(a,b,sizeof(a));
#define endl '\n'
#define TASK "TEST"
#define file() if (fopen(TASK".inp","r")){freopen(TASK".inp","r",stdin); freopen(TASK".out","w",stdout);}
using namespace std;
const int MOD=1e9+7;
const int maxn=2e5+7;
const int inf=1e18;
int n,k;
int cost_J[maxn],cost_I[maxn];
int pre[maxn];
int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    file();
    cin>>n>>k;
    string s;
    cin>>s;
    s="$"+s;
    for (int i=0;i<=n;i++)
    {
        cost_J[i]=cost_I[i]=inf;
        pre[i]=-inf;
    }
    int ans=1e18;
    int cur_cnt=0;
    for (int l=1,r=1;r<=n;r++)
    {
        if (s[r]=='J')cur_cnt++;
        while (cur_cnt>k||(cur_cnt==k&&s[l]!='J'))
        {
            if (s[l]=='J')cur_cnt--;
            l++;
        }
        if (cur_cnt>=k&&s[r]=='J')cost_J[r]=(r-l+1)-k;
        pre[r]=max(pre[r-1],-cost_J[r]+r+1+k);
    }   
    cur_cnt=0;
    for (int l=n,r=n;l>=1;l--)
    {
        if (s[l]=='I')cur_cnt++;
        while (cur_cnt>k||(cur_cnt==k&&s[r]!='I'))
        {
            if (s[r]=='I')cur_cnt--;
            r--;
        }
        if (cur_cnt>=k&&s[l]=='I')cost_I[l]=(r-l+1)-k;
    }
    cur_cnt=0;
     for (int l=1,r=1;r<=n;r++)
    {
        if (s[r]=='O')cur_cnt++;
        while (cur_cnt>k||(cur_cnt==k&&s[l]!='O'))
        {
            if (s[l]=='O')cur_cnt--;
            l++;
        }
        if (cur_cnt>=k&&s[r]=='I')
        {
            ans=min(ans,cost_I[r]+r-pre[l-1]);
        }
    }   
    cout<<(ans>=1e18?-1:ans);
    return 0;
}

Compilation message (stderr)

ho_t2.cpp: In function 'int32_t main()':
ho_t2.cpp:25:50: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 | #define file() if (fopen(TASK".inp","r")){freopen(TASK".inp","r",stdin); freopen(TASK".out","w",stdout);}
      |                                           ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:38:5: note: in expansion of macro 'file'
   38 |     file();
      |     ^~~~
ho_t2.cpp:25:81: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 | #define file() if (fopen(TASK".inp","r")){freopen(TASK".inp","r",stdin); freopen(TASK".out","w",stdout);}
      |                                                                          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:38:5: note: in expansion of macro 'file'
   38 |     file();
      |     ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...