Skip to content

Commit f7ec6c3

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents fab0f92 + 0158e50 commit f7ec6c3

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

copypasta/bits.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,14 @@ func bitsCollection() {
421421
}
422422

423423
//(接上)考虑乘法
424-
// 给一数组 a,元素均为正整数,求区间和等于区间积的区间个数
425-
// 核心思路是对于每个区间右端点,其对应的区间积不会超过 sum(a),且由于乘积至少要乘 2 才会变化,所以区间右端点对应的区间积至多有 O(log(sum(a))) 个不同的值
426-
// 而每个前缀和都是不同的,所以区间右端点对应的答案个数也至多有 O(log(sum(a))) 个
427-
// 因此总的答案个数至多为 O(nlog(sum(a)))
424+
// 问题:给一数组 a,元素均为正整数,求区间和等于区间积的区间个数
425+
// 我们来考虑对每个区间右端点,有多少个合法的区间左端点
426+
// 核心思路是,对于每个满足题目要求的区间,其区间积不会超过 sum(a)
427+
// 由于乘积至少要乘 2 才会变化,所以对于一个固定的区间右端点,不同的区间积至多有 O(log(sum(a))) 个
428+
// 同时由于元素均为正数,所以对一个固定的区间右端点,区间左端点也至多有 O(log(sum(a))) 个
429+
// 据此我们只需要在加入一个新的数后,去重并去掉区间积超过 sum(a) 的区间,就可以暴力做出此题
430+
// 注:根据以上推导过程,我们还可以得出总的答案个数至多为 O(nlog(sum(a)))
431+
// https://www.dotcpp.com/oj/problem2622.html
428432
countSumEqMul := func(a []int) (ans int) {
429433
tot := 0
430434
for _, v := range a {

main/1400-1499/1401B.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
. "fmt"
6+
"io"
7+
)
8+
9+
// github.com/EndlessCheng/codeforces-go
10+
func CF1401B(_r io.Reader, _w io.Writer) {
11+
in := bufio.NewReader(_r)
12+
out := bufio.NewWriter(_w)
13+
defer out.Flush()
14+
min := func(a, b int) int {
15+
if a > b {
16+
return b
17+
}
18+
return a
19+
}
20+
21+
var T, a, b, c, x, y, z int
22+
for Fscan(in, &T); T > 0; T-- {
23+
Fscan(in, &a, &b, &c, &x, &y, &z)
24+
ans := min(c, y) * 2
25+
cnt := c - min(c, y) + a
26+
if cnt < z {
27+
ans -= (z - cnt) * 2
28+
}
29+
Fprintln(out, ans)
30+
}
31+
}
32+
33+
//func main() { CF1401B(os.Stdin, os.Stdout) }

main/1400-1499/1401B_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/1401/B
9+
// https://codeforces.com/problemset/status/1401/problem/B
10+
func TestCF1401B(t *testing.T) {
11+
// just copy from website
12+
rawText := `
13+
inputCopy
14+
3
15+
2 3 2
16+
3 3 1
17+
4 0 1
18+
2 3 0
19+
0 0 1
20+
0 0 1
21+
outputCopy
22+
4
23+
2
24+
0`
25+
testutil.AssertEqualCase(t, rawText, 0, CF1401B)
26+
}

0 commit comments

Comments
 (0)