Skip to content

[Week 6] Solved SORTGAME - Choi-mina#194

Merged
Queue-ri merged 1 commit intomainfrom
week6-sortgame
Mar 13, 2022
Merged

[Week 6] Solved SORTGAME - Choi-mina#194
Queue-ri merged 1 commit intomainfrom
week6-sortgame

Conversation

@Choi-mina
Copy link
Copy Markdown
Collaborator

// ๋’ค์ง‘๊ธฐ ํšŸ์ˆ˜ ๊ณ„์‚ฐ
void bfs(int n) {
	vector<int> p(n);
  // ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”
	for (int i = 0; i < n; i++)
		p[i] = i;
	queue<vector<int>> q;
	q.push(p);
	sortt[p] = 0;
	while (!q.empty()) {
		vector<int> here = q.front();
		q.pop();
		// ํ˜„์žฌ ๋น„์šฉ
		int cost = sortt[here];
		// ๋ชจ๋“  ๋’ค์ง‘๊ธฐ ์ƒํ™ฉ ๊ณ ๋ ค
		for (int i = 0; i < n; i++)
			for (int j = i + 2; j <= n; j++) {
				reverse(here.begin() + i, here.begin() + j);
				// ๋ฐฐ์—ด์„ ๋’ค์ง‘์—ˆ์„๋•Œ ์•„์ง ๊ณ„์‚ฐ์ด ์•ˆ๋œ ๋ฐฐ์—ด์ด๋ฉด
				if (sortt.count(here) == 0) {
					// ํ˜„์žฌ ๋ฐฐ์—ด ๋น„์šฉ์— +1
					sortt[here] = cost + 1;
					q.push(here);
				}
				// ๋ฐฐ์—ด ์›์ƒ๋ณต๊ตฌ
				reverse(here.begin() + i, here.begin() + j);
			}
	}
}

๋’ค์ง‘๊ธฐ ์—ฐ์‚ฐ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
i๋Š” ๋’ค์ง‘๋Š” ์˜์—ญ์˜ ์‹œ์ž‘์ด๊ณ  j๋Š” ๋’ค์ง‘๋Š” ์˜์—ญ ๊ธธ์ด์ž…๋‹ˆ๋‹ค.
sortt.count(here) == 0์„ ์‚ฌ์šฉํ•ด์„œ ๊ณ„์‚ฐ์ด ์•ˆ๋œ ๋ถ€๋ถ„(๋ฐฉ๋ฌธX)์„ ํ™•์ธํ•˜๊ณ  ๊ณ„์‚ฐ์ด ์•ˆ๋˜์—ˆ๋‹ค๋ฉด ํ˜„์žฌ ๋ฐฐ์—ด ๋น„์šฉ์— +1์„ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

int solve(const vector<int>& p) {
	int n = p.size();
  // p์„ [0, ..., n-1]์˜ ์ˆœ์—ด๋กœ ๋ณ€ํ™˜
	vector<int> fixed(n);
	for (int i = 0; i < n; i++) {
		int small = 0;
		for (int j = 0; j < n; j++)
			if (p[j] < p[i])
				small++;
		fixed[i] = small;
	}
	return sortt[fixed];
}

bfs ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์ด์šฉํ•ด ๋’ค์ง‘๊ธฐ ์—ฐ์‚ฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
๋ฐฐ์—ด์„ 0~n-1 ์‚ฌ์ด์˜ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์ •๋ ฌํ•˜๊ธฐ๊นŒ์ง€์˜ ์—ฐ์‚ฐ ํšŸ์ˆ˜ ๋ฅผ ๋ฐ˜ํ™˜ํ–ˆ์Šต๋‹ˆ๋‹ค.

Closes #186

@Choi-mina Choi-mina requested a review from Queue-ri March 12, 2022 13:26
@Choi-mina Choi-mina self-assigned this Mar 12, 2022
@github-actions
Copy link
Copy Markdown

AOJI Report

โœ” Your code has been accepted!

๐ŸŽฏ AOJ Result

Judge ID Problem ID Language Result Performance
741801 SORTGAME cpp ์ •๋‹ต 844ms

โš  Performance doesn't match. (ยฑ4ms might be a temporary AOJ error)

๐ŸŽฏ Tested Commit

Hash Language File Path Performance
907c002 cpp STU/SORTGAME/Choi-mina.cpp 700ms

๐ŸŽฏ Submission Log

Click here to extend
Request to https://algospot.com/accounts/login/?next=/
<Response [200]>

// AOJI UUID: 43c8980a-b239-486d-868d-97a4c1104437
// This code was submitted by AOJI from https://github.com/Queue-ri/Advanced-Algorithm-Study.
// Please contact [email protected] if there's any problem.

