-
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 5, 2024
1 parent
544d106
commit 8bb4698
Showing
22 changed files
with
1,953 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,9 @@ | ||
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel. | ||
|
||
Let volume vj of barrel j be equal to the length of the minimal stave in it. | ||
|
||
|
||
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n. | ||
|
||
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above. | ||
|
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,109 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"sort" | ||
) | ||
|
||
func main() { | ||
reader := bufio.NewReader(os.Stdin) | ||
n, k, l := readThreeNums(reader) | ||
a := readNNums(reader, n*k) | ||
fmt.Println(solve(a, k, l)) | ||
} | ||
|
||
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, k int, l int) int { | ||
sort.Ints(a) | ||
|
||
n := len(a) | ||
|
||
vis := make([]bool, n) | ||
|
||
it := sort.Search(n, func(i int) bool { | ||
return a[i]-a[0] > l | ||
}) | ||
if it < n/k { | ||
return 0 | ||
} | ||
it-- | ||
// a[it] - a[0] <= l | ||
var res int | ||
r := n - 1 | ||
// stage 1 | ||
for i := it; i >= 0; i-- { | ||
if r-it < k-1 { | ||
break | ||
} | ||
var cnt int | ||
for cnt < k-1 { | ||
vis[r] = true | ||
cnt++ | ||
r-- | ||
} | ||
res += a[i] | ||
vis[i] = true | ||
} | ||
|
||
for i := n - 1; i >= 0; { | ||
var cnt int | ||
for i >= 0 && cnt < k { | ||
if !vis[i] { | ||
cnt++ | ||
} | ||
i-- | ||
} | ||
if cnt == k { | ||
res += a[i+1] | ||
} | ||
} | ||
return res | ||
} |
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,39 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func runSample(t *testing.T, a []int, k int, l int, expect int) { | ||
res := solve(a, k, l) | ||
|
||
if res != expect { | ||
t.Fatalf("Sample expect %d, but got %d", expect, res) | ||
} | ||
} | ||
|
||
func TestSample1(t *testing.T) { | ||
k, l := 2, 1 | ||
a := []int{2, 2, 1, 2, 3, 2, 2, 3} | ||
expect := 7 | ||
runSample(t, a, k, l, expect) | ||
} | ||
|
||
func TestSample2(t *testing.T) { | ||
k, l := 1, 0 | ||
a := []int{10, 10} | ||
expect := 20 | ||
runSample(t, a, k, l, expect) | ||
} | ||
|
||
func TestSample3(t *testing.T) { | ||
k, l := 2, 1 | ||
a := []int{5, 2} | ||
expect := 2 | ||
runSample(t, a, k, l, expect) | ||
} | ||
|
||
func TestSample4(t *testing.T) { | ||
k, l := 2, 1 | ||
a := []int{1, 2, 3, 4, 5, 6} | ||
expect := 0 | ||
runSample(t, a, k, l, 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,60 @@ | ||
In winter, the inhabitants of the Moscow Zoo are very bored, in particular, it concerns gorillas. You decided to entertain them and brought a permutation 𝑝 | ||
of length 𝑛 | ||
to the zoo. | ||
|
||
A permutation of length 𝑛 | ||
is an array consisting of 𝑛 | ||
distinct integers from 1 | ||
to 𝑛 | ||
in any order. For example, [2,3,1,5,4] | ||
is a permutation, but [1,2,2] | ||
is not a permutation (2 | ||
occurs twice in the array) and [1,3,4] | ||
is also not a permutation (𝑛=3 | ||
, but 4 | ||
is present in the array). | ||
|
||
The gorillas had their own permutation 𝑞 | ||
of length 𝑛 | ||
. They suggested that you count the number of pairs of integers 𝑙,𝑟 | ||
(1≤𝑙≤𝑟≤𝑛 | ||
) such that MEX([𝑝𝑙,𝑝𝑙+1,…,𝑝𝑟])=MEX([𝑞𝑙,𝑞𝑙+1,…,𝑞𝑟]) | ||
. | ||
|
||
The MEX | ||
of the sequence is the minimum integer positive number missing from this sequence. For example, MEX([1,3])=2 | ||
, MEX([5])=1 | ||
, MEX([3,1,2,6])=4 | ||
. | ||
|
||
You do not want to risk your health, so you will not dare to refuse the gorillas. | ||
|
||
|
||
### ideas | ||
1. 略一思考,还有点难~ | ||
2. 假设p[i] = 0, q[j] = 0, | ||
3. 那么任何不包括(i, j)的区间[l...r]它们的mex = 0 | ||
4. 那是不是就可以这样算呢? | ||
5. 不包括(i, j)的区间 = 总区间数 n * (n + 1) / 2 - 包含它们的区间数 = (i+1) * (n - j) ? | ||
6. 前面处理的是 mex = 0的情况, | ||
7. 接下来处理mex = 1的情况(它们必须把0包括进去) | ||
8. 好像不大对,比如如果0在两头的时候 | ||
9. 那些mex = 0 的区间 = 肯定不包含任何一个0的区间 | ||
10. 正难则反。有办法计算出,区间[l...r]中 mex(q) != mex(p)的方式吗? | ||
11. 假设 mex(p[l..r]) = x, mex(q[l...r]) = y | ||
12. 它们不相同 | ||
13. 考虑一个序列,如何找到mex | ||
14. 假设序列是 [2,3,1,5,4], | ||
15. mex[3:3] = 2, 其他的等于 1 | ||
16. mex[1:3] = 4, 其他的等于 1 | ||
17. mex[1:5] = 6, 其他的等于1 | ||
18. [1 3 2], [2 1 3] | ||
19. p_mex[1:2] = 2, q_mex[1:2] = 3 但是其他都等于1 | ||
20. p_mex[1:3] = 4, q_mex[1:3] = 4 | ||
21. [4, 1, 3, 2], [4, 3, 1, 2] | ||
22. [4, 1, 3] mex = 2 | ||
23. [4, 3, 1] mex = 2 | ||
24. 这个区间[1:3]它就是包含了1,但是不包含2,所以这样是这样的区间它们的mex = 2 | ||
25. 所以如果某个区间包含了1....x-1, 但是不包含x,那么这样的区间是相等的 | ||
26. mex = 0的区间假设已经get到了 | ||
27. 那么计算 mex = 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,128 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func main() { | ||
reader := bufio.NewReader(os.Stdin) | ||
|
||
n := readNum(reader) | ||
p := readNNums(reader, n) | ||
q := readNNums(reader, n) | ||
res := solve(p, q) | ||
fmt.Println(res) | ||
} | ||
|
||
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(p []int, q []int) int { | ||
n := len(p) | ||
for i := 0; i < n; i++ { | ||
p[i]-- | ||
q[i]-- | ||
} | ||
|
||
ps := getPosition(p) | ||
qs := getPosition(q) | ||
|
||
first := min(ps[0], qs[0]) | ||
last := max(ps[0], qs[0]) | ||
|
||
get := func(m int) int { | ||
if m <= 0 { | ||
return 0 | ||
} | ||
return m * (m + 1) / 2 | ||
} | ||
|
||
// mex = 0 的区间 | ||
var res int | ||
res += get(first) | ||
res += get(last - first - 1) | ||
res += get(n - last - 1) | ||
|
||
for x := 1; x < n; x++ { | ||
l := min(ps[x], qs[x]) | ||
r := max(ps[x], qs[x]) | ||
// mex = x的区间, 必须包含 [first...last], 但是不能包含(l, r) | ||
if l < first && last < r { | ||
res += (first - l) * (r - last) | ||
} else if l > last { | ||
res += (first + 1) * (l - last) | ||
} else if r < first { | ||
res += (first - r) * (n - last) | ||
} | ||
|
||
first = min(first, l) | ||
last = max(last, r) | ||
} | ||
|
||
return res + 1 | ||
} | ||
|
||
func getPosition(nums []int) []int { | ||
n := len(nums) | ||
pos := make([]int, n) | ||
for i, num := range nums { | ||
pos[num] = i | ||
} | ||
return pos | ||
} |
32 changes: 32 additions & 0 deletions
32
src/codeforces/set1/set17/set179/set1793/d/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,32 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func runSample(t *testing.T, p []int, q []int, expect int) { | ||
res := solve(p, q) | ||
|
||
if res != expect { | ||
t.Fatalf("Sample expect %d, but got %d", expect, res) | ||
} | ||
} | ||
|
||
func TestSample1(t *testing.T) { | ||
a := []int{1, 3, 2} | ||
b := []int{2, 1, 3} | ||
expect := 2 | ||
runSample(t, a, b, expect) | ||
} | ||
|
||
func TestSample2(t *testing.T) { | ||
a := []int{7, 3, 6, 2, 1, 5, 4} | ||
b := []int{6, 7, 2, 5, 3, 1, 4} | ||
expect := 16 | ||
runSample(t, a, b, expect) | ||
} | ||
|
||
func TestSample3(t *testing.T) { | ||
a := []int{1, 2, 3, 4, 5, 6} | ||
b := []int{6, 5, 4, 3, 2, 1} | ||
expect := 11 | ||
runSample(t, a, b, expect) | ||
} |
Oops, something went wrong.