Submission #13539241


Source Code Expand

#include<cstdio>
int n;
int dist[510][510];
int occupied[510][510];
int perm[300100];
int q[1200100][2];
int qs, qe;
int getdist(int x, int y) {
	if (x < 0 || y < 0 || x >= n || y >= n)return 0;
	return dist[x][y];
}
void qpush(int x, int y) {
	int cdist = dist[x][y];
	if (cdist > getdist(x + 1, y)) {
		cdist = getdist(x + 1, y);
	}
	if (cdist > getdist(x - 1, y)) {
		cdist = getdist(x - 1, y);
	}
	if (cdist > getdist(x, y + 1)) {
		cdist = getdist(x, y + 1);
	}
	if (cdist > getdist(x, y - 1)) {
		cdist = getdist(x, y - 1);
	}
	cdist += occupied[x][y];
	if (cdist < dist[x][y]) {
		dist[x][y] = cdist;
		q[qe][0] = x;
		q[qe][1] = y;
		qe++;
	}
}
void unoccupy(int x, int y) {
	occupied[x][y] = 0;
	qpush(x, y);
}
void qpoll() {
	int x = q[qs][0];
	int y = q[qs][1];
	qpush(x + 1, y);
	qpush(x - 1, y);
	qpush(x, y + 1);
	qpush(x, y - 1);
	qs++;
}
int main() {
	scanf("%d", &n);
	for (int i = 0; i < n*n; i++) {
		scanf("%d", &perm[i]);
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			occupied[i][j] = 1;
			dist[i][j] = i + 1;
			if (dist[i][j] > j + 1)dist[i][j] = j + 1;
			if (dist[i][j] > n - i)dist[i][j] = n - i;
			if (dist[i][j] > n - j)dist[i][j] = n - j;
		}
	}
	int ans = 0;
	for (int i = 0; i < n*n; i++) {
		int x = (perm[i] - 1) / n;
		int y = (perm[i] - 1) % n;
		qs = 0;
		qe = 0;
		unoccupy(x, y);
		while (qs < qe) {
			qpoll();
		}
		ans += getdist(x, y);
	}
	printf("%d", ans);
	return 0;
}

Submission Info

Submission Time
Task B - Joker
User gs12117
Language C++ (GCC 9.2.1)
Score 700
Code Size 1514 Byte
Status AC
Exec Time 930 ms
Memory 6568 KiB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:48:7: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   48 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
./Main.cpp:50:8: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   50 |   scanf("%d", &perm[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 3
AC × 24
Set Name Test Cases
Sample example0.txt, example1.txt, example2.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, example0.txt, example1.txt, example2.txt
Case Name Status Exec Time Memory
000.txt AC 21 ms 2100 KiB
001.txt AC 62 ms 2648 KiB
002.txt AC 811 ms 6568 KiB
003.txt AC 1 ms 1664 KiB
004.txt AC 1 ms 1696 KiB
005.txt AC 1 ms 1696 KiB
006.txt AC 1 ms 1708 KiB
007.txt AC 1 ms 1692 KiB
008.txt AC 1 ms 1776 KiB
009.txt AC 1 ms 1800 KiB
010.txt AC 1 ms 1736 KiB
011.txt AC 3 ms 1776 KiB
012.txt AC 2 ms 1876 KiB
013.txt AC 24 ms 2244 KiB
014.txt AC 26 ms 2292 KiB
015.txt AC 114 ms 2924 KiB
016.txt AC 906 ms 5256 KiB
017.txt AC 914 ms 4892 KiB
018.txt AC 921 ms 4952 KiB
019.txt AC 921 ms 5156 KiB
020.txt AC 930 ms 5404 KiB
example0.txt AC 1 ms 1700 KiB
example1.txt AC 3 ms 1704 KiB
example2.txt AC 1 ms 1744 KiB