답안 #1006711

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1006711 2024-06-24T07:36:07 Z uma 쌀 창고 (IOI11_ricehub) C++14
0 / 100
2 ms 604 KB
#include "ricehub.h"
#include <iostream>
#include <vector>
#include <algorithm>


using namespace std;

typedef long long ll;

int besthub(int R, int L, int positions[], long long B){
    int n = R;

    int left = 0;
    ll current_sum = 0;
    int max_len = 0;
    vector<ll> prefix_vec(n,0);

    prefix_vec[0] = positions[0];

//calculating prefix sum
    for(int  i = 1; i < n; i++){
        prefix_vec[i] = prefix_vec[i-1] + positions[i];
    }

//sliding window concepts
    for(int right = 0; right < n; right++){
        int median = left + (right-left)/2;

        ll left_cost = median > 0?((ll)(median-left)*positions[median] - (prefix_vec[median-1] - (left > 0)?prefix_vec[left-1]:0)):0;
        ll right_cost = (prefix_vec[right] - prefix_vec[median]) - (ll)(right - median)*positions[median];

        int currentCost = left_cost + right_cost;

        while(currentCost > B && median >= left){
            left++;

            median = left + (right-left)/2;

            left_cost = median > 0?((ll)(median-left)*positions[median] - (prefix_vec[median-1] - prefix_vec[left-1])):0;
            right_cost = (prefix_vec[right] - prefix_vec[median]) - (right - median)*positions[median];

            currentCost = left_cost + right_cost;
        } 
        max_len = max(max_len,right-left+1);
    }

    return max_len;
}

Compilation message

ricehub.cpp: In function 'int besthub(int, int, int*, long long int)':
ricehub.cpp:15:8: warning: unused variable 'current_sum' [-Wunused-variable]
   15 |     ll current_sum = 0;
      |        ^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -