Skip to content

Commit 0952cc5

Browse files
committed
[1800][1A] CF1283D BFS
1 parent 703cfa2 commit 0952cc5

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

main/1200-1299/1283D.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
. "fmt"
6+
"io"
7+
)
8+
9+
// github.com/EndlessCheng/codeforces-go
10+
func CF1283D(_r io.Reader, _w io.Writer) {
11+
in := bufio.NewReader(_r)
12+
out := bufio.NewWriter(_w)
13+
defer out.Flush()
14+
15+
var n, m int
16+
Fscan(in, &n, &m)
17+
a := make([]int, n)
18+
vis := make(map[int]bool, n+m*2)
19+
for i := range a {
20+
Fscan(in, &a[i])
21+
vis[a[i]] = true
22+
}
23+
q := []int{}
24+
add := func(v int) {
25+
for i := v - 1; i < v+2; i += 2 {
26+
if !vis[i] {
27+
vis[i] = true
28+
q = append(q, i)
29+
}
30+
}
31+
}
32+
for _, v := range a {
33+
add(v)
34+
}
35+
36+
ans := int64(0)
37+
y := make([]int, 0, m)
38+
for d := int64(1); ; d++ {
39+
tmp := q
40+
q = nil
41+
for _, v := range tmp {
42+
ans += d
43+
y = append(y, v)
44+
if len(y) == m {
45+
Fprintln(out, ans)
46+
for _, v := range y {
47+
Fprint(out, v, " ")
48+
}
49+
return
50+
}
51+
add(v)
52+
}
53+
}
54+
}
55+
56+
//func main() { CF1283D(os.Stdin, os.Stdout) }

main/1200-1299/1283D_test.go

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

0 commit comments

Comments
 (0)