-
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
Mar 26, 2024
1 parent
66137ed
commit fbb5a56
Showing
11 changed files
with
707 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/codeforces/set0/set1/set100/set160/set166/c/problem.md
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,22 @@ | ||
A median in an array with the length of n is an element which occupies position number after we sort the elements in the | ||
non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the | ||
number 2, and a median of array (0, 96, 17, 23) — the number 17. | ||
|
||
We define an expression as the integer part of dividing number a by number b. | ||
|
||
One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even | ||
look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the | ||
given array so that the median of the resulting array would be equal to x. | ||
|
||
Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the | ||
array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed | ||
to delete of change initial numbers of the array. | ||
|
||
While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. | ||
|
||
### ideas | ||
|
||
1. sort it of course | ||
2. 至多增加n个x | ||
3. 需要知道有多少个a = 大于x, b = 小于x, c= 等于x的部分 | ||
4. 让x为中位数,需要 b + c >= (a + b + c) / 2, (a + c) >= (a + b + c) / 2 |
106 changes: 106 additions & 0 deletions
106
src/codeforces/set0/set1/set100/set160/set166/c/solution.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,106 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func main() { | ||
reader := bufio.NewReader(os.Stdin) | ||
n, x := readTwoNums(reader) | ||
a := readNNums(reader, n) | ||
res := solve(a, x) | ||
|
||
fmt.Println(res) | ||
} | ||
|
||
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 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 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, x int) int { | ||
cnt := make([]int, 3) | ||
|
||
for _, num := range a { | ||
if num < x { | ||
cnt[0]++ | ||
} else if num == x { | ||
cnt[1]++ | ||
} else { | ||
cnt[2]++ | ||
} | ||
} | ||
cnt[0] += cnt[1] | ||
cnt[2] += cnt[1] | ||
n := len(a) | ||
for add := 0; add <= n; add++ { | ||
// add x | ||
if (add+n)%2 == 1 { | ||
// 最终是奇数个 | ||
if (cnt[0]+add) >= (add+n+1)/2 && (cnt[2]+add) >= (add+n+1)/2 { | ||
return add | ||
} | ||
} else { | ||
// 偶数个 | ||
if (cnt[0]+add) >= (add+n)/2 && (cnt[2]+add) >= (add+n)/2+1 { | ||
return add | ||
} | ||
} | ||
} | ||
|
||
return n + 1 | ||
} |
31 changes: 31 additions & 0 deletions
31
src/codeforces/set0/set1/set100/set160/set166/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,31 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func runSample(t *testing.T, a []int, x int, expect int) { | ||
res := solve(a, x) | ||
if res != expect { | ||
t.Fatalf("Sample expect %d, but got %d", expect, res) | ||
} | ||
} | ||
|
||
func TestSample1(t *testing.T) { | ||
a := []int{10, 20, 30} | ||
x := 10 | ||
expect := 1 | ||
runSample(t, a, x, expect) | ||
} | ||
|
||
func TestSample2(t *testing.T) { | ||
a := []int{1, 2, 3} | ||
x := 4 | ||
expect := 4 | ||
runSample(t, a, x, expect) | ||
} | ||
|
||
func TestSample3(t *testing.T) { | ||
a := []int{1, 2, 3} | ||
x := 3 | ||
expect := 2 | ||
runSample(t, a, x, 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,27 @@ | ||
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been | ||
played. | ||
|
||
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your | ||
friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute | ||
difference between number of wins of first and second team will be d1 and that of between second and third team will be | ||
d2. | ||
|
||
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. | ||
That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will | ||
win this tournament? | ||
|
||
Note that outcome of a match can not be a draw, it has to be either win or loss. | ||
|
||
### ideas | ||
|
||
1. n % 3 = 0 否则不能满足它们有相同的获胜场次 | ||
2. 假设到目前为止,分别获胜 x, y, z 那么有 abs(x - y) = d1, abs(y - z) = d2 | ||
3. x + y + z = k | ||
4. 假设 y > z | ||
5. 如果 y > x => y = x + d1 = z + d2 | ||
6. y = k - (x + z) | ||
7. 2 * y = x + z + d1 + d2 | ||
8. 3 * y = k + d1 + d2 => y | ||
9. 如果 y < x => y = x - d1 = z + d2 => 2 * y = x + z - d1 + d2 => 3 * y = k - d1 + d2 | ||
10. 如果 y < z | ||
11. 如果y > x => |
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,144 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
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 readUint64(bytes []byte, from int, val *uint64) int { | ||
i := from | ||
|
||
var tmp uint64 | ||
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' { | ||
tmp = tmp*10 + uint64(bytes[i]-'0') | ||
i++ | ||
} | ||
*val = tmp | ||
|
||
return i | ||
} | ||
|
||
func main() { | ||
reader := bufio.NewReader(os.Stdin) | ||
tc := readNum(reader) | ||
|
||
var buf bytes.Buffer | ||
|
||
for tc > 0 { | ||
tc-- | ||
nums := readNNums(reader, 4) | ||
res := solve(nums[0], nums[1], nums[2], nums[3]) | ||
if res { | ||
buf.WriteString("yes\n") | ||
} else { | ||
buf.WriteString("no\n") | ||
} | ||
} | ||
|
||
fmt.Print(buf.String()) | ||
} | ||
|
||
func solve(n int, k int, d1, d2 int) bool { | ||
if n%3 != 0 { | ||
return false | ||
} | ||
|
||
// x <= y (2种可能性) | ||
// z <= y (2种可能性) | ||
return solve1(n, k, d1, d2) || solve2(n, k, d1, d2) || solve3(n, k, d1, d2) || solve4(n, k, d1, d2) | ||
} | ||
|
||
func solve1(n int, k int, d1 int, d2 int) bool { | ||
// y - x = d1 => y = d1 + x | ||
// y - z = d2 => y = d2 + z | ||
// y + x + z = k => y = k - (x + z) | ||
// 3 * y = k + d1 + d2 | ||
y := k + d1 + d2 | ||
if y%3 != 0 { | ||
return false | ||
} | ||
y /= 3 | ||
return y <= n/3 && y-d1 >= 0 && y-d2 >= 0 | ||
} | ||
|
||
func solve2(n int, k int, d1 int, d2 int) bool { | ||
// y - x = d1 => y = d1 + x | ||
// z - y = d2 => y = z - d2 | ||
y := k - d2 + d1 | ||
if y < 0 || y%3 != 0 { | ||
return false | ||
} | ||
y /= 3 | ||
return y <= n/3 && y >= d1 && y+d2 <= n/3 | ||
} | ||
|
||
func solve3(n int, k int, d1 int, d2 int) bool { | ||
// x - y = d1 => y = x - d1 | ||
// y - z = d2 => y = z + d2 | ||
y := k - d1 + d2 | ||
if y < 0 || y%3 != 0 { | ||
return false | ||
} | ||
y /= 3 | ||
return y <= n/3 && y >= d2 && y+d1 <= n/3 | ||
} | ||
|
||
func solve4(n int, k int, d1 int, d2 int) bool { | ||
// x - y = d1 => y = x - d1 | ||
// z - y = d2 => y = z - d2 | ||
y := k - d1 - d2 | ||
if y < 0 || y%3 != 0 { | ||
return false | ||
} | ||
y /= 3 | ||
return y <= n/3 && y+d1 <= n/3 && y+d2 <= n/3 | ||
} |
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 @@ | ||
We call a string good, if after merging all the consecutive equal characters, the resulting string is palindrome. For | ||
example, "aabba" is good, because after the merging step it will become "aba". | ||
|
||
Given a string, you have to find two values: | ||
|
||
the number of good substrings of even length; | ||
the number of good substrings of odd length. | ||
|
||
### ideas | ||
|
||
1. A substring s[l, r] (1 ≤ l ≤ r ≤ n) of string s = s1s2... sn is string slsl + 1... sr. | ||
2. 字符串有a/b组成 (0/1) | ||
3. 如果s[i] = s[i-1], 那么到i-1的结果,可以直接用在i上,i会和i-1进行合并 (但是长度奇偶性要变化) | ||
4. 如果s[i] != s[i-1] 那么就需要知道到i-1为止,(假设s[i-1] = 0) 前面是1,且以0开始的(1没有被匹配的)的回文字串的结果 | ||
5. 肯定能确定的一个点是,到目前为止,字符串的pattern必然是 0101 或者1010 | ||
6. 所以dp[0]表示 01, dp[1]表示10, dp[2] 表示101, dp[3]表示010 | ||
7. 然后遇到一个新的0 01 =》 010, 10 =》 10 (但是奇偶性要变) | ||
8. 101 =》 010 (奇偶性有点麻烦了,如果是偶数个1,和奇数个1) | ||
9. 010 =》 010 (奇偶性要变) | ||
9. 不过至少进了一步 | ||
10. 01111 1000111 | ||
11. 如果不考虑merge的情况, 1010101010,其中任意两个0/1之间都是满足回文的条件的 | ||
12. 所以,对于当前0,只需要知道前面和自己相同parity位的0有多少个,加上去,就是奇数长度的计数 | ||
13. 100001 |
Oops, something went wrong.