Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 백준
- composable architecture
- 운영체제
- ReactorKit
- 대외활동
- spritekit
- 알고리즘
- dispatchqueue
- 문법
- 영남대
- Swift
- uikit
- widget
- 컴퓨터그래픽스
- TCA
- 스유
- 네트워크
- 스위프트
- 웹
- widgetkit
- Protocol
- 후기
- cs
- 리액트
- c++
- swift concurrency
- SwiftUI
- 위젯킷
- 멋사
- 멋쟁이사자처럼
Archives
- Today
- Total
맛동산이
(백준 10814) 나이순 정렬 본문
문제
https://www.acmicpc.net/problem/10814
잘못된 방법
처음에는 그냥 순회를 통해서 두번 정렬을 행하는것으로 했지만,
시간 초과가 나와서 해결방법을 고민하다가 정답을 봣다.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool comapare1(pair<int, string> a, pair<int, string> b)
{
return a.second > b.second;
}
int main()
{
int n;
cin >> n;
int age;
string name;
vector<pair<int, string>> sub;
for (int a = 0; a < n; a++)
{
cin >> age >> name;
sub.push_back(make_pair(age, name));
}
sort(sub.begin(), sub.end());
for (int a = 1; a < n; a++)
{
if (sub[a - 1].first == sub[a].first)
{
sort(sub.begin(), sub.end(), comapare1);
}
}
for (int a = 0; a < n; a++)
{
cout << sub[a].first << sub[a].second << endl;
}
}
보니까 for문 안에서 소팅을 해버려서 무조건 타임오버가 날수밖에 없는 구조였음.
정답코드
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
bool cmp (pair<int, string> u, pair<int, string> v)
{
return u.first < v.first;
}
int main()
{
int T;
cin >> T;
vector<pair<int, string>> vec(T);
for (int i = 0; i < T; i++)
cin >> vec[i].first >> vec[i].second;
stable_sort(vec.begin(), vec.end(), cmp);
for (int i = 0; i < T; i++)
cout << vec[i].first << " " << vec[i].second << "\n";
}
utility에 존재하는 stable_sort라는것을 사용해서 문제를 해결했고, 이외에도 다양하게 문제를 해결하는 방법이 있었다.
그중에서 내가 항상하는 구조체를 만드는 방법도 존재를해서 살짝 기분이 좋앗다.
반응형
'알고리즘 > 백준' 카테고리의 다른 글
(백준 1269) 대칭 차집합 (0) | 2022.06.29 |
---|---|
(백준 14425) 문자열 집합 (0) | 2022.06.27 |
(백준 10815) 숫자 카드 (0) | 2022.06.26 |
백준 1436 영화감독 숀 (0) | 2022.06.17 |
백준7568 덩치(c++) (0) | 2022.06.15 |