結果

問題 No.1029 JJOOII 3
ユーザー ngtkanangtkana
提出日時 2020-04-17 22:46:56
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 661 ms / 2,000 ms
コード長 2,544 bytes
コンパイル時間 2,734 ms
コンパイル使用メモリ 211,276 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 15:03:21
合計ジャッジ時間 11,226 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 21 ms
6,940 KB
testcase_04 AC 27 ms
6,944 KB
testcase_05 AC 205 ms
6,944 KB
testcase_06 AC 203 ms
6,940 KB
testcase_07 AC 189 ms
6,944 KB
testcase_08 AC 225 ms
6,944 KB
testcase_09 AC 190 ms
6,940 KB
testcase_10 AC 200 ms
6,944 KB
testcase_11 AC 233 ms
6,944 KB
testcase_12 AC 174 ms
6,944 KB
testcase_13 AC 200 ms
6,940 KB
testcase_14 AC 215 ms
6,944 KB
testcase_15 AC 3 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 168 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 100 ms
6,944 KB
testcase_21 AC 95 ms
6,944 KB
testcase_22 AC 113 ms
6,940 KB
testcase_23 AC 87 ms
6,944 KB
testcase_24 AC 100 ms
6,944 KB
testcase_25 AC 111 ms
6,940 KB
testcase_26 AC 159 ms
6,940 KB
testcase_27 AC 204 ms
6,940 KB
testcase_28 AC 172 ms
6,940 KB
testcase_29 AC 149 ms
6,940 KB
testcase_30 AC 193 ms
6,944 KB
testcase_31 AC 172 ms
6,940 KB
testcase_32 AC 661 ms
6,944 KB
testcase_33 AC 654 ms
6,944 KB
testcase_34 AC 654 ms
6,940 KB
testcase_35 AC 653 ms
6,940 KB
testcase_36 AC 651 ms
6,940 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 3 ms
6,944 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define ALL(v) std::begin(v),std::end(v)
using lint=long long;
using ld=long double;
template<class T>using numr=std::numeric_limits<T>;
void cmn(lint&x,lint y){if(x>y)x=y;}
int main(){
    std::cin.tie(nullptr);std::ios_base::sync_with_stdio(false);
    std::cout.setf(std::ios_base::fixed);std::cout.precision(15);
    lint n,V;std::cin>>n>>V;
    std::vector<std::vector<std::vector<lint>>>a(n,std::vector<std::vector<lint>>(3));
    std::vector<lint>sz(n),ws(n);
    for(lint i=0;i<n;i++){
        std::string s;std::cin>>s>>ws.at(i);
        sz.at(i)=s.length();
        for(lint j=0;j<3;j++){
            a.at(i).at(j).resize(sz.at(i)+1);
            for(lint k=0;k<sz.at(i);k++){
                a.at(i).at(j).at(k+1)=s.at(k)==std::string("JOI").at(j);
            }
            std::partial_sum(ALL(a.at(i).at(j)),a.at(i).at(j).begin());
        }
    }
    auto count=[&](lint i,lint j,lint l,lint r)->lint{
        return a.at(i).at(j).at(r)-a.at(i).at(j).at(l);
    };
    for(lint j=0;j<3;j++){
        lint sum=0;
        for(lint i=0;i<n;i++){
            sum+=count(i,j,0,sz.at(i));
        }
        if(sum==0){
            std::cout<<-1<<'\n';
            return 0;
        }
    }
    lint inf=numr<lint>::max();
    std::vector<std::vector<lint>>dp(3,std::vector<lint>(V+1,inf));
    for(lint j=0;j<3;j++){
        auto&&d=dp.at(j);
        d.at(0)=0;
        for(lint i=0;i<n;i++){
            lint v=count(i,j,0,sz.at(i)),w=ws.at(i);
            if(v==0)continue;
            for(lint k=0;k<V;k++){
                if(d.at(k)==inf)continue;
                cmn(d.at(std::min(V,k+v)),d.at(k)+w);
            }
            for(lint k=V-1;k>=0;k--){
                cmn(d.at(k),d.at(k+1));
            }
        }
    }
    lint ans=inf;
    for(lint i=0;i<n;i++)for(lint j=0;j<n;j++){
        for(lint k=0;k<sz.at(i);k++)for(lint l=0;l<sz.at(j);l++){
            cmn(ans,
                ws.at(i) + ws.at(j)
                + dp.at(0).at(std::max(0ll,V-count(i,0,0,k)))
                + dp.at(1).at(std::max(0ll,V-count(i,1,k,sz.at(i))-count(j,1,0,l)))
                + dp.at(2).at(std::max(0ll,V-count(j,2,l,sz.at(j)))));
        }
    }
    for(lint i=0;i<n;i++){
        for(lint k=0;k<sz.at(i);k++)for(lint l=k+1;l<sz.at(i);l++){
            if(count(i,1,k,l)<V)continue;
            cmn(ans,
                ws.at(i)
                + dp.at(0).at(std::max(0ll,V-count(i,0,0,k)))
                + dp.at(2).at(std::max(0ll,V-count(i,2,l,sz.at(i)))));
        }
    }
    std::cout<<ans<<'\n';
}
0