algorithm - Java Performance Very Slow in Competitive Programming -


i've spent hour trying optimize code, yet still seems tle.

 public static void main(string[] args) {     int testcase = sc.nextint();      for(int testcasecount = 0; testcasecount < testcase; ++testcasecount) {         int m = sc.nextint();         int r = sc.nextint();         int[] arr = new int[m];          (int = 0; < m; i++) {             arr[i] = i;         }          (int = 0; < r; i++) {             int x = sc.nextint() - 1;             int index = arr[x];             out.print(index + " ");             shift(arr, x, index);         }          out.println();     }      out.close(); }  public static void shift(int[] arr, int x, int pos) {     for(int = 0; < arr.length; i++) {         if(arr[i] < pos) ++arr[i];     }      arr[x] = 0; } 

this original problem i'm trying solve https://open.kattis.com/problems/moviecollection. tried use stack @ first found out that's way slow. in version, made array keeps track of index ith movie box located. when update location, shifts box want move assigning it's value in array 0, , ones had value less original value of box being moved 0 incremented one. fastest solution can possibly think of, feeling tells me binary search can involved somehow.

is me or java? there better solution?

any highly valued.

this can solved using math. example

input 3 3 3 1 1 output 2 1 0 

you need keep track of current position. need keep track of middle video (this handels 1 when current 3, should return 1).

current = 1; left = numofvideos; mid = 0; video in selected      if video > current:          video - current - mid;      else if video < current:          (current - mid) - video;      else:          0;      current = video;      if current == left || current == 0:           mid = 0      else:           mid = 1; 

if mid part not work, keep mid @ 1. assuming can take off stack @ time.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -