Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

9-yuyu0830 #36

Merged
merged 1 commit into from
May 11, 2024
Merged

9-yuyu0830 #36

merged 1 commit into from
May 11, 2024

Conversation

yuyu0830
Copy link
Collaborator

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆ˜์—… - ๋ฒ„๋ธ” ์ •๋ ฌ 3

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1์‹œ๊ฐ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๋ฌธ์ œ ์ดํ•ด

n์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง„ ์ˆ˜์—ด a, b๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. a๋ฅผ ๋ฒ„๋ธ” ์ •๋ ฌ ํ•˜๋ ค๊ณ  ํ•  ๋•Œ, ์ค‘๊ฐ„์— b์™€ ๊ฐ™์•„์ง€๋Š” ์ˆœ๊ฐ„์ด ์ƒ๊ธฐ๋Š”์ง€ ์ƒ๊ธฐ์ง€ ์•Š๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ 1, 0์œผ๋กœ ์ถœ๋ ฅํ•˜์ž

  • 5 โ‰ค n โ‰ค 10,000

๋ฌธ์ œ ํ’€์ด

์ฒ˜์Œ์—๋Š” ์ด๊ฑฐ ์ง์ ‘ ๋ฒ„๋ธ” ์ •๋ ฌ ํ•˜๋ฉด์„œ ์ผ์ผ์ด ๋น„๊ตํ•˜๋ฉด ์‹œ๊ฐ„์ด ๋ง์ด ๋˜๋‚˜...? ์‹ถ์—ˆ๋‹ค. ๋ฒ„๋ธ” ์ •๋ ฌ์€ ๋ฌด์กฐ๊ฑด n * (n - 1) / 2 ๋ฒˆ ์—ฐ์‚ฐํ• ํ…๋ฐ? ์‹ถ์–ด์„œ ์ตœ๋Œ€ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐ ํ•ด๋ณด๋‹ˆ ์•ฝ 5000๋งŒ๋ฒˆ... ์—ฌ๊ธฐ์— ๋งค ์ •๋ ฌ๋งˆ๋‹ค 10,000๊ฐœ์”ฉ ๋น„๊ตํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ๋ฌด์กฐ๊ฑด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜๊ฒ ๋‹ค ์‹ถ์—ˆ๋‹ค.

์ผ๋‹จ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜๋“  ๋ง๋“  ํ•œ๋ฒˆ ๋งจ๋•… ํ—ค๋”ฉ๋ถ€ํ„ฐ ํ•ด๋ณด์ž๋Š” ๋งˆ์Œ์œผ๋กœ ๊ฐ„๋‹จํžˆ ๋ฒ„๋ธ” ์ •๋ ฌ์„ ๊ตฌํ˜„ํ•˜๊ณ  ๋งค ๋ฒˆ ๋ฐฐ์—ด์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋น„๊ตํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์งฐ๋‹ค.

bool same() {
    for (int i = 0; i < n; i++) 
        if (arr[0][i] != arr[1][i]) return false;
    return true;
}

while (rnd) {
    // sort [0] to [ptr - rnd]
    int a = arr[0][ptr];
    int b = arr[0][ptr + 1];

    arr[0][ptr] = min(a, b);
    arr[0][ptr + 1] = max(a, b);

    // if this index is last index in this round
    if (++ptr == rnd) {
        // next round
        ptr = 0;
        rnd--;
    }

    if (same()) {
        // if arr is same
        printf("1\n");
        return 0;
    }
}

