File size: 4,345 Bytes
b27bfb3 | 1 2 3 4 5 | {"type": "session", "harness": "cp-practice-sim", "id": "cp-0009", "name": "Round Trip Fuel Stops", "mode": "chat", "topic": "Arrays and prefix sums", "difficulty": "hard", "tags": ["prefix sums", "sliding window minimum", "circular array"], "explanation_mode": "none", "tool_scenario": "none", "expected_recovery": "N/A - no fault injected.", "languages": ["ruby", "java", "javascript"], "unexecuted_languages": [], "time_complexity": "O(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. Reply with code only; no explanation. ", "timestamp": 1794032401700}}
{"type": "message", "message": {"role": "user", "content": "# Round Trip Fuel Stops\n\nTopic: Arrays and prefix sums | Difficulty: hard\n\n## Problem\nA 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.\n\n## Input\nThe first line contains n. The second line contains n integers a[0..n-1].\n\n## Output\nPrint the number of safe starting stations.\n\n## Constraints\n- 1 <= n <= 2*10^5\n- |a[i]| <= 10^9\n\n## Examples\n### Example 1\nInput:\n```\n3\n2 -3 1\n```\nOutput:\n```\n1\n```\nNote: Start 2 gives levels 1, 3, 0. Start 0 gives 2, -1 and start 1 gives -3, so only start 2 is safe.\n\n### Example 2\nInput:\n```\n2\n-1 1\n```\nOutput:\n```\n1\n```\nNote: The total is zero; only start 1 (levels 1, 0) is safe.\n\nSolve this in Ruby, Java 17, JavaScript (Node.js), one complete program per language. Code only, no explanation.", "timestamp": 1794032403537}}
{"type": "message", "message": {"role": "assistant", "content": "### Ruby\n```ruby\nn = gets.to_i\na = gets.split.map(&:to_i)\np = [0]\n(0...2 * n).each { |i| p << p[-1] + a[i % n] }\ndq = []\nhead = 0\npush = lambda do |t|\n dq.pop while dq.size > head && p[dq[-1]] >= p[t]\n dq << t\nend\n(1..n).each { |t| push.call(t) }\ncnt = 0\n(0...n).each do |s|\n push.call(s + n) if s > 0\n head += 1 while dq[head] < s + 1\n cnt += 1 if p[dq[head]] >= p[s]\nend\nputs cnt\n```\n\n### Java 17\n```java\nimport java.io.*;\nimport java.util.*;\n\npublic class Main {\n public static void main(String[] args) throws IOException {\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n int n = Integer.parseInt(br.readLine().trim());\n StringTokenizer st = new StringTokenizer(br.readLine());\n long[] a = new long[n];\n for (int i = 0; i < n; i++) a[i] = Long.parseLong(st.nextToken());\n long[] p = new long[2 * n + 1];\n for (int i = 0; i < 2 * n; i++) p[i + 1] = p[i] + a[i % n];\n int[] dq = new int[2 * n + 2];\n int head = 0, tail = 0;\n int cnt = 0;\n for (int t = 1; t <= n; t++) {\n while (tail > head && p[dq[tail - 1]] >= p[t]) tail--;\n dq[tail++] = t;\n }\n for (int s = 0; s < n; s++) {\n if (s > 0) {\n int t = s + n;\n while (tail > head && p[dq[tail - 1]] >= p[t]) tail--;\n dq[tail++] = t;\n }\n while (dq[head] < s + 1) head++;\n if (p[dq[head]] >= p[s]) cnt++;\n }\n System.out.println(cnt);\n }\n}\n```\n\n### JavaScript (Node.js)\n```javascript\nconst t = require('fs').readFileSync(0, 'utf8').split(/\\s+/).filter(x => x.length).map(Number);\nconst n = t[0];\nconst p = new Float64Array(2 * n + 1);\nfor (let i = 0; i < 2 * n; i++) p[i + 1] = p[i] + t[1 + (i % n)];\nconst dq = new Int32Array(2 * n + 2);\nlet head = 0, tail = 0, cnt = 0;\nfunction push(x) {\n while (tail > head && p[dq[tail - 1]] >= p[x]) tail--;\n dq[tail++] = x;\n}\nfor (let x = 1; x <= n; x++) push(x);\nfor (let s = 0; s < n; s++) {\n if (s > 0) push(s + n);\n while (dq[head] < s + 1) head++;\n if (p[dq[head]] >= p[s]) cnt++;\n}\nconsole.log(String(cnt));\n```", "timestamp": 1794032405511}}
|