#include<iostream>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
// sortt[vector] = vector๋ฅผ ์ •๋ ฌ์‹œํ‚ค๋Š”๋ฐ ํ•„์š”ํ•œ ๋’ค์ง‘๊ธฐ ํšŸ์ˆ˜
map<vector<int>, int> sortt;

// ๋’ค์ง‘๊ธฐ ํšŸ์ˆ˜ ๊ณ„์‚ฐ
void bfs(int n) {
	vector<int> p(n);
  // ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”
	for (int i = 0; i < n; i++)
		p[i] = i;
	queue<vector<int>> q;
	q.push(p);
	sortt[p] = 0;
	while (!q.empty()) {
		vector<int> here = q.front();
		q.pop();
		// ํ˜„์žฌ ๋น„์šฉ
		int cost = sortt[here];
		// ๋ชจ๋“  ๋’ค์ง‘๊ธฐ ์ƒํ™ฉ ๊ณ ๋ ค
		for (int i = 0; i < n; i++)
			for (int j = i + 2; j <= n; j++) {
				reverse(here.begin() + i, here.begin() + j);
				// ๋ฐฐ์—ด์„ ๋’ค์ง‘์—ˆ์„๋•Œ ์•„์ง ๊ณ„์‚ฐ์ด ์•ˆ๋œ ๋ฐฐ์—ด์ด๋ฉด
				if (sortt.count(here) == 0) {
					// ํ˜„์žฌ ๋ฐฐ์—ด ๋น„์šฉ์— +1
					sortt[here] = cost + 1;
					q.push(here);
				}
				// ๋ฐฐ์—ด ์›์ƒ๋ณต๊ตฌ
				reverse(here.begin() + i, here.begin() + j);
			}
	}
}

int solve(const vector<int>& p) {
	int n = p.size();
  // p์„ [0, ..., n-1]๋กœ ๋ณ€ํ™˜
	vector<int> fixed(n);
	for (int i = 0; i < n; i++) {
		int small = 0;
		for (int j = 0; j < n; j++)
			if (p[j] < p[i])
				small++;
		fixed[i] = small;
	}
	return sortt[fixed];
}

int main() {
	for (int i = 1; i <= 8; i++)
		bfs(i);
	int C;
	cin >> C;
	while (C--) {
		int N;
		cin >> N;
		vector<int> v;
		int value;
		for (int i = 0; i < N; i++) {
			cin >> value;
			v.push_back(value);
		}
		cout << solve(v) << endl;
	}
}

Request to https://algospot.com/judge/problem/submit/SORTGAME
<Response [200]>

+----+--------+----------+-------+------+-------+------+--------+--------+
|    |      # | ๋ฌธ์ œ       | ์ œ์ถœ์ž   | ์–ธ์–ด   | ๊ธธ์ด    | ๊ฒฐ๊ณผ   |   ์ˆ˜ํ–‰์‹œ๊ฐ„ | ์ œ์ถœ์‹œ๊ฐ„   |
|----+--------+----------+-------+------+-------+------+--------+--------|
|  0 | 741801 | SORTGAME | 0x10  | cpp  | 1.5KB | ์‹คํ–‰์ค‘  |    nan | ๋ฐฉ๊ธˆ ์ „   |
+----+--------+----------+-------+------+-------+------+--------+--------+
----
Parsed: 43c8980a-b239-486d-868d-97a4c1104437 from 741801
---
AOJ server is busy. Wait 5 seconds.
Update submission table.
+----+--------+----------+-------+------+-------+------+--------+--------+
|    |      # | ๋ฌธ์ œ       | ์ œ์ถœ์ž   | ์–ธ์–ด   | ๊ธธ์ด    | ๊ฒฐ๊ณผ   | ์ˆ˜ํ–‰์‹œ๊ฐ„   | ์ œ์ถœ์‹œ๊ฐ„   |
|----+--------+----------+-------+------+-------+------+--------+--------|
|  0 | 741801 | SORTGAME | 0x10  | cpp  | 1.5KB | ์ •๋‹ต   | 844ms  | ๋ฐฉ๊ธˆ ์ „   |
+----+--------+----------+-------+------+-------+------+--------+--------+
----
[AOJ Result]
- Judge ID: 741801
- Problem ID: SORTGAME
- Language: cpp
- Result: ์ •๋‹ต
- Performance: 844ms

1.0.0-alpha.4 | Developer | Workflow

@Queue-ri Queue-ri merged commit f44a75e into main Mar 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Week 6] SORTGAME self review - Choi-mina

2 participants