Skip to content

Commit 0c8ae81

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 2831703 + 3cb0529 commit 0c8ae81

File tree

17 files changed

+476
-22
lines changed

17 files changed

+476
-22
lines changed

copypasta/template/interactive_problem/main.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
// github.com/EndlessCheng/codeforces-go
1010
type (
1111
initData struct{ n int }
12-
request struct{ i int }
12+
request struct{ q []int }
1313
response struct{ v int }
1414
answer struct{ ans []int }
1515
)
@@ -30,9 +30,14 @@ func (io io) readInitData() (d initData) {
3030
return
3131
}
3232

33-
func (io io) query(req request) (resp response) {
34-
Fprintln(io.out, "?", req.i)
33+
func (io io) query(q request) (resp response) {
34+
Fprint(io.out, "?")
35+
for _, v := range q.q {
36+
Fprint(io.out, " ", v)
37+
}
38+
Fprintln(io.out)
3539
io.out.Flush()
40+
3641
Fscan(io.in, &resp.v)
3742
if resp.v < 0 {
3843
panic(-1)
@@ -41,9 +46,10 @@ func (io io) query(req request) (resp response) {
4146
}
4247

4348
func (io io) printAnswer(a answer) {
44-
Fprint(io.out, "! ")
49+
Fprint(io.out, "!")
50+
//Fprint(io.out, " ", len(a.ans))
4551
for _, v := range a.ans {
46-
Fprint(io.out, v, " ")
52+
Fprint(io.out, " ", v)
4753
}
4854
Fprintln(io.out)
4955
io.out.Flush()
@@ -56,7 +62,7 @@ func (io io) printAnswer(a answer) {
5662
}
5763

5864
func doInteraction(it interaction) {
59-
q := func(i int) int { return it.query(request{i}).v }
65+
q := func(q ...int) int { return it.query(request{q}).v }
6066
dt := it.readInitData()
6167
n := dt.n
6268
ans := make([]int, n) //

copypasta/template/interactive_problem/main_test.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ var failedCount int
1818
type mockIO struct {
1919
initData
2020
answer
21-
innerData []int
21+
hiddenData []int //
2222

2323
_t *testing.T
2424
caseNum int
2525
queryLimit int
2626
queryCnt int
2727
}
2828

29-
func (io *mockIO) String() (s string) {
30-
s = Sprintf("%v", io.innerData)
31-
//s = strings.Join(io.innerData, "\n")
32-
return
29+
func (io *mockIO) String() string {
30+
hStr := Sprintf("%v", io.hiddenData)
31+
//hStr := strings.Join(io.hiddenData, "\n")
32+
return Sprintf("%+v\n%s", io.initData, hStr)
3333
}
3434

3535
// Mock initData
@@ -40,9 +40,9 @@ func (io *mockIO) readInitData() (d initData) {
4040
// Check answer
4141
func (io *mockIO) printAnswer(actualAns answer) {
4242
expectedAns := io.answer
43-
if !assert.EqualValues(io._t, expectedAns, actualAns, "Wrong Answer %d\nInner Data:\n%v", io.caseNum, io) {
43+
if !assert.EqualValues(io._t, expectedAns, actualAns, "Wrong Answer %d\nCase Data:\n%v", io.caseNum, io) {
4444
if failedCount++; failedCount > failedCountLimit {
45-
io._t.Fatal("too many wrong answers, terminated")
45+
io._t.Fatal("too many failed cases, terminated")
4646
}
4747
}
4848

@@ -51,24 +51,24 @@ func (io *mockIO) printAnswer(actualAns answer) {
5151

5252
return true
5353
}
54-
if !assert.Truef(io._t, ansChecker(), "Wrong Answer %d\nMy Answer:\n%v\nInner Data:\n%v", io.caseNum, actualAns, io) {
54+
if !assert.Truef(io._t, ansChecker(), "Wrong Answer %d\nMy Answer:\n%v\nCase Data:\n%v", io.caseNum, actualAns, io) {
5555
if failedCount++; failedCount > failedCountLimit {
56-
io._t.Fatal("too many wrong answers, terminated")
56+
io._t.Fatal("too many failed cases, terminated")
5757
}
5858
}
5959
}
6060

6161
// Mock query
62-
func (io *mockIO) query(req request) (resp response) {
62+
func (io *mockIO) query(q request) (resp response) {
6363
if io.caseNum == debugCaseNum {
64-
Print("Query ", req, " ")
64+
Print("Query ", q, " => ")
6565
defer func() { Println(resp) }()
6666
}
67-
if io.queryCnt++; io.queryCnt > io.queryLimit {
68-
io._t.Fatalf("Query Limit Exceeded %d\nInner Data:\n%v", io.caseNum, io)
69-
}
7067

68+
io.queryCnt++
69+
//if io.queryCnt > io.queryLimit { io._t.Fatalf("Query Limit Exceeded %d\nCase Data:\n%v", io.caseNum, io) }
7170

71+
// calc q ...
7272

7373
return
7474
}
@@ -81,17 +81,27 @@ func Test_doInteraction(_t *testing.T) {
8181
}
8282
io := &mockIO{_t: _t, caseNum: tc}
8383

84+
// gen random data ...
8485
rg = testutil.NewRandGenerator()
8586
n := rg.Int(2, 4)
8687
a := rg.IntSlice(n, 1, n)
8788

8889
io.n = n
8990
io.ans = a
90-
io.innerData = a
91+
io.hiddenData = a
9192

93+
// set limit ...
9294
io.queryLimit = n + 30
9395

9496
doInteraction(io)
97+
98+
if io.queryCnt > io.queryLimit {
99+
io._t.Errorf("Query Limit Exceeded %d\n%d > %d\nCase Data:\n%v", io.caseNum, io.queryCnt, io.queryLimit, io)
100+
if failedCount++; failedCount > failedCountLimit {
101+
io._t.Fatal("too many failed cases, terminated")
102+
}
103+
}
104+
95105
if tc == checkTC {
96106
_t.Logf("%d cases checked.", tc)
97107
checkTC <<= 1

leetcode/weekly/272/a/a.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
// 遍历,找第一个回文串
4+
5+
// github.com/EndlessCheng/codeforces-go
6+
func firstPalindrome(words []string) string {
7+
next:
8+
for _, w := range words {
9+
for i, n := 0, len(w); i < n/2; i++ {
10+
if w[i] != w[n-1-i] {
11+
continue next
12+
}
13+
}
14+
return w
15+
}
16+
return ""
17+
}

leetcode/weekly/272/a/a_test.go

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

leetcode/weekly/272/b/b.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
// Go 模拟
4+
5+
// github.com/EndlessCheng/codeforces-go
6+
func addSpaces(s string, spaces []int) string {
7+
spaces = append(spaces, len(s)) // 小技巧:把 s 长度加到数组末尾,这样可以在循环内处理最后一段
8+
ans := []byte(s[:spaces[0]])
9+
for i := 1; i < len(spaces); i++ {
10+
ans = append(ans, ' ')
11+
ans = append(ans, s[spaces[i-1]:spaces[i]]...)
12+
}
13+
return string(ans)
14+
}
15+
16+
// github.com/EndlessCheng/codeforces-go
17+
func addSpaces2(s string, spaces []int) string {
18+
ans := make([]byte, 0, len(s)+len(spaces))
19+
ans = append(ans, s[:spaces[0]]...)
20+
for i := 1; i < len(spaces); i++ {
21+
ans = append(ans, ' ')
22+
ans = append(ans, s[spaces[i-1]:spaces[i]]...)
23+
}
24+
ans = append(ans, ' ')
25+
ans = append(ans, s[spaces[len(spaces)-1]:]...)
26+
return string(ans)
27+
}

leetcode/weekly/272/b/b_test.go

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

leetcode/weekly/272/c/c.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
/* 分组循环
4+
5+
将 $\textit{prices}$ 按照平滑下降的定义分成若干组。例如 $[3,2,1,4]$ 分为 $[3,2,1]$ 和 $[4]$ 两组。
6+
7+
对于每一组的所有非空子数组,都是平滑下降的。设该组长度为 $m$,则该组的非空子数组个数为
8+
9+
$$
10+
C_{m+1}^2 = \dfrac{m(m+1)}{2}
11+
$$
12+
13+
累加每组的非空子区间个数即为答案。
14+
15+
- 时间复杂度:$O(n)$,其中 $n$ 是数组 $\textit{prices}$ 的长度。注意下面代码内外层循环共用同一个变量 $i$,时间复杂度就是 `i++` 的执行次数,即 $O(n)$。
16+
- 空间复杂度:$O(1)$,我们只需要常数的空间保存若干变量。
17+
18+
*/
19+
20+
// github.com/EndlessCheng/codeforces-go
21+
func getDescentPeriods(prices []int) (ans int64) {
22+
for i, n := 0, len(prices); i < n; {
23+
i0 := i
24+
for i++; i < n && prices[i] == prices[i-1]-1; i++ {
25+
}
26+
ans += int64(i-i0) * int64(i-i0+1) / 2
27+
}
28+
return
29+
}

leetcode/weekly/272/c/c_test.go

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

leetcode/weekly/272/d/d.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import "sort"
4+
5+
/* 最长上升子序列
6+
7+
将 $\textit{arr}$ 每隔 $k$ 个数取一个元素,分为若干组,例如当 $k=3$ 时,分为如下三组:
8+
9+
- $arr[0],arr[3],arr[6],\cdots$
10+
- $arr[1],arr[4],arr[7],\cdots$
11+
- $arr[2],arr[5],arr[8],\cdots$
12+
13+
根据题意,组与组之间是互不影响的,我们需要让每一组内的元素单调不降。
14+
15+
题目要求需要修改的最少元素个数,这可以通过求不需要修改的最多元素个数来求出,也就是该组元素的最长不降子序列。(不了解的同学可以看看第 300 题)
16+
17+
需要注意的是:
18+
19+
- 求最长严格递增子序列需要二分找到大于或等于当前元素的元素位置(即 C++ 中的 `lower_bound`);
20+
- 求最长非降子序列需要二分找到大于当前元素的元素位置(即 C++ 中的 `upper_bound`)。
21+
22+
累加所有组的最长不降子序列的长度即为最多可以保留的元素个数,用 $\textit{arr}$ 的长度减去该个数即为答案。
23+
24+
*/
25+
26+
// github.com/EndlessCheng/codeforces-go
27+
func kIncreasing(arr []int, k int) int {
28+
save := 0
29+
for i, n := 0, len(arr); i < k && i < n; i++ {
30+
f := []int{}
31+
for j := i; j < n; j += k {
32+
v := arr[j]
33+
if p := sort.SearchInts(f, v+1); p < len(f) {
34+
f[p] = v
35+
} else {
36+
f = append(f, v)
37+
}
38+
}
39+
save += len(f)
40+
}
41+
return len(arr) - save
42+
}

leetcode/weekly/272/d/d_test.go

Lines changed: 31 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)