Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 95ccd90

Browse files
1
1 parent 6b60b86 commit 95ccd90

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

‎src/main/kotlin/p34xx/Problem3480.kt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,20 @@ fun main() {
2323
var result = 0L
2424

2525
val save = hashMapOf<Int, Long>()
26-
for (start in 1..n) {
26+
27+
var start = 1
28+
while (index < conflictingPairs.size) {
29+
val nextStart = conflictingPairs[index][0]
30+
2731
val end = ends.firstEntry()?.key ?: (n + 1)
28-
result += end - start
32+
33+
result += (end * 2L - start - nextStart) * (nextStart - start + 1) / 2
2934

3035
if (ends[end] == 1) {
31-
save[end] = (save[end] ?: 0) + (ends.higherKey(end) ?: (n + 1)) - end
36+
save[end] = (save[end] ?: 0) + 1L* ((ends.higherKey(end) ?: (n + 1)) - end) * (nextStart - start +1)
3237
}
3338

34-
while (index < conflictingPairs.size && conflictingPairs[index][0] == start) {
39+
while (index < conflictingPairs.size && conflictingPairs[index][0] == nextStart) {
3540
conflictingPairs[index++][1].also {
3641
ends[it]?.also { c ->
3742
if (c == 1) {
@@ -42,17 +47,21 @@ fun main() {
4247
}
4348
}
4449
}
50+
51+
start = nextStart + 1
4552
}
4653

54+
result += (n - start + 2L) * (n - start + 1) / 2
55+
4756
return result + save.values.max()
4857
}
4958
}
5059

5160
expect {
5261
Solution().maxSubarrays(
53-
4, arrayOf(
54-
intArrayOf(2, 3),
55-
intArrayOf(1, 4),
62+
100000, arrayOf(
63+
intArrayOf(50000, 50001),
64+
intArrayOf(99999, 100000),
5665
)
5766
)
5867
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /