Skip to content

Commit 0a28492

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 8b46c53 + a4e39a1 commit 0a28492

File tree

15 files changed

+598
-2
lines changed

15 files changed

+598
-2
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Interactive Problem Template
22

3-
The idea is design an interface below, then implement it both in `main.go` and `main_test.go`.
3+
Use below interface and implement it both in `main.go` and `main_test.go`.
44

55
```go
66
type interaction interface {
@@ -10,4 +10,4 @@ type interaction interface {
1010
}
1111
```
1212

13-
In this way we can mock the IO part in `main_test.go`.
13+
In this way we can mock the IO part.

main/1500-1599/1593F.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
. "fmt"
5+
"io"
6+
)
7+
8+
// github.com/EndlessCheng/codeforces-go
9+
func CF1593F(in io.Reader, out io.Writer) {
10+
abs := func(x int) int {
11+
if x < 0 {
12+
return -x
13+
}
14+
return x
15+
}
16+
17+
var T, n, ma, mb int
18+
var s string
19+
for Fscan(in, &T); T > 0; T-- {
20+
Fscan(in, &n, &ma, &mb, &s)
21+
minD, ans := n, "-1"
22+
t := make([]byte, n)
23+
vis := [40][40][40][41]bool{}
24+
var f func(i, x, y, c int)
25+
f = func(i, x, y, c int) {
26+
if i == n {
27+
if x == 0 && y == 0 && abs(n-c*2) < minD {
28+
minD = abs(n - c*2)
29+
ans = string(t)
30+
}
31+
return
32+
}
33+
if vis[i][x][y][c] {
34+
return
35+
}
36+
vis[i][x][y][c] = true
37+
t[i] = 'R'
38+
f(i+1, (x*10+int(s[i]&15))%ma, y, c+1)
39+
t[i] = 'B'
40+
f(i+1, x, (y*10+int(s[i]&15))%mb, c)
41+
}
42+
f(0, 0, 0, 0)
43+
Fprintln(out, ans)
44+
}
45+
}
46+
47+
//func main() { CF1593F(os.Stdin, os.Stdout) }

main/1500-1599/1593F_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"github.com/EndlessCheng/codeforces-go/main/testutil"
5+
"testing"
6+
)
7+
8+
// https://codeforces.com/problemset/problem/1593/F
9+
// https://codeforces.com/problemset/status/1593/problem/F
10+
func TestCF1593F(t *testing.T) {
11+
// just copy from website
12+
rawText := `
13+
inputCopy
14+
4
15+
5 3 13
16+
02165
17+
4 2 1
18+
1357
19+
8 1 1
20+
12345678
21+
2 7 9
22+
90
23+
outputCopy
24+
RBRBR
25+
-1
26+
BBRRRRBB
27+
BR`
28+
testutil.AssertEqualCase(t, rawText, 0, CF1593F)
29+
}

main/1500-1599/1593G.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
. "fmt"
6+
"io"
7+
)
8+
9+
// github.com/EndlessCheng/codeforces-go
10+
func CF1593G(_r io.Reader, _w io.Writer) {
11+
in := bufio.NewReader(_r)
12+
out := bufio.NewWriter(_w)
13+
defer out.Flush()
14+
abs := func(x int) int {
15+
if x < 0 {
16+
return -x
17+
}
18+
return x
19+
}
20+
21+
var T, q, l, r int
22+
var s string
23+
for Fscan(in, &T); T > 0; T-- {
24+
Fscan(in, &s, &q)
25+
sum := make([]int, len(s)+1)
26+
for i, b := range s {
27+
sum[i+1] = sum[i]
28+
if b < 42 {
29+
sum[i+1] += i&1<<1 - 1
30+
}
31+
}
32+
for ; q > 0; q-- {
33+
Fscan(in, &l, &r)
34+
Fprintln(out, abs(sum[r]-sum[l-1]))
35+
}
36+
}
37+
}
38+
39+
//func main() { CF1593G(os.Stdin, os.Stdout) }

main/1500-1599/1593G_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"github.com/EndlessCheng/codeforces-go/main/testutil"
5+
"testing"
6+
)
7+
8+
// https://codeforces.com/problemset/problem/1593/G
9+
// https://codeforces.com/problemset/status/1593/problem/G
10+
func TestCF1593G(t *testing.T) {
11+
// just copy from website
12+
rawText := `
13+
inputCopy
14+
3
15+
([))[)()][]]
16+
3
17+
1 12
18+
4 9
19+
3 6
20+
))))))
21+
2
22+
2 3
23+
1 4
24+
[]
25+
1
26+
1 2
27+
outputCopy
28+
0
29+
2
30+
1
31+
0
32+
0
33+
0`
34+
testutil.AssertEqualCase(t, rawText, 0, CF1593G)
35+
}

