BOJ : https://www.acmicpc.net/problem/10825

 

구조체 operator 를 사용한 정렬을 해보고 싶은데 할줄 몰라서

일단 bool 함수를 만들어서 해결했다.

operator 함수 공부한 후 재포스팅해야겠다!

 

더보기
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
29
30
31
32
#include <iostream>
#include <string>
#include <cstdlib>
#include <algorithm>
using namespace std;
 
struct stud {
    string name;
    int K, E, M;
}stu[100001];
 
bool cmp(stud& a, stud& b) {
    if (a.K != b.K) return a.K > b.K;
    else if (a.E != b.E) return a.E < b.E;
    else if (a.M != b.M) return a.M > b.M;
    else return a.name < b.name;
}
 
int main() {
    ios::sync_with_stdio(false);
    int N; cin >> N;
 
    for (int i = 0; i < N; i++) {
        cin >> stu[i].name >> stu[i].K >> stu[i].E >> stu[i].M;
    }
 
    sort(stu, stu + N, cmp);
    for (int i = 0; i < N; i++) {
        cout << stu[i].name << '\n';
    }
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 11652번 카드  (0) 2020.01.11
BOJ 10989번 수 정렬하기 3  (0) 2020.01.10
BOJ 10814번 나이순 정렬  (0) 2020.01.10
BOJ 11651번 좌표 정렬하기2  (0) 2020.01.10
BOJ 11650번 좌표 정렬하기  (0) 2020.01.10

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

 

pair 를 통하여 구현하였다.

 

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
 
pair<pair<int,int>string> pii[100001];
 
 
int main() {
    ios::sync_with_stdio(false);
    int N; cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> pii[i].first.first >> pii[i].second;
        pii[i].first.second = i;
    }
 
    sort(pii, pii + N);
    for (int i = 0; i < N; i++cout << pii[i].first.first << ' ' << pii[i].second << '\n';
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 10989번 수 정렬하기 3  (0) 2020.01.10
BOJ 10825번 국영수  (0) 2020.01.10
BOJ 11651번 좌표 정렬하기2  (0) 2020.01.10
BOJ 11650번 좌표 정렬하기  (0) 2020.01.10
BOJ 2751번 수 정렬하기2  (0) 2020.01.10

BOJ : https://www.acmicpc.net/problem/11651

 

 

2개의 소스로 시간을 비교해보았다.

학교 알고리즘 과제를 할때 정렬이 필요한 경우 compare 함수를 만들어서 사용했던 경험이 많은데

이 경우와 변수 위치를 조금 바꾸어 compare 함수 없이 정렬을 했을 때 

시간복잡도가 큰 문제의 경우 compare 함수를 만들어서 문제를 푼 경우가 시간이 더욱 오래걸렸었다.

 

소스1. compare 함수 사용

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
pair<intint> pii[100001];
 
bool cmp(pair<intint>& a, pair<intint>& b) {
    if (a.second != b.second) return a.second < b.second;
    else return a.first < b.first;
}
 
int main() {
    int N; cin >> N;
    for (int i = 0; i < N; i++cin >> pii[i].first >> pii[i].second;
 
    sort(pii, pii + N, cmp);
    for (int i = 0; i < N; i++cout << pii[i].first << ' ' << pii[i].second << '\n';
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

소스2. 기본 STL sort 사용

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
pair<intint> pii[100001];
 
int main() {
    int N; cin >> N;
    for (int i = 0; i < N; i++cin >> pii[i].second >> pii[i].first;
 
    sort(pii, pii + N);
    for (int i = 0; i < N; i++cout << pii[i].second << ' ' << pii[i].first << '\n';
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

6분전이라 적힌 AC 가 compare 함수를 사용한 예제이다.

겨우 10만 케이스인데도 시간차이가 보인다면

학교과제할때 크게 느꼈던 천만~ 같은 경우 compare 함수를 사용하는 것은 다시 한번 생각해볼만 한 것 같다.

 

'algorithm > BOJ' 카테고리의 다른 글

BOJ 10825번 국영수  (0) 2020.01.10
BOJ 10814번 나이순 정렬  (0) 2020.01.10
BOJ 11650번 좌표 정렬하기  (0) 2020.01.10
BOJ 2751번 수 정렬하기2  (0) 2020.01.10
BOJ 11052번 카드 구매하기  (0) 2020.01.10

BOJ : https://www.acmicpc.net/problem/11650

 

STL 의 기본 sort 는 첫번째 인자를 비교 후 값이 같으면 2번째 인자 3번쨰 인자 이런식으로 비교하도록 되어있다.

 

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
pair<intint> pii[100001];
 
int main() {
    int N; cin >> N;
    for (int i = 0; i < N; i++cin >> pii[i].first >> pii[i].second;
 
    sort(pii, pii + N);
    for (int i = 0; i < N; i++cout << pii[i].first << ' ' << pii[i].second << '\n';
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 10814번 나이순 정렬  (0) 2020.01.10
BOJ 11651번 좌표 정렬하기2  (0) 2020.01.10
BOJ 2751번 수 정렬하기2  (0) 2020.01.10
BOJ 11052번 카드 구매하기  (0) 2020.01.10
BOJ 2225번 합분해  (0) 2020.01.09

+ Recent posts