Submission #100163

#TimeUsernameProblemLanguageResultExecution timeMemory
100163luciocfBoxes with souvenirs (IOI15_boxes)C++14
35 / 100
3 ms384 KiB
#include <bits/stdc++.h>
#include "boxes.h"

using namespace std;

const int maxn = 1e7+10;

typedef long long ll;

int p[maxn];

ll pref[maxn], suf[maxn];

long long delivery(int N, int K, int L, int positions[])
{
	int n = N, k = K;
	for (int i = 1; i <= n; i++)
		p[i] = positions[i-1];

	int mid = L/2;
	int last_p = 0, last_s = n+1;

	if (p[1] <= mid) pref[1] = 2ll*(ll)p[1], last_p = 1;
	for (int i = 2; i <= n; i++)
	{
		if (p[i] > mid) break;

		pref[i] = 2ll*(ll)p[i] + pref[max(0, i-k)];
		last_p = i;
	}

	if (p[n] > mid) suf[n] = 2ll*((ll)L-(ll)p[n]), last_s = n;

	for (int i = n-1; i >= 1; i--)
	{
		if (p[i] <= mid) break;

		suf[i] = 2ll*((ll)L-(ll)p[i]) + suf[min(n+1, i+k)];
		last_s = i;
	}

	ll ans = pref[last_p] + suf[last_s];

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= k; j++)
		{
			if (!last_p && last_s > n) break;

			if (!last_p) last_s++;
			else if (last_s > n) last_p--;
			else if (p[last_p] > L-p[last_s]) last_p--;
			else last_s++;
		}

		ans = min(ans, 1ll*(ll)i*(ll)L + pref[last_p] + suf[last_s]);
	}

	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...