-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnikita_and_the_game.hs
More file actions
52 lines (46 loc) · 1.43 KB
/
nikita_and_the_game.hs
File metadata and controls
52 lines (46 loc) · 1.43 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import Data.Functor
import Control.Applicative
import Data.Maybe
import qualified Data.Vector as V
bsearch :: Int -> Int -> Int -> V.Vector Int -> Maybe Int
bsearch lidx plsum prsum xs = bsearch_ lidx plsum prsum xs
bsearch_ :: Int -> Int -> Int -> V.Vector Int -> Maybe Int
bsearch_ lidx plsum prsum xs
| V.length xs == 1 = if plsum == prsum + V.head xs
then return lidx
else Nothing
| otherwise =
let
half = (V.length xs) `div` 2
(l, r) = V.splitAt half xs
lsum = plsum + V.sum l
rsum = prsum + V.sum r
in
if lsum == 0 && rsum == 0
then Just lidx
else
if lsum == rsum
then return (lidx + half)
else if lsum < rsum
then bsearch (lidx + half) lsum prsum r
else bsearch lidx plsum rsum l
split :: V.Vector Int -> Maybe (V.Vector Int, V.Vector Int)
split xs =
let idx = bsearch 0 0 0 xs
in V.splitAt <$> idx <*> Just xs
solve :: V.Vector Int -> Int
solve xs
| V.all (==0) xs = (V.length xs) - 1
| V.length xs < 2 = 0
| otherwise =
case split xs of
Just (l, r) ->
1 + (max (solve l) (solve r))
Nothing -> 0
main = do
t <- read <$> getLine
mapM (\_ -> do
n <- read <$> getLine
xs <- V.fromList <$> map read <$> take n <$> words <$> getLine
putStrLn $ show $ solve xs
) [1..t]