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

github : https://github.com/junho0956/Algorithm/blob/master/1517/1517/%EC%86%8C%EC%8A%A4.cpp

 

버블소트는 O(N^2) 의 시간복잡도를 가지는 정렬입니다.

현재 인덱스를 기준으로 다음 인덱스와 비교하여 swap 이 이루어지는 형태의 정렬인데

 

이 문제는 최대크기가 50만개이므로 O(N^2) 의 시간복잡도로 문제를 해결하면 시간초과를 받게 될 것 입니다.

swap이 이루어지는건 크기가 작은 인덱스가 크기가 큰 인덱스보다 뒤로 밀려날 때 입니다.

 

이를 분할정복을 이용하여 해결할 수 있습니다.

보통 분할정복을 이용한 정렬이 merge_sort 와 quick_sort 가 있으며

그 중 우선 분할 후 합치는 과정을 거치는 merge_sort 를 이용하여 문제를 해결할 수 있습니다.

이 문제를 이용하면 inversion 과 같은 도치수도 쉽게 풀 수 있습니다.

 

partition 단계에서 왼쪽과 오른쪽을 비교했을 때 만약 오른쪽이 왼쪽보다 작다면

=> 왼쪽의 남은 확인하지않은 인덱스들은 오른쪽보다는 당연히 큰 값으로 남아있으므로

그 수만큼 갯수를 더해주면 됩니다.

 

더보기
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
using namespace std;
 
#define MAX 500000
 
int arr[MAX];
int dup[MAX];
 
long long partition(int s, int m, int e) {
    int left = s, right = m + 1;
    int index = 0;
    long long result = 0;
    while (left <= m && right <= e) {
        if (arr[right] >= arr[left]) {
            dup[index++= arr[left++];
        }
        else {
            result += m - left + 1;
            dup[index++= arr[right++];
        }
    }
 
    while (left <= m) dup[index++= arr[left++];
    while (right <= e) dup[index++= arr[right++];
 
    for (int i = 0; i <= e - s; i++) {
        arr[s + i] = dup[i];
    }
 
    return result;
}
 
long long merge_sort(int s, int e) {
    long long result = 0;
    if (s < e) {
        int pivot = (s + e) / 2;
        result += merge_sort(s, pivot);
        result += merge_sort(pivot + 1, e);
        result += partition(s, pivot, e);
    }
    return result;
}
 
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int N; cin >> N;
 
    for (int i = 0; i < N; i++cin >> arr[i];
 
    long long result = merge_sort(0,N-1);
 
    cout << result;
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

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

BOJ 11047번 동전 0  (0) 2020.01.17
BOJ 10090번 Counting Inversions  (0) 2020.01.17
BOJ 1992번 쿼드트리  (0) 2020.01.17
BOJ 1780번 종이의 개수  (0) 2020.01.17
BOJ 11728번 배열 합치기  (0) 2020.01.17

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

github : https://github.com/junho0956/Algorithm/blob/master/1992/1992/%EC%86%8C%EC%8A%A4.cpp

 

이전에 풀었던 BOJ 1780번 종이의 개수와 같은 문제이다.

/2 씩 해가면서 분할하여 확인하면된다.

 

더보기
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
33
34
35
36
37
38
39
40
#include <cstdio>
using namespace std;
#pragma warning(disable:4996)
int arr[64][64];
 
void quadTree(int N, int y, int x) {
    bool test = true;
    for (int i = y; i < y + N; i++) {
        for (int k = x; k < x + N; k++) {
            if (arr[i][k] != arr[y][x]) {
                test = false;
                break;
            }
        }
        if (!test) break;
    }
 
    if (test) printf("%d",arr[y][x]);
    else {
        printf("(");
        int div = N / 2;
        for (int i = 0; i < 2; i++) {
            for (int k = 0; k < 2; k++) {
                quadTree(div, y + i * div, x + k*div);
            }
        }
        printf(")");
    }
}
 
int main() {
    int N; scanf("%d",&N);
 
    for (int i = 0; i < N; i++)
        for (int k = 0; k < N; k++)
            scanf("%1d"&arr[i][k]);
 
    quadTree(N, 00);
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

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

BOJ 10090번 Counting Inversions  (0) 2020.01.17
BOJ 1517번 버블 소트  (0) 2020.01.17
BOJ 1780번 종이의 개수  (0) 2020.01.17
BOJ 11728번 배열 합치기  (0) 2020.01.17
BOJ 11662번 민호와 강호  (0) 2020.01.16

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

github : https://github.com/junho0956/Algorithm/blob/master/1780/1780/%EC%86%8C%EC%8A%A4.cpp

 

그냥 반복문으로 전체를 훑어도 되지만

문제의 분류가 분할정복이여서 요구하는대로 구현하였다.

 

더보기
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;
 
int N;
int arr[2187][2187]; // 3^7
int counter[3]; // -1 0 1
 
void paper_count(int n, int y, int x) {
    bool test = true;
    for (int i = y; i < y+n; i++) {
        for (int k = x; k < x+n; k++) {
            if (arr[i][k] != arr[y][x]) {
                test = false;
                break;
            }
        }
        if (!test) break;
    }
 
    if (test) {
        if (arr[y][x] == -1) counter[2]++;
        else counter[arr[y][x]]++;
    }
    else {
        int div = n / 3;
        for (int i = 0; i < 3; i++) {
            for (int k = 0; k < 3; k++) {
                paper_count(div, y + i * div, x + k * div);
            }
        }
    }
}
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> N;
    for (int i = 0; i < N; i++) {
        for (int k = 0; k < N; k++) {
            cin >> arr[i][k];
        }
    }
 
    paper_count(N, 00);
    cout << counter[2<< '\n' << counter[0<< '\n' << counter[1];
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

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

BOJ 1517번 버블 소트  (0) 2020.01.17
BOJ 1992번 쿼드트리  (0) 2020.01.17
BOJ 11728번 배열 합치기  (0) 2020.01.17
BOJ 11662번 민호와 강호  (0) 2020.01.16
BOJ 10816번 숫자 카드2  (0) 2020.01.16

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

github : https://github.com/junho0956/Algorithm/blob/master/11728/11728/%EC%86%8C%EC%8A%A4.cpp

 

문제는 사실 분할정복을 통한 해결을 요구하고 있지만.. 

그냥 합쳤습니다..

 

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

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

BOJ 1992번 쿼드트리  (0) 2020.01.17
BOJ 1780번 종이의 개수  (0) 2020.01.17
BOJ 11662번 민호와 강호  (0) 2020.01.16
BOJ 10816번 숫자 카드2  (0) 2020.01.16
BOJ 10815번 숫자 카드  (0) 2020.01.16

+ Recent posts