-

[BOJ]10814-Sort 본문

Algorithm/Baekjoon

[BOJ]10814-Sort

Boogallee 2020. 3. 12. 21:14

https://www.acmicpc.net/problem/10814

 

10814번: 나이순 정렬

온라인 저지에 가입한 사람들의 나이와 이름이 가입한 순서대로 주어진다. 이때, 회원들을 나이가 증가하는 순으로, 나이가 같으면 먼저 가입한 사람이 앞에 오는 순서로 정렬하는 프로그램을 작성하시오.

www.acmicpc.net

 

1. Stable Sort 라고 해서 괜히 무턱대고 병합 정렬 사용했다가는 낭패. 너무 복잡해진다.

2. Stable Sort 가 아닌 Sort 를 사용해서 똑같은 결과를 낼 수 있다.

3. 문제를 잘 읽어야함. 나이순 + 나이가 같으면 가입순이다. 이름 사전순 비교하지 않는다.

 

첫번째 코드

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;


struct info {
	string str;
	string name;
	int idx;
	int age;
};

vector<info> v;

bool F(info a, info b) {
	if (a.age != b.age) return a.age < b.age;
	else {
		return a.idx < b.idx;
	}
}

int main() {
	int N;
	scanf("%d", &N);

	for (int i = 0; i < N; i++) {
		info get;
		cin >> get.age >> get.name;
		get.str = to_string(get.age) +" " + get.name;
		get.idx = i;
		v.push_back(get);
	}

	sort(v.begin(), v.end(), F);
	for (int i = 0; i < N; i++) {
		cout << v[i].str << "\n";
	}

	return 0;
}

두 번째 코드

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>

using namespace std;


vector<vector<string>> v(201);


int main() {
	int N;
	scanf("%d", &N);

	for (int i = 0; i < N; i++) {
		int age;
		string name;
		cin >> age >> name;
		v[age].push_back(name);
	}

	for (int i = 0; i < 201; i++) {
		if (!v[i].empty()) {
			for (int j = 0; j < v[i].size(); j++) {
				cout << i << " " << v[i][j] << '\n';
			}
		}
	}
	return 0;
}

'Algorithm > Baekjoon' 카테고리의 다른 글

[BOJ]1431 - Sort  (0) 2020.03.12
[BOJ]10825  (0) 2020.03.10
[BOJ]11004 - 퀵소트/퀵셀렉션/머지소트  (0) 2020.03.09
[BOJ]2136_DFS  (0) 2019.07.10
1890 점프  (0) 2018.02.12
Comments