Browse Source

Solution TLE pour Edu 105-C

main
Mattéo Delabre 1 month ago
parent
commit
98a24d5a47
Signed by: matteo <spam@delab.re> GPG Key ID: AE3FBD02DC583ABB
12 changed files with 128 additions and 136 deletions
  1. +4
    -0
      codeforces/codeforces-edu-105/C-gen.py
  2. +34
    -0
      codeforces/codeforces-edu-105/C-in4
  3. +4
    -0
      codeforces/codeforces-edu-105/C-in5
  4. +4
    -0
      codeforces/codeforces-edu-105/C-in6
  5. +4
    -0
      codeforces/codeforces-edu-105/C-in7
  6. +5
    -0
      codeforces/codeforces-edu-105/C-out1
  7. +2
    -0
      codeforces/codeforces-edu-105/C-out2
  8. +1
    -0
      codeforces/codeforces-edu-105/C-out3
  9. +1
    -0
      codeforces/codeforces-edu-105/C-out5
  10. +1
    -0
      codeforces/codeforces-edu-105/C-out6
  11. +68
    -28
      codeforces/codeforces-edu-105/C.py
  12. +0
    -108
      codeforces/codeforces-edu-105/C2.py

+ 4
- 0
codeforces/codeforces-edu-105/C-gen.py View File

@@ -0,0 +1,4 @@
print(1)
print(200_000, 200_000)
print(*range(1, 200_001))
print(*range(1, 200_001))

+ 34
- 0
codeforces/codeforces-edu-105/C-in4 View File

@@ -0,0 +1,34 @@
11
10 2
-20 -19 -16 -11 -7 -2 3 9 21 30
-22 -11
5 8
-11 -2 -1 12 22
-27 -25 -12 -11 4 6 8 25
3 9
-22 -17 -15
-28 -26 -8 -7 4 5 14 17 24
2 10
12 26
-20 -19 -17 -8 -4 -1 11 12 16 20
10 1
-29 -26 -25 -21 -20 -15 -14 8 23 27
12
5 5
-20 -15 8 14 27
-9 -2 13 14 29
1 9
-22
-30 -26 -21 -20 -2 8 21 22 23
2 1
-30 -15
27
7 7
-26 -14 -10 -6 -2 18 24
-26 -24 -13 -9 -5 3 25
6 3
-24 -19 -11 -10 -4 8
-22 -16 7
6 9
-23 -18 -11 7 23 29
-30 -28 -17 -6 -5 9 15 18 22

+ 4
- 0
codeforces/codeforces-edu-105/C-in5 View File

@@ -0,0 +1,4 @@
1
10 5
-23 -19 -14 -11 -6 -1 4 12 19 21
-23 -22 -18 2 28

+ 4
- 0
codeforces/codeforces-edu-105/C-in6 View File

@@ -0,0 +1,4 @@
1
9 7
-27 -24 -23 -20 -16 -7 4 12 21
-25 -24 -21 -20 6 13 16

+ 4
- 0
codeforces/codeforces-edu-105/C-in7
File diff suppressed because it is too large
View File


+ 5
- 0
codeforces/codeforces-edu-105/C-out1 View File

@@ -0,0 +1,5 @@
4
2
0
3
1

+ 2
- 0
codeforces/codeforces-edu-105/C-out2 View File

@@ -0,0 +1,2 @@
2
2

+ 1
- 0
codeforces/codeforces-edu-105/C-out3 View File

@@ -0,0 +1 @@
8

+ 1
- 0
codeforces/codeforces-edu-105/C-out5 View File

@@ -0,0 +1 @@
4

+ 1
- 0
codeforces/codeforces-edu-105/C-out6 View File

@@ -0,0 +1 @@
4

+ 68
- 28
codeforces/codeforces-edu-105/C.py View File

@@ -1,4 +1,63 @@
from bisect import bisect_left

def scan(a, b):
max_aligned = 0

if not a or not b:
return 0

# Find pre-aligned boxes
c = []
cur_box = 0
cur_pos = 0

while cur_box < len(a):
while cur_pos < len(b) and a[cur_box] > b[cur_pos]:
cur_pos += 1

if cur_pos < len(b) and a[cur_box] == b[cur_pos]:
c.append(a[cur_box])

cur_box += 1

# Simulate and find maximum alignment
pushing = 0
cur_box = 0
cur_pos = 0
cur_pre = 0

# Move boxes behind the first position
while cur_box < len(a) and a[cur_box] <= b[cur_pos] + pushing:
pushing += 1
cur_box += 1

while cur_pre < len(c) and c[cur_pre] < b[cur_pos] + pushing:
cur_pre += 1

while cur_pos < len(b):
cur_aligned = 0
next_pos = cur_pos

# Count overlapping positions
while next_pos < len(b) and b[next_pos] < b[cur_pos] + pushing:
cur_aligned += 1
next_pos += 1

