Submission #827160

# Submission time Handle Problem Language Result Execution time Memory
827160 2023-08-16T09:27:12 Z wortelworm Rice Hub (IOI11_ricehub) C++17
100 / 100
134 ms 1620 KB
#include "ricehub.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int besthub(int RiceCount, int Length, int RiceFields[], long long Budget)
{
    if (RiceCount <= 5000) {

        ll best_count = 0;
        for (ll i = 0; i < RiceCount; i++)
        {
            ll x = RiceFields[i];

            ll cost = 0;
            ll count = 1;
            ll backwards_index = i-1;
            ll forwards_index = i+1;
            while (cost <= Budget) {
                if (backwards_index < 0 && forwards_index >= RiceCount) {
                    return RiceCount;
                }

                ll backwards = backwards_index < 0 ? 1e9 : x-RiceFields[backwards_index];
                ll forwards = forwards_index >= RiceCount ? 1e9 : RiceFields[forwards_index]-x;

                if (forwards < backwards) {
                    cost += forwards;
                    forwards_index++;
                    count++;
                } else {
                    cost += backwards;
                    backwards_index--;
                    count++;
                }
            }
            count --;
            best_count = max(best_count, count);
        }
        return best_count;
    }


    ll count_before = 0;
    ll count_after = 0;
    ll cost = 0;

    ll best_count = 1;

    for (ll i = 1; i < RiceCount; i++)
    {
        // set the hub at this field
        ll x = RiceFields[i];
        ll distance = RiceFields[i] - RiceFields[i-1];

        // account for the prev_index now also having distance
        count_before++;

        cost += distance * count_before;
        cost -= distance * count_after;

        if (count_after > 0) {
            count_after --;
        }

        auto add_after = [&](){
            while (cost <= Budget && i+count_after < RiceCount-1) {
                count_after++;
                cost += RiceFields[i+count_after] - x;
            }

            if (cost > Budget && count_after > 0) {
                cost -= RiceFields[i+count_after] - x;
                count_after--;
            }
        };


        add_after();
        while (
            count_before > 0
            && (
                cost > Budget
                || (
                    i+count_after < RiceCount-1 &&
                    (x - RiceFields[i-count_before]) >= (RiceFields[i+count_after+1] - x)
                )
            )) {
    
            cost -= x - RiceFields[i-count_before];
            count_before--;

            add_after();
        }

        ll current_count = count_before + count_after + 1;
        if (current_count > best_count) {
            best_count = current_count;
        }
    }

    return best_count;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 308 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 312 KB Output is correct
15 Correct 0 ms 312 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 312 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 2 ms 212 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 2 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 1 ms 308 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 2 ms 212 KB Output is correct
15 Correct 1 ms 312 KB Output is correct
16 Correct 1 ms 312 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 3 ms 212 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Correct 5 ms 212 KB Output is correct
21 Correct 5 ms 340 KB Output is correct
22 Correct 3 ms 324 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 41 ms 328 KB Output is correct
25 Correct 36 ms 340 KB Output is correct
26 Correct 134 ms 348 KB Output is correct
27 Correct 31 ms 348 KB Output is correct
28 Correct 26 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 10 ms 1480 KB Output is correct
4 Correct 11 ms 1456 KB Output is correct
5 Correct 4 ms 836 KB Output is correct
6 Correct 4 ms 852 KB Output is correct
7 Correct 8 ms 1236 KB Output is correct
8 Correct 8 ms 1312 KB Output is correct
9 Correct 4 ms 724 KB Output is correct
10 Correct 4 ms 708 KB Output is correct
11 Correct 9 ms 1280 KB Output is correct
12 Correct 11 ms 1244 KB Output is correct
13 Correct 5 ms 852 KB Output is correct
14 Correct 5 ms 852 KB Output is correct
15 Correct 7 ms 1364 KB Output is correct
16 Correct 7 ms 1364 KB Output is correct
17 Correct 8 ms 1552 KB Output is correct
18 Correct 12 ms 1548 KB Output is correct
19 Correct 10 ms 1620 KB Output is correct
20 Correct 9 ms 1620 KB Output is correct