tinycodinglm / cp2 /cp-0044.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
3.86 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.

User 2026-11-08T17:20:03.537Z

Deadline Juggler

Topic: Sorting and greedy | Difficulty: easy

Problem

A single worker must do n tasks one after another starting at time 0, without pauses or interruptions. Task i takes d_i time and has deadline t_i. If task i finishes at time f_i, its lateness is max(0, f_i - t_i). You choose the order of the tasks. Print the smallest possible value of the maximum lateness.

Input

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

Output

Print one integer: the minimum possible maximum lateness.

Constraints

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

Examples

Example 1

Input:

markdown
3
3 4
2 3
4 12

Output:

markdown
1

Note: Order by deadline: (2,3), (3,4), (4,12) finish at 2, 5, 9; the second is late by 1.

Example 2

Input:

markdown
2
5 100
5 100

Output:

markdown
0

Solve this in Rust, TypeScript, C++17, one complete program per language. Include a concise rationale (approach, correctness idea, time/space complexity).

Assistant 2026-11-08T17:20:05.511Z

Approach. Process tasks in non-decreasing deadline order and track the maximum of finish time minus deadline, floored at zero.

Correctness. If two adjacent tasks are in the wrong deadline order, swapping them does not increase the maximum lateness, so sorting by deadline is optimal.

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

Rust

rust
use std::io::*;
fn main() {
    let mut t = String::new();
    stdin().read_to_string(&mut t).unwrap();
    let mut it = t.split_whitespace().map(|x| x.parse::<i64>().unwrap());
    let n = it.next().unwrap() as usize;
    let mut v: Vec<(i64, i64)> = Vec::new();
    for _ in 0..n {
        let d = it.next().unwrap();
        let t = it.next().unwrap();
        v.push((t, d));
    }
    v.sort();
    let mut cur = 0i64;
    let mut best = 0i64;
    for (t, d) in v {
        cur += d;
        best = best.max(cur - t);
    }
    println!("{}", best);
}

TypeScript

typescript
declare const require: any;
const d: number[] = require('fs').readFileSync(0, 'utf8').split(/\s+/).filter((x: string) => x).map(Number);
const n: number = d[0];
const v: number[][] = [];
for (let i = 0; i < n; i++) v.push([d[2 + 2 * i], d[1 + 2 * i]]);
v.sort((a, b) => a[0] - b[0]);
let cur = 0, best = 0;
for (const [t, dur] of v) {
  cur += dur;
  best = Math.max(best, cur - t);
}
console.log(best);

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());
    long long cur = 0, best = 0;
    for (auto &p : v) {
        cur += p.second;
        best = max(best, cur - p.first);
    }
    printf("%lld\n", best);
}