Submission #486135

# Submission time Handle Problem Language Result Execution time Memory
486135 2021-11-10T15:30:49 Z Jomnoi Rice Hub (IOI11_ricehub) C++17
0 / 100
6 ms 460 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 = 1e9;
    int pos;
    for(int i = 0; i < R; i++) {
        if(abs(X[i] - mid) < closest) {
            closest = abs(X[i] - mid);
            pos = i;
        }
    }
    int l = pos, r = pos;
    if(pos == R - 1) {
        l--;
    }
    else {
        r++;
    }
    long long sum = 0;
    int ct = 0;
    while(1) {
        closest = 1e18;
        int ok = -1;
        if(l >= 0 && closest > mid - X[l]) {
            closest = mid - X[l];
            ok = 0;
        }
        if(r < R && closest > X[r] - mid) {
            closest = 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;
    while(l <= r) {
        int mid = (l + r) / 2;
        int cost1 = cost(R, X, mid, B);
        int l2 = l + 1, r2 = r, pos2 = r;
        while(l2 <= r2) {
            int mid2 = (l2 + r2) / 2;
            int cost2 = cost(R, X, mid2, B);
            if(cost1 == cost2) {
                l2 = mid2 + 1;
            }
            else {
                r2 = mid2 - 1;
                pos2 = min(pos2, mid2);
            }
        }
        int 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;
}

// int main() {
//     ios_base::sync_with_stdio(0);
//     cin.tie(0);
//     int r, l, b;
//     cin >> r >> l >> b;
//     int x[l];
//     for(int i = 0; i < r; i++) {
//         cin >> x[i];
//     }
//     cout << besthub(r, l, x, b);
//     return 0;
// }

/*
5 20 6
1 2 10 12 14
*/

Compilation message

ricehub.cpp: In function 'int cost(int, int*, int, long long int)':
ricehub.cpp:20:10: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
   20 |         r++;
      |         ~^~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 1 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 440 KB Output is correct
2 Incorrect 6 ms 460 KB Output isn't correct
3 Halted 0 ms 0 KB -