結果

問題 No.1028 闇討ち
ユーザー fura
提出日時 2020-05-12 05:58:42
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 190 ms / 2,000 ms
コード長 718 bytes
コンパイル時間 2,522 ms
コンパイル使用メモリ 195,916 KB
最終ジャッジ日時 2025-01-10 10:34:49
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:10:21: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   10 |         int n; scanf("%d",&n);
      |                ~~~~~^~~~~~~~~
main.cpp:13:29: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   13 |                 int a; scanf("%d",&a); a--;
      |                        ~~~~~^~~~~~~~~

ソースコード

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