답안 #803785

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
803785 2023-08-03T06:00:48 Z 박상훈(#10101) Shifty Grid (CCO17_shifty) C++17
15 / 25
46 ms 1612 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int n, m;

int a[101][101], b[101][101];
pair<int, int> pos[100100];
vector<array<int, 3>> Q;

void shiftR(int i, int c){
	assert(0 <= c && c < m);
	for (int j=0;j<m;j++) b[i][(j+c)%m] = a[i][j];
	for (int j=0;j<m;j++){
		a[i][j] = b[i][j];
		pos[a[i][j]] = {i, j};
	} 

	Q.push_back({1, i+1, c});
}

void shiftC(int j, int c){
	assert(0 <= c && c < n);
	for (int i=0;i<n;i++) b[(i+c)%n][j] = a[i][j];
	for (int i=0;i<n;i++){
		a[i][j] = b[i][j];
		pos[a[i][j]] = {i, j};
	}

	Q.push_back({2, j+1, c});
}

void solveR(int p){
	for (int j=0;j<m;j++){
		auto [cx, cy] = pos[p*m + j];
		if (cx==p && cy==j) continue;
		if (cx==p){
			shiftC(j, 1);
			shiftC(cy, 1);
			shiftR(p+1, (m+j-cy) % m);
			shiftC(j, n-1);
			shiftC(cy, n-1);
		}

		else if (cy==j){
			shiftR(cx, 1);
			shiftC(j, (n+cx-p) % n);
			shiftR(cx, m-1);
			shiftC(j, (n+p-cx) % n);
		}

		else{
			shiftC(j, (n+cx-p) % n);
			shiftR(cx, (m+j-cy) % m);
			shiftC(j, (n+p-cx) % n);
		}
	}

	// printf("ok\n");
	// for (int i=0;i<n;i++){
	// 	for (int j=0;j<m;j++) printf("%d ", a[i][j]);
	// 	printf("\n");
	// }
}

void swap2(int p, int q){
	shiftC(p, 1);
	shiftR(0, (m+q-p) % m);
	shiftC(q, 1);
	shiftR(0, (m+p-q) % m);
	shiftC(p, 1);
}

void sort2(){
	for (int j=0;j<m;j++){
		auto [cx, cy] = pos[m+j];
		if (cy==j) continue;
		swap2(j, cy);
	}
}

void swapn(int p, int q){
	for (int i=0;i<n;i++){
		if (i%2==0){
			shiftC(q, m-1);
			shiftR(n-1, (m+q-p) % n);
		}

		else{
			shiftC(q, m-1);
			shiftR(n-1, (m+p-q) % n);
		}
	}

	shiftC(q, m-1);

}

void sortn(){
	for (int j=0;j<m;j++){
		auto [cx, cy] = pos[(n-1)*m+j];
		if (cy==j) continue;
		swapn(j, cy);
	}
}

void solve(){
	Q.clear();
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++) pos[a[i][j]] = {i, j};
	}

	for (int i=0;i<n-1;i++) solveR(i);
	if (m==2){
		if (a[n-1][0] > a[n-1][1]) shiftR(n-1, 1);
	}
	else if (n==2) sort2();
	else sortn();
}

mt19937 seed(1557);
uniform_int_distribution<int> rng(0, 2147483647);
int getrand(int l, int r){return rng(seed)%(r-l+1) + l;}

vector<array<int, 3>> X;

void gen(){
	n = 6, m = 6;
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++) a[i][j] = i*m+j;
	}

	X.clear();
	Q.clear();
	for (int z=0;z<2;z++){
		int typ = getrand(0, 1);
		if (typ==0){
			shiftR(getrand(0, n-1), getrand(0, m-1));
		}

		else{
			shiftC(getrand(0, m-1), getrand(0, n-1));
		}
	}

	X = Q;
}

set<vector<int>> st;
int stress(int tc){
	printf("----------------------------------\n");
	printf("Stress #%d\n", tc);
	gen();

	printf("Input:\n");
	printf("%d %d\n", n, m);
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++) printf("%d ", a[i][j]);
		printf("\n");
	}
	printf("\n");

	solve();

	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++) if (a[i][j]!=i*m+j){
			printf("Failed:\n");

			for (int i=0;i<n;i++){
				for (int j=0;j<m;j++) printf("%d ", a[i][j]);
				printf("\n");
			}

			// vector<int> V;
			// for (int j=0;j<m;j++) V.push_back(a[i][j]);

			// for (int j=0;j<m-1;j++){
			// 	swap(a[i][j], a[i][j+1]);
			// 	if (is_sorted(a[i], a[i]+m)) return 1;
			// 	swap(a[i][j], a[i][j+1]);
			// }
			// st.insert(V);

			assert(0);
		}
	}

	printf("Accepted: %d\n", (int)Q.size());
	return 0;
}

void simulate(int n, int m, const vector<array<int, 3>> &Q){
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++) a[i][j] = i*m + j;
	}

	for (auto &[x, y, z]:Q){
		if (x==1) shiftR(y-1, z);
		else shiftC(y-1, z);

		printf("------------------------\n");
		printf("%d %d %d\n", x, y, z);
		for (int i=0;i<n;i++){
			for (int j=0;j<m;j++) printf("%2d ", a[i][j]);
			printf("\n");
		}
	}

	printf("ok %d\n", (int)Q.size());
}

int main(){
	// for (int i=1;i<=100;i++) stress(i);

	// int cnt = 1;
	// while(true){
	// 	if (stress(cnt++)) break;
	// }

	// printf("ok %d + %d generated\n", (int)X.size(), (int)Q.size());
	// for (auto &x:Q) X.push_back(x);

	// simulate(6, 6, X);
	// for (auto &V:st){
	// 	for (auto &x:V) printf("%d ", x);
	// 	printf("\n");
	// }

	scanf("%d %d", &n, &m);
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			scanf("%d", a[i]+j);
			pos[a[i][j]] = {i, j};
		} 
	}

	solve();

	// printf("ok done\n");
	// for (int i=0;i<n;i++){
	// 	for (int j=0;j<m;j++) printf("%d ", a[i][j]);
	// 	printf("\n");
	// }

	printf("%d\n", (int)Q.size());
	for (auto &[x, y, z]:Q) printf("%d %d %d\n", x, y, z);
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:230:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  230 |  scanf("%d %d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:233:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  233 |    scanf("%d", a[i]+j);
      |    ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 23 ms 1048 KB Output is correct
7 Correct 26 ms 1184 KB Output is correct
8 Correct 21 ms 1044 KB Output is correct
9 Correct 2 ms 380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 23 ms 1048 KB Output is correct
19 Correct 26 ms 1184 KB Output is correct
20 Correct 21 ms 1044 KB Output is correct
21 Correct 2 ms 380 KB Output is correct
22 Correct 16 ms 976 KB Output is correct
23 Correct 46 ms 1612 KB Output is correct
24 Correct 43 ms 1504 KB Output is correct
25 Correct 33 ms 1352 KB Output is correct
26 Correct 23 ms 1124 KB Output is correct
27 Correct 44 ms 1580 KB Output is correct
28 Correct 43 ms 1440 KB Output is correct
29 Correct 43 ms 1480 KB Output is correct
30 Correct 42 ms 1560 KB Output is correct
31 Correct 40 ms 1528 KB Output is correct
32 Correct 39 ms 1488 KB Output is correct
33 Runtime error 4 ms 920 KB Execution killed with signal 6
34 Halted 0 ms 0 KB -