Submission #75708067


Source Code Expand

#include<bits/stdc++.h>
#define int long long
#define inf 1e18
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define per(i,r,l) for(int i=(r);i>=(l);--i)

using namespace std;
int h,w,n;
struct mat{
    int a[4][4];
    mat(){rep(i,0,3)rep(j,0,3)a[i][j]=inf;}
};
mat mul(mat x,mat y){
    mat z;
    rep(i,0,3)rep(j,0,3)rep(k,0,3)z.a[i][j]=min(z.a[i][j],x.a[i][k]+y.a[k][j]);
    return z;
}
mat p[35];
struct vec{
    int a[4];
    vec(){rep(i,0,3)a[i]=inf;}
};
vec mul(vec x,mat y){
    vec z;
    rep(i,0,3)rep(j,0,3)z.a[j]=min(z.a[j],x.a[i]+y.a[i][j]);
    return z;
}
vec adv(vec x,int d){
    if(d>1){
        int q=d-1;
        rep(k,0,30)if((q>>k)&1)x=mul(x,p[k]);
    }
    vec z;
    int add[4]={2,2,2,4};
    rep(i,0,3)z.a[i]=x.a[i]+add[i];
    return z;
}
void get_e(vector<int>&c,int&l,int&r,int&lr){
    c.erase(unique(c.begin(),c.end()),c.end());
    int u=c.front(),v=c.back(),s=c.size();
    l=2*(v-1);r=2*(w-u);lr=min(l,r);
    rep(i,0,s-2)lr=min(lr,2*(c[i]-1)+2*(w-c[i+1]));
    return;
}
signed main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>h>>w>>n;
    vector<pair<int,int>> pts;
    rep(i,1,n){
        int R,C;cin>>R>>C;
        pts.push_back({R,C});
    }
    pts.push_back({1,1});
    sort(pts.begin(),pts.end());
    vector<int> rv;
    vector<vector<int>> cols;
    for(auto&pt:pts){
        if(rv.empty()||rv.back()!=pt.first){
            rv.push_back(pt.first);
            cols.push_back({pt.second});
        }else{
            cols.back().push_back(pt.second);
        }
    }
    int l,r,lr;
    get_e(cols.back(),l,r,lr);
    if(rv.back()==1){
        cout<<l<<'\n';
        return 0;
    }
    int ip[4][4]={
        {2, 2*w, w+1, 2},
        {2*w, 2, w+1, 2},
        {w+1, w+1, 2, w+1},
        {2*w, 2*w, w+1, 4}
    };
    rep(i,0,3)rep(j,0,3)p[0].a[i][j]=ip[i][j];
    rep(i,1,30)p[i]=mul(p[i-1],p[i-1]);
    vec dp;
    dp.a[0]=l;dp.a[1]=r;dp.a[2]=w-1;dp.a[3]=lr;
    int m=rv.size();
    per(i,m-1,1){
        int cur=rv[i],pre=rv[i-1],d=cur-pre;
        dp=adv(dp,d);
        if(pre>1){
            get_e(cols[i-1],l,r,lr);
            int c1=w-1,c2=2*w-2;
            int it[4][4]={
                {l,  c2, c1, lr},
                {c2, r,  c1, lr},
                {c1, c1, lr, c1},
                {c2, c2, c1, lr}
            };
            mat t;
            rep(u,0,3)rep(v,0,3)t.a[u][v]=it[u][v];
            dp=mul(dp,t);
        }else{
            get_e(cols[0],l,r,lr);
            cout<<min({dp.a[0]+l,dp.a[1]+2*w-2,dp.a[2]+w-1,dp.a[3]+2*w-2})<<'\n';
            return 0;
        }
    }
    return 0;
}

Submission Info

