tinycodinglm / cp2 /cp-0048.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
13.2 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-08T21:20:03.537Z

Crammed Course Load

Topic: Sorting and greedy | Difficulty: hard

Problem

A student has n courses. Course i needs d_i days of study and must be finished by the end of day t_i. The student studies one course at a time without interruption and without idle days, starting on day 1 (so the first course studied finishes at the end of day d). Courses may be skipped entirely and a skipped course costs no days. A course counts only if it is finished by its deadline. Print the maximum number of courses that can be completed.

Input

The first line contains n. Each of the next n lines contains d_i and t_i.

Output

Print one integer: the maximum number of courses that can be completed on time.

Constraints

  • 1 <= n <= 2*10^5
  • 1 <= d_i <= 10^9
  • 1 <= t_i <= 10^15

Examples

Example 1

Input:

markdown
4
100 200
200 1300
1000 1250
2000 3200

Output:

markdown
3

Note: Take courses 1, 3, 2 in deadline order: finishing times 100, 1100, 1300 all meet the deadlines.

Example 2

Input:

markdown
3
5 4
3 3
4 3

Output:

markdown
1

Note: Courses 1 and 3 are too long for their own deadlines, so only course 2 can be completed.

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-08T21:20:05.511Z

Writing the Kotlin solution and testing it on the examples.

Assistant 2026-11-08T21:20:09.870Z

Saving the example inputs.

2 tool calls (write_file)
Assistant 2026-11-08T21:20:16.536Z

Building and testing it.

Assistant 2026-11-08T21: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-08T21:20:24.942Z

Retry succeeded. Running every example.

2 tool calls (run_shell)
Assistant 2026-11-08T21:20:31.785Z

Cross-checking with the C++17 version.

Assistant 2026-11-08T21:20:35.832Z

Compiling the C++17 solution.

Assistant 2026-11-08T21:20:40.427Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-08T21:20:46.547Z

Cross-checking with the Go version.

Assistant 2026-11-08T21:20:50.712Z

Compiling the Go solution.

Assistant 2026-11-08T21:20:55.425Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-08T21: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.io.*
import java.util.*

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val n = br.readLine().trim().toInt()
    val c = Array(n) { val st = StringTokenizer(br.readLine()); longArrayOf(st.nextToken().toLong(), st.nextToken().toLong()) }
    c.sortBy { it[1] }
    val pq = PriorityQueue<Long>(compareByDescending { it })
    var total = 0L
    for (x in c) {
        pq.add(x[0])
        total += x[0]
        if (total > x[1]) total -= pq.poll()
    }
    println(pq.size)
}

C++17

cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    scanf("%d", &n);
    vector<pair<long long, long long>> v(n);
    for (auto &p : v) scanf("%lld %lld", &p.second, &p.first);
    sort(v.begin(), v.end());
    priority_queue<long long> pq;
    long long total = 0;
    for (auto &p : v) {
        pq.push(p.second);
        total += p.second;
        if (total > p.first) {
            total -= pq.top();
            pq.pop();
        }
    }
    printf("%zu\n", pq.size());
}

Go

go
package main

import (
    "bufio"
    "container/heap"
    "fmt"
    "os"
    "sort"
)

type MaxHeap []int

func (h MaxHeap) Len() int            { return len(h) }
func (h MaxHeap) Less(i, j int) bool  { return h[i] > h[j] }
func (h MaxHeap) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *MaxHeap) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *MaxHeap) Pop() interface{} {
    o := *h
    x := o[len(o)-1]
    *h = o[:len(o)-1]
    return x
}

func main() {
    rd := bufio.NewReaderSize(os.Stdin, 1<<20)
    var n int
    fmt.Fscan(rd, &n)
    type c struct{ d, t int }
    v := make([]c, n)
    for i := range v {
        fmt.Fscan(rd, &v[i].d, &v[i].t)
    }
    sort.Slice(v, func(a, b int) bool { return v[a].t < v[b].t })
    h := &MaxHeap{}
    total := 0
    for _, x := range v {
        heap.Push(h, x.d)
        total += x.d
        if total > x.t {
            total -= heap.Pop(h).(int)
        }
    }
    fmt.Println(h.Len())
}