Submission #889853

# Submission time Handle Problem Language Result Execution time Memory
889853 2023-12-20T08:17:11 Z vjudge1 Chorus (JOI23_chorus) C++17
0 / 100
7000 ms 348 KB
#include <bits/stdc++.h>
#define ll long long
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define bc back()
#define ar array
#define vll vector<ll> 
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class _T>
bool chmin(_T &x, const _T &y){
    if(x>y){
        x=y;
        return true;
    }
    return false;
}
template <class _T>
bool chmax(_T &x, const _T &y){
    bool flag=false;
    if (x<y){
        x=y;flag|=true;
    }
    return flag;
}
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
const ll inf=2e18+7;
const ll mod=1e9+7;
const ll N=1e5+7;
const ld eps=1e-9;
void solve(){
	ll i,j;
	ll n,k;
	str s;
	cin>>n>>k;
	cin>>s;
	str os;
	ll ans=inf;
	for(ll mask=0;mask<(1ll<<s.sz);mask++){
		ll c=0,c2=0;
		str ss;
		for(i=0;i<n*2;i++){
			if(mask&(1ll<<i)){
				c++;
				ss+='A';
			}
			else {
				ss+='B';
				c2++;
			}
		}
		if(c!=n)continue;
		c=c2=0;
		j=0;
		for(i=0;i<k;i++){
			while(j<n*2 && ss[j]=='A'){
				c++;
				j++;
			}
			while(j<n*2 && c){
				if(ss[j]=='A'){
					c2++;
				}else c--;
				j++;
			}
			c=c2;
			c2=0;
		}
		if(j<n*2)continue;
		set<ll> as,bs;
		s=os;
		for(i=0;i<n*2;i++){
			if(s[i]=='A')as.ins(i);
			else bs.ins(i);
		}
		ll sum=0;
		cout<<ss<<endl;
		for(auto i : as)cout<<i<<' ';
		cout<<endl;
		for(auto i : bs)cout<<i<<' ';
		cout<<endl;
		for(i=0;i<n*2;i++){
			if(s[i]!=ss[i]){
				if(s[i]=='A'){
					sum+=*bs.begin()-i;
					swap(s[i],s[*bs.begin()]);
					as.ins(*bs.begin());
					bs.erase(bs.begin());
				}else{
					sum+=*as.begin()-i;
					swap(s[i],s[*as.begin()]);
					bs.ins(*as.begin());
					as.erase(as.begin());
				}
			}
			else{
				if(s[i]=='A')as.erase(as.begin());
				else bs.erase(bs.begin());
			}
		}
		cout<<sum<<endl;
		ans=min(ans,sum);
	}
	cout<<ans<<endl;
}
signed main(){
	start();
    ll t=1;
	//cin>>t;
    while(t--) solve();
    return 0;
}
/*
1
6
5 4 3 2 1 0





*/

Compilation message

chorus.cpp: In function 'void fre(std::string)':
chorus.cpp:42:27: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 | void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
      |                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
chorus.cpp:42:64: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 | void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
      |                                                         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Execution timed out 7067 ms 348 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 7067 ms 348 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 7067 ms 348 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 7067 ms 348 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 7067 ms 348 KB Time limit exceeded
2 Halted 0 ms 0 KB -