답안 #889881

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
889881 2023-12-20T08:55:05 Z vjudge1 Chorus (JOI23_chorus) C++17
16 / 100
7000 ms 456 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=s;
	ll ans=inf;
	set<ll> as,bs;
	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;
		for(i=0;i<n*2;i++){
			if(s[i]=='A')as.ins(i);
			else bs.ins(i);
		}
		ll sum=0;
		for(i=0;i<n*2;i++){
			if(s[i]!=ss[i]){
				if(s[i]=='A'){
					ll x=*bs.begin();
					sum+=(x-i);
					swap(s[i],s[x]);
					as.ins(x);
					as.erase(as.begin());
				}else{
					ll x=*as.begin();
					sum+=(x-i);
					swap(s[i],s[x]);
					bs.ins(x);
					bs.erase(bs.begin());
				}
			}
			if(s[i]=='A')as.erase(as.begin());
			else bs.erase(bs.begin());
		}
		ans=min(ans,sum);
		s=os;
	}
	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);}
      |                                                         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 116 ms 436 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 118 ms 348 KB Output is correct
8 Correct 109 ms 348 KB Output is correct
9 Correct 109 ms 428 KB Output is correct
10 Correct 110 ms 348 KB Output is correct
11 Correct 132 ms 344 KB Output is correct
12 Correct 138 ms 432 KB Output is correct
13 Correct 133 ms 348 KB Output is correct
14 Correct 110 ms 436 KB Output is correct
15 Correct 132 ms 348 KB Output is correct
16 Correct 127 ms 452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 116 ms 436 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 118 ms 348 KB Output is correct
8 Correct 109 ms 348 KB Output is correct
9 Correct 109 ms 428 KB Output is correct
10 Correct 110 ms 348 KB Output is correct
11 Correct 132 ms 344 KB Output is correct
12 Correct 138 ms 432 KB Output is correct
13 Correct 133 ms 348 KB Output is correct
14 Correct 110 ms 436 KB Output is correct
15 Correct 132 ms 348 KB Output is correct
16 Correct 127 ms 452 KB Output is correct
17 Execution timed out 7018 ms 344 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 116 ms 436 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 118 ms 348 KB Output is correct
8 Correct 109 ms 348 KB Output is correct
9 Correct 109 ms 428 KB Output is correct
10 Correct 110 ms 348 KB Output is correct
11 Correct 132 ms 344 KB Output is correct
12 Correct 138 ms 432 KB Output is correct
13 Correct 133 ms 348 KB Output is correct
14 Correct 110 ms 436 KB Output is correct
15 Correct 132 ms 348 KB Output is correct
16 Correct 127 ms 452 KB Output is correct
17 Execution timed out 7018 ms 344 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 116 ms 436 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 118 ms 348 KB Output is correct
8 Correct 109 ms 348 KB Output is correct
9 Correct 109 ms 428 KB Output is correct
10 Correct 110 ms 348 KB Output is correct
11 Correct 132 ms 344 KB Output is correct
12 Correct 138 ms 432 KB Output is correct
13 Correct 133 ms 348 KB Output is correct
14 Correct 110 ms 436 KB Output is correct
15 Correct 132 ms 348 KB Output is correct
16 Correct 127 ms 452 KB Output is correct
17 Execution timed out 7018 ms 344 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 116 ms 436 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 118 ms 348 KB Output is correct
8 Correct 109 ms 348 KB Output is correct
9 Correct 109 ms 428 KB Output is correct
10 Correct 110 ms 348 KB Output is correct
11 Correct 132 ms 344 KB Output is correct
12 Correct 138 ms 432 KB Output is correct
13 Correct 133 ms 348 KB Output is correct
14 Correct 110 ms 436 KB Output is correct
15 Correct 132 ms 348 KB Output is correct
16 Correct 127 ms 452 KB Output is correct
17 Execution timed out 7018 ms 344 KB Time limit exceeded
18 Halted 0 ms 0 KB -