Ayumu the chimp is given the task of sorting a group of integers a 0 , a 1 , a 2 , a 3 . . into a non-decreasing order. Unfortunately Ayumu can only perform one operation,a single shift. That is,he can only move the last element of a sequence to the beginning. a 0 , a 1 , a 2 … a n → a n , a 0 , a 1 … a n − 1 Let F ( a ) be the minimum number of times Ayumu has to do the single shift so that the sequence a is sorted. If a cannot be sorted with the shift operation then F ( a ) is equal to − 1 .
This text file contains 100 lines and in each line there is a sequence a . Find the sum of F ( a ) for all the sequences in the text file.
Explicit examples
For the sequence a = [ 2 , 1 ] , F ( a ) = 1 .
If a = [ 1 ] , F ( a ) = 0
If a = [ 1 , 3 , 2 ] , F ( a ) = − 1
If a = [ 1 , 2 ] , F ( a ) = 0
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
basic outline with file in location and file stripped of '[' and ']'
This solution works fine in O(N) where N is the number of integers: for each sequence, I count the times when the i-est integer is strictly less than his predecessor (with the variable " numb ") and I save the lenght of the righmost non-decreasing subsequence (with the variable " j ").
The only bug in this program is the case when numb is equal to one but the rightmost integer of the sequence is bigger than the leftmost integer of the sequence: but it's easy to debug (we have just to introduce a variable to store the leftmost integer of the sequence and then the condition to re-order our list of integer is ( numb == one AND leftmost >= rightmost ) )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
java code:
import java.io.*;
public class brilliant201409071744{
public static void main(String[] args){
int sum = 0;
BufferedReader file = null;
try{file = new BufferedReader(new FileReader("brilliant201409071744.txt"));}catch(Exception ex){}
String line;
try{while(!(line=file.readLine()).equals("")){
String[] split = line.split(" ");
int[] numbers = new int[split.length+1];
for(int i=0;i<split.length;i++) numbers[i] = Integer.parseInt(split[i]);
numbers[split.length] = numbers[0];
int f = -2;
for(int i=0;i<split.length;i++){
if(numbers[i]>numbers[i+1]){
if(f==-2) f = split.length-i-1;
else f = -1;
}
}
if(f==-2) f=0;
sum += f;
}}catch(Exception ex){}
System.out.println(sum);
}
}
output:
14575
Press any key to continue . . .
Note: I have modified the text file to remove all the punctuation marks.
Pseudocode:
For every set
If the set a is ordered then F(a)=Switch Counter. Stop.
if the set is not orderered:
2.2. the first element<the last one:
F(a) = -1. Stop.
2.3. the first element>=the last one
switch
increase Switch Counter.
goto 1.
Of course with "switch" I meant that the content of the sequence deprived of the last element be shifted to the right by one place, leaving space for putting it on the first position.
A rather inefficient way of going about things, but luckily computers are fast and the data set was small. The valid lines are rotated arrays of increasing numbers. Find the smallest element then rotate them back, and add the amount you had to rotate by to your accumulator. If 'rotated != sorted(rotated)' we know the original array wasn't an array of increasing numbers, so we subtract one from our accumulator.
import ast
def rotate(array, pivot):
# the new array starts at the pivot
return array[pivot:] + array[:pivot]
accum = 0
for line in open('lists').readlines():
# the data set conveniently uses python syntax
line = ast.literal_eval(line)
smallest = line.index(min(line))
rotated = rotate(line, smallest)
if rotated != sorted(line):
accum -= 1
continue
if smallest == 0:
continue
accum += len(line) - smallest
print accum
This solution isn't actually 100% correct. Consider the input:
[1, 1, 2, 2, 3, 4, 5, 1]
The list is totally valid (it takes 1 shift to sort it), but your code would deem F ( x ) = − 1 in that case because it would rotate on the first 1 , not the one at the end. However, there are in fact no lists in the input file with this property, so the solution works anyway.
My solution is basically the same as yours, except I checked for the wrap-around case. Both solutions are O ( N l o g ( N ) ) , where N is the length of the list, and minimum bound is O ( N ) , so I don't know about calling this inefficient. O ( N ) is probably possible too, though.
Problem Loading...
Note Loading...
Set Loading...
This is not optimized at all, I just wrote what I first had in mind since there is only a 100 lines.