Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Enter your code here. Read input from STDIN. Print output to STDOUT
- % Your class should be named solution
- -module(solution).
- -export([main/0]).
- read_hights(0, Acc) -> Acc;
- read_hights(N, Acc) ->
- {ok, [H]} = io:fread("", "~d"),
- read_hights(N - 1, [H|Acc]).
- %smallest(Array, S, L) ->
- % lists:foldl(fun(Idx, {Smallest, SIdx}) ->
- % E = array:get(Idx, Array),
- % case E < Smallest of
- % true -> {E, Idx};
- % false -> {Smallest, SIdx}
- % end
- % end, {array:get(S, Array), S} , lists:seq(S, S + L - 1)).
- smallest(Array, S, L) -> smallest_1(Array, S, L, {array:get(S, Array), S}).
- smallest_1(_, _, 0, {Smallest, SIdx}) -> {Smallest, SIdx};
- smallest_1(Array, S, L, {Smallest, SIdx}) ->
- E = array:get(S, Array),
- case E < Smallest of
- true -> smallest_1(Array, S + 1, L -1, {E, S});
- false -> smallest_1(Array, S + 1, L -1, {Smallest, SIdx})
- end.
- max_of(A, B, C) ->
- case (A >= B) and (A >= C) of
- true -> A;
- false -> case (B >= A) and (B >= C) of
- true -> B;
- false -> case (C >= A) and (C >= B) of
- true -> C;
- false -> error
- end
- end
- end.
- max_area(_, _, L, {ResRef, Parent}) when L =< 0 ->
- Parent ! {ResRef, 0};
- max_area(Hights, S, L, {ResRef, Parent}) ->
- {Smallest, SIdx} = smallest(Hights, S, L),
- LRef = make_ref(),
- RRef = make_ref(),
- Self = self(),
- spawn(fun() -> max_area(Hights, S, SIdx - S, {LRef, Self}) end),
- spawn(fun() -> max_area(Hights, SIdx + 1, L - SIdx - 1 + S, {RRef, Self}) end),
- TA = Smallest * L,
- LA = receive {LRef, LRes} -> LRes end,
- RA = receive {RRef, RRes} -> RRes end,
- Parent ! {ResRef, max_of(TA, LA, RA)}.
- get_area(Hights, S, L) ->
- Ref = make_ref(),
- Self = self(),
- spawn(fun() -> max_area(Hights, S, L, {Ref, Self}) end),
- receive
- {Ref, Res} -> Res
- end.
- main() ->
- {ok, [N]} = io:fread("", "~d"),
- Hights = array:from_list(read_hights(N, [])),
- io:format("~w~n", [get_area(Hights, 0, N)]).
Advertisement
Add Comment
Please, Sign In to add comment