답안 #1041398

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1041398 2024-08-02T02:44:53 Z That_Salamander 쌀 창고 (IOI11_ricehub) C++17
100 / 100
9 ms 1884 KB
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <cstring>
#include <queue>
#include <unordered_set>
#include <unordered_map>

#define FOR(var,bound) for(int var = 0; var < bound; var++)
#define FORB(var,lb,ub) for (int var = lb; var < ub; var++)
#define FORR(var,bound) for(int var = bound-1; var >= 0; var--)

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;

int besthub(int R, int L, int X[], ll B) {
    ll currCost = 0;

    int lidx = 0;
    int ridx = -1;
    int best = 0;

    while (ridx < R - 1) {
        if (currCost + (X[ridx + 1] - X[0]) <= B) {
            currCost += X[++ridx] - X[0];
        } else break;
    }

    best = (ridx - lidx + 1);

    //cout << "If hub is at " << X[0] << " can reach " << best << " farms!" << endl;

    FORB(i, 1, R) {
        int numMovingBack = (i - lidx);
        int numMovingForward = (ridx - i + 1);

        currCost += numMovingBack * (ll) (X[i] - X[i-1]);
        currCost -= numMovingForward * (ll) (X[i] - X[i-1]);

        if (ridx < i) {
            ridx = i;
        }

        while (currCost > B) {
            if (X[ridx] - X[i] < X[i] - X[lidx]) {
                currCost -= (X[i] - X[lidx++]);
            } else {
                currCost -= (X[ridx--] - X[i]);
            }
        }

        while (ridx < R - 1) {
            if (currCost + X[ridx + 1] - X[i] <= B) {
                currCost += X[++ridx] - X[i];
            } else if (ridx > lidx && X[i] - X[lidx] > X[ridx+1] - X[i]) {
                currCost += X[++ridx] - X[i];
                currCost -= X[i] - X[lidx++];
            } else break;
        }

        if (ridx == R - 1) {
            while (lidx > 0 && currCost + X[i] - X[lidx-1] <= B) {
                currCost += X[i] - X[lidx-1];
                lidx--;
            }
        }

        best = max(best, ridx - lidx + 1);

        //cout << "If hub is at " << X[i] << " can reach " << ridx - lidx + 1 << " farms!" << endl;
        //cout << "Cost = " << currCost << endl;

        /*int count = 1;
        ll cost = 0;

        int nl = i - 1;
        int nr = i + 1;

        while (nl >= 0 && nr < R) {
            if (X[nr] - X[i] > X[i] - X[nl]) {
                if (cost + X[i] - X[nl] > B) break;
                cost += X[i] - X[nl];
                count++;
                nl--;
            } else {
                if (cost + X[nr] - X[i] > B) break;
                cost += X[nr] - X[i];
                count++;
                nr++;
            }
        }

        if (nl < 0) {
            while (nr < R) {
                if (cost + X[nr] - X[i] > B) break;
                cost += X[nr] - X[i];
                count++;
                nr++;
            }
        } else {
            while (nl >= 0) {
                if (cost + X[i] - X[nl] > B) break;
                cost += X[i] - X[nl];
                count++;
                nl--;
            }
        }

        best = max(best, count);*/
    }

    return best;
}

#ifdef LOCAL_TEST
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int R, L, B;
    cin >> R >> L >> B;
    vector<int> X(R);

    FOR(i, R) cin >> X[i];

    cout << besthub(R, L, X.data(), B) << endl;
}
#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 352 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 448 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 448 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 8 ms 1884 KB Output is correct
4 Correct 9 ms 1884 KB Output is correct
5 Correct 4 ms 860 KB Output is correct
6 Correct 3 ms 908 KB Output is correct
7 Correct 6 ms 1412 KB Output is correct
8 Correct 7 ms 1372 KB Output is correct
9 Correct 3 ms 860 KB Output is correct
10 Correct 3 ms 840 KB Output is correct
11 Correct 8 ms 1884 KB Output is correct
12 Correct 7 ms 1744 KB Output is correct
13 Correct 4 ms 816 KB Output is correct
14 Correct 4 ms 988 KB Output is correct
15 Correct 5 ms 1484 KB Output is correct
16 Correct 6 ms 1372 KB Output is correct
17 Correct 6 ms 1676 KB Output is correct
18 Correct 7 ms 1628 KB Output is correct
19 Correct 7 ms 1628 KB Output is correct
20 Correct 7 ms 1628 KB Output is correct