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

16-2secondag #69

Merged
merged 3 commits into from
Mar 4, 2024
Merged

16-2secondag #69

merged 3 commits into from
Mar 4, 2024

Conversation

2secondag
Copy link
Collaborator

@2secondag 2secondag commented Feb 27, 2024

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

์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ

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

40๋ถ„

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

๋ฌธ์ œ ์„ค๋ช…

image
image

์ฝ”๋“œ ์„ค๋ช…

for i in range(N):
  x,y = map(int ,sys.stdin.readline().split())
  list.append([x,y])
  • ๊ฐ๊ฐ ํ• ๋‹น๋œ x์™€ y๋Š” [x, y] ํ˜•ํƒœ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด list์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
list.sort()

for i in list:
  print(i[0],i[1])
  • sort()๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ๋˜๋ฉฐ ์—ฌ๊ธฐ์„œ๋Š” ์ˆซ์ž์Œ์˜ ์ฒซ ๋ฒˆ์งธ ์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋œ๋‹ค.

  • i๋Š” ๋ฆฌ์ŠคํŠธ์—์„œ ํ•œ ์ˆซ์ž ์Œ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

  • i[0]๊ณผ i[1]์€ ๊ฐ๊ฐ ์ˆซ์ž ์Œ์˜ ์ฒซ ๋ฒˆ์งธ ์—ด๊ณผ ๋‘ ๋ฒˆ์งธ ์—ด์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

์ „์ฒด ์ฝ”๋“œ

import sys
N = int(sys.stdin.readline())
list = []

for i in range(N):
 x,y = map(int ,sys.stdin.readline().split())
 list.append([x,y])

list.sort()

for i in list:
 print(i[0],i[1])

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

  • ์ฒ˜์Œ์— ์œ„์น˜ x, y๋ฅผ N*2 ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์„œ x,y๋ฅผ ๋”ฐ๋กœ ์ž…๋ ฅ๋ฐ›๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๋ณด๋‹ค๊ฐ€ ์งˆ๋ฌธ ๊ฒŒ์‹œํŒ์„ ๋ณด๋‹ค ๋ณด๋‹ˆ..... ใ…Žใ…Žใ…Ž ์ด์ค‘ ๋ฆฌ์ŠคํŠธ ๊ฐ€ ์žˆ๋”๋ผ๊ตฌ์š”

  • x์ขŒํ‘œ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์œผ๋กœ๋ผ๋Š” ์กฐ๊ฑด ๋–„๋ฌธ์— ์ฒ˜์Œ์—๋Š” sortํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ฝ”๋“œ๋ฅผ ์งœ๋‹ค๊ฐ€ ๋‚˜์ค‘์— ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ๊ธฐ๋ณธ์ ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜๋ฉฐ, ์ฒซ ๋ฒˆ์งธ ์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

  • ๊ทธ๋ ‡๋‹ค๋ฉด ๋‘๋ฒˆ์งธ ์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?
    (์ฑ—gpt์˜ ๋„์›€์„ ๋ฐ›์•„... )

my_list.sort(key=lambda x: x[1])

๋ฉ”์„œ๋“œ์˜ key ๋งค๊ฐœ๋ณ€์ˆ˜์— ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์ •๋ ฌ ๊ธฐ์ค€์„ ์ง€์ •ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋‘ ๋ฒˆ์งธ ์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๋„๋ก ์„ค์ •.

์ด์ค‘ ๋ฆฌ์ŠคํŠธ_ ์„ค๋ช…1
์„ค๋ช… 2

  • ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋กœ ๋‹ค๋ฅธ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋„ฃ์€ ๊ฒƒ
  • ๋‹ค์ฐจ์› ๋ฆฌ์ŠคํŠธ ๋˜๋Š” ๋‹ค์ค‘ ๋ฆฌ์ŠคํŠธ๋ผ๊ณ  ๋ถ€๋ฆ„

Copy link
Member

@miniron-v miniron-v left a comment

Choose a reason for hiding this comment

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

