Skip to content

Commit 1f5b3f1

Browse files
committed
AcWing 第 30 场周赛
1 parent eef7dca commit 1f5b3f1

File tree

6 files changed

+199
-0
lines changed

6 files changed

+199
-0
lines changed

misc/acwing/weekly/30/a/a.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
. "fmt"
5+
"io"
6+
"os"
7+
)
8+
9+
// github.com/EndlessCheng/codeforces-go
10+
func run(in io.Reader, out io.Writer) {
11+
var n, h, v, ans int
12+
for Fscan(in, &n, &h); n > 0; n-- {
13+
if Fscan(in, &v); v > h {
14+
ans += 2
15+
} else {
16+
ans++
17+
}
18+
}
19+
Fprint(out, ans)
20+
}
21+
22+
func main() { run(os.Stdin, os.Stdout) }

misc/acwing/weekly/30/a/a_test.go

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

misc/acwing/weekly/30/b/b.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+
"bytes"
6+
. "fmt"
7+
"io"
8+
"os"
9+
)
10+
11+
// github.com/EndlessCheng/codeforces-go
12+
func run(in io.Reader, out io.Writer) {
13+
var s string
14+
Fscan(bufio.NewReader(in), &s)
15+
t := []byte(s)
16+
x := 0
17+
for i, c := range s {
18+
if c == '(' {
19+
x++
20+
} else if x > 0 {
21+
x--
22+
} else {
23+
t[i] = ' ' // 从左往右扫描,将无法与左括号匹配的右括号标记为空格
24+
}
25+
}
26+
27+
x = 0
28+
for i := len(s) - 1; i >= 0; i-- {
29+
if s[i] == ')' {
30+
x++
31+
} else if x > 0 {
32+
x--
33+
} else {
34+
t[i] = ' ' // 从右往左扫描,将无法与右括号匹配的左括号标记为空格
35+
}
36+
}
37+
38+
// 按空格划分字符串,统计最长的子串及该长度子串出现次数
39+
maxL, cnt := 0, 1
40+
for _, s := range bytes.Fields(t) {
41+
if len(s) > maxL {
42+
maxL, cnt = len(s), 1
43+
} else if len(s) == maxL {
44+
cnt++
45+
}
46+
}
47+
Fprint(out, maxL, cnt)
48+
}
49+
50+
func main() { run(os.Stdin, os.Stdout) }

misc/acwing/weekly/30/b/b_test.go

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

misc/acwing/weekly/30/c/c.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
. "fmt"
6+
"io"
7+
"os"
8+
"sort"
9+
)
10+
11+
// github.com/EndlessCheng/codeforces-go
12+
func run(_r io.Reader, _w io.Writer) {
13+
in := bufio.NewReader(_r)
14+
out := bufio.NewWriter(_w)
15+
defer out.Flush()
16+
17+
var a, b, l, r, q int
18+
Fscan(in, &a, &b, &q)
19+
g := gcd(a, b)
20+
ds := []int{}
21+
for d := 1; d*d <= g; d++ {
22+
if g%d == 0 {
23+
ds = append(ds, d)
24+
if d*d < g {
25+
ds = append(ds, g/d)
26+
}
27+
}
28+
}
29+
sort.Ints(ds)
30+
31+
for ; q > 0; q-- {
32+
Fscan(in, &l, &r)
33+
d := ds[sort.SearchInts(ds, r+1)-1] // <= r 的最大因子
34+
if d >= l {
35+
Fprintln(out, d)
36+
} else {
37+
Fprintln(out, -1)
38+
}
39+
}
40+
}
41+
42+
func main() { run(os.Stdin, os.Stdout) }
43+
44+
func gcd(a, b int) int {
45+
for a != 0 {
46+
a, b = b%a, a
47+
}
48+
return b
49+
}

misc/acwing/weekly/30/c/c_test.go

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)