답안 #926967

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
926967 2024-02-14T05:59:16 Z MtSaka Chorus (JOI23_chorus) C++17
100 / 100
2383 ms 118392 KB
#include<bits/stdc++.h>
#define overload(a,b,c,d,...) d
#define rep1(a) for(ll _=0;_<(ll)a;++_)
#define rep2(i,n) for(ll i=0;i<(ll)n;++i)
#define rep3(i,l,r) for(ll i=(ll)l;i<(ll)r;++i)
#define rep(...) overload(__VA_ARGS__,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep1(i,a) for(ll i=(ll)a-1;i>=0;--i)
#define rrep2(i,a,b) for(ll i=(ll)b-1;i>=(ll)a;--i)
#define rrep(...) overload(__VA_ARGS__,rrep2,rrep1)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pb push_back
#define eb emplace_back
using namespace std;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
template<typename T,typename U>inline bool chmin(T&a,const U&b){return (a>b?a=b,true:false);}
template<typename T,typename U>inline bool chmax(T&a,const U&b){return (a<b?a=b,true:false);}
static constexpr ll mod1=998244353;
static constexpr ll mod=1000000007;
static constexpr ll inf=numeric_limits<ll>::max()/2;
using vl=vector<ll>;
struct CHT{
    private:
    deque<array<ll,3>>dq;
    ll floor_div(ll x,ll y){
        if(x>=0)return x/y;
        return x/y-(x%y?1:0);
    }
    bool need(array<ll,3>l1,array<ll,3>l2,array<ll,3>add){
        return floor_div(l2[1]-l1[1],l1[0]-l2[0])<floor_div(add[1]-l2[1],l2[0]-add[0]);
    }
    public:
    CHT():dq(){}
    void add(ll a,ll b,ll id){
        array<ll,3>l{a,b,id};
        while(dq.size()>=2&&!need(dq[dq.size()-2],dq[dq.size()-1],l)){
            dq.pop_back();
        }
        dq.emplace_back(l);
    }
    pair<ll,ll>get(ll x){
        if(dq.empty())return {inf,0};
        while(dq.size()>=2&&dq[0][0]*x+dq[0][1]>dq[1][0]*x+dq[1][1]){
            dq.pop_front();
        }
        return pair<ll,ll>{dq[0][0]*x+dq[0][1],dq[0][2]};
    }
};
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n,k;cin>>n>>k;
    string s;cin>>s;
    vl ida;
    ida.reserve(n);
    rep(i,2*n){
        if(s[i]=='A')ida.eb(i);
    }
    rep(i,n)ida[i]-=i;
    ll id=0;
    vl idx(n+1);
    rep(i,n+1){
        while(id<n&&ida[id]<i)id++;
        idx[i]=id;
    }
    vl suma(n+1);
    rep(i,n)suma[i+1]=suma[i]+ida[i];
    vector<vl>v(n+1);
    rep(i,n+1){
        v[max(i,idx[i])].eb(i);
    }
    set<ll>st;
    vl bef(n+1,-1);
    rep(i,n){
        st.insert(i);
        for(auto j:v[i])st.erase(j);
        if(st.size())bef[i+1]=*st.begin();
    }
    //cerr<<ida.size()<<" "<<idb.size()<<endl;
    auto f=[&](ll lambda)->pair<ll,ll> {
        vector<pair<ll,ll>>dp(n+1,{inf,inf});
        dp[0]={0,0};
        CHT cht;
        rep(i,1,n+1){
            for(auto j:v[i-1]){
                cht.add(-j,j*(i-1)-suma[i-1]+dp[j].first,dp[j].second);
            }
            auto mi=cht.get(i);mi.second++;mi.first+=suma[i]+lambda;
            chmin(dp[i],mi);
            if(bef[i]!=-1)chmin(dp[i],pair<ll,ll>{dp[bef[i]].first+lambda,dp[bef[i]].second+1});
        }
        return dp[n];
    };
    ll l=-1,r=n*(n-1)/2+1;
    while(r-l>1){
        ll mid=(l+r)/2;
        auto tmp=f(mid);
        if(tmp.second>k)l=mid;
        else r=mid;
    }
    //cerr<<l<<" "<<r<<endl;
    auto b=f(r);
    cout<<b.first-k*r<<endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 452 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 452 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 452 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 452 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 3 ms 496 KB Output is correct