max_aligned = max(max_aligned, cur_aligned + len(c) - cur_pre)

# Move boxes between the current and the next position
if cur_pos + 1 < len(b):
while cur_box < len(a) and a[cur_box] <= b[cur_pos + 1] + pushing:
pushing += 1
cur_box += 1

while cur_pre < len(c) and c[cur_pre] < b[cur_pos + 1] + pushing:
cur_pre += 1

cur_pos += 1

return max_aligned

T = int(input())

for t in range(T):
@@ -6,34 +65,15 @@ for t in range(T):
a = list(map(int, input().split()))
b = list(map(int, input().split()))

b_m = bisect_left(b, 0)

a_il = 0
a_m = bisect_left(a, 0)
a_ir = len(a) - 1

opt = 0

for b_i in range(0, b_m):
while a_il < a_m and a[a_il] < b[b_i]:
a_il += 1

if a_il < a_m:
opt += 1
a_il += 1
print("pos", b[b_i], "found")
else:
print("pos", b[b_i], "missed")
mid_box = bisect_left(a, 0)
mid_pos = bisect_left(b, 0)

for b_i in range(m - 1, b_m - 1, -1):
while a_ir >= a_m and a[a_ir] > b[b_i]:
a_ir -= 1
a_left = [-box for box in a[:mid_box][::-1]]
a_right = a[mid_box:]
b_left = [-pos for pos in b[:mid_pos][::-1]]
b_right = b[mid_pos:]

if a_ir >= a_m:
opt += 1
a_ir -= 1
print("pos", b[b_i], "found")
else:
print("pos", b[b_i], "missed")
max_left = scan(a_left, b_left)
max_right = scan(a_right, b_right)

print(opt)
print(max_left + max_right)

+ 0
- 108
codeforces/codeforces-edu-105/C2.py View File

@@ -1,108 +0,0 @@
from bisect import bisect_left
from math import inf
T = int(input())

for t in range(T):
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

a_m = bisect_left(a, 0)

csq_left = []
csq_right = []
cur_csq = b[0]

inpos_left = []
inpos_right = []
a_i = 0

for b_i in range(m):
if b_i > 0 and b[b_i] != b[b_i - 1] + 1:
if cur_csq < 0:
csq_left.append((b[b_i - 1], cur_csq - 1))
else:
csq_right.append((cur_csq, b[b_i - 1] + 1))

cur_csq = b[b_i]

while a_i < n and a[a_i] < b[b_i]:
a_i += 1

if a_i < n and a[a_i] == b[b_i]:
if b[b_i] < 0:
inpos_left.append(b[b_i])
else:
inpos_right.append(b[b_i])

if cur_csq < 0:
csq_left.append((b[-1], cur_csq - 1))
else:
csq_right.append((cur_csq, b[-1] + 1))

csq_left.reverse()
inpos_left.reverse()

max_left = 0
csq_i = 0
inpos_i = 0

while csq_i < len(csq_left) and csq_left[csq_i][1] > a[a_m - 1]:
csq_i += 1

for a_i in range(a_m - 1, -1, -1):
nxt_a_v = a[a_i - 1] if a_i > 0 else -inf

while csq_i < len(csq_left) and csq_left[csq_i][1] > nxt_a_v:
while inpos_i < len(inpos_left) and (
inpos_right[inpos_i] < csq_right[csq_i][0]
or (
(csq_right[csq_i][0]
<= inpos_right[inpos_i]
<= csq_right[csq_i][1])
and (a_m - a_i
> inpos_right[inpos_i] - csq_right[csq_i][1])
)):
inpos_i += 1

csq_v = csq_left[csq_i]
max_left = max(
max_left,
min(csq_v[0] - csq_v[1], a_m - a_i)
+ len(inpos_left) - inpos_i
)

csq_i += 1

max_right = 0
csq_i = 0
inpos_i = 0

while csq_i < len(csq_right) and csq_right[csq_i][0] < a[a_m]:
csq_i += 1

for a_i in range(a_m, n):
nxt_a_v = a[a_i + 1] if a_i + 1 < n else inf

while csq_i < len(csq_right) and csq_right[csq_i][0] < nxt_a_v:
while inpos_i < len(inpos_right) and (
inpos_right[inpos_i] < csq_right[csq_i][0]
or (
(csq_right[csq_i][0]
<= inpos_right[inpos_i]
<= csq_right[csq_i][1])
and (a_i - a_m + 1
> inpos_right[inpos_i] - csq_right[csq_i][0])
)):
inpos_i += 1

csq_v = csq_right[csq_i]
max_right = max(
max_right,
min(csq_v[1] - csq_v[0], a_i - a_m + 1)
+ len(inpos_right) - inpos_i
)

csq_i += 1

print(max_left + max_right)

Loading…
Cancel
Save