답안 #486149

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
486149 2021-11-10T15:54:29 Z Jomnoi 쌀 창고 (IOI11_ricehub) C++17
68 / 100
160 ms 632 KB
#include <bits/stdc++.h>
#include <ricehub.h>
#define DEBUG 0
using namespace std;

int cost(int R, int X[], int mid, long long B) {
    long long closest = 1e18, sum = 0;
    int l, r, pos, ct = 0, ok;
    for(int i = 0; i < R; i++) {
        if(abs(X[i] - mid) < closest) {
            closest = abs(X[i] - mid);
            pos = i;
        }
    }
    if(X[pos] <= mid) {
        r = min(R - 1, pos + 1);
        l = r - 1;
    }
    else {
        l = max(0, pos - 1);
        r = l + 1;
    }
    l = r - 1;
    while(1) {
        closest = 1e18;
        if(l >= 0 && closest > abs(mid - X[l])) {
            closest = abs(mid - X[l]);
            ok = 0;
        }
        if(r < R && closest > abs(X[r] - mid)) {
            closest = abs(X[r] - mid);
            ok = 1;
        }
        if(sum + closest > B) {
            return ct;
        }
        ct++;
        sum += closest;
        if(!ok) {
            l--;
        }
        else {
            r++;
        }
    }
    return R;
}

int besthub(int R, int L, int X[], long long B) {
    int l = 1, r = L, ans = 0, mid, cost1;
    int l2, r2, mid2, pos2, cost2;
    while(l <= r) {
        mid = (l + r) / 2;
        cost1 = cost(R, X, mid, B);
        l2 = l + 1, r2 = r, pos2 = r;
        while(l2 <= r2) {
            mid2 = (l2 + r2) / 2;
            if(cost1 == cost(R, X, mid2, B)) {
                l2 = mid2 + 1;
            }
            else {
                r2 = mid2 - 1;
                pos2 = min(pos2, mid2);
            }
        }
        cost2 = cost(R, X, pos2, B);
        if(cost1 > cost2) {
            r = mid - 1;
            ans = max(ans, cost1);
        }
        else {
            l = mid + 1;
            ans = max(ans, cost2);
        }
    }
    return ans;
}

Compilation message

ricehub.cpp: In function 'int cost(int, int*, int, long long int)':
ricehub.cpp:39:9: warning: 'ok' may be used uninitialized in this function [-Wmaybe-uninitialized]
   39 |         if(!ok) {
      |         ^~
ricehub.cpp:20:24: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
   20 |         l = max(0, pos - 1);
      |                    ~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 2 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 2 ms 204 KB Output is correct
24 Correct 3 ms 204 KB Output is correct
25 Correct 9 ms 324 KB Output is correct
26 Correct 9 ms 320 KB Output is correct
27 Correct 6 ms 204 KB Output is correct
28 Correct 4 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 332 KB Output is correct
2 Correct 6 ms 332 KB Output is correct
3 Correct 131 ms 632 KB Output is correct
4 Correct 160 ms 588 KB Output is correct
5 Incorrect 14 ms 460 KB Output isn't correct
6 Halted 0 ms 0 KB -