제출 #1355771

#제출 시각아이디문제언어결과실행 시간메모리
1355771truongthaiduonglaptrinhStove (JOI18_stove)C++20
0 / 100
0 ms344 KiB
// duonglaptrinh

# include <bits/stdc++.h>
using namespace std;

const int N = 1e5+5;

bool find_Max(int &ans, int x) {
    // cout<<x<<"\n";
    if(x == -1) {
        return 0;
    }
    ans = min(ans, x);
    return 1;
}

int n, k;
int a[N];

int check(int mid) {
    int ans = 0, i = 1, sl = 0;

    while(i <= n) {
        sl++;
        int j = i;
        while(j <= n && a[j] - a[i] + 1 <= mid) {
            j++;
        }
        ans += (a[j - 1] - a[i] + 1);
        i = j;
    }

    if(sl > k) {
        return -1;
    }

    return ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    if(fopen("nhap.inp", "r")) {
        freopen("nhap.inp", "r", stdin);
        freopen("nhap.out", "w", stdout);
    }

    cin>>n>>k;
    for(int i = 1; i <= n; i++) {
        cin>>a[i];
    }

    sort(a + 1, a + 1 + n);

    int l = 1, r = a[n] + 1, ans = a[n] + 1;

    while(l <= r) {
        int mid = (l + r) / 2;
        // cout<<mid<<" ";
        if(find_Max(ans, check(mid))) {
            r = mid - 1;
        }
        else {
            l = mid + 1;
        }
    }

    cout<<ans;

    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

stove.cpp: In function 'int main()':
stove.cpp:46:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |         freopen("nhap.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
stove.cpp:47:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |         freopen("nhap.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…