結果

問題 No.2332 Make a Sequence
ユーザー KKT89
提出日時 2023-05-28 15:36:43
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,037 bytes
コンパイル時間 2,737 ms
コンパイル使用メモリ 228,008 KB
最終ジャッジ日時 2025-02-13 13:38:26
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 46 WA * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(
            chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

vector<int> Zalgo(vector<int> s){
    int n=s.size();
    vector<int> a(n,0);
    int from=-1,last=-1;
    for(int i=1;i<n;i++){
        int &same=a[i];
        if(from!=-1){
            same=min(a[i-from],last-i);
            same=max(0,same);
        }
        while(i+same<n&&s[same]==s[i+same]){
            same++;
        }
        if(last<i+same){
            last=i+same;
            from=i;
        }
    }
    a[0]=n;
    return a;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,m; cin >> n >> m;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    a.push_back(-1);
    for (int i = 0; i < m; ++i) {
        int x; cin >> x;
        a.push_back(x);
    }
    vector<ll> c(m);
    for (int i = 0; i < m; ++i) {
        cin >> c[i];
    }
    vector<pair<ll,int>> v;
    for (int i = 0; i < m; ++i) {
        v.push_back({c[i], i});
    }
    sort(v.begin(), v.end());
    vector<ll> uo(m,-1);
    set<int> st;
    for (int i = 0; i < m; ++i) {
        st.insert(i);
    }
    auto Z = Zalgo(a);
    for (auto p:v) {
        int i = p.second;
        int len = Z[n+1+i];
        while (true) {
            auto it = st.lower_bound(i+len);
            if (it == st.begin()) break;
            it--;
            if (*it < i) break;
            uo[*it] = p.first; st.erase(it);
        }
    }
    ll cost = 0;
    for (int i = 0; i < m; ++i) {
        ll u = uo[i];
//        cout << u << endl;
        if (u == -1) {
            cost = -1;
            break;
        }
        cost += u;
    }
    cout << cost << endl;
}
0