Submission Time
Task G - Traveling Door-to-Door Salesman (Stairs)
User Fourier_WJY
Language C++23 (GCC 15.2.0)
Score 0
Code Size 2685 Byte
Status WA
Exec Time 99 ms
Memory 32604 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 700
Status
AC × 2
AC × 40
WA × 15
Set Name Test Cases
Sample 00-sample-01.txt, 00-sample-02.txt
All 00-sample-01.txt, 00-sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 02-m-01.txt, 02-m-02.txt, 02-m-03.txt, 02-m-04.txt, 02-m-05.txt, 02-m-06.txt, 02-m-07.txt, 02-m-08.txt, 02-m-09.txt, 02-m-10.txt, 03-len-01.txt, 03-len-02.txt, 03-len-03.txt, 03-len-04.txt, 03-len-05.txt, 03-len-06.txt, 03-len-07.txt, 03-len-08.txt, 03-len-09.txt, 03-len-10.txt, 04-edge-01.txt, 04-edge-02.txt, 04-edge-03.txt, 04-edge-04.txt, 04-edge-05.txt, 04-edge-06.txt, 04-edge-07.txt, 04-edge-08.txt, 04-edge-09.txt, 05-gapmax-01.txt, 05-gapmax-02.txt, 05-gapmax-03.txt, 05-gapmax-04.txt, 05-gapmax-05.txt, 05-gapmax-06.txt, 05-gapmax-07.txt, 05-gapmax-08.txt, 05-gapmax-09.txt, 05-gapmax-10.txt, 05-gapmax-11.txt, 05-gapmax-12.txt, 05-gapmax-13.txt, 05-gapmax-14.txt, 05-gapmax-15.txt, 05-gapmax-16.txt
Case Name Status Exec Time Memory
00-sample-01.txt AC 1 ms 3644 KiB
00-sample-02.txt AC 1 ms 3444 KiB
01-01.txt AC 1 ms 3528 KiB
01-02.txt AC 52 ms 14436 KiB
01-03.txt AC 54 ms 14320 KiB
01-04.txt AC 59 ms 14420 KiB
01-05.txt AC 92 ms 32552 KiB
01-06.txt WA 13 ms 6936 KiB
01-07.txt WA 12 ms 5640 KiB
01-08.txt WA 97 ms 32604 KiB
02-m-01.txt AC 56 ms 14448 KiB
02-m-02.txt AC 57 ms 13348 KiB
02-m-03.txt AC 58 ms 11892 KiB
02-m-04.txt AC 60 ms 11828 KiB
02-m-05.txt AC 60 ms 11624 KiB
02-m-06.txt AC 58 ms 11636 KiB
02-m-07.txt AC 61 ms 12076 KiB
02-m-08.txt AC 62 ms 11940 KiB
02-m-09.txt WA 64 ms 12644 KiB
02-m-10.txt WA 73 ms 16044 KiB
03-len-01.txt WA 94 ms 32488 KiB
03-len-02.txt WA 78 ms 20868 KiB
03-len-03.txt WA 73 ms 16300 KiB
03-len-04.txt WA 69 ms 15332 KiB
03-len-05.txt AC 61 ms 11692 KiB
03-len-06.txt AC 57 ms 11624 KiB
03-len-07.txt AC 57 ms 11672 KiB
03-len-08.txt AC 58 ms 11696 KiB
03-len-09.txt AC 61 ms 11652 KiB
03-len-10.txt AC 58 ms 11816 KiB
04-edge-01.txt AC 99 ms 32552 KiB
04-edge-02.txt AC 60 ms 11736 KiB
04-edge-03.txt WA 96 ms 32596 KiB
04-edge-04.txt AC 55 ms 11868 KiB
04-edge-05.txt WA 88 ms 20836 KiB
04-edge-06.txt AC 58 ms 11880 KiB
04-edge-07.txt AC 92 ms 32604 KiB
04-edge-08.txt AC 96 ms 32552 KiB
04-edge-09.txt AC 76 ms 20876 KiB
05-gapmax-01.txt AC 58 ms 11624 KiB
05-gapmax-02.txt AC 59 ms 11736 KiB
05-gapmax-03.txt AC 58 ms 11552 KiB
05-gapmax-04.txt AC 58 ms 11632 KiB
05-gapmax-05.txt AC 58 ms 11732 KiB
05-gapmax-06.txt AC 58 ms 11688 KiB
05-gapmax-07.txt WA 58 ms 11564 KiB
05-gapmax-08.txt WA 59 ms 11552 KiB
05-gapmax-09.txt AC 58 ms 11556 KiB
05-gapmax-10.txt AC 59 ms 11608 KiB
05-gapmax-11.txt WA 59 ms 11624 KiB
05-gapmax-12.txt AC 59 ms 11592 KiB
05-gapmax-13.txt WA 29 ms 7652 KiB
05-gapmax-14.txt AC 29 ms 7524 KiB
05-gapmax-15.txt AC 28 ms 7500 KiB
05-gapmax-16.txt AC 29 ms 7452 KiB