๋Œ๋ ค๋ณด๋‹ˆ ์ด๊ฒŒ ์›ฌ๊ฑธ ์ค‘๊ฐ„๊นŒ์ง€ ์ž˜ ๊ฐ€๋‹ค๊ฐ€ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค. ๋กœ์ง ์ž์ฒด๋Š” ๋ฌธ์ œ๊ฐ€ ์—†๊ณ  ์‹œ๊ฐ„์„ ์ค„์ผ ํ•„์š”๊ฐ€ ์žˆ๋Š” ์ผ€์ด์Šค๊ฐ€ ์žˆ๋‹ค๋Š” ๋œป์ด์˜€๋‹ค. ์ฝ”๋“œ๋ฅผ ์ฐฌ์ฐฌํžˆ ์‚ดํŽด๋ณด๋ฉฐ ๊ณ ๋ฏผํ•˜๋˜ ์ค‘ ์•„๊นŒ ์ƒ๊ฐํ–ˆ๋˜ "๋งค๋ฒˆ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋น„๊ตํ•˜๋ฉด ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ฆด ๊ฒƒ ๊ฐ™์€๋ฐ" ๋ผ๋Š” ๊ณ ๋ฏผ์ด ๋– ์˜ฌ๋ž๊ณ , ์ •๋ ฌ๋˜๋Š” ๋ฐฐ์—ด์˜ ํŠน์ง• ์ƒ ์•ž๋ถ€๋ถ„์ด ๊ฐ™๋‹ค๋ฉด ๊ทธ ์•ž๋ถ€๋ถ„์€ ์ •๋ ฌํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ๊ฑธ ๊นจ๋‹ฌ์•˜๋‹ค.

  1. ๋งŒ์•ฝ ์ •๋ ฌ ํ•˜๋Š” ๋„์ค‘ ๊ฐ™์•„์ง€๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์ƒ๊ธด๋‹ค๋ฉด ์•ž์˜ ์ด๋ฏธ ๊ฐ™์€ ๋ถ€๋ถ„์€ ๋‹ค์‹œ ๋น„๊ตํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
  2. ๋งŒ์•ฝ ๊ฐ™์•„์ง€๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์—†๋‹ค๊ณ  ํ•ด๋„ ๊ตณ์ด ์•ž์˜ ๋ถ€๋ถ„์„ ๋‹ค์‹œ ๋น„๊ตํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ์–ด์ฐจํ”ผ ์ค‘๊ฐ„๋ถ€ํ„ฐ ๊ฐ™์•„์งˆ ์ผ์ด ์—†์„ํ…Œ๋‹ˆ๊นŒ.

๊ทธ๋ž˜์„œ same() ํ•จ์ˆ˜๋งŒ ์กฐ๊ธˆ ์†๋ดค๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ฐฐ์—ด ๋น„๊ต๋ฅผ ์ •๋ง ์ตœ์•…์˜ ๊ฒฝ์šฐ์—๋„ 5,000๋งŒ(์ •๋ ฌ ํ›„ ํ•จ์ˆ˜ ํ˜ธ์ถœ) + 1๋งŒ๋ฒˆ(๋น„๊ต) ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค!

bool same() {
    // start at 'arrPtr'
    for (int i = arrPtr; i < n; i++) {
        // if diff, terminate this function
        if (arr[0][i] != arr[1][i]) return false;
        // if same, then that index will same forever. no reason to compare again.
        arrPtr++;
    }
    return true;
}

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์šฐ์—ฐํžˆ ์ง€๋‚˜๊ฐ€๋‹ค ๋ณธ ๋ฌธ์ œ๋ผ์„œ ํ’€์–ด๋ดค๋Š”๋ฐ ์ •๋ ฌ ๋‹ค์‹œ ๊ณต๋ถ€ ํ•ด์•ผ๊ฒ ๊ตฌ๋‚˜ ๋Š๊ผˆ๋‹ค..

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋Š” ๋ฒ„๋ธ”์ •๋ ฌ์—์„œ ํฐ ์ˆ˜ ๋ถ€ํ„ฐ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์„ ์ฑ„ํƒํ•˜์˜€์Šต๋‹ˆ๋‹ค.
ํฐ ์ˆ˜๋Š” ์ € ๋’ค์— ๋ฐฐ์น˜๋˜๊ธฐ ๋•Œ๋ฌด๋„น
ํฐ์ˆ˜์˜ ์œ„์น˜๊ฐ€ B์™€ ์ผ์น˜ํ•  ๋•Œ๊นŒ์ง€ swapํ•˜๋Š” ์‹์œผ๋กœ ํ•˜์—ฌ ์ง„ํ–‰ํ•ด์ค๋‹ˆ๋‹ค.
๊ทธ๋Ÿผ ๊ฐ ์ˆ˜์˜ ์œ„์น˜๊ฐ€ B์˜ ์œ„์น˜์— ๋งž๊ฒŒ ์กฐ์ •ํ•˜๊ธฐ ์‹œ์ž‘ํ•  ๊ฒƒ์ด๊ณ  B์˜ ๊ฐ’์ด ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์„ ๊ฒฝ์šฐ ๊ต์ฒด๊ฐ€ ์ผ์–ด๋‚˜์ง€ ์•Š์œผ๋ฏ€๋กœ ํ•ด๋‹น ์œ„์น˜ ๊ทธ๋Œ€๋กœ ๊ณ ์ •๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>

using namespace std;

int N, n;
vector<int> A;
unordered_map<int, int> B;
priority_queue<int, vector<int>, less<int>> pq;

