結果

問題 No.596 郵便配達
ユーザー はむこはむこ
提出日時 2017-10-14 12:19:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 439 ms / 5,000 ms
コード長 2,529 bytes
コンパイル時間 1,275 ms
コンパイル使用メモリ 150,604 KB
実行使用メモリ 85,120 KB
最終ジャッジ日時 2023-08-11 02:10:38
合計ジャッジ時間 4,927 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
14,804 KB
testcase_01 AC 13 ms
14,748 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 159 ms
27,692 KB
testcase_05 AC 439 ms
84,256 KB
testcase_06 AC 36 ms
8,196 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 95 ms
14,744 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 416 ms
85,120 KB
testcase_21 AC 422 ms
84,888 KB
testcase_22 AC 425 ms
84,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)
#define repi(i,a,b) for(long long i = (long long)(a); i < (long long)(b); i++)
#define all(x) (x).begin(), (x).end()
template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

using ll = int; using vll = vector<ll>; using vvll = vector<vll>; using P = pair<ll, ll>;

inline void input(int &v){ v=0;char c=0;int p=1; while(c<'0' || c>'9'){if(c=='-')p=-1;c=getchar();} while(c>='0' && c<='9'){v=(v<<3)+(v<<1)+c-'0';c=getchar();} v*=p; }
static const long long INF = 1e9;

ll dp[2][4][3];
int main(void) {
    ios::sync_with_stdio(false); cin.tie(0);
    ll n, m; input(n), input(m);
    vll pos1(n+5), pos2(n+5);
    ll l = INF, r = -INF;
    rep(i, m) {
        ll x, d; input(x), input(d); chmin(l, x), chmax(r, x); // xは手紙の位置;
        rep(j, d) {
            ll tmp; input(tmp); chmin(l, tmp), chmax(r, tmp);
            if (x > tmp) 
                pos1[tmp]++, pos1[x]--;
            if (x < tmp) 
                pos2[x]++, pos2[tmp]--;
        }
    }

    rep(i, pos1.size()-1) 
        pos1[i+1] += pos1[i], pos2[i+1] += pos2[i];

    ll ret = INF;
    rep(_, 2) {
        vll pos;
        if (_ == 0) 
            pos = pos2;
        else {
            pos = pos1;
            reverse(all(pos));
            l = pos.size() - l;
            r = pos.size() - r;
            swap(l, r);
        }

        rep(i, 2) rep(j, 4) rep(h, 3) dp[i][j][h] = INF;
        dp[l%2][0][1] = 1;
        if (!pos[l]) 
            dp[l%2][0][0] = 0;

        repi(i, l+1, r) {
            ll curr = i % 2, prev = (i+1) % 2;
            rep(j, 4) rep(h, 3) 
                dp[curr][j][h] = INF;

            rep(j, 4) {
                chmin(dp[curr][j][2], dp[prev][j][0] + 2);
                chmin(dp[curr][j][2], dp[prev][j][2] + 2);

                chmin(dp[curr][j][1], dp[prev][j][1] + 1);
                if (j == 0 || j == 1) chmin(dp[curr][(j == 0 ? 2 : 3)][1], dp[prev][j][0] + 1);

                if (!pos[i]) {
                    chmin(dp[curr][j][0], dp[prev][j][0] + 0);
                    chmin(dp[curr][j][0], dp[prev][j][2] + 0);
                    if (j == 0) chmin(dp[curr][1][0], dp[prev][j][1] + 0); 
                }
            }
        }
        rep(j, 4) rep(h, 3) 
            chmin(ret, dp[(r-1)%2][j][h]);
    }
    cout << ret + r - l << endl;

    return 0;
}
0