QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#596589 | #6338. Chorus | Mispertion# | 0 | 0ms | 3648kb | C++23 | 3.2kb | 2024-09-28 16:03:40 | 2024-09-28 16:03:42 |
answer
#include<bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
#define int ll
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define F first
#define S second
#define getlast(s) (*s.rbegin())
#define debg cout << "OK\n"
const ld PI = 3.1415926535;
const int N = 1e3+5;
const int M = 50 + 1;
const int mod = 1e9+7;
const int infi = 1e15;
const ll infl = LLONG_MAX;
const int P = 31;
int mult(int a, int b) {
return a * 1LL * b % mod;
}
int sum(int a, int b) {
if (a + b < 0)
return a + b + mod;
if (a + b >= mod)
return a + b - mod;
return a + b;
}
ll binpow(ll a, ll n) {
if (n == 0)
return 1;
if (n % 2 == 1) {
return binpow(a, n - 1) * a % mod;
} else {
ll b = binpow(a, n / 2);
return b * b % mod;
}
}
int n, k, ans, dp[N][N], ps[N], R[N];
string s;
void normalize(){
vector<int> psa = {}, psb = {};
for(int i = 1; i <= 2 * n; i++){
if(s[i] == 'A')
psa.pb(i);
else
psb.pb(i);
}
string ns = "#";
int balance = 0, skp = 0, cur = 0;
for(int i = 1; i <= 2 * n; i++){
if(s[i] == 'A' && skp){
skp--;
continue;
}
if(s[i] == 'B' && balance == 0){
ns += "A";
ans += (psa[cur] - sz(ns) + 1);
balance++;
skp++;
i--;
cur++;
continue;
}
if(s[i] == 'B'){
ns += "B";
balance--;
}else{
ns += "A";
cur++;
balance++;
}
}
s = ns;
}
pii getans(int C){
vector<pii> dp(n + 2, {infi, infi});
dp[n + 1] = {0, 0};
for(int i = n; i >= 1; i--){
int cur = i, sm = 0;
for(int j = i + 1; j <= n + 1; j++){
while(cur < j && ps[j - 1] > R[cur]){
sm += R[cur];
cur++;
}
dp[i] = min(dp[i], {dp[j].F + (((ps[j - 1] + (ps[j - 1] - (cur - i) + 1)) * (cur - i)) / 2) - sm, dp[j].S + 1});
}
dp[i].F += C;
}
return dp[1];
}
void solve(){
cin >> n >> k;
cin >> s;
s = "#" + s;
normalize();
queue<int> st;
int cnt = 0;
for(int i = 2 * n; i >= 1; i--){
if(s[i] == 'B'){
st.push(i);
continue;
}
R[n - cnt] = st.front();
ps[n - cnt] = i;
st.pop();
cnt++;
}
int lo = -1, hi = 10 * n * n + 1;
while(lo + 1 < hi){
int C = (lo + hi) / 2;
auto e = getans(C);
if(e.S > k)
lo = C;
else
hi = C;
}
int C = hi;
auto e = getans(C);
cout << e.F + ans - (e.S * C) << '\n';
}
signed main() {
mispertion;
int t = 1;
//cin >> t;
while(t--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 16
Accepted
time: 0ms
memory: 3648kb
input:
1 1 BA
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Wrong Answer
time: 0ms
memory: 3640kb
input:
7 5 ABBAAABBABABBA
output:
4
result:
wrong answer 1st numbers differ - expected: '3', found: '4'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Skipped
Dependency #1:
0%