main/1600-1699/1609D.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
. "fmt"
6+
"io"
7+
"sort"
8+
)
9+
10+
// github.com/EndlessCheng/codeforces-go
11+
func CF1609D(_r io.Reader, _w io.Writer) {
12+
in := bufio.NewReader(_r)
13+
out := bufio.NewWriter(_w)
14+
defer out.Flush()
15+
16+
var n, d, v, w, ex int
17+
Fscan(in, &n, &d)
18+
fa := make([]int, n+1)
19+
sz := make([]int, n+1)
20+
for i := range fa {
21+
fa[i] = i
22+
sz[i] = 1
23+
}
24+
var find func(int) int
25+
find = func(x int) int {
26+
if fa[x] != x {
27+
fa[x] = find(fa[x])
28+
}
29+
return fa[x]
30+
}
31+
32+
for ; d > 0; d-- {
33+
Fscan(in, &v, &w)
34+
v, w = find(v), find(w)
35+
if v == w {
36+
ex++
37+
} else {
38+
if sz[v] > sz[w] {
39+
v, w = w, v
40+
}
41+
sz[w] += sz[v]
42+
sz[v] = 0 // 注意这里
43+
fa[v] = w
44+
}
45+
a := append([]int(nil), sz...)
46+
sort.Ints(a) // or kth
47+
s := -1
48+
for i := n; i >= n-ex; i-- {
49+
s += a[i]
50+
}
51+
Fprintln(out, s)
52+
}
53+
}
54+
55+
//func main() { CF1609D(os.Stdin, os.Stdout) }

main/1600-1699/1609D_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"github.com/EndlessCheng/codeforces-go/main/testutil"
5+
"testing"
6+
)
7+
8+
// https://codeforces.com/problemset/problem/1609/D
9+
// https://codeforces.com/problemset/status/1609/problem/D
10+
func TestCF1609D(t *testing.T) {
11+
// just copy from website
12+
rawText := `
13+
inputCopy
14+
7 6
15+
1 2
16+
3 4
17+
2 4
18+
7 6
19+
6 5
20+
1 7
21+
outputCopy
22+
1
23+
1
24+
3
25+
3
26+
3
27+
6
28+
inputCopy
29+
10 8
30+
1 2
31+
2 3
32+
3 4
33+
1 4
34+
6 7
35+
8 9
36+
8 10
37+
1 4
38+
outputCopy
39+
1
40+
2
41+
3
42+
4
43+
5
44+
5
45+
6
46+
8`
47+
testutil.AssertEqualCase(t, rawText, -1, CF1609D)
48+
}

main/1600-1699/1611D.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
. "fmt"
6+
"io"
7+
)
8+
9+
// github.com/EndlessCheng/codeforces-go
10+
func CF1611D(_r io.Reader, _w io.Writer) {
11+
in := bufio.NewReader(_r)
12+
out := bufio.NewWriter(_w)
13+
defer out.Flush()
14+
15+
var T, n, root, v int
16+
for Fscan(in, &T); T > 0; T-- {
17+
Fscan(in, &n)
18+
pa := make([]int, n)
19+
for i := range pa {
20+
Fscan(in, &pa[i])
21+
if pa[i]--; pa[i] == i {
22+
root = i
23+
}
24+
}
25+
Fscan(in, &v)
26+
ok := v-1 == root
27+
dep := make([]int, n)
28+
for i := 1; i < n; i++ {
29+
Fscan(in, &v)
30+
if !ok {
31+
continue
32+
}
33+
v--
34+
if pa[v] != root && dep[pa[v]] == 0 {
35+
ok = false
36+
}
37+
dep[v] = i
38+
}
39+
if ok {
40+
for v, d := range dep {
41+
Fprint(out, d-dep[pa[v]], " ")
42+
}
43+
Fprintln(out)
44+
} else {
45+
Fprintln(out, -1)
46+
}
47+
}
48+
}
49+
50+
//func main() { CF1611D(os.Stdin, os.Stdout) }

main/1600-1699/1611D_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"github.com/EndlessCheng/codeforces-go/main/testutil"
5+
"testing"
6+
)
7+
8+
// https://codeforces.com/problemset/problem/1611/D
9+
// https://codeforces.com/problemset/status/1611/problem/D
10+
func TestCF1611D(t *testing.T) {
11+
// just copy from website
12+
rawText := `
13+
inputCopy
14+
4
15+
5
16+
3 1 3 3 1
17+
3 1 2 5 4
18+
3
19+
1 1 2
20+
3 1 2
21+
7
22+
1 1 2 3 4 5 6
23+
1 2 3 4 5 6 7
24+
6
25+
4 4 4 4 1 1
26+
4 2 1 5 6 3
27+
outputCopy
28+
1 10 0 102 100
29+
-1
30+
0 3 100 1 1 2 4
31+
6 5 10 0 2 3
32+
inputCopy
33+
1
34+
9
35+
2 2 2 2 2 2 2 2 2
36+
1 2 6 7 3 9 5 8 4
37+
outputCopy
38+
-1`
39+
testutil.AssertEqualCase(t, rawText, -1, CF1611D)
40+
}

0 commit comments

Comments
 (0)