답안 #935113

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
935113 2024-02-28T16:49:11 Z kim Chorus (JOI23_chorus) C++17
0 / 100
1 ms 8540 KB
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
using pii=pair<int,int>;
#define f first
#define s second
#define eb emplace_back

const ll inf=1e18;

int a[1000005],b[1000005],pos[1000005],pos2[1000005];
ll qs[1000005];

struct line{
    ll m,c;
    line(ll m,ll c):m(m),c(c){}
    ll operator()(const ll &x)const{return m*x+c;}
};
struct cht{
    deque<line> dq;
    bool check(line a,line b,line c){
        return (a.c-b.c)*(a.m-c.m)>=(c.c-a.c)*(b.m-a.m);
    }
    void insert(ll m,ll c){
        line f(m,c);
        while(dq.size()>1&&check(dq.end()[-2],dq.back(),f)) dq.pop_back();
        dq.eb(f);
    }
    ll qr(ll x){
        if(dq.empty()) return inf;
        while(dq.size()>1&&dq.front()(x)>dq[1](x)) dq.pop_front();
        return dq.front()(x);
    }
    void clear(){dq.clear();}
}dp;
vector<pair<int,ll>> vec;
/*
dp[R]=dp[L]+f(L+1,R);

pos2[L+1]>R :   dp[R]=dp[L];
        <=R :   dp[R]= -L*R   +dp[L]-qs[pos2[L+1]-1]+pos2[L+1]*(L+1)-L   +(qs[R]-R-1);

*/
void play(int n,bool z){
    dp.clear();
    vector<pair<int,ll>> vec2(1,{0,0});
    queue<pair<int,ll>> q;
    q.emplace(0,0);
    for(int i=1;i<=n;++i){
        while(q.size()&&pos2[q.front().f+1]<=i){
            auto [j,x]=q.front(); q.pop();
            dp.insert(-j,x-qs[pos2[j+1]-1]+pos2[j+1]*(j+1)-j);
        }
        ll val=dp.qr(i)+qs[i]-i-1;
        if(q.size()) val=min(val,q.front().s);
        q.emplace(vec[i]);
        vec[i].s=val;
    }
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0);

    int n,K; cin>>n>>K;
    string s; cin>>s;
    for(int i=0,ia=0,ib=0;i<s.length();++i){
        if(s[i]=='A') a[++ia]=i;
        else b[++ib]=i;
    }
    for(int i=1,j=1;i<=n;++i){
        while(j<=n&&b[j]<a[i]) ++j;
        pos[i]=j;
        qs[i]=qs[i-1]+j;
    }
    for(int i=1,j=1;i<=n;++i){
        while(j<=n&&pos[j]<=i) ++j;
        j=max(i,j);
        pos2[i]=j;
    }

    vec.eb(0,0);
    for(int i=1;i<=n;++i) vec.eb(i,qs[i]-i);
    for(int k=2;k<=K;++k) play(n,k&1);

    cout<<vec[n].s;

    return 0;
}
/*

for(int i=1;i<=n;++i){
    for(int j=i;j<=n;++j){
        cost[i][j]=cost[i][j-1]+max(0,pos[j]-i);
    }
}


70,45,37,33

*/

Compilation message

chorus.cpp: In function 'int main()':
chorus.cpp:67:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |     for(int i=0,ia=0,ib=0;i<s.length();++i){
      |                           ~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Incorrect 1 ms 8540 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Incorrect 1 ms 8540 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Incorrect 1 ms 8540 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Incorrect 1 ms 8540 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Incorrect 1 ms 8540 KB Output isn't correct
5 Halted 0 ms 0 KB -