863B - Kayaking - CodeForces Solution


brute force greedy sortings *1500

Please click on ads to support us..

Python Code:

n = int(input(""))
LIST = [int(i) for i in input().split(" ")]
LIST=sorted(LIST)
MIN = 1000*52
for i in range(len(LIST)-1):
    for j in range(i+1,len(LIST)):
        l=list(LIST)
        idx = 0
        instab = 0
        l.pop(i)
        l.pop(j-1)
        for k in range(1,len(l),2):
            instab += l[k]-l[k-1]
        MIN = min(MIN,instab)
print(MIN)

C++ Code:

#include <bits/stdc++.h>
using namespace std;

int main() {
	int n; cin >> n;
	vector<int> v;
	for (int i=0;i<2*n;i++){
		int x; cin>>x;
		v.push_back(x);
	}
	sort (v.begin(),v.end());
	int mininst = 1e9;
	for (int i=0; i<2*n;i++){
		for (int j=i+1;j<2*n;j++){
			vector<int> v2;
			for (int k=0;k<2*n;k++){
				if (k==i||k==j){
					continue;
				}
				else {
					v2.push_back(v[k]);
				}
			}
			int inst=0;
			for (int k=0;k<2*n-2;k+=2){
				inst += v2[k+1]-v2[k];
			}
			mininst=min(mininst,inst);
		}
	}
	cout<<mininst;
}


Comments

Submit
0 Comments
More Questions

1075B - Taxi drivers and Lyft
1562A - The Miracle and the Sleeper
1216A - Prefixes
1490C - Sum of Cubes
868A - Bark to Unlock
873B - Balanced Substring
1401D - Maximum Distributed Tree
1716C - Robot in a Hallway
1688B - Patchouli's Magical Talisman
99A - Help Far Away Kingdom
622B - The Time
1688C - Manipulating History
1169D - Good Triple
1675B - Make It Increasing
588A - Duff and Meat
1541B - Pleasant Pairs
1626B - Minor Reduction
1680A - Minimums and Maximums
1713A - Traveling Salesman Problem
1713B - Optimal Reduction
1710A - Color the Picture
1686B - Odd Subarrays
251A - Points on Line
427C - Checkposts
1159A - A pile of stones
508A - Pasha and Pixels
912A - Tricky Alchemy
1249A - Yet Another Dividing into Teams
1713C - Build Permutation
1699A - The Third Three Number Problem