Submission #61266
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | #include<iostream> #include<vector> #include<cmath> using namespace std; signed main(){ cin.tie(nullptr); ios_base::sync_with_stdio( false ); int n,q; cin>>n>>q; //スポットの数と移動回数の入力 vector< int > x(n+1); vector< int > y(n+1); int i; for (i=1;i<=n;i++){ //スポットの場所入力 cin>>x[i]>>y[i]; } vector< long long > k(q+1); for (i=1;i<=q;i++){ //差の絶対値との比較の基準の入力 cin>>k[i]; } int px; //今いる場所のx座標 int py; //今いる場所のy座標 px=1; py=1; long long manx; //マンハッタン距離のz座標 long long many; //マンハッタン距離のy座標 vector< long long > manm(q+1); //差の絶対値 vector< long long > ans(q+1); //移動距離 long long minx,miny,minm; long long minx2,miny2,temp; int px2,py2; int j; int a=0; for (i=1;i<=q;i++){ manx=x[1]-px; //最初の値を仮定 manx= abs (manx); many=y[1]-py; many= abs (many); manm[i]=manx+many-k[i]; manm[i]= abs (manm[i]); temp=manm[i]; if (manx+many==0){ manm[i]=100000000000LL; } for (j=2;j<=n;j++){ //累積min minx=x[j]-px; minx= abs (minx); miny=y[j]-py; miny= abs (miny); minm=minx+miny-k[i]; minm= abs (minm); if (minx+miny==0){ continue ; } if (manm[i]>minm){ manm[i]=minm; minx2=minx; miny2=miny; px2=x[j]; py2=y[j]; a=0; } else if (manm[i]==minm){ //同じスポットがある場合 a++; } } if (manm[i]==temp){ //移動距離の入力 ans[i]=manx+many; } if (manm[i]!=temp){ ans[i]=minx2+miny2; } if (a!=0){ ans[i]=(px-1)+(py-1); } if (manm[i]==temp){ //場所の移動 px=x[1]; py=y[1]; } if (manm[i]!=temp){ px=px2; py=py2; } if (a!=0){ px=1; py=1; } a=0; } long long sum=0; for (i=1;i<=q;i++){ //移動距離の合計 sum+=ans[i]; } cout<<sum<< "\n" ; return (0); } |
ステータス
項目 | データ |
---|---|
問題 | 1354 - 山田之国 |
ユーザー名 | ei2005 |
投稿日時 | 2020-07-27 16:04:23 |
言語 | C++14 |
状態 | Accepted |
得点 | 50 |
ソースコード長 | 2570 Byte |
最大実行時間 | 32 ms |
最大メモリ使用量 | 784 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 50 / 50 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
in01.txt | AC | 28 ms | 604 KB |
1
|
in02.txt | AC | 18 ms | 420 KB |
1
|
in03.txt | AC | 22 ms | 488 KB |
1
|
in04.txt | AC | 24 ms | 540 KB |
1
|
in05.txt | AC | 20 ms | 580 KB |
1
|
in06.txt | AC | 26 ms | 496 KB |
1
|
in07.txt | AC | 30 ms | 540 KB |
1
|
in08.txt | AC | 24 ms | 464 KB |
1
|
in09.txt | AC | 23 ms | 512 KB |
1
|
in10.txt | AC | 30 ms | 688 KB |
1
|
in11.txt | AC | 21 ms | 608 KB |
1
|
in12.txt | AC | 26 ms | 696 KB |
1
|
in13.txt | AC | 32 ms | 740 KB |
1
|
in14.txt | AC | 24 ms | 784 KB |
1
|
in15.txt | AC | 20 ms | 576 KB |
1
|
in16.txt | AC | 15 ms | 620 KB |
1
|
in17.txt | AC | 15 ms | 420 KB |
1
|
in18.txt | AC | 28 ms | 476 KB |
1
|
in19.txt | AC | 21 ms | 532 KB |
1
|
in20.txt | AC | 22 ms | 588 KB |
1
|
in21.txt | AC | 22 ms | 636 KB |
1
|
sample01.txt | AC | 26 ms | 588 KB |
1
|
sample02.txt | AC | 18 ms | 668 KB |
1
|