๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 14627๋ฒˆ - ํŒŒ๋‹ญํŒŒ๋‹ญ

by dkswnkk 2022. 9. 13.

๋ฌธ์ œ

 

14627๋ฒˆ: ํŒŒ๋‹ญํŒŒ๋‹ญ

์ฒซ์งธ ์ค„์— ์Šน๊ท ์ด๊ฐ€ ์‹œ์žฅ์—์„œ ์‚ฌ ์˜จ ํŒŒ์˜ ๊ฐœ์ˆ˜ S(1 ≤ S ≤ 1,000,000), ๊ทธ๋ฆฌ๊ณ  ์ฃผ๋ฌธ๋ฐ›์€ ํŒŒ๋‹ญ์˜ ์ˆ˜ C(1 ≤ C ≤ 1,000,000)๊ฐ€ ์ž…๋ ฅ๋œ๋‹ค. ํŒŒ์˜ ๊ฐœ์ˆ˜๋Š” ํ•ญ์ƒ ํŒŒ๋‹ญ์˜ ์ˆ˜๋ฅผ ๋„˜์ง€ ์•Š๋Š”๋‹ค. (S ≤ C) ๊ทธ ํ›„, S ์ค„์—

www.acmicpc.net

 

์ฝ”๋“œ

#include <iostream>
#include <vector>

#define ll long long int
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    ll S, C; cin>>S>>C;
    vector<ll> v(S);
    for(int i=0; i<S; i++) cin>>v[i];
    
    ll start = 1, end = 1e9;
    ll mod = 0, sum = 0;
    
    while(start <= end){
        ll mid = (start + end) / 2;
        ll sum = 0;
        for(int i=0; i<v.size(); i++){
            if(v[i] >= mid) sum += v[i] / mid;
        }
        if(sum >= C){
            mod = mid;
            start = mid + 1;
        }
        else end = mid - 1;
    }
    
    for(int i=0; i<v.size(); i++) sum += v[i];
    cout<< sum - mod * C;
}

 

ํ’€์ด

ํŒŒ๋‹ญ ํ•˜๋‚˜๋‹น ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ํŒŒ์˜ ๊ธธ์ด๋ฅผ ์ด๋ถ„ ํƒ์ƒ‰์„ ํ†ตํ•ด์„œ ์ •ํ•ด์ค๋‹ˆ๋‹ค. ์ •ํ•œ ํŒŒ์˜ ๊ธธ์ด๋ฅผ ๊ณ„์† ์ด๋ถ„ ํƒ์ƒ‰์„ ํ†ตํ•ด ๊ฐฑ์‹ ํ•˜๊ณ , ์ตœ์ข…์ ์œผ๋กœ (๋ชจ๋“  ํŒŒ์˜ ์–‘) - (๋‹ญ ๊ฐœ์ˆ˜ * ํŒŒ๋‹ญ ํ•˜๋‚˜๋‹น ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ํŒŒ์˜ ๊ธธ์ด)๋ฅผ ๋นผ์ฃผ๋ฉด ์ •๋‹ต์ด ๋ฉ๋‹ˆ๋‹ค.

๋Œ“๊ธ€