tinycodinglm / cp2 /cp-0013.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
11.9 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. Tools: write_file(path, content) writes a UTF-8 file (creating directories); run_shell(command) runs a bash command and returns its output and exit status. Test solutions on the provided examples before answering.

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

Two Shelf Price Pairs

Topic: Two pointers and sliding window | Difficulty: easy

Problem

Shelf A has n items with prices a[1..n] and shelf B has m items with prices b[1..m]; both lists are sorted in non-decreasing order and may contain repeated prices. Count the pairs (i, j) such that a[i] + b[j] = S. Pairs are distinguished by positions, so equal prices at different positions count separately.

Input

The first line contains n, m and S. The second line contains a[1..n]; the third line contains b[1..m].

Output

Print the number of pairs.

Constraints

  • 1 <= n, m <= 2*10^5
  • 1 <= a[i], b[j] <= 10^9
  • 1 <= S <= 2*10^9

Examples

Example 1

Input:

markdown
4 3 5
1 2 2 3
3 3 4

Output:

markdown
5

Note: (1,4) gives one pair; each of the two 2s pairs with each of the two 3s, giving four more.

Example 2

Input:

markdown
3 3 4
2 2 2
2 2 2

Output:

markdown
9

Solve this task. Write solutions in C#, JavaScript (Node.js), Python 3, test what you can run on the examples, and send back the final programs. Include a concise rationale (approach, correctness idea, time/space complexity).

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

Writing the C# solution and testing it on the examples.

Assistant 2026-11-07T10:20:09.870Z

Saving the example inputs.

2 tool calls (write_file)
Assistant 2026-11-07T10:20:16.536Z

Compiling the C# solution.

Assistant 2026-11-07T10:20:20.465Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T10:20:27.386Z

Cross-checking with the JavaScript (Node.js) version.

Assistant 2026-11-07T10:20:31.785Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T10:20:38.061Z

Cross-checking with the Python 3 version.

Assistant 2026-11-07T10:20:42.930Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T10:20:48.561Z

Approach. Walk i from the left of A and j from the right of B. On equal sums count the full runs of equal values on both sides, multiply their lengths and skip both runs.

Correctness. Smaller sums can only grow by advancing i and larger ones only shrink by retreating j, so no pair is skipped; each matching pair of runs is counted once as a product.

Complexity. Time: O(n + m). Space: O(n + m).

All solutions below passed every example.

C#

csharp
using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main()
    {
        var t = Console.In.ReadToEnd().Split((char[])null, StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray();
        int n = (int)t[0], m = (int)t[1];
        long s = t[2];
        long[] a = t.Skip(3).Take(n).ToArray();
        long[] b = t.Skip(3 + n).Take(m).ToArray();
        int i = 0, j = m - 1;
        long ans = 0;
        while (i < n && j >= 0)
        {
            long v = a[i] + b[j];
            if (v < s) i++;
            else if (v > s) j--;
            else
            {
                int i2 = i;
                while (i2 < n && a[i2] == a[i]) i2++;
                int j2 = j;
                while (j2 >= 0 && b[j2] == b[j]) j2--;
                ans += (long)(i2 - i) * (j - j2);
                i = i2;
                j = j2;
            }
        }
        Console.WriteLine(ans);
    }
}

JavaScript (Node.js)

javascript
const t = require('fs').readFileSync(0, 'utf8').split(/\s+/).filter(x => x.length).map(Number);
const n = t[0], m = t[1], s = t[2];
const a = t.slice(3, 3 + n), b = t.slice(3 + n, 3 + n + m);
let i = 0, j = m - 1, ans = 0;
while (i < n && j >= 0) {
  const v = a[i] + b[j];
  if (v < s) i++;
  else if (v > s) j--;
  else {
    let i2 = i;
    while (i2 < n && a[i2] === a[i]) i2++;
    let j2 = j;
    while (j2 >= 0 && b[j2] === b[j]) j2--;
    ans += (i2 - i) * (j - j2);
    i = i2;
    j = j2;
  }
}
console.log(String(ans));

Python 3

python
import sys
t = sys.stdin.read().split()
n, m, s = int(t[0]), int(t[1]), int(t[2])
a = list(map(int, t[3:3 + n]))
b = list(map(int, t[3 + n:3 + n + m]))
i, j, ans = 0, m - 1, 0
while i < n and j >= 0:
    v = a[i] + b[j]
    if v < s:
        i += 1
    elif v > s:
        j -= 1
    else:
        i2 = i
        while i2 < n and a[i2] == a[i]:
            i2 += 1
        j2 = j
        while j2 >= 0 and b[j2] == b[j]:
            j2 -= 1
        ans += (i2 - i) * (j - j2)
        i, j = i2, j2
print(ans)