tinycodinglm / cp2 /cp-0010.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame
13.1 kB
cp-practice-sim trace
System You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Give only a concise rationale: approach, correctness idea, and time/space com...

You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Give only a concise rationale: approach, correctness idea, and time/space complexity. Tools: write_file(path, content) writes a UTF-8 file (creating directories); run_shell(command) runs a bash command and returns its output and exit status. Test solutions on the provided examples before answering.

User 2026-11-07T07:20:03.537Z

Steady Median Window

Topic: Arrays and prefix sums | Difficulty: advanced

Problem

Given an array a[1..n], the median of a block of length m is the element at position ceil(m/2) (1-indexed) when the block is sorted in non-decreasing order. Among all contiguous blocks of length at least L, find the maximum possible median.

Input

The first line contains n and L. The second line contains n integers a[1..n].

Output

Print one integer: the largest median of a block with length at least L.

Constraints

  • 1 <= L <= n <= 2*10^5
  • 1 <= a[i] <= 10^9

Examples

Example 1

Input:

markdown
5 2
1 9 9 1 1

Output:

markdown
9

Note: The block [9, 9] has median 9.

Example 2

Input:

markdown
6 4
3 1 4 1 5 9

Output:

markdown
4

Note: The block [4, 1, 5, 9] sorts to 1 4 5 9 and its median (position 2) is 4; no valid block does better.

Solve this task. Write solutions in Python 3, C#, TypeScript, test what you can run on the examples, and send back the final programs. Include a concise rationale (approach, correctness idea, time/space complexity).

Assistant 2026-11-07T07:20:05.511Z

Writing the Python 3 solution and testing it on the examples.

Assistant 2026-11-07T07:20:09.870Z

Saving the example inputs.

2 tool calls (write_file)
Assistant 2026-11-07T07:20:16.536Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T07:20:22.635Z

Cross-checking with the C# version.

Assistant 2026-11-07T07:20:27.386Z

Compiling the C# solution.

Assistant 2026-11-07T07:20:31.785Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T07:20:38.061Z

Cross-checking with the TypeScript version.

Assistant 2026-11-07T07:20:42.930Z

Compiling the TypeScript solution.

Assistant 2026-11-07T07:20:46.547Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T07:20:53.000Z

Approach. Binary search the answer x over the sorted values. The block median is at least x iff, after mapping elements to +1 (>= x) and -1 (< x), the block sum is positive; existence of such a block of length >= L is tested with prefix sums and a running minimum.

Correctness. The median at position ceil(m/2) is >= x iff more than m/2 elements are >= x, i.e. the +-1 sum is positive; feasibility is monotone in x, so binary search finds the largest feasible x, which is a value present in the array.

Complexity. Time: O(n log n). Space: O(n).

All solutions below passed every example.

Python 3

python
import sys
t = sys.stdin.read().split()
n, L = int(t[0]), int(t[1])
a = [int(x) for x in t[2:2 + n]]

def ok(x):
    P = [0] * (n + 1)
    for i in range(n):
        P[i + 1] = P[i] + (1 if a[i] >= x else -1)
    mn = 1 << 60
    for i in range(L, n + 1):
        if P[i - L] < mn:
            mn = P[i - L]
        if P[i] - mn > 0:
            return True
    return False

vals = sorted(set(a))
lo, hi = 0, len(vals) - 1
while lo < hi:
    mid = (lo + hi + 1) // 2
    if ok(vals[mid]):
        lo = mid
    else:
        hi = mid - 1
print(vals[lo])

C#

csharp
using System;
using System.IO;
using System.Linq;

class Program
{
    static int n, L;
    static int[] a;

    static bool Ok(int x)
    {
        int[] P = new int[n + 1];
        for (int i = 0; i < n; i++) P[i + 1] = P[i] + (a[i] >= x ? 1 : -1);
        int mn = int.MaxValue;
        for (int i = L; i <= n; i++)
        {
            if (P[i - L] < mn) mn = P[i - L];
            if (P[i] - mn > 0) return true;
        }
        return false;
    }

    static void Main()
    {
        var t = Console.In.ReadToEnd().Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
        n = int.Parse(t[0]);
        L = int.Parse(t[1]);
        a = new int[n];
        for (int i = 0; i < n; i++) a[i] = int.Parse(t[2 + i]);
        int[] vals = a.Distinct().OrderBy(v => v).ToArray();
        int lo = 0, hi = vals.Length - 1;
        while (lo < hi)
        {
            int mid = (lo + hi + 1) / 2;
            if (Ok(vals[mid])) lo = mid; else hi = mid - 1;
        }
        Console.WriteLine(vals[lo]);
    }
}

TypeScript

typescript
declare const require: any;
const t: number[] = require('fs').readFileSync(0, 'utf8').split(/\s+/).filter((x: string) => x.length).map(Number);
const n = t[0], L = t[1];
const a: number[] = t.slice(2, 2 + n);

function ok(x: number): boolean {
  const P = new Int32Array(n + 1);
  for (let i = 0; i < n; i++) P[i + 1] = P[i] + (a[i] >= x ? 1 : -1);
  let mn = 1 << 30;
  for (let i = L; i <= n; i++) {
    if (P[i - L] < mn) mn = P[i - L];
    if (P[i] - mn > 0) return true;
  }
  return false;
}

const vals: number[] = Array.from(new Set(a)).sort((x, y) => x - y);
let lo = 0, hi = vals.length - 1;
while (lo < hi) {
  const mid = (lo + hi + 1) >> 1;
  if (ok(vals[mid])) lo = mid; else hi = mid - 1;
}
console.log(String(vals[lo]));