File size: 3,860 Bytes
b27bfb3
 
 
 
1
2
3
4
5
{"type": "session", "harness": "cp-practice-sim", "id": "cp-0044", "name": "Deadline Juggler", "mode": "chat", "topic": "Sorting and greedy", "difficulty": "easy", "tags": ["sorting", "scheduling", "exchange argument"], "explanation_mode": "concise-rationale", "tool_scenario": "none", "expected_recovery": "N/A - no fault injected.", "languages": ["rust", "typescript", "cpp"], "unexecuted_languages": [], "time_complexity": "O(n log n)", "space_complexity": "O(n)"}
{"type": "message", "message": {"role": "system", "content": "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. ", "timestamp": 1794158401700}}
{"type": "message", "message": {"role": "user", "content": "# Deadline Juggler\n\nTopic: Sorting and greedy | Difficulty: easy\n\n## Problem\nA 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.\n\n## Input\nThe first line contains n. Each of the next n lines contains d_i and t_i.\n\n## Output\nPrint one integer: the minimum possible maximum lateness.\n\n## Constraints\n- 1 <= n <= 2*10^5\n- 1 <= d_i <= 10^9\n- 1 <= t_i <= 10^15\n\n## Examples\n### Example 1\nInput:\n```\n3\n3 4\n2 3\n4 12\n```\nOutput:\n```\n1\n```\nNote: Order by deadline: (2,3), (3,4), (4,12) finish at 2, 5, 9; the second is late by 1.\n\n### Example 2\nInput:\n```\n2\n5 100\n5 100\n```\nOutput:\n```\n0\n```\n\nSolve this in Rust, TypeScript, C++17, one complete program per language. Include a concise rationale (approach, correctness idea, time/space complexity).", "timestamp": 1794158403537}}
{"type": "message", "message": {"role": "assistant", "content": "**Approach.** Process tasks in non-decreasing deadline order and track the maximum of finish time minus deadline, floored at zero.\n\n**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.\n\n**Complexity.** Time: O(n log n). Space: O(n).\n\n### Rust\n```rust\nuse std::io::*;\nfn main() {\n    let mut t = String::new();\n    stdin().read_to_string(&mut t).unwrap();\n    let mut it = t.split_whitespace().map(|x| x.parse::<i64>().unwrap());\n    let n = it.next().unwrap() as usize;\n    let mut v: Vec<(i64, i64)> = Vec::new();\n    for _ in 0..n {\n        let d = it.next().unwrap();\n        let t = it.next().unwrap();\n        v.push((t, d));\n    }\n    v.sort();\n    let mut cur = 0i64;\n    let mut best = 0i64;\n    for (t, d) in v {\n        cur += d;\n        best = best.max(cur - t);\n    }\n    println!(\"{}\", best);\n}\n```\n\n### TypeScript\n```typescript\ndeclare const require: any;\nconst d: number[] = require('fs').readFileSync(0, 'utf8').split(/\\s+/).filter((x: string) => x).map(Number);\nconst n: number = d[0];\nconst v: number[][] = [];\nfor (let i = 0; i < n; i++) v.push([d[2 + 2 * i], d[1 + 2 * i]]);\nv.sort((a, b) => a[0] - b[0]);\nlet cur = 0, best = 0;\nfor (const [t, dur] of v) {\n  cur += dur;\n  best = Math.max(best, cur - t);\n}\nconsole.log(best);\n```\n\n### C++17\n```cpp\n#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n    int n;\n    scanf(\"%d\", &n);\n    vector<pair<long long, long long>> v(n);\n    for (auto &p : v) scanf(\"%lld %lld\", &p.second, &p.first);\n    sort(v.begin(), v.end());\n    long long cur = 0, best = 0;\n    for (auto &p : v) {\n        cur += p.second;\n        best = max(best, cur - p.first);\n    }\n    printf(\"%lld\\n\", best);\n}\n```", "timestamp": 1794158405511}}