'전체 글'에 해당되는 글 84건

  1. 2020.06.03 :: [BOJ/1377] 버블 소트
  2. 2020.06.02 :: [BOJ/11004] K번째 수
  3. 2020.06.01 :: [BOJ/11652] 카드
Algorithm/Problem Solving 2020. 6. 3. 23:47

(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)

 

문제 링크

- https://www.acmicpc.net/problem/1377

 

사용 알고리즘

- 정렬

 

풀이

- Bubble Sort를 1회 수행 시, 숫자가 앞으로 1칸 밖에 이동하지 못함

- 따라서 앞으로 가장 많이 이동한 칸의 수 + 1

 

소스 코드

- https://github.com/moomini/algorithm/blob/master/boj/1377.cpp

#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
 
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    int n; cin >> n;
    vector<pair<intint>> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i].first;
        a[i].second = i;
    }
    sort(a.begin(), a.end());
    int ans = -1;
    for (int i = 0; i < n; ++i) {
        if (ans < (a[i].second - i)) ans = a[i].second - i;
    }
    cout << ans + 1 << "\n";
    return 0;
}
cs

'Algorithm > Problem Solving' 카테고리의 다른 글

[BOJ/11004] K번째 수  (0) 2020.06.02
[BOJ/11652] 카드  (0) 2020.06.01
[BOJ/10989] 수 정렬하기 3  (0) 2020.05.26
[BOJ/10825] 국영수  (0) 2020.05.24
[BOJ/10814] 나이순 정렬  (0) 2020.05.22
posted by DevMoomin
:
Algorithm/Problem Solving 2020. 6. 2. 21:06

(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)

 

문제 링크

- https://www.acmicpc.net/problem/11004

 

사용 알고리즘

- 정렬

 

풀이

- STL nth_element 이용

 

소스 코드

- https://github.com/moomini/algorithm/blob/master/boj/11004.cpp

#include <cstdio>
#include <algorithm>
 
using namespace std;
 
int arr[5000003];
 
int main(void) {
    int n, k; scanf("%d %d"&n, &k);
    --k;
    for (int i = 0; i < n; ++i) scanf("%d"&arr[i]);
    nth_element(arr, arr + k, arr + n);
    printf("%d\n", arr[k]);
    return 0;
}
cs

'Algorithm > Problem Solving' 카테고리의 다른 글

[BOJ/1377] 버블 소트  (0) 2020.06.03
[BOJ/11652] 카드  (0) 2020.06.01
[BOJ/10989] 수 정렬하기 3  (0) 2020.05.26
[BOJ/10825] 국영수  (0) 2020.05.24
[BOJ/10814] 나이순 정렬  (0) 2020.05.22
posted by DevMoomin
:
Algorithm/Problem Solving 2020. 6. 1. 21:36

(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)

 

문제 링크

- https://www.acmicpc.net/problem/11652

 

사용 알고리즘

- 정렬

 

풀이

- 정렬 후 앞에서부터 인접한 수를 비교하며 개수를 셈

 

소스 코드

- https://github.com/moomini/algorithm/blob/master/boj/11652.cpp

#include <cstdio>
#include <algorithm>
 
using namespace std;
 
long long arr[100003];
 
int main(void) {
    int n; scanf("%d"&n);
    for (int i = 0; i < n; ++i) scanf("%lld"&arr[i]);
    sort(arr, arr + n);
    int ansCount = 1, tempCount = 1;
    long long ans = arr[0];
    for (int i = 1; i < n; ++i) {
        if (arr[i] == arr[i - 1]) {
            ++tempCount;
        }
        else {
            if (ansCount < tempCount) {
                ans = arr[i - 1];
                ansCount = tempCount;
            }
            tempCount = 1;
        }
    }
    if (ansCount < tempCount) ans = arr[n - 1];
    printf("%lld\n", ans);
    return 0;
}
cs

'Algorithm > Problem Solving' 카테고리의 다른 글

[BOJ/1377] 버블 소트  (0) 2020.06.03
[BOJ/11004] K번째 수  (0) 2020.06.02
[BOJ/10989] 수 정렬하기 3  (0) 2020.05.26
[BOJ/10825] 국영수  (0) 2020.05.24
[BOJ/10814] 나이순 정렬  (0) 2020.05.22
posted by DevMoomin
: