QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#601254#6338. ChorusMispertion40 630ms5776kbC++233.6kb2024-09-29 21:52:542024-09-29 21:52:55

Judging History

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

  • [2024-09-29 21:52:55]
  • 评测
  • 测评结果:40
  • 用时:630ms
  • 内存:5776kb
  • [2024-09-29 21:52:54]
  • 提交

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 = 5e3+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[2 * N], R[2 * N], prp[N], prc[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++;
            }
            int cntB = prc[ps[j - 1]] - prc[R[i] - 1];
            int sumB = prp[ps[j - 1]] - prp[R[i] - 1];
            if(cntB > 0)
                dp[i] = min(dp[i], {dp[j].F + cntB * ps[j - 1] - sumB - cntB * (cntB - 1) / 2, dp[j].S + 1});
            else
                dp[i] = min(dp[i], {dp[j].F, dp[j].S + 1});
        }
        dp[i].F += C;
    }
    return dp[1];
}

void solve(){
    cin >> n >> k;
    cin >> s;
    s = "#" + s;
    normalize();
    queue<int> st;
    for(int i = 1; i <= 2 * n; i++){
        prc[i] = prc[i - 1] + (s[i] == 'B');
        prp[i] = prp[i - 1] + (s[i] == 'B') * i;
    }
    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 - (k * C) << '\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: 3636kb

input:

1 1
BA

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

7 5
ABBAAABBABABBA

output:

3

result:

ok 1 number(s): "3"

Test #3:

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

input:

10 3
BABBABAABAABBABBBAAA

output:

26

result:

ok 1 number(s): "26"

Test #4:

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

input:

10 2
AAABBABABBAAABBBAABB

output:

11

result:

ok 1 number(s): "11"

Test #5:

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

input:

10 1
BBBBBBBBBBAAAAAAAAAA

output:

100

result:

ok 1 number(s): "100"

Test #6:

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

input:

10 2
BBBBBBBBBBAAAAAAAAAA

output:

75

result:

ok 1 number(s): "75"

Test #7:

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

input:

10 9
BBBBBBBBBBAAAAAAAAAA

output:

56

result:

ok 1 number(s): "56"

Test #8:

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

input:

10 10
BBBBBBBBBBAAAAAAAAAA

output:

55

result:

ok 1 number(s): "55"

Test #9:

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

input:

10 10
ABABABABABABABABABAB

output:

0

result:

ok 1 number(s): "0"

Test #10:

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

input:

10 2
ABAAABABABBBABABABAB

output:

14

result:

ok 1 number(s): "14"

Test #11:

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

input:

10 4
ABAABBAAABBBAAABBBAB

output:

2

result:

ok 1 number(s): "2"

Test #12:

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

input:

10 4
ABAAABBBAAABBBAABBAB

output:

2

result:

ok 1 number(s): "2"

Subtask #2:

score: 24
Accepted

Dependency #1:

100%
Accepted

Test #13:

score: 24
Accepted
time: 1ms
memory: 3584kb

input:

179 54
AAABABABABBAAABBABBABBABBBAAABAAAAABBBABAAAAABABBBAABBBABABBAABABAABABBBBABAABAABABABBBABBAABABBAABBAABABBAAABAAAAAAAABBAAAAABAAABBBBBBBABBAABBBABABAABBAABBABABABBABAAABABAAABABABBAABABAAABBABABABABABBAAABABBBBBBBAABBBAABABBBBABAABBAAAABAABBABABAABAAABABAAAABBBAABAAABBABABBBABAAABAABBBABBBBBA...

output:

41

result:

ok 1 number(s): "41"

Test #14:

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

input:

500 93
ABABAABBBBABAABAABAAAAABABBBBBABABBABAAAABAABBBBABAABBBAAABBABABAAAABAABBABAAABBAAABBBABBAAAAAABABABBBABABBBAABAAABAABABAAABAAABBAAABABBBAABBABBBABBBAABAAAABBBAABBBABAAAAAABABABBBABAAAABBBBBAABBBABAAABAABBBABABAABABBBBABBABBBBBBBABAAAABAABAABBABBBAABBBAAAAAAABBAABAAABBBABBBAAABABBBBAABBBABABB...

output:

235

result:

ok 1 number(s): "235"

Test #15:

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

input:

500 273
AAABBABAABAAABABAABBBABAAAABBAAABBABAAABBABABAAAABBBAAABBBAABAABAABBABABABABBAAABBBBBBBAABABBAABABBBAABBBBAAABAAAABABBABBBBBAABAABABAAAABBABABABAAAABBBAAAAABABBAABABABAAABBABAAAABABBBAAABABAABBAABBBAABAABBBAABABBBBABAAABAABBBBABBABAAABABBAABABBABBBBBBBABAABAAAABABABAAABABABABAABBAABBBABBBAAB...

output:

0

result:

ok 1 number(s): "0"

Test #16:

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

input:

500 1
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

output:

250000

result:

ok 1 number(s): "250000"

Test #17:

score: 24
Accepted
time: 3ms
memory: 3588kb

input:

500 2
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

output:

187500

result:

ok 1 number(s): "187500"

Test #18:

score: 24
Accepted
time: 5ms
memory: 3632kb

input:

500 499
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

output:

125251

result:

ok 1 number(s): "125251"

Test #19:

score: 24
Accepted
time: 3ms
memory: 3800kb

input:

500 500
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

output:

125250

result:

ok 1 number(s): "125250"

Test #20:

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

input:

500 10
ABAAABBBAABBABABAABAABABBBAABABABBAABABAABAABABABBBBABAABBAAAAAAABAABAABAABBABBABABBBABABBAABABBBBAABBBBABAAAAAAABAABAAABABABBAABABBABBAAAABAABBBAABABBABABBBBAAAABABBAAAAABAABBBBBBBBABBABBBAABBABBAAAABAABBBBBBAABBBAAAABABBAAABAABAABBBBAABBAAABBAABBAAAAABAAAAAABBAABABBABABBBABAAABBBAAABABAAABB...

output:

9129

result:

ok 1 number(s): "9129"

Test #21:

score: 24
Accepted
time: 5ms
memory: 3856kb

input:

500 500
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...

output:

0

result:

ok 1 number(s): "0"

Test #22:

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

input:

500 416
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...

output:

84

result:

ok 1 number(s): "84"

Test #23:

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

input:

499 167
ABAABBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAAB...

output:

2

result:

ok 1 number(s): "2"

Test #24:

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

input:

499 167
ABAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAA...

output:

2

result:

ok 1 number(s): "2"

Subtask #3:

score: 0
Wrong Answer

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Test #25:

score: 0
Wrong Answer
time: 630ms
memory: 3864kb

input:

4918 2048
ABBBBBBAAAAABBBBAAAABBABBAAAABBBAABBBABABBAAAABBBAAAABAABBAAABAAABAABBAAABAAABBBAABBBBBBABAABBBBAAAAABBBBBABBBABABBBBBABAAAAAABAABBABBABBBBBAABAABAAAAAABBBBBAABBBABBBAAABBABBABBBAABBBBBBABBBABAABABABABABAAABBABABBAABAABBBBABABBAABABABABBAABABABBABABABAAABABBBABBBABBABBBBAAAABBBBAAABBBABBBA...

output:

-978449

result:

wrong answer 1st numbers differ - expected: '138308', found: '-978449'

Subtask #4:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

0%

Subtask #5:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

0%