khalfella

johns_fence

Dec 8th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. % Enter your code here. Read input from STDIN. Print output to STDOUT
  2. % Your class should be named solution
  3.  
  4. -module(solution).
  5. -export([main/0]).
  6. read_hights(0, Acc) -> Acc;
  7. read_hights(N, Acc) ->
  8. {ok, [H]} = io:fread("", "~d"),
  9. read_hights(N - 1, [H|Acc]).
  10.  
  11.  
  12.  
  13. %smallest(Array, S, L) ->
  14. % lists:foldl(fun(Idx, {Smallest, SIdx}) ->
  15. % E = array:get(Idx, Array),
  16. % case E < Smallest of
  17. % true -> {E, Idx};
  18. % false -> {Smallest, SIdx}
  19. % end
  20. % end, {array:get(S, Array), S} , lists:seq(S, S + L - 1)).
  21.  
  22. smallest(Array, S, L) -> smallest_1(Array, S, L, {array:get(S, Array), S}).
  23.  
  24. smallest_1(_, _, 0, {Smallest, SIdx}) -> {Smallest, SIdx};
  25. smallest_1(Array, S, L, {Smallest, SIdx}) ->
  26. E = array:get(S, Array),
  27. case E < Smallest of
  28. true -> smallest_1(Array, S + 1, L -1, {E, S});
  29. false -> smallest_1(Array, S + 1, L -1, {Smallest, SIdx})
  30. end.
  31.  
  32. max_of(A, B, C) ->
  33. case (A >= B) and (A >= C) of
  34. true -> A;
  35. false -> case (B >= A) and (B >= C) of
  36. true -> B;
  37. false -> case (C >= A) and (C >= B) of
  38. true -> C;
  39. false -> error
  40. end
  41. end
  42. end.
  43.  
  44.  
  45. max_area(_, _, L, {ResRef, Parent}) when L =< 0 ->
  46. Parent ! {ResRef, 0};
  47. max_area(Hights, S, L, {ResRef, Parent}) ->
  48. {Smallest, SIdx} = smallest(Hights, S, L),
  49. LRef = make_ref(),
  50. RRef = make_ref(),
  51. Self = self(),
  52. spawn(fun() -> max_area(Hights, S, SIdx - S, {LRef, Self}) end),
  53. spawn(fun() -> max_area(Hights, SIdx + 1, L - SIdx - 1 + S, {RRef, Self}) end),
  54. TA = Smallest * L,
  55. LA = receive {LRef, LRes} -> LRes end,
  56. RA = receive {RRef, RRes} -> RRes end,
  57. Parent ! {ResRef, max_of(TA, LA, RA)}.
  58.  
  59.  
  60.  
  61. get_area(Hights, S, L) ->
  62. Ref = make_ref(),
  63. Self = self(),
  64. spawn(fun() -> max_area(Hights, S, L, {Ref, Self}) end),
  65. receive
  66. {Ref, Res} -> Res
  67. end.
  68.  
  69. main() ->
  70. {ok, [N]} = io:fread("", "~d"),
  71. Hights = array:from_list(read_hights(N, [])),
  72. io:format("~w~n", [get_area(Hights, 0, N)]).
Advertisement
Add Comment
Please, Sign In to add comment