๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/CodeUp

2633 : Lower Bound

by dkswnkk 2022. 1. 14.

๋ฌธ์ œ

 

Lower Bound

์ฒซ ์ค„์— ํ•œ ์ •์ˆ˜ $n$๊ณผ ์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฐ’ $k$๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ž…๋ ฅ๋˜๊ณ , ๋‘˜์งธ ์ค„์— $n$๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ž…๋ ฅ๋œ๋‹ค. (๋‹จ, $2 <= n <= 100,000$ , ๊ฐ ์›์†Œ์˜ ํฌ๊ธฐ๋Š” $100,000,000$์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค

codeup.kr

 

์ฝ”๋“œ

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    
    int n,k; cin>>n>>k;
    vector<int>v;
    
    for(int i=0; i<n; i++){
        int inp; cin>>inp;
        v.push_back(inp);
    }
    sort(v.begin(),v.end());
    
    int start=0, end = n-1;
    while(start<=end){
        int mid = (start+end)/2;
        if(v[mid]<k) start=mid+1;
        else end=mid-1;;
    }
    
    cout<<start+1;
}

 

ํ’€์ด

๋‹จ์ˆœ ์ด๋ถ„ ํƒ์ƒ‰ ๊ตฌํ˜„ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

upper_bound, lower_bound STL์ด ์žˆ๋Š” ๊ฒƒ์€ ์•Œ๊ณ  ์žˆ์ง€๋งŒ, ํ•ด๋‹น STL์‚ฌ์šฉ์ด ์ต์ˆ™ํ•˜์ง€ ์•Š์•„์„œ ์ง์ ‘ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

์ฑ„์ 

 

๋Œ“๊ธ€