제출 #1009739

#제출 시각아이디문제언어결과실행 시간메모리
1009739ProtonDecay314선물상자 (IOI15_boxes)C++17
20 / 100
76 ms404 KiB
/*
???

*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
#define first fi;
#define second se;

vll facts = {1ll, 1ll, 2ll, 6ll, 24ll, 120ll, 720ll, 5040ll, 40320ll, 362880ll, 3628800ll};

ll delivery(int intn, int intk, int intl, int pos_int[]) {
    ll n = intn, k = intk, l = intl;
    vll pos(n, 0ll);
    for(ll i = 0; i < n; i++) {
        pos[i] = pos_int[i];
    }

    ll ans = 0ll;
    if(k == 1) {
        for(ll i = 0; i < n; i++) {
            ans += min(pos[i], l - pos[i]) << 1ll;
        }
    } else if(k == n) {
        sort(pos.begin(), pos.end());
        ll max_left = 0ll;
        ll max_right = l;
        for(ll i = 0; i < n; i++) {
            if(pos[i] > (l >> 1ll) || ((l & 1) == 0ll && pos[i] == (l >> 1ll))) max_right = min(max_right, pos[i]);
            if(l - pos[i] > (l >> 1ll)) max_left = max(max_left, pos[i]);
        }

        ans = min(l, (l - max_right + max_left) << 1ll);
    } else if(n <= 10) {
        ll cur_cost = 0ll, cur_pos = 0ll, diff = 0ll;
        vll inds;
        for(ll i = 0; i < n; i++) {
            inds.push_back(i);
        }

        for(ll i = 0; i < facts[n]; i++) {
            cur_cost = cur_pos = 0ll;

            for(ll j = 0; j < n - 1; j++) {
                diff = abs(cur_pos - pos[j]);
                cur_cost += min(diff, l - diff);
                cur_pos = pos[j];
                if((j % k) == k - 1) {
                    cur_cost += min(cur_pos, l - cur_pos);
                    cur_pos = 0ll;
                }
            }
            if(n % k != 0ll) {
                cur_cost += min(pos[n - 1], l - pos[n - 1]);
            }

            ans = min(ans, cur_cost);

            next_permutation(inds.begin(), inds.end());
        }
    }
    return ans;
}

#ifdef DEBUG
int main() {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios_base::sync_with_stdio(false);

    int n, k, l;
    cin >> n >> k >> l;

    vi positions(n, 0);
    for(int& pos : positions) {
        cin >> pos;
    }

    cout << delivery(n, k, l, &positions[0]) << endl;
}
#endif
#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...