Permalink
Browse files

Solution for 10041 added

  • Loading branch information...
kharus committed Mar 30, 2012
1 parent b85df63 commit 0712a39a347b14fce1346dfa72960c6c8f987d94
Showing with 54 additions and 0 deletions.
  1. +43 −0 10041/10041.cc
  2. +11 −0 10041/print_distance.rb
View
@@ -0,0 +1,43 @@
+#include<iostream>
+#include<vector>
+#include<cstdlib>
+#include<algorithm>
+
+using namespace std;
+
+int min_distance(vector<int>& addresses);
+
+int main(){
+ vector<int> results;
+ int number_of_cases;
+ cin>>number_of_cases;
+
+
+ for(int i=1; i<=number_of_cases; i++){
+ int number_of_relatives;
+ vector<int> relatives_addresses;
+ cin>>number_of_relatives;
+ for(int n=1; n<=number_of_relatives; n++){
+ int relative_address;
+ cin>>relative_address;
+ relatives_addresses.push_back(relative_address);
+ }
+ results.push_back(min_distance(relatives_addresses));
+ }
+
+ for(int i=0; i<number_of_cases; i++){
+ cout<<results[i]<<endl;
+ }
+
+}
+
+int min_distance(vector<int>& addresses){
+ sort(addresses.begin(), addresses.end());
+ int target_index = addresses.size()/2;
+ int target_distance=0;
+ for (int i=0; i<addresses.size(); i++){
+ target_distance+=abs(addresses[i]-addresses[target_index]);
+ }
+ return target_distance;
+}
+
View
@@ -0,0 +1,11 @@
+raw_addresses = gets
+adresses = raw_addresses.split
+numbers = adresses.collect{ |s| s.to_i}
+
+min_address = numbers.min
+max_address = numbers.max
+
+min_address.upto max_address do | number |
+ distance = numbers.reduce(0){|total, x| total+=(x-number).abs}
+ puts "#{number}:\t#{distance}"
+end

0 comments on commit 0712a39

Please sign in to comment.