[c++] BOJ 1461번 :: λ„μ„œκ΄€

λ„μ„œκ΄€

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

문제

μ„Έμ€€μ΄λŠ” λ„μ„œκ΄€μ—μ„œ μΌν•œλ‹€. λ„μ„œκ΄€μ˜ κ°œλ°©μ‹œκ°„μ΄ λλ‚˜μ„œ μ„Έμ€€μ΄λŠ” μ‚¬λžŒλ“€μ΄ 마ꡬ 놓은 책을 λ‹€μ‹œ κ°€μ Έλ‹€ 놓아야 ν•œλ‹€. μ„Έμ€€μ΄λŠ” ν˜„μž¬ 0에 있고, μ‚¬λžŒλ“€μ΄ 마ꡬ 놓은 책도 μ „λΆ€ 0에 μžˆλ‹€. 각 μ±…λ“€μ˜ μ›λž˜ μœ„μΉ˜κ°€ μ£Όμ–΄μ§ˆ λ•Œ, 책을 λͺ¨λ‘ μ œμžλ¦¬μ— λ†”λ‘˜ λ•Œ λ“œλŠ” μ΅œμ†Œ 걸음 수λ₯Ό κ³„μ‚°ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€. μ„Έμ€€μ΄λŠ” ν•œ κ±ΈμŒμ— μ’Œν‘œ 1μΉΈμ”© κ°€λ©°, μ±…μ˜ μ›λž˜ μœ„μΉ˜λŠ” μ •μˆ˜ μ’Œν‘œμ΄λ‹€. 책을 λͺ¨λ‘ μ œμžλ¦¬μ— 놔둔 ν›„μ—λŠ” λ‹€μ‹œ 0으둜 λŒμ•„μ˜¬ ν•„μš”λŠ” μ—†λ‹€. 그리고 μ„Έμ€€μ΄λŠ” ν•œ λ²ˆμ— μ΅œλŒ€ Mꢌ의 책을 λ“€ 수 μžˆλ‹€.

μž…λ ₯

첫째 쀄에 μ±…μ˜ 개수 Nκ³Ό, 세쀀이가 ν•œ λ²ˆμ— λ“€ 수 μžˆλŠ” μ±…μ˜ 개수 M이 주어진닀. λ‘˜μ§Έ μ€„μ—λŠ” μ±…μ˜ μœ„μΉ˜κ°€ 주어진닀. N은 10,000보닀 μž‘κ±°λ‚˜ 같은 μžμ—°μˆ˜μ΄κ³ , M은 10,000보닀 μž‘κ±°λ‚˜ κ°™λ‹€. μ±…μ˜ μœ„μΉ˜λŠ” 0이 μ•„λ‹ˆλ©°, κ·Έ μ ˆλŒ“κ°’μ΄ 10,000보닀 μž‘κ±°λ‚˜ κ°™λ‹€.

좜λ ₯

첫째 쀄에 정닡을 좜λ ₯ν•œλ‹€.

예제 μž…λ ₯ 1

7 2
-37 2 -6 -39 -29 11 -28

예제 좜λ ₯ 1

131

λ‚˜μ˜ λ‹΅

μƒκ°μ˜ 흐름

전체 수 μ •λ ¬ => μ–‘μˆ˜μ™€ 음수의 μ ˆλŒ“κ°’ 비ꡐ (μ—†μœΌλ©΄ μžλ™μœΌλ‘œ 정해짐) => 큰 μͺ½μœΌλ‘œ λ‚˜μ€‘μ— => μž‘μ€ μͺ½ λ¨Όμ € 큰 μˆ˜λΆ€ν„° Mκ°œμ”© 끊고, ν•΄λ‹Ή index κ³±ν•˜κΈ° 2 => 큰 μͺ½μ€ μ ˆλŒ€κ°’μ΄ 큰 μˆ˜λΆ€ν„° Mκ°œμ”© 끊고, ν•΄λ‹Ή index κ³±ν•˜κΈ° 2 (κ°€μž₯ 큰 μ ˆλŒ“κ°’μ€ κ³±ν•˜κΈ° 1)

μ½”λ“œ

<κ΅¬ν˜„ μ „ λŒ€μΆ© 짜기>

sort(arr)
arr1 = 음수 λ°°μ—΄ (absν•΄μ„œ sort)
arr2 = μ–‘μˆ˜ λ°°μ—΄
sum = 0
if(abs(arr1[size-1])<arr2[size-1]): // 음수 λ¨Όμ €
    gothere(0, size);
    flag = 1
    gothere(0, size)

gothere(start,end):
    end-1 ~ startκΉŒμ§€ Mκ°œμ”© λŠμ–΄μ„œ index μ €μž₯ => idx_list
    sum+=idx_listμš”μ†Œ*2
    if(flagκ°€ 1)
        sum-=idx_list λ§ˆμ§€λ§‰ μš”μ†Œ

<κ΅¬ν˜„>

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int N, M;
int booklist[10000];
int positive[10000];
int negative[10000];
int flag = 0;
int sum = 0;

enum {POS,NEG};

void gothere(int start, int end, int kind) {
    int* which;
    if (kind == POS)
        which = positive;
    else
        which = negative;

    vector<int> idx_list;
    int idx = end - 1;
    while (idx >= 0) {
        idx_list.push_back(which[idx]);
        idx -= M;
    }

    for (int i = 0; i < idx_list.size(); i++)
        sum += idx_list[i] * 2;

    if (flag == 1) // λ§ˆμ§€λ§‰ μš”μ†Œ ν•œ 번 λΉΌκΈ°
        sum -= idx_list[0];
}

int main() {
    cin >> N >> M;
    for (int i = 0; i < N; i++) {
        cin >> booklist[i];
    }
    sort(&booklist[0], &booklist[N]);

    // μ–‘μˆ˜ 음수 λ°°μ—΄ λ‚˜λˆ„μ–΄ λ‹΄κΈ°
    int tmp = 0;
    int n_size = 0;
    while (booklist[tmp] < 0) {
        negative[n_size++] = abs(booklist[tmp++]);
    }
    int p_size = 0;
    while (tmp < N) {
        positive[p_size++] = booklist[tmp++];
    }

    sort(&negative[0], &negative[n_size]);

    if (positive[p_size - 1] < negative[n_size - 1]) { // μ ˆλŒ“κ°’ 비ꡐ
        gothere(0, p_size, POS); // μž‘μ€ κ±° λ¨Όμ €
        flag = 1;
        gothere(0, n_size, NEG);
    }
    else {
        gothere(0, n_size, NEG); // μž‘μ€ κ±° λ¨Όμ €
        flag = 1;
        gothere(0, p_size, POS);
    }

    cout << sum;

}

ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€

8 2
-10000 -3 -2 -1 2 100 1000 1001

=> 12206

8 3
-10000 -3 -2 -1 2 100 1000 1001

=> 12008

λ‹€λ₯Έ λ‹΅

  1. 본인은 booklist둜 λ°›κ³  두 λ°°μ—΄λ‘œ λ‚˜λˆ΄μœΌλ‚˜ λ°›μœΌλ©΄μ„œ λ°”λ‘œ μ–‘μˆ˜ 음수 μ‘°κ±΄κ²€μ‚¬ν•΄μ„œ negative, positive λ°°μ—΄λ‘œ λ‚˜λˆ΄μ–΄λ„ λœλ‹€.
λ°˜μ‘ν˜•