C++์—์„  ์•„๋ž˜์ฒ˜๋Ÿผ ์ปค์Šคํ…€ ์ •๋ ฌ ๊ธฐ์ค€์„ ์„ธ์šฐ๊ณ  sortํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ดˆ์€๋‹˜์ด ์ฒ˜์Œ์— ํ•˜์‹œ๋ ค๋˜ NxN ๋ฐฐ์—ด์— ๋„ฃ๊ณ  ์ˆœํšŒํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ธ ๊ฑฐ ๊ฐ™์•„์š”! (๋ฉ”๋ชจ๋ฆฌ๋งŒ ์•ˆ ํ„ฐ์ง„๋‹ค๋ฉด)

#include <iostream>
#include <algorithm>
#include <vector>

bool compare(std::pair<int, int> a, std::pair<int, int> b) {
	if (a.first == b.first) {
		return a.second < b.second;
	}

	return a.first < b.first;
}

int main() {
	int n;
	std::cin >> n;

	std::vector<std::pair<int, int>> coordinates;

	while (n--) {
		int a, b;
		std::cin >> a >> b;

		coordinates.push_back(std::make_pair(a, b));
	}

	std::sort(coordinates.begin(), coordinates.end(), compare);

	for (auto coordinate : coordinates) {
		std::cout << coordinate.first << " " << coordinate.second << "\n";
	}
}

@bomik0221
Copy link
Member

C++์—์„  ์•„๋ž˜์ฒ˜๋Ÿผ ์ปค์Šคํ…€ ์ •๋ ฌ ๊ธฐ์ค€์„ ์„ธ์šฐ๊ณ  sortํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ดˆ์€๋‹˜์ด ์ฒ˜์Œ์— ํ•˜์‹œ๋ ค๋˜ NxN ๋ฐฐ์—ด์— ๋„ฃ๊ณ  ์ˆœํšŒํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ธ ๊ฑฐ ๊ฐ™์•„์š”! (๋ฉ”๋ชจ๋ฆฌ๋งŒ ์•ˆ ํ„ฐ์ง„๋‹ค๋ฉด)

#include <iostream>
#include <algorithm>
#include <vector>

bool compare(std::pair<int, int> a, std::pair<int, int> b) {
	if (a.first == b.first) {
		return a.second < b.second;
	}

	return a.first < b.first;
}

int main() {
	int n;
	std::cin >> n;

	std::vector<std::pair<int, int>> coordinates;

	while (n--) {
		int a, b;
		std::cin >> a >> b;

		coordinates.push_back(std::make_pair(a, b));
	}

	std::sort(coordinates.begin(), coordinates.end(), compare);

	for (auto coordinate : coordinates) {
		std::cout << coordinate.first << " " << coordinate.second << "\n";
	}
}

์ •ํ™•ํ•˜๊ฒŒ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ๋– ์˜ฌ๋ฆฌ๊ณ .. ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์„œ ๋ฆฌ๋ทฐํ•˜๋ ค๋‹ค๊ฐ€.. ์„ ์ˆ˜๋ฅผ ๋นผ์•—๊น€์„ ๊นจ๋‹ซ๋‹ค..
์˜ค๋Š˜๋„ ๋ฏผ์ฒฉํ•œ ํ•˜๋ฃจ ๋˜์„ธ์š”

Copy link
Collaborator

@Redish03 Redish03 left a comment

Choose a reason for hiding this comment

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

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹น ๐ŸŽ†

์ •๋ ฌ์ด๋ผ๋ฉด heap์„ ์“ฐ๋Š” ๊ฒƒ๋„ ๋ฐฉ๋ฒ•์ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”...! ๋‡Œํ”ผ์…œ์ž…๋‹ˆ๋‹ค ํ•˜ํ•˜

Comment on lines +11 to +12
for i in list:
print(i[0],i[1])
Copy link
Collaborator

Choose a reason for hiding this comment

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

ํŒŒ์ด์ฌ ๋ฌธ๋ฒ•...์ด ์‹ ๊ธฐํ•˜๋„ค์š” ๋˜๊ฒŒ ใ…‹ใ…‹ใ…Žใ…‹

@2secondag 2secondag merged commit 3bbfe10 into main Mar 4, 2024
@2secondag 2secondag deleted the origin/16-2secondag branch March 4, 2024 13:00
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.

4 participants