제출 #725950

#제출 시각아이디문제언어결과실행 시간메모리
725950adrilen쌀 창고 (IOI11_ricehub)C++17
68 / 100
79 ms2884 KiB
#include "ricehub.h"
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> pii;



int besthub(int n, int L, int X[], long long b)
{
    vector <ll> fields;
    for (int i = 0; i < n; i++) fields.emplace_back(X[i]);

    if (n <= 5e3)
    {
        ll cur = 0;
        if (n == 1) return 1;
        int output = 0;

        ll p;
        int left, right;
        for (int i = 0; i < n; i++)
        {
            p = fields[i];
            cur = b;
            left = i - 1, right = i + 1;

            while (true)
            {
                if (left < 0 && right == n)
                {
                    break;
                } else if (left < 0)
                {   
                    if (cur >= fields[right] - p) {
                        cur -= fields[right] - p;
                        right++;
                    } else break;
                } else if (right == n)
                {
                    if (cur >= p - fields[left]) {
                        cur -= p - fields[left];
                        left--;
                    } else break;
                }
                else if (p - fields[left] < fields[right] - p)
                {
                    if (cur >= p - fields[left]) {
                        cur -= p - fields[left];
                        left--;
                    } else break;
                } else {
                    if (cur >= fields[right] - p) {
                        cur -= fields[right] - p;
                        right++;
                    } else break;
                }
            }
            output = max(output, right - left - 1);
        }
        return output;
    }

    auto lt = fields.begin(), pos = fields.begin(), rt = fields.begin() + 1;
    ll left, right;
    left = right = 0;
    ll cur = 0;
    ll output = 0;


    while (pos != fields.end())
    {

        while (b < cur)
        {
            cur -= *pos - *lt;
            lt++, left--;
        }

        while (rt != fields.end() && cur + *rt - *pos <= b)
        {   
            cur += *rt - *pos;
            right++, rt++;
        }

        while (rt != fields.end() && (*pos - *lt) > (*rt - *pos))
        {
            left--, right++, lt++, rt++;
            while (rt != fields.end() && cur + *rt - *pos <= b)
            {   
                cur += *rt - *pos;
                right++, rt++;
            }
        }

        output = max(output, left + right + 1);

        if (pos + 1 != fields.end()) {
            cur += (*(pos + 1) - *pos) * (left + 1);
            cur += (*pos - *(pos + 1)) * (right);
        }
        pos++, left++, right--;

    }
    return output;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...