結果

問題 No.2759 Take Pictures, Elements?
ユーザー GOTKAKOGOTKAKO
提出日時 2024-05-17 21:46:28
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,091 bytes
コンパイル時間 2,321 ms
コンパイル使用メモリ 213,280 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-12-20 13:30:25
合計ジャッジ時間 3,248 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12 WA * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N,Q; cin >> N >> Q;
    map<int,vector<int>> rev;
    for(int i=0; i<N; i++){
        int a; cin >> a;
        rev[a].push_back(i);
    }
    int inf = 1e9;
    vector<int> dp(N,inf); dp.at(0) = 0;
    while(Q--){
        int b; cin >> b;
        vector<int> &ap = rev[b],next(N,inf);
        int pos = 0;
        for(int i=0; i<ap.size()-1; i++){
            int pos1 = ap.at(i),pos2 = ap.at(i+1);
            while(pos < pos2){
                next.at(pos) = min(dp.at(pos1)+abs(pos1-pos),dp.at(pos2)+abs(pos2-pos));
                pos++;
            }
        }
        {
            int apos = ap.back();
            for(int i=0; i<N; i++) next.at(apos) = min(next.at(apos),dp.at(i)+abs(apos-i));
        }
        for(int i=1; i<N; i++) next.at(i) = min(next.at(i),next.at(i-1)+1);
        for(int i=N-2; i>=0; i--) next.at(i) = min(next.at(i),next.at(i+1)+1);
        
        swap(dp,next);
    }

    cout << *min_element(dp.begin(),dp.end()) << endl;
}
0