23 Correct 1 ms 452 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 452 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 452 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 3 ms 496 KB Output is correct
23 Correct 1 ms 452 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 8 ms 860 KB Output is correct
30 Correct 7 ms 984 KB Output is correct
31 Correct 7 ms 860 KB Output is correct
32 Correct 4 ms 1056 KB Output is correct
33 Correct 5 ms 860 KB Output is correct
34 Correct 6 ms 860 KB Output is correct
35 Correct 5 ms 860 KB Output is correct
36 Correct 8 ms 860 KB Output is correct
37 Correct 8 ms 988 KB Output is correct
38 Correct 5 ms 1052 KB Output is correct
39 Correct 6 ms 860 KB Output is correct
40 Correct 5 ms 860 KB Output is correct
41 Correct 5 ms 860 KB Output is correct
42 Correct 3 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 452 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 452 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 3 ms 496 KB Output is correct
23 Correct 1 ms 452 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 8 ms 860 KB Output is correct
30 Correct 7 ms 984 KB Output is correct
31 Correct 7 ms 860 KB Output is correct
32 Correct 4 ms 1056 KB Output is correct
33 Correct 5 ms 860 KB Output is correct
34 Correct 6 ms 860 KB Output is correct
35 Correct 5 ms 860 KB Output is correct
36 Correct 8 ms 860 KB Output is correct
37 Correct 8 ms 988 KB Output is correct
38 Correct 5 ms 1052 KB Output is correct
39 Correct 6 ms 860 KB Output is correct
40 Correct 5 ms 860 KB Output is correct
41 Correct 5 ms 860 KB Output is correct
42 Correct 3 ms 860 KB Output is correct
43 Correct 103 ms 6484 KB Output is correct
44 Correct 177 ms 10716 KB Output is correct
45 Correct 180 ms 10464 KB Output is correct
46 Correct 109 ms 12260 KB Output is correct
47 Correct 121 ms 12264 KB Output is correct
48 Correct 125 ms 12264 KB Output is correct
49 Correct 133 ms 12296 KB Output is correct
50 Correct 206 ms 10608 KB Output is correct
51 Correct 207 ms 10464 KB Output is correct
52 Correct 134 ms 12260 KB Output is correct
53 Correct 137 ms 12260 KB Output is correct
54 Correct 188 ms 10724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 452 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 452 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 3 ms 496 KB Output is correct
23 Correct 1 ms 452 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 8 ms 860 KB Output is correct
30 Correct 7 ms 984 KB Output is correct
31 Correct 7 ms 860 KB Output is correct
32 Correct 4 ms 1056 KB Output is correct
33 Correct 5 ms 860 KB Output is correct
34 Correct 6 ms 860 KB Output is correct
35 Correct 5 ms 860 KB Output is correct
36 Correct 8 ms 860 KB Output is correct
37 Correct 8 ms 988 KB Output is correct
38 Correct 5 ms 1052 KB Output is correct
39 Correct 6 ms 860 KB Output is correct
40 Correct 5 ms 860 KB Output is correct
41 Correct 5 ms 860 KB Output is correct
42 Correct 3 ms 860 KB Output is correct
43 Correct 103 ms 6484 KB Output is correct
44 Correct 177 ms 10716 KB Output is correct
45 Correct 180 ms 10464 KB Output is correct
46 Correct 109 ms 12260 KB Output is correct
47 Correct 121 ms 12264 KB Output is correct
48 Correct 125 ms 12264 KB Output is correct
49 Correct 133 ms 12296 KB Output is correct
50 Correct 206 ms 10608 KB Output is correct
51 Correct 207 ms 10464 KB Output is correct
52 Correct 134 ms 12260 KB Output is correct
53 Correct 137 ms 12260 KB Output is correct
54 Correct 188 ms 10724 KB Output is correct
55 Correct 1342 ms 66492 KB Output is correct
56 Correct 1863 ms 110752 KB Output is correct
57 Correct 2137 ms 102388 KB Output is correct
58 Correct 1337 ms 118392 KB Output is correct
59 Correct 1518 ms 118260 KB Output is correct
60 Correct 1536 ms 118392 KB Output is correct
61 Correct 1508 ms 118256 KB Output is correct
62 Correct 2369 ms 102428 KB Output is correct
63 Correct 2383 ms 102620 KB Output is correct
64 Correct 1595 ms 118152 KB Output is correct
65 Correct 1547 ms 118256 KB Output is correct
66 Correct 2292 ms 104944 KB Output is correct
67 Correct 1534 ms 118260 KB Output is correct