algo3rythm's blog

By algo3rythm, history, 4 months ago, , Hello everyone,

Since yesterday I have been reading the above stated problem but am not still able to come up with a solution, I have seen the editorial but i don't understand the proof for the algorithm.

LINK TO PROBLEM: http://usaco.org/index.php?page=viewproblem2&cpid=834

PROBLEM STATEMENT

Keeping an eye on long term career possibilities beyond the farm, Bessie the cow has started learning algorithms from various on-line coding websites. Her favorite algorithm thus far is "bubble sort". Here is Bessie's implementation, in cow-code, for sorting an array A of length N.

sorted = false
while (not sorted):
sorted = true
moo
for i = 0 to N-2:
if A[i+1] < A[i]:
swap A[i], A[i+1]
sorted = false

Apparently, the "moo" command in cow-code does nothing more than print out "moo". Strangely, Bessie seems to insist on including it at various points in her code.

Given an input array, please predict how many times "moo" will be printed by Bessie's code.

N<=100,000, each element being an integer in the range 0…10^9. Input elements are not guaranteed to be distinct.

Please help me find an easier to understand solution to the problem, Thanks.  Comments (5)
 » find an easier to understand solution to the problem Easier than what? You didn't show the editorial so it's hard to help you.For each number $x$, think how many times it will move to the left (i.e. $x$ will be swapped with its left neighbor). Also, think whether it's possible that there is something bigger than $x$ on the left but we won't move $x$ to the left in this moo-iteration.
•  » » I'm sorry i will link the editorial
•  » » » My idea was completely different than the editorial so I'm not sure anymore if it's correct. I wanted to claim that for each element we count the number of greater elements to the right, and the answer is the maximum of those counts. I hinted in the first comment on how to prove that — but maybe I made a mistake, idk.As for the intended solution, you just need to prove that max(i - a[i]) decreases by $1$ when it's positive.
 » Auto comment: topic has been updated by algo3rythm (previous revision, new revision, compare).
 » I used 2 HashMaps, one going from the number to its original index, and another going from the number to its frequency. The answer is the max from k = 0 -> n of (index of kth largest number) — k — (frequency of kth largest number).Code: BufferedReader f = new BufferedReader(new FileReader("sort.in")); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("sort.out"))); int n = Integer.parseInt(f.readLine()); array = new int[n]; sorta = new int[n]; HashMap hm = new HashMap(); HashMap freq = new HashMap(); for(int k = 0; k < n; k++){ array[k] = Integer.parseInt(f.readLine()); sorta[k] = array[k]; if(!freq.containsKey(array[k])){ freq.put(array[k],0); } else { freq.put(array[k],freq.get(array[k])+1); } hm.put(array[k],k); } Arrays.sort(sorta); int max = 0; for(int k = 0; k < n; k++){ max = Math.max(max, hm.get(sorta[k])-k-freq.get(sorta[k])); } System.out.println(max+1); out.println(max+1); out.close();