제출 #1039296

#제출 시각아이디문제언어결과실행 시간메모리
1039296idas선물상자 (IOI15_boxes)C++11
25 / 100
1 ms348 KiB
#include "boxes.h"
#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i=(begin); i<(end); i++)

using namespace std;
typedef long long ll;

const ll LINF=1e18;
const int MxN=1e7+10;
int n, k, l;

long long delivery(int N, int K, int L, int P[]) {
    n=N; k=K; l=L;

    int neg=0;
    vector<int> p;
    FOR(i, 0, n)
    {
        if(P[i]==0){
            neg++;
            continue;
        }
        p.push_back(P[i]);
    }
    n-=neg;

    bool bad=false;
    FOR(i, 0, n) if(p[i]==l/2) bad=true;

    ll ans=LINF, tot=0;
    if((l&1) || !bad){
        FOR(z, 0, 2)
        {
            int mx=n;
            FOR(i, 0, n)
            {
                if(p[i]>l/2){
                    mx=i;
                    break;
                }
            }
//            cerr << "max: " << mx << endl;

            int st_in=(mx)%k-1;
            if(st_in>=0) tot+=p[st_in]*2;

            for(int i=st_in+k; i<mx; i+=k)
            {
                tot+=p[i]*2;
            }

            FOR(i, 0, n)
            {
                p[i]=l-p[i];
            }
            reverse(p.begin(), p.end());
        }
        ans=min(ans, tot);
    }

    tot=0;
    FOR(z, 0, 2)
    {
        int st_in=n%k-1;
        if(st_in>=0) tot+=p[st_in]*2;

        bool fnd=true, crossed=false;
        for(int i=st_in+k; i<n; i+=k){
            if(i==n-1) fnd=true;
            if(crossed) tot+=min(p[i], l-p[i]);
            else tot+=p[i];
            if(p[i]>=l/2) crossed=true;
            tot+=min(p[i], l-p[i]);
        }

        assert(fnd);

        ans=min(ans, tot);
        tot=0;

        FOR(i, 0, n)
        {
            p[i]=l-p[i];
        }
        reverse(p.begin(), p.end());
    }

    return ans;
}
#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...