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

Two-Stage Bakery

Topic: Sorting and greedy | Difficulty: hard

Problem

A bakery processes n batches. Every batch must first be kneaded by the single kneading machine for a_i minutes and afterwards baked by the single oven for b_i minutes. Each machine handles one batch at a time and cannot be interrupted; a batch can enter the oven only after its kneading is complete, and the oven may wait idle. Both machines process the batches in the same chosen order. Print the smallest possible time at which the last batch leaves the oven (all work starts at time 0).

Input

The first line contains n. Each of the next n lines contains a_i and b_i.

Output

Print one integer: the minimum finishing time.

Constraints

  • 1 <= n <= 2*10^5
  • 1 <= a_i, b_i <= 10^9

Examples

Example 1

Input:

markdown
3
3 6
5 2
1 2

Output:

markdown
12

Note: Order (1,2), (3,6), (5,2): kneading ends at 1, 4, 9; the oven finishes at 3, 10, 12.

Example 2

Input:

markdown
1
4 5

Output:

markdown
9

Solve this in Ruby, Java 17, JavaScript (Node.js), one complete program per language. Include a concise rationale (approach, correctness idea, time/space complexity).

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

Approach. Apply Johnson's rule: batches with a_i <= b_i go first in increasing a_i, the rest go afterwards in decreasing b_i. Simulate the two machines to get the finishing time.

Correctness. For two adjacent batches i, j, putting i first is no worse whenever min(a_i, b_j) <= min(a_j, b_i); this relation is realised by the described order, and exchanges toward it never increase the makespan.

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

Ruby

ruby
n = gets.to_i
g1 = []
g2 = []
n.times do
  a, b = gets.split.map(&:to_i)
  if a <= b
    g1 << [a, b]
  else
    g2 << [a, b]
  end
end
g1.sort_by! { |x| x[0] }
g2.sort_by! { |x| -x[1] }
t1 = 0
t2 = 0
(g1 + g2).each do |a, b|
  t1 += a
  t2 = [t1, t2].max + b
end
puts t2

Java 17

java
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine().trim());
        List<long[]> g1 = new ArrayList<>(), g2 = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            long a = Long.parseLong(st.nextToken()), b = Long.parseLong(st.nextToken());
            if (a <= b) g1.add(new long[]{a, b});
            else g2.add(new long[]{a, b});
        }
        g1.sort((x, y) -> Long.compare(x[0], y[0]));
        g2.sort((x, y) -> Long.compare(y[1], x[1]));
        g1.addAll(g2);
        long t1 = 0, t2 = 0;
        for (long[] j : g1) {
            t1 += j[0];
            t2 = Math.max(t1, t2) + j[1];
        }
        System.out.println(t2);
    }
}

JavaScript (Node.js)

javascript
const d = require('fs').readFileSync(0, 'utf8').split(/\s+/).filter(x => x).map(Number);
const n = d[0];
const g1 = [], g2 = [];
for (let i = 0; i < n; i++) {
  const a = d[1 + 2 * i], b = d[2 + 2 * i];
  if (a <= b) g1.push([a, b]); else g2.push([a, b]);
}
g1.sort((x, y) => x[0] - y[0]);
g2.sort((x, y) => y[1] - x[1]);
let t1 = 0, t2 = 0;
for (const [a, b] of g1.concat(g2)) {
  t1 += a;
  t2 = Math.max(t1, t2) + b;
}
console.log(t2);