QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#596451#6338. ChorusMispertion#16 6ms4424kbC++233.1kb2024-09-28 15:49:222024-09-28 15:49:23

Judging History

你现在查看的是最新测评结果

  • [2024-09-28 15:49:23]
  • 评测
  • 测评结果:16
  • 用时:6ms
  • 内存:4424kb
  • [2024-09-28 15:49:22]
  • 提交

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 = 500 + 1;
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;
}

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++;
    }
    dp[n + 1][0] = 0LL;
    for(int i = 0; i <= n; i++)
        dp[i][0] = infi;
    for(int k = 1; k <= n; k++){
        for(int i = 1; i <= n; i++){
            dp[i][k] = infi;
            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][k] = min(dp[i][k], dp[j][k - 1] + (((ps[j - 1] + (ps[j - 1] - (cur - i) + 1)) * (cur - i)) / 2) - sm);
            }
        }
        dp[n + 1][k] = infi;
    }
    int ret = infi;
    for(int i = 1; i <= k; i++){
        ret = min(ret, ans + dp[1][i]);
    }
    cout << ret << '\n';
}   

signed main() {
    mispertion;
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

详细

Subtask #1:

score: 16
Accepted

Test #1:

score: 16
Accepted
time: 0ms
memory: 3660kb

input:

1 1
BA

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 16
Accepted
time: 0ms
memory: 3612kb

input:

7 5
ABBAAABBABABBA

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: 16
Accepted
time: 0ms
memory: 3880kb

input:

10 3
BABBABAABAABBABBBAAA

output:

26

result:

ok 1 number(s): "26"

Test #4:

score: 16
Accepted
time: 0ms
memory: 3664kb

input:

10 2
AAABBABABBAAABBBAABB

output:

11

result:

ok 1 number(s): "11"

Test #5:

score: 16
Accepted
time: 0ms
memory: 3616kb

input:

10 1
BBBBBBBBBBAAAAAAAAAA

output:

100

result:

ok 1 number(s): "100"

Test #6:

score: 16
Accepted
time: 0ms
memory: 3720kb

input:

10 2
BBBBBBBBBBAAAAAAAAAA

output:

75

result:

ok 1 number(s): "75"

Test #7:

score: 16
Accepted
time: 0ms
memory: 3684kb

input:

10 9
BBBBBBBBBBAAAAAAAAAA

output:

56

result:

ok 1 number(s): "56"

Test #8:

score: 16
Accepted
time: 0ms
memory: 3716kb

input:

10 10
BBBBBBBBBBAAAAAAAAAA

output:

55

result:

ok 1 number(s): "55"

Test #9:

score: 16
Accepted
time: 0ms
memory: 3628kb

input:

10 10
ABABABABABABABABABAB

output:

0

result:

ok 1 number(s): "0"

Test #10:

score: 16
Accepted
time: 0ms
memory: 3660kb

input:

10 2
ABAAABABABBBABABABAB

output:

14

result:

ok 1 number(s): "14"

Test #11:

score: 16
Accepted
time: 0ms
memory: 3688kb

input:

10 4
ABAABBAAABBBAAABBBAB

output:

2

result:

ok 1 number(s): "2"

Test #12:

score: 16
Accepted
time: 0ms
memory: 3908kb

input:

10 4
ABAAABBBAAABBBAABBAB

output:

2

result:

ok 1 number(s): "2"

Subtask #2:

score: 0
Runtime Error

Dependency #1:

100%
Accepted

Test #13:

score: 24
Accepted
time: 6ms
memory: 4424kb

input:

179 54
AAABABABABBAAABBABBABBABBBAAABAAAAABBBABAAAAABABBBAABBBABABBAABABAABABBBBABAABAABABABBBABBAABABBAABBAABABBAAABAAAAAAAABBAAAAABAAABBBBBBBABBAABBBABABAABBAABBABABABBABAAABABAAABABABBAABABAAABBABABABABABBAAABABBBBBBBAABBBAABABBBBABAABBAAAABAABBABABAABAAABABAAAABBBAABAAABBABABBBABAAABAABBBABBBBBA...

output:

41

result:

ok 1 number(s): "41"

Test #14:

score: 0
Runtime Error

input:

500 93
ABABAABBBBABAABAABAAAAABABBBBBABABBABAAAABAABBBBABAABBBAAABBABABAAAABAABBABAAABBAAABBBABBAAAAAABABABBBABABBBAABAAABAABABAAABAAABBAAABABBBAABBABBBABBBAABAAAABBBAABBBABAAAAAABABABBBABAAAABBBBBAABBBABAAABAABBBABABAABABBBBABBABBBBBBBABAAAABAABAABBABBBAABBBAAAAAAABBAABAAABBBABBBAAABABBBBAABBBABABB...

output:


result:


Subtask #3:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%

Subtask #5:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%