๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜/BOJ

[C++/๋ฐฑ์ค€] 10989 : ์ˆ˜ ์ •๋ ฌํ•˜๊ธฐ 3

728x90

๋ฌธ์ œ

N๊ฐœ์˜ ์ˆ˜๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ˆ˜์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 10,000,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด ์ˆ˜๋Š” 10,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.


 

๋ธŒ๋ก ์ฆˆ 1ํ‹ฐ์–ด.

์ฒ˜์Œ์—๋Š” ์•„๋ฌด ์ƒ๊ฐ ์—†์ด ๋ฐฐ์—ด์—๋‹ค๊ฐ€ ์ž…๋ ฅ์„ ๋ชจ๋‘ ์ €์žฅํ•˜๊ณ , ๋ฒ„๋ธ” ์ •๋ ฌ ์‹์œผ๋กœ ํ’€๋ฉด ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ

ํŠน์ดํ•˜๊ฒŒ๋„ ์ด ๋ฌธ์ œ๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ์ด 8MB์ด์—ˆ๋‹ค.(์•„์ฃผ ์ž‘์€ ํŽธ)

๊ทธ๋ž˜์„œ ๋ฐฐ์—ด ํฌ๊ธฐ + ์ •๋ ฌ ์‹œ๊ฐ„ -> ์‹คํŒจ ์˜ˆ๊ฐ

// ์•„๋ž˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ ์ฝ”๋“œ
int main() {
	int n;
	int a = 0;
	int nums[10000000] = { 0, };
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> nums[i];
	}
	for (int i = 0; i < n; i++) {
		for (int j = i; j < n; j++) {
			if (nums[i] > nums[j]) {
				a = nums[i];
				nums[i] = nums[j];
				nums[j] = a;
			}
		}
	}
	for (int i = 0; i < n; i++) {
		cout << nums[i] << "\n";
	}
}

 

๊ทธ๋ž˜์„œ 10000๊ฐœ์งœ๋ฆฌ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  ์ž…๋ ฅ๋ฐ›์€ ๊ณณ์˜ ์ธ๋ฑ์Šค๋งŒ countํ•˜์—ฌ ๊ทธ ๊ฐฏ์ˆ˜๋งŒํผ ์ธ๋ฑ์Šค๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ

๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด์•ผ ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ฒ˜์Œ์— ์“ธ๋ฐ์—†์ด if๋ฌธ ๋„ฃ์—ˆ๋‹ค๊ฐ€ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋– ์„œ

if๋ฌธ์€ ์ง€์šฐ๊ณ , cin/cout ์ž…์ถœ๋ ฅ ์‹œ๊ฐ„ ์ค„์ด๋Š” ์ฝ”๋“œ๊นŒ์ง€ ๋„ฃ์–ด์„œ ๋งˆ์นจ๋‚ด ์„ฑ๊ณตํ–ˆ๋‹ค.

 

#include <iostream>
using namespace std;

int main() {
	ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int n;
	int a = 0;
	int nums[10000] = { 0, };
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a;
		nums[a-1] += 1;
	}
	for (int i = 0; i < 10000; i++) {
		for (int j = 0; j < nums[i]; j++) {
			cout << i + 1 << "\n";
		}
	}
}

728x90