fork(3) download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Test
  5. {
  6. enum CurveState {
  7. GoingDown=0, EqGoingDown=1, NotGoingDown=2
  8. }
  9. private static IList<int> GetValleys(IList<int> a) {
  10. var res = new List<int>();
  11. if (a.Count < 2) {
  12. return res;
  13. }
  14. int lastEq = 0;
  15. CurveState s = CurveState.NotGoingDown;
  16. for (var i = 1 ; i != a.Count ; i++) {
  17. switch(Math.Sign(a[i]-a[i-1])) {
  18. case -1:
  19. s = CurveState.GoingDown;
  20. break;
  21. case 0:
  22. if (s == CurveState.GoingDown) {
  23. lastEq = i;
  24. }
  25. s = (s==CurveState.NotGoingDown) ? CurveState.NotGoingDown : CurveState.EqGoingDown;
  26. break;
  27. case 1:
  28. if (s == CurveState.GoingDown) {
  29. res.Add(i-1);
  30. } else if (s == CurveState.EqGoingDown) {
  31. res.Add((lastEq+i-1)/2);
  32. }
  33. s = CurveState.NotGoingDown;
  34. break;
  35. }
  36. }
  37. return res;
  38. }
  39. public static void Main()
  40. {
  41. int[] data = new[] {0,0,1,2,4,7,9,6,4,4,2,1,0,0,0,0,0,3,5,5,10,3,2,1,4,5,7,7,12,11,8,4,2,
  42. 1,1,1,2,4,9,4,2,2,0,1,3,6,13,11,5,5,2,2,3,4,7,11,8};
  43. IList<int> x = GetValleys(data);
  44. for (var i=0 ; i != data.Length ; i++ ) {
  45. Console.WriteLine("{0} {1}", data[i], x.IndexOf(i) >= 0 ? "*" : "");
  46. }
  47. }
  48. }
Success #stdin #stdout 0.05s 24304KB
stdin
Standard input is empty
stdout
0 
0 
1 
2 
4 
7 
9 
6 
4 
4 
2 
1 
0 
0 
0 *
0 
0 
3 
5 
5 
10 
3 
2 
1 *
4 
5 
7 
7 
12 
11 
8 
4 
2 
1 
1 *
1 
2 
4 
9 
4 
2 
2 
0 *
1 
3 
6 
13 
11 
5 
5 
2 
2 *
3 
4 
7 
11 
8