Skip to content

Commit

Permalink
aaa
Browse files Browse the repository at this point in the history
  • Loading branch information
WangSenyuan committed Jun 22, 2024
1 parent 8cb8d31 commit 98c0820
Show file tree
Hide file tree
Showing 15 changed files with 1,314 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/codeforces/set1/set19/set193/set1937/b/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
You are given a 2×𝑛
grid filled with zeros and ones. Let the number at the intersection of the 𝑖
-th row and the 𝑗
-th column be 𝑎𝑖𝑗
.

There is a grasshopper at the top-left cell (1,1)
that can only jump one cell right or downwards. It wants to reach the bottom-right cell (2,𝑛)
. Consider the binary string of length 𝑛+1
consisting of numbers written in cells of the path without changing their order.

Your goal is to:

Find the lexicographically smallest†
string you can attain by choosing any available path;
Find the number of paths that yield this lexicographically smallest string.
If two strings 𝑠
and 𝑡
have the same length, then 𝑠
is lexicographically smaller than 𝑡
if and only if in the first position where 𝑠
and 𝑡
differ, the string 𝑠
has a smaller element than the corresponding element in 𝑡
.

### ideas
1. 任何一个时刻,要么在第一行,要么在第二行,如果到了第二行,那么就一直要到末尾
2. 分层,第一层是(1, 1), 第二层是(2, 1), (1, 2), 且在每一层都取前面的,激活下一层
130 changes: 130 additions & 0 deletions src/codeforces/set1/set19/set193/set1937/b/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package main

import (
"bufio"
"bytes"
"fmt"
"os"
)

func main() {
reader := bufio.NewReader(os.Stdin)

tc := readNum(reader)

var buf bytes.Buffer

for tc > 0 {
tc--
readNum(reader)
grid := make([]string, 2)
grid[0] = readString(reader)
grid[1] = readString(reader)
res, cnt := solve(grid)
buf.WriteString(fmt.Sprintf("%s\n%d\n", res, cnt))
}

fmt.Print(buf.String())
}

func readString(reader *bufio.Reader) string {
s, _ := reader.ReadString('\n')
for i := 0; i < len(s); i++ {
if s[i] == '\n' || s[i] == '\r' {
return s[:i]
}
}
return s
}

func readInt(bytes []byte, from int, val *int) int {
i := from
sign := 1
if bytes[i] == '-' {
sign = -1
i++
}
tmp := 0
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + int(bytes[i]-'0')
i++
}
*val = tmp * sign
return i
}

func readNum(reader *bufio.Reader) (a int) {
bs, _ := reader.ReadBytes('\n')
readInt(bs, 0, &a)
return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
res := readNNums(reader, 2)
a, b = res[0], res[1]
return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
res := readNNums(reader, 3)
a, b, c = res[0], res[1], res[2]
return
}

func readNNums(reader *bufio.Reader, n int) []int {
res := make([]int, n)
x := 0
bs, _ := reader.ReadBytes('\n')
for i := 0; i < n; i++ {
for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
x++
}
x = readInt(bs, x, &res[i])
}
return res
}

func solve(grid []string) (string, int) {
n := len(grid[0])

dp := make([][]int, 2)
for i := 0; i < 2; i++ {
dp[i] = make([]int, n)
}

dp[0][0] = 1

// 也就是只有到 s[0][i+1] == s[1][i]的时候,才能继续
for i := 1; i < n; i++ {
if dp[0][i-1] > 0 {
if grid[0][i] == grid[1][i-1] {
dp[0][i] = dp[0][i-1]
dp[1][i-1] += dp[0][i-1]
} else if grid[0][i] < grid[1][i-1] {
dp[0][i] = dp[0][i-1]
dp[1][i-1] = 0
} else {
dp[0][i] = 0
dp[1][i-1] += dp[0][i-1]
}
dp[1][i] += dp[1][i-1]
} else {
dp[1][i] = dp[1][i-1]
}
}

dp[1][n-1] += dp[0][n-1]

buf := make([]byte, n+1)
a, b := 1, n-1
for i := n; i >= 0; i-- {
buf[i] = grid[a][b]
if b == 0 || a == 1 && dp[a-1][b] > 0 {
a--
} else {
b--
}
}

return string(buf), dp[1][n-1]
}
51 changes: 51 additions & 0 deletions src/codeforces/set1/set19/set193/set1937/b/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import "testing"

func runSample(t *testing.T, grid []string, expect_string string, expect_cnt int) {
res, cnt := solve(grid)

if res != expect_string || cnt != expect_cnt {
t.Fatalf("Sample expect %s, %d, but got %s, %d", expect_string, expect_cnt, res, cnt)
}
}

func TestSample1(t *testing.T) {
grid := []string{
"00",
"00",
}
expect_string := "000"
expect_cnt := 2
runSample(t, grid, expect_string, expect_cnt)
}

func TestSample2(t *testing.T) {
grid := []string{
"1101",
"1100",
}
expect_string := "11000"
expect_cnt := 1
runSample(t, grid, expect_string, expect_cnt)
}

func TestSample3(t *testing.T) {
grid := []string{
"00100111",
"11101101",
}
expect_string := "001001101"
expect_cnt := 4
runSample(t, grid, expect_string, expect_cnt)
}

func TestSample4(t *testing.T) {
grid := []string{
"010",
"010",
}
expect_string := "0010"
expect_cnt := 1
runSample(t, grid, expect_string, expect_cnt)
}
31 changes: 31 additions & 0 deletions src/codeforces/set1/set19/set194/set1946/c/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
You are given a tree with 𝑛
vertices.

Your task is to find the maximum number 𝑥
such that it is possible to remove exactly 𝑘
edges from this tree in such a way that the size of each remaining connected component†
is at least 𝑥
.

Two vertices 𝑣
and 𝑢
are in the same connected component if there exists a sequence of numbers 𝑡1,𝑡2,…,𝑡𝑘
of arbitrary length 𝑘
, such that 𝑡1=𝑣
, 𝑡𝑘=𝑢
, and for each 𝑖
from 1
to 𝑘−1
, vertices 𝑡𝑖
and 𝑡𝑖+1
are connected by an edge.


### ideas
1. when k = n - 1, x = 1
2. 且如果能够达到 x, 那么同样 x - 1 也是ok的
3. 所以符合2分的性质,所以可以从底部开始,满足x的时候,删除一条边,看看最后剩下的部分
4. 但是有个问题就是,比如有个子树,它的size > x 但是size < 2 * x,它有3个子树,a,b,c且每一个都 < x
5. 所以要删除它,最好是找个size = x的位置, 似乎只能浪费了
6. dfs不大行,要bfs才行。从外围开始,选中最少的点,这样浪费的少
Loading

0 comments on commit 98c0820

Please sign in to comment.