Submission #595487

#TimeUsernameProblemLanguageResultExecution timeMemory
595487ApiramBoxes with souvenirs (IOI15_boxes)C++14
20 / 100
1 ms308 KiB
#include "boxes.h"
#include<bits/stdc++.h>
using namespace std;
long long delivery(int N, int K, int L, int p[]) {
	 if (K == 1){
		 long long ans = 0;
		for (int i = 0;i<N;++i){
			ans +=min(p[i],L - p[i]) * 2;
		}
		return ans;
	 }
	 else if (K == N){
		long long ans = min({L,(L - p[0]) * 2,p[N - 1] * 2});
		for (int i = 0;i<N - 1;++i){
			ans = min(ans,(long long)p[i] * 2 + (L - p[i + 1]) * 2);
		}
		return ans;
	 }
	 else{
		 long long ans = 0;
		 vector<int>have(N);
		 vector<int>parent(N);
		 for (int i = 0;i<N;++i){
			 if (p[i] < L - p[i]){
				parent[i] = 0;
			 }
			 else parent[i] = L;
			 ans+=min(p[i],L - p[i]) * 2;
			 have[i] = K - 1;
		 }
		 for (int i = 1;i<N;++i){
			if (have[i - 1] > 0 && min((p[i] - p[i - 1]) * 2,p[i] - p[i - 1] + L - p[i] - p[i - 1]) < abs(parent[i] - p[i]) * 2){
				ans-=abs(parent[i] - p[i]) * 2;
				ans+=min((p[i] - p[i - 1]) * 2,p[i] - p[i - 1] + L - p[i] - p[i - 1]);
				have[i] = have[i - 1] - 1;
				have[i - 1] = 0;
				parent[i] = p[i - 1];
			}
		 }
		 for (int i = N - 2;i>=0;--i){
			if (have[i + 1] > 0 &&min((p[i + 1] - p[i]) * 2,p[i + 1] - p[i] + p[i] - (L - p[i + 1])) < abs(parent[i] - p[i])*2 ){
				ans-=abs(parent[i] - p[i])*2;
				ans+=min((p[i + 1] - p[i]) * 2,p[i + 1] - p[i] + p[i] - (L - p[i + 1]));
				have[i] = have[i + 1] - 1;
				have[i] = 0;
				parent[i] = p[i - 1];
			}
		 }
		 return ans;
	 //first compute the total distance we have to travel to satisfy the teams
	 }
    return 0;
}

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