이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k; cin >> n >> k;
string s; cin >> s;
int m = s.size();
auto get = [&](auto &u){ return u == 'A' ? -1 : 0; };
auto ok = [&](auto &s){
int cnt = 0, sf = 0;
for ( int i = m - 1; i >= 0; i-- ){
if ( s[i] == 'B' ){
++sf;
} else{
cnt++;
if ( !sf ) return false;
int tmp = sf; sf = 0;
int j = i + 1;
while ( j - 1 >= 0 && tmp + get(s[j - 1]) >= 0 ){
tmp += get(s[--j]);
if ( s[j] == 'B' ) sf++;
}
i = j;
}
}
return cnt <= k;
};
vector <int> a;
for ( int i = 0; i < m; i++ ){
if ( s[i] == 'B' ) a.pb(i);
}
auto t = s;
sort(all(t));
int mn = n * n + 1;
do{
if ( !ok(t) ) continue;
vector <int> b;
for ( int i = 0; i < m; i++ ){
if ( t[i] == 'B' ) b.pb(i);
}
int ans = 0;
for ( int i = 0; i < n; i++ ){
ans += abs(a[i] - b[i]);
}
chmin(mn, ans);
} while ( next_permutation(all(t)) );
cout << mn;
cout << '\n';
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |