[ 백준 ] 1263 / 시간 관리

金弘均·2021년 9월 15일
0

Baekjoon Online Judge

목록 보기
118/228
post-thumbnail

# Appreciation

/*
 * Problem :: 1263 / 시간 관리
 *
 * Kind :: Binary Search
 *
 * Insight
 * - 최대한 늦게 일을 시작할 수 있는 시간이 몇 시일까...
 *   가장 빨리 끝내야 하는 일(Si 가 가장작은 일)을 w 라고 할 때,
 *   가능한 범위는 [0, w.s - w.t] 이다
 *   + 범위가 [0, 10^6) 정도이면 N 이 (0, 1000] 이니까
 *     O(10^9) 이면... 시간 제한에 걸릴것 같다
 *     # 이분 탐색으로 찾아버리면 되지 뭐
 */

# Code

//
//  BOJ
//  ver.C++
//
//  Created by GGlifer
//
//  Open Source

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

using namespace std;

#define endl '\n'

// Set up : Global Variables
struct Work { int t, s; };

// Set up : Functions Declaration
bool operator < (const Work &u, const Work &v);
bool isPossible(int h, vector<Work> &works);


int main()
{
    // Set up : I/O
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    // Set up : Input
    int N; cin >> N;
    vector<Work> works(N);
    for (int i=0; i<N; i++) {
        cin >> works[i].t >> works[i].s;
    }

    // Process
    sort(works.begin(), works.end()); /* 빨리 끝내야 하는 일 순서대로 정렬 */

    int l = 0;
    Work fw = works.front(); /* 가장 빨리 끝내야 하는 일 */
    int r = fw.s - fw.t;

    int ans = -1;
    while (l <= r) {
        int m = (l + r) / 2;
        if (isPossible(m, works)) {
            ans = m;
            l = m+1;
        } else {
            r = m-1;
        }
    }

    // Control : Output
    cout << ans << endl;
}

// Helper Functions
bool operator < (const Work &u, const Work &v)
/* 일 구조체 비교(작음)를 위한 함수 */
{
    return make_pair(u.s, u.t) < make_pair(v.s, v.t);
}

bool isPossible(int h, vector<Work> &works)
/* h 시부터 시작하여 일을 끝낼 수 있으면 true 를 반환, 그 외 false 를 반환 */
{
    for (Work work : works) {
        h += work.t; /* 현재 시간에 주어진 일에 걸리는 시간을 더함 */
        if (h > work.s) /* 마감시간을 넘기면 */
            return false; /* 실패 */
    } return true;
}
profile
이런 미친 게임을 봤나! - 옥냥이

0개의 댓글