File tree 2 files changed +72
-0
lines changed
2 files changed +72
-0
lines changed Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ . "fmt"
5
+ "io"
6
+ "math/big"
7
+ "sort"
8
+ )
9
+
10
+ // https://github.com/EndlessCheng
11
+ type vec60 struct { x , y int }
12
+ func (a vec60 ) sub (b vec60 ) vec60 { return vec60 {a .x - b .x , a .y - b .y } }
13
+ func (a vec60 ) dot (b vec60 ) int { return a .x * b .x + a .y * b .y }
14
+ func (a vec60 ) detCmp (b vec60 ) int {
15
+ v := new (big.Int ).Mul (big .NewInt (int64 (a .x )), big .NewInt (int64 (b .y )))
16
+ w := new (big.Int ).Mul (big .NewInt (int64 (a .y )), big .NewInt (int64 (b .x )))
17
+ return v .Cmp (w )
18
+ }
19
+
20
+ func cf660F (in io.Reader , out io.Writer ) {
21
+ var n , v , s , s2 , ans int
22
+ Fscan (in , & n )
23
+ q := []vec60 {{}}
24
+ for i := 1 ; i <= n ; i ++ {
25
+ Fscan (in , & v )
26
+ s += v
27
+ s2 += v * i
28
+
29
+ p := vec60 {- s , 1 }
30
+ j := sort .Search (len (q )- 1 , func (j int ) bool { return p .dot (q [j ]) > p .dot (q [j + 1 ]) })
31
+ ans = max (ans , p .dot (q [j ])+ s2 )
32
+
33
+ p = vec60 {i , s * i - s2 }
34
+ for len (q ) > 1 && q [len (q )- 1 ].sub (q [len (q )- 2 ]).detCmp (p .sub (q [len (q )- 1 ])) >= 0 {
35
+ q = q [:len (q )- 1 ]
36
+ }
37
+ q = append (q , p )
38
+ }
39
+ Fprint (out , ans )
40
+ }
41
+
42
+ //func main() { cf660F(bufio.NewReader(os.Stdin), os.Stdout) }
Original file line number Diff line number Diff line change
1
+ // Generated by copypasta/template/generator_test.go
2
+ package main
3
+
4
+ import (
5
+ "github.com/EndlessCheng/codeforces-go/main/testutil"
6
+ "testing"
7
+ )
8
+
9
+ // https://codeforces.com/problemset/problem/660/F
10
+ // https://codeforces.com/problemset/status/660/problem/F?friends=on
11
+ func Test_cf660F (t * testing.T ) {
12
+ testCases := [][2 ]string {
13
+ {
14
+ `6
15
+ 5 -1000 1 -3 7 -8` ,
16
+ `16` ,
17
+ },
18
+ {
19
+ `5
20
+ 1000 1000 1001 1000 1000` ,
21
+ `15003` ,
22
+ },
23
+ {
24
+ `3
25
+ -60 -70 -80` ,
26
+ `0` ,
27
+ },
28
+ }
29
+ testutil .AssertEqualStringCase (t , testCases , 0 , cf660F )
30
+ }
You can’t perform that action at this time.
0 commit comments