Tuesday, January 13, 2015

Topcoder SRM 645 div2 250 problem~

I attended SRM 645 last night.
that time I feel frustrated because of my understanding.

there are three problems and I was able to solve easy one.
but failed.

anyway

To solve this problem.
Algorithm is not needed.


my answer is below




 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
35
36
37
38
package srm646;

import java.util.Arrays;

public class OtherBacteriesColony {

 public static void main(String args[]) {

  int[] colonies = { 24, 8, 56, 96, 97, 89, 68, 2, 81, 19, 68, 90, 33,
    85, 37 };
  System.out.println(Arrays.toString(performTheExperiment(colonies)));
 }

 static int[] performTheExperiment(int[] colonies) {

  int n= colonies.length;
  boolean changed = true;
  
  while (changed) {
   changed = false;
   int []b = colonies.clone();
   for (int i = 1; i < n- 1; i++) {
    if (colonies[i - 1] > colonies[i]
      && colonies[i + 1] > colonies[i]) {
     // +1
     b[i] ++;
     changed= true;
    } 
    if (colonies[i - 1] < colonies[i] && colonies[i + 1] < colonies[i]) {
     b[i] --;
     changed = true;
    }
   }
   colonies= b;
  }
  return colonies;
 }
}

No comments:

Post a Comment