๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/๋ฐฑ์ค€

[๋ฐฑ์ค€] 1477 ํœด๊ฒŒ์†Œ ์„ธ์šฐ๊ธฐ

by Jouureee 2021. 4. 5.

๋ฌธ์ œ :

www.acmicpc.net/problem/1477

 

1477๋ฒˆ: ํœด๊ฒŒ์†Œ ์„ธ์šฐ๊ธฐ

์ฒซ์งธ ์ค„์— ํ˜„์žฌ ํœด๊ฒŒ์†Œ์˜ ๊ฐœ์ˆ˜ N, ๋” ์ง€์œผ๋ ค๊ณ  ํ•˜๋Š” ํœด๊ฒŒ์†Œ์˜ ๊ฐœ์ˆ˜ M, ๊ณ ์†๋„๋กœ์˜ ๊ธธ์ด L์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉฐ, M๋„ 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. L์€ 100๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 1000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜

www.acmicpc.net

 

๋ถ„์„ :

ํ’€๋• ๋ชฐ๋ž๋Š”๋ฐ ์˜ˆ์ „ ๋ผ์šฐํ„ฐ ๋ฌธ์ œ์™€ ๋น„์Šทํ•œ ๋ฐฉ์‹์˜ ๋ฌธ์ œ๋‹ค!! 

 

์ƒ๊ฐํ•œ ํ‹€๋ฆฐ ๋ฐฉ๋ฒ• :

for๋ฌธ์„ ํ•œ๋ฒˆ์”ฉ ๋Œ๋•Œ๋งˆ๋‹ค ๊ฐ€์žฅ ๊ฐ„๊ฒฉ์ด ํฐ ํœด๊ฒŒ์†Œ ์ง€์ ์„ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ  ๊ทธ ์ง€์ ์— ํœด๊ฒŒ์†Œ๋ฅผ ์„ธ์šด๋‹ค.

//
//  1477_rest.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/04/05.
//

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>


using namespace std;
int already, will, dis;
int min_num = 0;
vector<int> v;

void set_rest(){
    int tmp_min = 0, tmp_idx;
    int tmp_tmp_idx = 0;
    while(will--){
        sort(v.begin(), v.end());
        for(int i = 1; i < (int)v.size(); i++){
            if(tmp_min < (v.at(i) - v.at(i - 1))){
                tmp_min = v.at(i) - v.at(i - 1);
                tmp_tmp_idx = v.at(i - 1) + v.at(i);
            }
        }
        
        tmp_idx = tmp_min / 2;
        tmp_min = 0;
        v.push_back(tmp_tmp_idx/2);
    }
    
    sort(v.begin(), v.end());
    for(int i = 1; i < (int)v.size(); i++){
        if(min_num < (v.at(i) - v.at(i - 1))){
            min_num = v.at(i) - v.at(i - 1);
        }
    }
    
}

int main(){
    cin >> already >> will >> dis;
    v.push_back(0);
    for(int i = 0; i < already; i++){
        int temp;
        cin >>temp;
        v.push_back(temp);
    }
    v.push_back(dis);
    set_rest();
    return 0;
}

 

ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ํ’€๋ฉด ์•ˆ๋˜๋Š”๊ฒŒ ์ง€์ ์— ํœด๊ฒŒ์†Œ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ !

์˜ˆ๋ฅผ ๋“ค์–ด {0, 300, 500} ์— ํœด๊ฒŒ์†Œ ๋‘๊ฐœ๋ฅผ ์„ธ์šฐ๊ณ  ์‹ถ์„ ๋•Œ -> {0, 150, 300, 400, 500}๋„ ๋˜์ง€๋งŒ {0, 100, 200, 300, 500}์œผ๋กœ ๋‚˜๋ˆ„์–ด์งˆ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋ ‡๊ฒŒ ํ’€๋ฉด ์•ˆ๋˜๊ณ  ์ด์ง„ ํƒ์ƒ‰ ๋ฐฉ๋ฒ•์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. 

๋‹ค๋ฅธ ๋ธ”๋กœ๊ทธ ๊ธ€๋“ค ๋ณด๋ฉด n์ด ์ž‘์•„ ๊ผญ ์ด์ง„ ํƒ์ƒ‰์œผ๋กœ ํ’€์ง€ ์•Š์•„๋„ ๋œ๋‹ค ํ•˜๋Š”๋ฐ ๋‚˜๋Š” ์ด์ง„ ํƒ์ƒ‰ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€๊ณ ์ž ํ•˜์˜€๋‹ค.

 

0, dist๋ฅผ ๋‘๋ฒˆ์งธ์ค„ ์‹œ์ž‘ ์ „, ๋ ๋ฒกํ„ฐ์— ๋„ฃ์–ด์ฃผ๊ณ  

left์™€ right๋ฅผ ์žก์•„์ค€๋‹ค. 

mid๋Š” ์ƒˆ๋กญ๊ฒŒ ์„ธ์šธ ํœด๊ฒŒ์†Œ์˜ ์œ„์น˜๋‹ค. 

๊ทธ๋ฆฌ๊ณ  rest๋Š” ๋‘ ํœด๊ฒŒ์†Œ ์‚ฌ์ด ์„ธ์šธ ์ˆ˜ ์žˆ๋Š” ํŽธ์˜์ ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ ์ด๊ฒƒ์ด ์„ธ์šฐ๊ธฐ๋กœํ•œ ํŽธ์˜์  ๊ฐœ์ˆ˜๋ณด๋‹ค ๋งŽ๋‹ค๋ฉด left๋ฅผ mid + 1ํ•˜์—ฌ ๊ฐ„๊ฒฉ์„ ์ค„์ธ๋‹ค. 

๋ฐ˜๋‹ค๋กœ ์„ธ์šฐ๊ณ ์ž ํ•œ ํŽธ์˜์ ์˜ ๊ฐœ์ˆ˜๋ณด๋‹ค ์ ๋‹ค๋ฉด right๋ฅผ mid - 1 ํ•˜์—ฌ ๊ฐ„๊ฒฉ์„ ๋Š˜๋ฆฌ๋Š” ๊ฒƒ์ด๋‹ค. 

 

c++ ์ฝ”๋“œ :

//
//  1477_rest.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/04/05.
//

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
int already, will, dis;
int min_num = 0;
vector<int> v;
void set_rest(){
    sort(v.begin(), v.end());
    int left = 1; int right = dis;
    while(left <= right){
        int mid = (left + right)/2;
        int rest = 0;
        for(int i = 1; i < (int)v.size(); i++){
            //0์ด ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋ฉด ๊ธฐ์กด์— ์žˆ๋˜ ์œ„์น˜์— ๋‹ค์‹œ ์„ธ์šฐ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ 
            if((v.at(i) - v.at(i - 1)) % mid == 0){
                rest += (v.at(i) - v.at(i - 1) - 1)/ mid;
            }else{
                rest += (v.at(i) - v.at(i - 1))/ mid;
            }
        }
        if(rest > will) left = mid + 1;
        else right = mid - 1;
    }
    cout << left << '\n';
}

int main(){
    cin >> already >> will >> dis;
    v.push_back(0);
    for(int i = 0; i < already; i++){
        int temp;
        cin >>temp;
        v.push_back(temp);
    }
    v.push_back(dis);
    set_rest();
    return 0;
}

๋Œ“๊ธ€