tinycodinglm / cp2 /cp-0036.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
3.14 kB
{"type": "session", "harness": "cp-practice-sim", "id": "cp-0036", "name": "Palindromic Shelf Segments", "mode": "chat", "topic": "Hashing and counting (maps/sets)", "difficulty": "medium", "tags": ["bitmask", "prefix parity", "counting"], "explanation_mode": "none", "tool_scenario": "none", "expected_recovery": "N/A - no fault injected.", "languages": ["javascript", "ruby", "csharp"], "unexecuted_languages": [], "time_complexity": "O(20 n)", "space_complexity": "O(2^20)"}
{"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": 1794129601700}}
{"type": "message", "message": {"role": "user", "content": "# Palindromic Shelf Segments\n\nTopic: Hashing and counting (maps/sets) | Difficulty: medium\n\n## Problem\nYou are given a sequence of n integers, each between 0 and 19. Count the contiguous non-empty segments whose elements can be rearranged into a palindrome, that is, segments in which at most one value occurs an odd number of times.\n\n## Input\nThe first line contains n. The second line contains n integers.\n\n## Output\nOne integer, the number of such segments.\n\n## Constraints\n- 1 <= n <= 2*10^5\n- 0 <= a_i <= 19\n\n## Examples\n### Example 1\nInput:\n```\n3\n1 1 2\n```\nOutput:\n```\n5\n```\nNote: Three single elements, [1,1] and [1,1,2]; [1,2] fails.\n\n### Example 2\nInput:\n```\n4\n0 1 2 3\n```\nOutput:\n```\n4\n```\n\n### Example 3\nInput:\n```\n4\n5 5 5 5\n```\nOutput:\n```\n10\n```\n\nSolve this in JavaScript (Node.js), Ruby, C#, one complete program per language. Code only, no explanation.", "timestamp": 1794129603537}}
{"type": "message", "message": {"role": "assistant", "content": "### JavaScript (Node.js)\n```javascript\nconst t = require('fs').readFileSync(0, 'utf8').split(/\\s+/).filter(x => x).map(Number);\nconst n = t[0];\nconst cnt = new Int32Array(1 << 20);\ncnt[0] = 1;\nlet cur = 0, ans = 0;\nfor (let i = 1; i <= n; i++) {\n cur ^= 1 << t[i];\n ans += cnt[cur];\n for (let b = 0; b < 20; b++) ans += cnt[cur ^ (1 << b)];\n cnt[cur]++;\n}\nconsole.log(String(ans));\n```\n\n### Ruby\n```ruby\ndata = STDIN.read.split.map(&:to_i)\nn = data[0]\ncnt = Array.new(1 << 20, 0)\ncnt[0] = 1\ncur = 0\nans = 0\n(1..n).each do |i|\n cur ^= 1 << data[i]\n ans += cnt[cur]\n 20.times { |b| ans += cnt[cur ^ (1 << b)] }\n cnt[cur] += 1\nend\nputs ans\n```\n\n### C#\n```csharp\nusing System;\n\nclass Program\n{\n static void Main()\n {\n var t = Console.In.ReadToEnd().Split((char[])null, StringSplitOptions.RemoveEmptyEntries);\n int n = int.Parse(t[0]);\n int[] cnt = new int[1 << 20];\n cnt[0] = 1;\n int cur = 0;\n long ans = 0;\n for (int i = 1; i <= n; i++)\n {\n cur ^= 1 << int.Parse(t[i]);\n ans += cnt[cur];\n for (int b = 0; b < 20; b++) ans += cnt[cur ^ (1 << b)];\n cnt[cur]++;\n }\n Console.WriteLine(ans);\n }\n}\n```", "timestamp": 1794129605511}}