제출 #1137324

#제출 시각아이디문제언어결과실행 시간메모리
1137324viwlesxq선물상자 (IOI15_boxes)C++20
10 / 100
0 ms328 KiB
#include "boxes.h"
#include <bits/stdc++.h>

using namespace std;

long long delivery(int n, int k, int l, int p[]) {
    auto dist = [&](int x, int y) {
        if (x > y) swap(x, y);
        return min(y - x, x + l - y);
    };

    long long res = 0;

    deque<int> q;
    for (int i = 0; i < n; ++i) {
        q.push_back(p[i]);
    }

    int pos = 0;

    while (!q.empty()) {
        int f, c = k;
        if (dist(pos, q.front()) < dist(pos, q.back())) f = 1;
        else f = 0;

        while (!q.empty() && c--) {
            if (f) {
                if (dist(pos, q.front()) < dist(pos, q.back())) {
                    res += dist(pos, q.front());
                    pos = q.front();
                    q.pop_front();
                } else break;
            } else {
                if (dist(pos, q.front()) >= dist(pos, q.back())) {
                    res += dist(pos, q.back());
                    pos = q.back();
                    q.pop_back();
                } else break;
            }
        }

        res += dist(pos, 0);
        pos = 0;
    }

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