結果

問題 No.1995 CHIKA Road
ユーザー otoshigo
提出日時 2022-07-01 21:55:11
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 298 ms / 2,000 ms
コード長 1,276 bytes
コンパイル時間 2,500 ms
コンパイル使用メモリ 210,664 KB
最終ジャッジ日時 2025-01-30 02:46:11
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for (int i = 0; i < n; i++)
#define rrep(i,n) for (int i = n-1; i >= 0; i--)
#define rep2(i,a,b) for (int i = a; i < b; i++)
#define rrep2(i,a,b) for (int i = a-1; i >= b; i--)
#define rep3(i,a,b,c) for (int i = a; i < b; i+=c)
#define rrep3(i,a,b,c) for (int i = a-1; i >= b; i-=c)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;}
template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;}

int n,m;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>n>>m;
    vector<int>V={0,n-1};
    map<int,vector<int>>G;
    rep(i,m){
        int a,b;cin>>a>>b;--a;--b;
        G[a].push_back(b);
        V.push_back(a);
        V.push_back(b);
    }
    sort(all(V));
    V.erase(unique(all(V)),V.end());
    int l=V.size();
    map<int,int>mp;
    rep(i,l){
        mp[V[i]]=i;
    }
    vector<ll>dp(l,1LL<<60);
    dp[0]=0;
    rep(i,l-1){
        chmin(dp[i+1],dp[i]+2*(V[i+1]-V[i]));
        for(auto b:G[V[i]]){
            int j=mp[b];
            chmin(dp[j],dp[i]+(2*b-2*V[i]-1));
        }
    }
    cout<<dp[l-1]<<"\n";
}
0