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

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.

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

Round Trip Fuel Stops

Topic: Arrays and prefix sums | Difficulty: hard

Problem

A circular road has n stations numbered 0..n-1 in order, and after station n-1 comes station 0. Passing through station i changes the fuel in a truck by a[i] (it may be negative). A truck starts at some station s with an empty tank, and visits stations s, s+1, ..., s+n-1 (indices modulo n), applying each change on arrival. The start s is called safe if the fuel level is never negative after any of the n changes. Count the safe starting stations.

Input

The first line contains n. The second line contains n integers a[0..n-1].

Output

Print the number of safe starting stations.

Constraints

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

Examples

Example 1

Input:

markdown
3
2 -3 1

Output:

markdown
1

Note: Start 2 gives levels 1, 3, 0. Start 0 gives 2, -1 and start 1 gives -3, so only start 2 is safe.

Example 2

Input:

markdown
2
-1 1

Output:

markdown
1

Note: The total is zero; only start 1 (levels 1, 0) is safe.

Solve this in Ruby, Java 17, JavaScript (Node.js), one complete program per language. Code only, no explanation.

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

Ruby

ruby
n = gets.to_i
a = gets.split.map(&:to_i)
p = [0]
(0...2 * n).each { |i| p << p[-1] + a[i % n] }
dq = []
head = 0
push = lambda do |t|
  dq.pop while dq.size > head && p[dq[-1]] >= p[t]
  dq << t
end
(1..n).each { |t| push.call(t) }
cnt = 0
(0...n).each do |s|
  push.call(s + n) if s > 0
  head += 1 while dq[head] < s + 1
  cnt += 1 if p[dq[head]] >= p[s]
end
puts cnt

Java 17

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

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());
        StringTokenizer st = new StringTokenizer(br.readLine());
        long[] a = new long[n];
        for (int i = 0; i < n; i++) a[i] = Long.parseLong(st.nextToken());
        long[] p = new long[2 * n + 1];
        for (int i = 0; i < 2 * n; i++) p[i + 1] = p[i] + a[i % n];
        int[] dq = new int[2 * n + 2];
        int head = 0, tail = 0;
        int cnt = 0;
        for (int t = 1; t <= n; t++) {
            while (tail > head && p[dq[tail - 1]] >= p[t]) tail--;
            dq[tail++] = t;
        }
        for (int s = 0; s < n; s++) {
            if (s > 0) {
                int t = s + n;
                while (tail > head && p[dq[tail - 1]] >= p[t]) tail--;
                dq[tail++] = t;
            }
            while (dq[head] < s + 1) head++;
            if (p[dq[head]] >= p[s]) cnt++;
        }
        System.out.println(cnt);
    }
}

JavaScript (Node.js)

javascript
const t = require('fs').readFileSync(0, 'utf8').split(/\s+/).filter(x => x.length).map(Number);
const n = t[0];
const p = new Float64Array(2 * n + 1);
for (let i = 0; i < 2 * n; i++) p[i + 1] = p[i] + t[1 + (i % n)];
const dq = new Int32Array(2 * n + 2);
let head = 0, tail = 0, cnt = 0;
function push(x) {
  while (tail > head && p[dq[tail - 1]] >= p[x]) tail--;
  dq[tail++] = x;
}
for (let x = 1; x <= n; x++) push(x);
for (let s = 0; s < n; s++) {
  if (s > 0) push(s + n);
  while (dq[head] < s + 1) head++;
  if (p[dq[head]] >= p[s]) cnt++;
}
console.log(String(cnt));