Submission #336044

# Submission time Handle Problem Language Result Execution time Memory
336044 2020-12-14T14:40:50 Z aryan12 Rice Hub (IOI11_ricehub) C++17
0 / 100
4 ms 876 KB
#include <bits/stdc++.h>
#include "ricehub.h"
using namespace std;

int besthub(int r, int l, int x[], long long b) {
    long long pre[r + 1], suf[r + 1];
    pre[0] = 0;
    pre[1] = 0;
    for(long long i = 2; i <= r; i++) {
        pre[i] = pre[i - 1] + (x[i] - x[i - 1]) * (i - 1);
    }
    suf[r] = 0;
    for(long long i = r - 1; i >= 1; i--) {
        suf[i] = suf[i + 1] + (x[i + 1] - x[i]) * (r - i);
    }
    /*for(long long i = 1; i <= r; i++) {
        cout << pre[i] << " ";
    }
    cout << endl;
    for(long long i = 1; i <= r; i++) {
        cout << suf[i] << " ";
    }
    cout << endl;*/
    long long ans = 1;
    for(long long i = 2; i <= r; i++) {
        long long left = 1, right = i, best = r;
        while(left <= right) {
            long long LeftPoint = (left + right) / 2;
            long long OptimalField = (LeftPoint + i) / 2;
            long long cost = pre[OptimalField], cost1 = suf[OptimalField];
            cost -= (pre[LeftPoint] + ((x[OptimalField] - x[LeftPoint]) * (LeftPoint - 1)));
            cost1 -= (suf[i] + ((x[i] - x[OptimalField]) * (r - i)));
            cost = abs(cost);
            cost1 = abs(cost);
            cost += cost1;

            /*for(long long j = LeftPoint; j <= i; j++) {
                cost += abs(x[j] - x[OptimalField]);
            }*/
            //cout << "i = " << i;
            //cout << ", left = " << left << ", right = " << right << ", LeftPoint = " << LeftPoint << ", Optimal Field = " << OptimalField << endl;
            if(cost <= b) {
                right = LeftPoint - 1;
                best = LeftPoint;
            }
            else {
                left = LeftPoint + 1;
            }
        }
        //cout << i << " " << best << endl;
        ans = max(ans, i - best + 1);
    }
    int lol = ans;
    return lol;
}

/*int main() {
    //cout << "Input the number of rice fields" << endl;
    int r;
    cin >> r;
    //cout << "Input the maximum coordinate" << endl;
    int l;
    cin >> l;
    //cout << "Input r numbers, denoting the rice fields" << endl;
    int x[r + 1];
    for(int i = 1; i <= r; i++) {
        cin >> x[i];
    }
    //cout << "Input the budget" << endl;
    int b;
    cin >> b;
    cout << besthub(r, l, x, b) << endl;
}
*/
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 768 KB Output is correct
2 Incorrect 4 ms 876 KB Output isn't correct
3 Halted 0 ms 0 KB -