-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
WangSenyuan
committed
Jun 17, 2024
1 parent
e399863
commit cae5df6
Showing
18 changed files
with
1,182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array 𝑎=[1,3,3,7] | ||
is good because there is the element 𝑎4=7 | ||
which equals to the sum 1+3+3 | ||
. | ||
|
||
You are given an array 𝑎 | ||
consisting of 𝑛 | ||
integers. Your task is to print all indices 𝑗 | ||
of this array such that after removing the 𝑗 | ||
-th element from the array it will be good (let's call such indices nice). | ||
|
||
For example, if 𝑎=[8,3,5,2] | ||
, the nice indices are 1 | ||
and 4 | ||
: | ||
|
||
if you remove 𝑎1 | ||
, the array will look like [3,5,2] | ||
and it is good; | ||
if you remove 𝑎4 | ||
, the array will look like [8,3,5] | ||
and it is good. | ||
You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func main() { | ||
reader := bufio.NewReader(os.Stdin) | ||
|
||
n := readNum(reader) | ||
a := readNNums(reader, n) | ||
res := solve(a) | ||
|
||
fmt.Println(len(res)) | ||
|
||
s := fmt.Sprintf("%v", res) | ||
|
||
fmt.Println(s[1 : len(s)-1]) | ||
} | ||
|
||
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(a []int) []int { | ||
freq := make(map[int]int) | ||
|
||
var sum int | ||
for _, num := range a { | ||
sum += num | ||
freq[num]++ | ||
} | ||
|
||
var res []int | ||
for i, num := range a { | ||
freq[num]-- | ||
tmp := sum - num | ||
if tmp%2 == 0 { | ||
tmp /= 2 | ||
if freq[tmp] > 0 { | ||
res = append(res, i+1) | ||
} | ||
} | ||
freq[num]++ | ||
} | ||
return res | ||
} |
28 changes: 28 additions & 0 deletions
28
src/codeforces/set1/set10/set107/set1077/c/solution_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"reflect" | ||
"sort" | ||
"testing" | ||
) | ||
|
||
func runSample(t *testing.T, a []int, expect []int) { | ||
res := solve(a) | ||
|
||
sort.Ints(expect) | ||
|
||
if !reflect.DeepEqual(res, expect) { | ||
t.Fatalf("Sample expect %v, but got %v", expect, res) | ||
} | ||
} | ||
|
||
func TestSample1(t *testing.T) { | ||
a := []int{2, 5, 1, 2, 2} | ||
expect := []int{4, 1, 5} | ||
runSample(t, a, expect) | ||
} | ||
|
||
func TestSample2(t *testing.T) { | ||
a := []int{2, 1, 2, 4, 3} | ||
runSample(t, a, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
You are given a string 𝑠 | ||
of length 𝑛 | ||
consisting of lowercase Latin characters. Find the length of the shortest string 𝑘 | ||
such that several (possibly one) copies of 𝑘 | ||
can be concatenated together to form a string with the same length as 𝑠 | ||
and, at most, one different character. | ||
|
||
More formally, find the length of the shortest string 𝑘 | ||
such that 𝑐=𝑘+⋯+𝑘𝑥 times | ||
for some positive integer 𝑥 | ||
, strings 𝑠 | ||
and 𝑐 | ||
has the same length and 𝑐𝑖≠𝑠𝑖 | ||
for at most one 𝑖 | ||
(i.e. there exist 0 | ||
or 1 | ||
such positions). | ||
|
||
### ideas | ||
1. k = n时,是成立的 | ||
2. k肯定是n个一个因子 | ||
3. 然后在给定k的情况下,可以在O(n)的时间内检查? | ||
4. 是可以的 | ||
5. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
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) | ||
s := readString(reader) | ||
res := solve(s) | ||
buf.WriteString(fmt.Sprintf("%d\n", res)) | ||
} | ||
|
||
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(s string) int { | ||
n := len(s) | ||
flag := make([]bool, n) | ||
check := func(k int) bool { | ||
for i := 0; i < k; i++ { | ||
flag[i] = false | ||
} | ||
for i := 0; i < n; i++ { | ||
if s[i] != s[i%k] { | ||
flag[i%k] = true | ||
} | ||
} | ||
diff := -1 | ||
for i := 0; i < k; i++ { | ||
if flag[i] { | ||
if diff >= 0 { | ||
return false | ||
} | ||
diff = i | ||
} | ||
} | ||
if diff < 0 { | ||
return true | ||
} | ||
if n/k == 2 { | ||
return true | ||
} | ||
|
||
var expect byte | ||
|
||
if s[diff] == s[diff+k] || s[diff] == s[diff+2*k] { | ||
// s[diff]是多的那方 | ||
expect = s[diff] | ||
} else { | ||
expect = s[diff+k] | ||
} | ||
var cnt int | ||
for i := diff; i < n; i += k { | ||
if s[i] != expect { | ||
cnt++ | ||
} | ||
} | ||
return cnt == 1 | ||
} | ||
res := n | ||
for k := 1; k <= n/k; k++ { | ||
if n%k == 0 { | ||
if check(k) { | ||
res = min(res, k) | ||
} | ||
if k*k != n && check(n/k) { | ||
res = min(res, n/k) | ||
} | ||
} | ||
} | ||
return res | ||
} |
29 changes: 29 additions & 0 deletions
29
src/codeforces/set1/set19/set195/set1950/e/solution_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func runSample(t *testing.T, s string, expect int) { | ||
res := solve(s) | ||
|
||
if res != expect { | ||
t.Fatalf("Sample expect %d, but got %d", expect, res) | ||
} | ||
} | ||
|
||
func TestSample1(t *testing.T) { | ||
s := "abaa" | ||
expect := 1 | ||
runSample(t, s, expect) | ||
} | ||
|
||
func TestSample2(t *testing.T) { | ||
s := "slavicgslavic" | ||
expect := 13 | ||
runSample(t, s, expect) | ||
} | ||
|
||
func TestSample3(t *testing.T) { | ||
s := "hshahaha" | ||
expect := 2 | ||
runSample(t, s, expect) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Find the minimum height of a rooted tree† | ||
with 𝑎+𝑏+𝑐 | ||
vertices that satisfies the following conditions: | ||
|
||
𝑎 | ||
vertices have exactly 2 | ||
children, | ||
𝑏 | ||
vertices have exactly 1 | ||
child, and | ||
𝑐 | ||
vertices have exactly 0 | ||
children. | ||
If no such tree exists, you should report it. | ||
|
||
|
||
### ideas | ||
1. 显然c是叶子节点的个数 | ||
2. b是只有一个子child的内部节点 | ||
3. 它们只能排成一条线 | ||
4. (b + c - 1) / c + 1 是它们的最低高度 | ||
5. 先假设能整除,此时可以认为仍然有c的叶子节点 | ||
6. 然后就是a个有两个节点的内部节点。这样的节点可以组成一个完全二叉树, | ||
7. a <= c, |
Oops, something went wrong.