Submission #783269

#TimeUsernameProblemLanguageResultExecution timeMemory
783269shezittBoxes with souvenirs (IOI15_boxes)C++17
20 / 100
1 ms212 KiB
#include <bits/stdc++.h>
#include <boxes.h>
using namespace std;

#define sz(x) (int) x.size()
#define dbg(x) cout << #x << ": " << x << endl;

using ll = long long;

long long delivery(int N, int K, int L, int p[]){
	// SUBTASK 1
	if(K == 1){
		ll ans = 0;
		for(int i=0; i<N; ++i){
			ans += min(p[i], L - p[i]) * 2;
		}
		return ans;	
	}
	
	// SUBTASK 2
	if(K == N){
		ll ans = 2e9+5;
		sort(p, p+N);
		
		// opcion 1 - desde izquierda voy y regreso hasta el final
		ans = p[N-1] * 2;
		
		// opcion 2 - desde derecha voy y regreso hasta el comienzo
		ans = min(ans, (L - p[0])*2ll);
		
		// opcion 3 - vuelta completa
		ans = min(ans, 1ll*L);
		
		// opcion 4 - voy por la izquierda hasta cierto punto, vuelvo y voy desde la derecha hasta donde me faltaba
		if(N > 1){ 
			for(int i=0; i+1<N; ++i){
				ll x = p[i];
				ll y = p[i+1];
				ans = min(ans, 2*x + 2*(L - y));
			}
		}
		return ans;
	}
	
	// SUBTASK 3
	if(N <= 10){
		ll ans = 2e9+5;
		for(int mask=0; mask<(1<<N); ++mask){
			// 0 -> from left
			// 1 -> from right
			
			ll res = 0;
			
			vector<int> left, right;
			for(int i=0; i<N; ++i){
				if((1<<i) & mask){
					right.push_back(p[i]);
				} else {
					left.push_back(p[i]);
				}
			}
			
			// left
			ll cur = K;
			ll prev = 0;
			for(int pos : left){
				res += pos - prev;
				prev = pos;
				cur--;
				if(cur == 0){
					// vuelvo al origen
					res += pos;
					prev = 0;
					cur = K;
				}
			}
			
			// right
			cur = K;
			prev = L;
			for(int i=sz(right)-1; i>=0; --i){
				int pos = right[i];
				res += prev - pos;
				prev = pos;
				cur--;
				if(cur == 0){
					// vuelvo al origen
					res += L - pos;
					prev = L;
					cur = K;
				}
			}
			
			ans = min(ans, res);
			
		}
		
		return ans;
	}
	
	return -1;
}
#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...