結果

問題 No.596 郵便配達
ユーザー はむこはむこ
提出日時 2017-10-14 12:23:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 482 ms / 5,000 ms
コード長 2,665 bytes
コンパイル時間 1,476 ms
コンパイル使用メモリ 150,444 KB
実行使用メモリ 85,244 KB
最終ジャッジ日時 2023-08-11 02:10:47
合計ジャッジ時間 4,845 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
14,808 KB
testcase_01 AC 12 ms
14,704 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 166 ms
27,760 KB
testcase_05 AC 482 ms
84,148 KB
testcase_06 AC 38 ms
8,264 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 97 ms
14,932 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 433 ms
85,244 KB
testcase_21 AC 436 ms
85,080 KB
testcase_22 AC 434 ms
85,040 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); assert(1<=n&&n<=7e6); assert(1<=m&&m<=5e5);
    vll pos1(n+5), pos2(n+5);
    ll l = INF, r = -INF;

    ll dsum = 0;
    rep(i, m) {
        ll x, d; input(x), input(d); assert(0<=x&&x<n); assert(1<=d); dsum+=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]--;
        }
    }
    assert(1<=dsum&&dsum<=1e6);

    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