tinycodinglm / cp2 /cp-0018.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
14.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. Reply with code only; no explanation. Tools: write_file(path, content) writes...

You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Reply with code only; no explanation. 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. Fault-handling policy: Detect: the tool returns an infrastructure error (worker restarted / timeout) with no program output or exit status, so it is not a program failure. Report: say the tool failed, not the code. Recover: retry the same command once; if it fails again, fall back to another language or report the limitation.

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

Exact Spread Windows

Topic: Two pointers and sliding window | Difficulty: hard

Problem

For an array a[1..n], the spread of a block is its maximum minus its minimum. Count the contiguous non-empty blocks whose spread is exactly k.

Input

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

Output

Print the number of blocks with spread exactly k.

Constraints

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

Examples

Example 1

Input:

markdown
4 1
1 2 2 3

Output:

markdown
4

Note: The blocks are [1,2], [1,2,2], [2,3] and [2,2,3].

Example 2

Input:

markdown
3 0
5 5 5

Output:

markdown
6

Solve this task. Write solutions in Kotlin, C++17, Go, test what you can run on the examples, and send back the final programs. Final answer: code only, no explanation.

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

Writing the Kotlin solution and testing it on the examples.

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

Saving the example inputs.

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

Building and testing it.

Assistant 2026-11-07T15:20:20.465Z

No exit status or program output came back, so the tool failed, not the code. Retrying the same command once.

Assistant 2026-11-07T15:20:24.942Z

Retry succeeded. Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T15:20:31.785Z

Cross-checking with the C++17 version.

Assistant 2026-11-07T15:20:35.832Z

Compiling the C++17 solution.

Assistant 2026-11-07T15:20:40.427Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T15:20:46.547Z

Cross-checking with the Go version.

Assistant 2026-11-07T15:20:50.712Z

Compiling the Go solution.

Assistant 2026-11-07T15:20:55.425Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T15:21:01.722Z

Note: one tool call failed with a sandbox restart; I retried it once and it passed.

All solutions below passed every example.

Kotlin

kotlin
import java.util.*

fun main() {
    val st = StringTokenizer(System.`in`.bufferedReader().readText())
    val n = st.nextToken().toInt()
    val k = st.nextToken().toLong()
    val a = LongArray(n) { st.nextToken().toLong() }
    fun f(lim: Long): Long {
        if (lim < 0) return 0L
        val mx = IntArray(n)
        val mn = IntArray(n)
        var mxh = 0; var mxt = 0; var mnh = 0; var mnt = 0
        var l = 0
        var total = 0L
        for (r in 0 until n) {
            while (mxt > mxh && a[mx[mxt - 1]] <= a[r]) mxt--
            mx[mxt++] = r
            while (mnt > mnh && a[mn[mnt - 1]] >= a[r]) mnt--
            mn[mnt++] = r
            while (a[mx[mxh]] - a[mn[mnh]] > lim) {
                l++
                if (mx[mxh] < l) mxh++
                if (mn[mnh] < l) mnh++
            }
            total += (r - l + 1).toLong()
        }
        return total
    }
    println(f(k) - f(k - 1))
}

C++17

cpp
#include <bits/stdc++.h>
using namespace std;
int n;
vector<long long> a;
long long f(long long lim) {
    if (lim < 0) return 0;
    vector<int> mx(n), mn(n);
    int mxh = 0, mxt = 0, mnh = 0, mnt = 0, l = 0;
    long long total = 0;
    for (int r = 0; r < n; r++) {
        while (mxt > mxh && a[mx[mxt - 1]] <= a[r]) mxt--;
        mx[mxt++] = r;
        while (mnt > mnh && a[mn[mnt - 1]] >= a[r]) mnt--;
        mn[mnt++] = r;
        while (a[mx[mxh]] - a[mn[mnh]] > lim) {
            l++;
            if (mx[mxh] < l) mxh++;
            if (mn[mnh] < l) mnh++;
        }
        total += r - l + 1;
    }
    return total;
}
int main() {
    long long k;
    scanf("%d %lld", &n, &k);
    a.resize(n);
    for (auto &x : a) scanf("%lld", &x);
    printf("%lld\n", f(k) - f(k - 1));
}

Go

go
package main

import (
    "bufio"
    "fmt"
    "os"
)

var n int
var a []int64

func f(lim int64) int64 {
    if lim < 0 {
        return 0
    }
    mx := make([]int, n)
    mn := make([]int, n)
    mxh, mxt, mnh, mnt, l := 0, 0, 0, 0, 0
    var total int64
    for r := 0; r < n; r++ {
        for mxt > mxh && a[mx[mxt-1]] <= a[r] {
            mxt--
        }
        mx[mxt] = r
        mxt++
        for mnt > mnh && a[mn[mnt-1]] >= a[r] {
            mnt--
        }
        mn[mnt] = r
        mnt++
        for a[mx[mxh]]-a[mn[mnh]] > lim {
            l++
            if mx[mxh] < l {
                mxh++
            }
            if mn[mnh] < l {
                mnh++
            }
        }
        total += int64(r - l + 1)
    }
    return total
}

func main() {
    in := bufio.NewReaderSize(os.Stdin, 1<<20)
    var k int64
    fmt.Fscan(in, &n, &k)
    a = make([]int64, n)
    for i := range a {
        fmt.Fscan(in, &a[i])
    }
    fmt.Println(f(k) - f(k-1))
}