답안 #825387

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
825387 2023-08-14T19:13:10 Z grogu Chorus (JOI23_chorus) C++14
61 / 100
5642 ms 196616 KB
#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define eb emplace_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}
#define si(a) (ll)(a.size())
using namespace std;


#define maxn 5005
ll n,k;
ll a[2*maxn];
ll c[maxn];
ll ps[maxn];
ll sz = 0;
ll dp[maxn][maxn];
struct line{
    ll n,k;
    line(){n = llinf;k = 0;}
    line(ll n_,ll k_){n = n_,k = k_;}
    ll operator()(ll x){
        return k*x+n;
    }
};
line t[2*maxn];
ll ls[2*maxn],rs[2*maxn],tsz = 0,root = 0;
void init(ll &v,ll tl,ll tr){
    if(!v) v = ++tsz;
    t[v] = line();
    if(tl==tr) return;
    ll mid = (tl+tr)/2;
    init(ls[v],tl,mid);
    init(rs[v],mid+1,tr);
}
void upd(ll v,ll tl,ll tr,line f){
    line g = t[v];
    if(tl==tr){
        if(f(tl)<g(tl)) t[v] = f;
        return;
    }
    ll mid = (tl+tr)/2;
    if(f(tl)>g(tl)) swap(f,g);
    if(f(mid)<=g(mid)){t[v] = f;upd(rs[v],mid+1,tr,g);}
    else{t[v] = g;upd(ls[v],tl,mid,f);}
}
ll get(ll v,ll tl,ll tr,ll i){
    if(tl==tr) return t[v](i);
    ll mid = (tl+tr)/2;
    if(i<=mid) return min(get(ls[v],tl,mid,i),t[v](i));
    return min(get(rs[v],mid+1,tr,i),t[v](i));
}
int main(){
	ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
    cin >> n >> k;
    string s; cin >> s;
    for(ll i = 1;i<=2*n;i++){
        if(s[i-1]=='A') a[i] = 1;
    }
    ll b = 0;
    for(ll i = 2*n;i>=1;i--){
        if(a[i]==1){
            c[++sz] = b;
        }else b++;
    }
    for(ll i = 1;i<=n;i++) ps[i] = ps[i-1] + c[i];
    dp[0][0] = 0;
    for(ll i = 1;i<=n;i++) dp[i][0] = dp[0][i] = llinf;
    ll e = 0;
    for(ll j = 1;j<=k;j++){
        multiset<ll> s;
        init(root,1,n);
        e = 1;
        s.insert(dp[0][j-1]);
        for(ll i = 1;i<=n;i++){
            while(e<=i&&c[e]<=i){
                s.erase(s.find(dp[e-1][j-1]));
                line f(ps[e-1]+dp[e-1][j-1],-e);
                upd(root,1,n,f);
                e++;
            }
            dp[i][j] = llinf;
            if(si(s)) dp[i][j] = *s.begin();
            dp[i][j] = min(dp[i][j],get(root,1,n,i) - ps[e-1] + e*i);
            s.insert(dp[i][j-1]);
        }
    }
    //for(ll i = 0;i<=n;i++) ceri(dp[i],0,k);
    cout<<dp[n][k]<<endl;
	return (0-0);
}
/**
5 2
AABABABBAB

**/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 480 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 480 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 2 ms 1236 KB Output is correct
18 Correct 11 ms 2924 KB Output is correct
19 Correct 26 ms 3572 KB Output is correct
20 Correct 2 ms 2516 KB Output is correct
21 Correct 1 ms 2516 KB Output is correct
22 Correct 29 ms 4444 KB Output is correct
23 Correct 30 ms 4504 KB Output is correct
24 Correct 2 ms 2588 KB Output is correct
25 Correct 36 ms 4448 KB Output is correct
26 Correct 28 ms 4120 KB Output is correct
27 Correct 15 ms 3156 KB Output is correct
28 Correct 14 ms 3156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 480 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 2 ms 1236 KB Output is correct
18 Correct 11 ms 2924 KB Output is correct
19 Correct 26 ms 3572 KB Output is correct
20 Correct 2 ms 2516 KB Output is correct
21 Correct 1 ms 2516 KB Output is correct
22 Correct 29 ms 4444 KB Output is correct
23 Correct 30 ms 4504 KB Output is correct
24 Correct 2 ms 2588 KB Output is correct
25 Correct 36 ms 4448 KB Output is correct
26 Correct 28 ms 4120 KB Output is correct
27 Correct 15 ms 3156 KB Output is correct
28 Correct 14 ms 3156 KB Output is correct
29 Correct 2023 ms 99532 KB Output is correct
30 Correct 5642 ms 190368 KB Output is correct
31 Correct 1238 ms 61340 KB Output is correct
32 Correct 9 ms 21204 KB Output is correct
33 Correct 11 ms 21200 KB Output is correct
34 Correct 4396 ms 196616 KB Output is correct
35 Correct 4133 ms 196572 KB Output is correct
36 Correct 19 ms 21588 KB Output is correct
37 Correct 1037 ms 60228 KB Output is correct
38 Correct 3867 ms 182788 KB Output is correct
39 Correct 2367 ms 114140 KB Output is correct
40 Correct 1656 ms 86256 KB Output is correct
41 Correct 1725 ms 86248 KB Output is correct
42 Correct 1993 ms 86332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 480 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 2 ms 1236 KB Output is correct
18 Correct 11 ms 2924 KB Output is correct
19 Correct 26 ms 3572 KB Output is correct
20 Correct 2 ms 2516 KB Output is correct
21 Correct 1 ms 2516 KB Output is correct
22 Correct 29 ms 4444 KB Output is correct
23 Correct 30 ms 4504 KB Output is correct
24 Correct 2 ms 2588 KB Output is correct
25 Correct 36 ms 4448 KB Output is correct
26 Correct 28 ms 4120 KB Output is correct
27 Correct 15 ms 3156 KB Output is correct
28 Correct 14 ms 3156 KB Output is correct
29 Correct 2023 ms 99532 KB Output is correct
30 Correct 5642 ms 190368 KB Output is correct
31 Correct 1238 ms 61340 KB Output is correct
32 Correct 9 ms 21204 KB Output is correct
33 Correct 11 ms 21200 KB Output is correct
34 Correct 4396 ms 196616 KB Output is correct
35 Correct 4133 ms 196572 KB Output is correct
36 Correct 19 ms 21588 KB Output is correct
37 Correct 1037 ms 60228 KB Output is correct
38 Correct 3867 ms 182788 KB Output is correct
39 Correct 2367 ms 114140 KB Output is correct
40 Correct 1656 ms 86256 KB Output is correct
41 Correct 1725 ms 86248 KB Output is correct
42 Correct 1993 ms 86332 KB Output is correct
43 Runtime error 6 ms 1416 KB Execution killed with signal 11
44 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 480 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 2 ms 1236 KB Output is correct
18 Correct 11 ms 2924 KB Output is correct
19 Correct 26 ms 3572 KB Output is correct
20 Correct 2 ms 2516 KB Output is correct
21 Correct 1 ms 2516 KB Output is correct
22 Correct 29 ms 4444 KB Output is correct
23 Correct 30 ms 4504 KB Output is correct
24 Correct 2 ms 2588 KB Output is correct
25 Correct 36 ms 4448 KB Output is correct
26 Correct 28 ms 4120 KB Output is correct
27 Correct 15 ms 3156 KB Output is correct
28 Correct 14 ms 3156 KB Output is correct
29 Correct 2023 ms 99532 KB Output is correct
30 Correct 5642 ms 190368 KB Output is correct
31 Correct 1238 ms 61340 KB Output is correct
32 Correct 9 ms 21204 KB Output is correct
33 Correct 11 ms 21200 KB Output is correct
34 Correct 4396 ms 196616 KB Output is correct
35 Correct 4133 ms 196572 KB Output is correct
36 Correct 19 ms 21588 KB Output is correct
37 Correct 1037 ms 60228 KB Output is correct
38 Correct 3867 ms 182788 KB Output is correct
39 Correct 2367 ms 114140 KB Output is correct
40 Correct 1656 ms 86256 KB Output is correct
41 Correct 1725 ms 86248 KB Output is correct
42 Correct 1993 ms 86332 KB Output is correct
43 Runtime error 6 ms 1416 KB Execution killed with signal 11
44 Halted 0 ms 0 KB -