結果

問題 No.1028 闇討ち
ユーザー furafura
提出日時 2020-05-12 05:58:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 718 bytes
コンパイル時間 1,830 ms
コンパイル使用メモリ 202,436 KB
実行使用メモリ 12,240 KB
最終ジャッジ日時 2023-09-27 11:05:42
合計ジャッジ時間 5,696 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 6 ms
4,380 KB
testcase_10 AC 8 ms
4,380 KB
testcase_11 AC 29 ms
5,420 KB
testcase_12 AC 125 ms
11,796 KB
testcase_13 AC 123 ms
11,672 KB
testcase_14 AC 68 ms
9,512 KB
testcase_15 AC 23 ms
5,096 KB
testcase_16 AC 135 ms
11,932 KB
testcase_17 AC 131 ms
12,236 KB
testcase_18 AC 131 ms
11,932 KB
testcase_19 AC 135 ms
11,932 KB
testcase_20 AC 143 ms
12,240 KB
testcase_21 AC 134 ms
11,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const int INF=1<<29;

int main(){
	int n; scanf("%d",&n);
	vector<pair<int,int>> P[1000];
	rep(i,n) rep(j,n) {
		int a; scanf("%d",&a); a--;
		P[a].emplace_back(i,j);
	}

	auto calc=[&](int a,int i){
		int cost=0;
		for(const auto& p:P[a]) cost+=max(abs(p.first-i),p.second);
		return cost;
	};

	int ans=0;
	rep(a,n){
		int lo=0,hi=n;
		while(hi-lo>2){
			int mi1=(2*lo+hi)/3;
			int mi2=(lo+2*hi)/3;

			int cost1=calc(a,mi1);
			int cost2=calc(a,mi2);
			if(cost1<=cost2) hi=mi2;
			else             lo=mi1;
		}
		int res=INF;
		for(int i=lo;i<hi;i++) res=min(res,calc(a,i));
		ans+=res;
	}
	printf("%d\n",ans);

	return 0;
}
0