void Solve()
{
	cin >> N;
	N += 1;
	A.assign(N, 0);

	for (int i = 1; i < N; i++)
	{
		cin >> n;
		A[i] = n;
		pq.push(n);
	}

	for (int i = 1; i < N; i++)
	{
		cin >> n;
		B[n] = i;
	}

	bool flag = true;
	// ํฐ ๊ฐ’์ด ๋’ค๋กœ ๋จผ์ € ์ •๋ ฌ๋จ.
	// ๋ณ€๊ฒฝ๋˜๋Š” ๊ฐ’์€ 
	while (flag && !pq.empty())
	{
		int cur = pq.top();
		pq.pop();

		int start = 1;
		while (A[start] != cur) start++;

		for (int i = start; i < B[cur]; i++)
		{
			swap(A[i], A[i + 1]);
		}
	}

	for (int i = 1; i < N; i++)
	{
		if (i != B[A[i]])
		{
			flag = false;
			break;
		}
	}

	cout << flag ? 1 : 0;
}

int main()
{
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	Solve();

	return 0;
}

ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ํ’€์—ˆ์„ ๋•Œ 71%์—์„œ ๋ง‰ํžˆ๋”๊ตฐ์š” ์–ด๋–ค ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ์žˆ์„๊นŒ์š”..? ๋ช‡์‹œ๊ฐ„์งธ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.

๋‹ค๋ฅธ ํ’€์ด

๋‘ ๋ฐฐ์—ด์ด ์ผ์น˜ํ•˜๋Š” ๊ฒ€์‚ฌ ๋ถ€๋ถ„์„ ๋ณ€๊ฒฝ์ด ์ผ์–ด๋‚ฌ์„ ๋•Œ์™€ ๊ทธ ๋ณ€๊ฒฝ์ด ์ผ์–ด๋‚œ ์‹œ์ ์—์„œ A์™€ B๊ฐ€ ์ผ์น˜ํ•  ๊ฒฝ์šฐ ๋™์ผํ•œ ๋ฐฐ์—ด์ธ์ง€ ๊ฒ€์‚ฌํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ฑ„ํƒํ•˜์˜€์Šต๋‹ˆ๋‹ค.

int b = arr[0][ptr + 1];

arr[0][ptr] = min(a, b);
arr[0][ptr + 1] = max(a, b);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swap์„ ์“ฐ๋ฉด ๋Œ€์†Œ ๊ด€๊ณ„๋งŒ ๋น„๊ตํ•ด์„œ ๋ฐ”๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šด๋‹ˆ๋‹ค!

Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ทธ๋ƒฅ ๋ฒ„๋ธ”์ •๋ ฌ๋กœ ํ•˜๋ฉด ์—ญ์‹œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๋„ค์š”. ์ €๋Š” swapํ•˜๊ณ  A์™€ B์˜ ๋‹ค์Œ ์›์†Œ๊ฐ€ ๊ฐ™๋‹ค๋ฉด ๋˜ ๋น„๊ตํ•ด์ฃผ๋Š” ์‹์œผ๋กœ ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. python์œผ๋กœ๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ์—ฌ์ „ํžˆ ๋ฐœ์ƒํ•ด์„œ pypy๋กœ ํ•˜๋‹ˆ๊นŒ ํ†ต๊ณผ๋˜์—ˆ์Šต๋‹ˆ๋‹ค..

import sys
input = sys.stdin.readline

n = int(input())

A = list(map(int,input().split()))
B = list(map(int,input().split()))

def bubble_sort(A,B):
  for i in range(len(A) - 1, 0, -1):
      for j in range(i):
          if A[j] > A[j + 1]:
              A[j], A[j + 1] = A[j + 1], A[j]
              if A[j+1] == B[j+1]:
                if A==B:
                  print(1)
                  sys.exit(0)
  print(0)

if A==B:
  print(1)
  sys.exit(0)
else:
  bubble_sort(A,B)

Copy link
Contributor

@dhlee777 dhlee777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฒ„๋ธ”์ •๋ ฌ์„ ํ†ตํ•ด ์ฒ˜์Œ๋ถ€ํ„ฐ๋๊นŒ์ง€ ๋น„๊ตํ•ด์ฃผ๋ฉด ์—„์ฒญ๋‚œ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ์— ๋‘๊ฐ€์ง€ ์กฐ๊ฑด์„ ๊ฑธ์–ด์ฃผ์–ด ๊ฒฝ์šฐ์˜์ˆ˜๋ฅผ ์ž˜๋ผ๋‚ด์ฃผ์–ด์•ผํ•˜๋Š”๊ตฐ์š”.. ๋ฒ„๋ธ”์ •๋ ฌ์— ๋Œ€ํ•ด ์ฐพ์•„๋ณด๋ฉฐ ๊ฐœ๋…๋„ ์ž˜ ์ตํ˜€๊ฐ‘๋‹ˆ๋‹ค..!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants