Atelier des rouesLab FR
Objects/Documentation

ancres.py

Reproducible program or output for the lots dossier.

Download ↓
"""Optimal contiguous query batches with a bounded span, versus exhaustive partitions."""
from collections import deque
from itertools import combinations_with_replacement
import json


def optimal(points,a,b,cap):
    dp=[0]
    queue=deque()
    for j,x in enumerate(points):
        value=dp[j]-b*x
        while queue and queue[-1][1]>=value:queue.pop()
        queue.append((j,value))
        while x-points[queue[0][0]]>cap:queue.popleft()
        dp.append(a+b*x+queue[0][1])
    return dp[-1]

Read in context

Comments

Comments from both language editions appear here.

The discussion is open for reading. I’ll enable submissions once spam protection is configured.