| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 889706 | vjudge1 | Chorus (JOI23_chorus) | C++17 | 7027 ms | 456 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 : 1; };
auto ok = [&](auto &s){
queue <int> q;
vector <int> nxt(m, m + 1);
for ( int i = m - 1; i >= 0; i-- ){
if ( s[i] == 'B' ) q.push(i);
else{
if ( q.empty() ){
return false;
}
nxt[i] = q.front();
q.pop();
}
}
int cnt = 1, lst = -1;
vector <int> c(m);
for ( int i = m - 1; i >= 0; i-- ){
if ( s[i] != 'B' ) continue;
if ( lst == -1 ){
lst = i;
c[lst] = i;
} else{
int mn = m + 1;
for ( int k = lst - 1; k > i; k-- ){
chmin(mn, nxt[k]);
}
if ( mn <= c[lst] ){
c[i] = i;
++cnt;
} else c[i] = c[lst];
lst = i;
}
}
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]);
}
auto flag = chmin(mn, ans);
} while ( next_permutation(all(t)) );
cout << mn;
cout << '\n';
}
Compilation message (stderr)
| # | 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... | ||||
