File size: 3,874 Bytes
b27bfb3 | 1 2 3 4 5 | {"type": "session", "harness": "cp-practice-sim", "id": "cp-0006", "name": "Greenhouse Square Yield", "mode": "chat", "topic": "Arrays and prefix sums", "difficulty": "medium", "tags": ["2D prefix sums", "grid"], "explanation_mode": "none", "tool_scenario": "none", "expected_recovery": "N/A - no fault injected.", "languages": ["javascript", "ruby", "csharp"], "unexecuted_languages": [], "time_complexity": "O(n*m)", "space_complexity": "O(n*m)"}
{"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": 1794021601700}}
{"type": "message", "message": {"role": "user", "content": "# Greenhouse Square Yield\n\nTopic: Arrays and prefix sums | Difficulty: medium\n\n## Problem\nA greenhouse is an n by m grid of plots; plot (i, j) has a signed yield g[i][j] (negative means a net loss). A sprinkler covers a k by k square of plots aligned with the grid. Find the maximum total yield over all placements of the square that lie fully inside the grid.\n\n## Input\nThe first line contains n, m and k. Each of the next n lines contains m integers.\n\n## Output\nPrint the maximum total yield of a k by k square.\n\n## Constraints\n- 1 <= n, m and n*m <= 2*10^5\n- 1 <= k <= min(n, m)\n- |g[i][j]| <= 10^9\n\n## Examples\n### Example 1\nInput:\n```\n3 3 2\n1 2 3\n4 5 6\n7 8 9\n```\nOutput:\n```\n28\n```\n\n### Example 2\nInput:\n```\n2 2 1\n-3 -1\n-4 -2\n```\nOutput:\n```\n-1\n```\nNote: All values are negative, so the answer is the largest single cell.\n\nSolve this in JavaScript (Node.js), Ruby, C#, one complete program per language. Code only, no explanation.", "timestamp": 1794021603537}}
{"type": "message", "message": {"role": "assistant", "content": "### JavaScript (Node.js)\n```javascript\nconst t = require('fs').readFileSync(0, 'utf8').split(/\\s+/).filter(x => x.length).map(Number);\nlet p = 0;\nconst n = t[p++], m = t[p++], k = t[p++];\nconst w = m + 1;\nconst S = new Float64Array((n + 1) * w);\nfor (let i = 1; i <= n; i++) {\n for (let j = 1; j <= m; j++) {\n S[i * w + j] = t[p++] + S[(i - 1) * w + j] + S[i * w + j - 1] - S[(i - 1) * w + j - 1];\n }\n}\nlet best = -Infinity;\nfor (let i = k; i <= n; i++) {\n for (let j = k; j <= m; j++) {\n const v = S[i * w + j] - S[(i - k) * w + j] - S[i * w + j - k] + S[(i - k) * w + j - k];\n if (v > best) best = v;\n }\n}\nconsole.log(String(best));\n```\n\n### Ruby\n```ruby\nt = STDIN.read.split.map(&:to_i)\nn, m, k = t[0], t[1], t[2]\np = 3\ns = Array.new(n + 1) { Array.new(m + 1, 0) }\n(1..n).each do |i|\n (1..m).each do |j|\n s[i][j] = t[p] + s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1]\n p += 1\n end\nend\nbest = nil\n(k..n).each do |i|\n (k..m).each do |j|\n v = s[i][j] - s[i - k][j] - s[i][j - k] + s[i - k][j - k]\n best = v if best.nil? || v > best\n end\nend\nputs best\n```\n\n### C#\n```csharp\nusing System;\nusing System.IO;\nusing System.Linq;\n\nclass Program\n{\n static void Main()\n {\n var t = Console.In.ReadToEnd().Split((char[])null, StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray();\n int p = 0;\n int n = (int)t[p++], m = (int)t[p++], k = (int)t[p++];\n var s = new long[n + 1, m + 1];\n for (int i = 1; i <= n; i++)\n for (int j = 1; j <= m; j++)\n s[i, j] = t[p++] + s[i - 1, j] + s[i, j - 1] - s[i - 1, j - 1];\n long best = long.MinValue;\n for (int i = k; i <= n; i++)\n for (int j = k; j <= m; j++)\n {\n long v = s[i, j] - s[i - k, j] - s[i, j - k] + s[i - k, j - k];\n if (v > best) best = v;\n }\n Console.WriteLine(best);\n }\n}\n```", "timestamp": 1794021605511}}
|