Compy's Blog
443 words
2 minutes
[BOJ] Street Development
2025-10-15

Street Development

TimeLimitMemoryLimitConditionTAG
1s2048MB(1 ≤ N ≤ 10^6)Binary Search, Greedy, Implementation

아니 문제가 아이디어는 그대로 사용했는데… 제 로직에서 over flow를 예상해서 long long을 사용했지만 또 overflow가 나올 것을 생각하지 못하고 정말 오랫동안 생각했네요.. ㅠㅜㅜㅜ

정답 코드
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <sstream>
#include <iomanip>
#include <string>
#include <numeric>
#include <unordered_set>
#include <unordered_map>
#include <climits>
#include <math.h>
using namespace std;
using ll = long long;
using ld = long double;
//ll MOD = 1'000'000'007;
ll MOD = 1'000'000'000;
//ll MOD = 1.8446744074E19;

int L, N;
ll robots[1000001];
ll dis[1000001];

// 문제를 너무 대충 봄.
// 그냥 간격 자체를 찾는거롤 하는게 맞는데 뭐... 뭐를 한거지 젠장
bool calculation(ll power){
    ll right = robots[N-1] - power;
    dis[N-1] = right;
    
    // right
    for(int i = N-2; i > -1; i--){
        ll remaining = power;
        if(right > robots[i]) remaining -= (right - robots[i]) * 2;
        
        right = robots[i] - remaining;
        if(right > L) right = L;
        dis[i] = right;
//        cout << i << " " << dis[i] <<"\n";
    }
    
    ll left = robots[0] + power;
    
    if(left >= dis[1]) return true;
    
    for(int i = 1; i < N-1; i++){
        ll remaining = power;
        if(left < robots[i]) remaining -= (robots[i] - left) * 2;
        
        
        left = robots[i] + remaining;
        if(left < 0) left = 0;
//        cout << left << " " << dis[i+1] << "\n";
        if(left >= dis[i+1]) return true;
    }
    
    return false;
}
ll solution(){
    
    ll result = LLONG_MAX;
    
    ll left = 0, right = robots[N-1] + 1;
    
    while(left <= right){
        ll pivot = (left + right) /2;
        
        
        if(calculation(pivot)){
            result = min(result, pivot);
            right = pivot-1;
            
        }
        else left = pivot+1;
    }
    
//    cout << left << " " << right << "\n";
    
    return (result);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    cin >> L >> N;
    
    for(int i = 0; i < N; i++) cin >> robots[i];
    
    cout << solution();
//    cout << calculation(64975);
//    for(int i = L; i > -1; i--){
//        if(calculation(i)) continue;
//        cout << i <<"\n";
//        break;
//    }
    return 0;
}
/*
 20 1
 10
 
 100 5
 
 10000 8
 0 1 3 5 7 11 13 17
 
 100000 3
 10 100 101
 100 5
 10 20 30 40 50
 
 10000 8
 0 1 3 5 7 11 13 17
 
*/


[BOJ] Street Development
https://compy07.github.io/Blog/posts/boj/32838/
Author
뒹굴뒹굴 이정훈 공부방
Published at
2025-10-15