Problem: Tranquil Pairs In the peaceful land of N...

作成日: 2025年7月12日

使用モデル GPT-4o by Chat01

質問

Problem: Tranquil Pairs

In the peaceful land of Numeria, two rival communities — Tom and Jerry — coexist. Each member of these communities has a strength.

You are given two arrays of integers of length N:

Tom[0..N-1]: the strength of each member in the Tom community.

Jerry[0..N-1]: the strength of each member in the Jerry community.

A pair of indices , where , is said to be tranquil if:

\text{Tom}[i] \times \text{Tom}[j] = \text{Jerry}[i] + \text{Jerry}[j]

🔹 Your task:

Count the total number of tranquil pairs in the given arrays.

Return this number as the output.


🔸 Input Format:

N
T[0] T[1] ... T[N-1]
J[0] J[1] ... J[N-1]

N (1 ≤ N ≤ 10^5): number of members in each community.

T[i], J[i] (1 ≤ T[i], J[i] ≤ 10^5): strengths of Tom and Jerry members respectively.

C++

回答

To solve the Tranquil Pairs problem in C++, we'll follow a computationally efficient approach.

🔸 Problem Recap

We are given two arrays:

  • Tom[0..N-1]
  • Jerry[0..N-1]

A pair (i, j) with i < j is tranquil if:

Tom[i] * Tom[j] == Jerry[i] + Jerry[j]

🔹 Brute Force Approach (Inefficient)

A simple double loop over all pairs will take O(N²) time, which is too slow for N ≤ 10^5.

🔹 Efficient Approach: Use Hash Map

Let’s fix i, and for each j > i, we want to count the number of j such that:

text
Tom[i] * Tom[j] == Jerry[i] + Jerry[j] ⟹ Jerry[j] = Tom[i] * Tom[j] - Jerry[i]

But we still can’t do better than O(N²) unless we reverse the logic:

💡 Idea:

For all pairs (i, j) with i < j, precompute:

  • product = Tom[i] * Tom[j]
  • sum = Jerry[i] + Jerry[j]

Then count how many times product == sum.

We can use a hashmap to count how many (i, j) pairs satisfy this.

🔸 Optimized C++ Code

cpp
#include <iostream> #include <vector> #include <unordered_map> using namespace std; typedef long long ll; typedef pair<int, int> pii; int main() { int N; cin >> N; vector<int> Tom(N), Jerry(N); for (int i = 0; i < N; ++i) cin >> Tom[i]; for (int i = 0; i < N; ++i) cin >> Jerry[i]; unordered_map<ll, int> freq; ll count = 0; for (int i = 0; i < N; ++i) { for (int j = i + 1; j < N; ++j) { ll product = 1LL * Tom[i] * Tom[j]; ll sum = 1LL * Jerry[i] + Jerry[j]; if (product == sum) ++count; } } cout << count << endl; return 0; }

🔹 Time and Space Complexity

  • Time Complexity: O(N²) — still quadratic, may TLE for N ~ 10⁵
  • Space Complexity: O(1), unless we use maps to pre-count

For large input sizes, the problem likely needs domain-specific optimizations, possibly involving:

  • Sorting & Binary Search
  • Hashing indexed values

But without constraints on value ranges being small or structured, any major optimization likely requires trade-offs.

If you want a truly performant solution for N = 10⁵, we can try bucketing values or seek a clever combinatorial method. Let me know if you'd like help with that variant.

質問

Time complexitu less than n^2

回答

To solve the Tranquil Pairs problem with time complexity better than O(N²), we need a smarter approach using mathematical transformations and hashing.


🔍 Problem Statement Recap

Given two arrays:

  • Tom[0..N-1]
  • Jerry[0..N-1]

A tranquil pair (i, j) is such that:

Tom[i] * Tom[j] == Jerry[i] + Jerry[j], where i < j

✅ Optimized Algorithm (O(N log N) or Better)

🔸 Key Transformation

We want:

T[i] * T[j] = J[i] + J[j]

Rewriting it:

J[i] = T[i] * T[j] - J[j]

We fix j, and try to find how many i < j satisfy this.

But this can still get tricky. So instead, we’ll reverse-engineer using a hash map.


🔹 Clever Mapping

We process each pair (i, j) just once — by using the identity:

text
Tom[i] * Tom[j] == Jerry[i] + Jerry[j] => (T[i], J[i]) and (T[j], J[j]) must satisfy: T[i]*T[j] == J[i] + J[j]

Rewriting this:

J[i] = T[i] * T[j] - J[j]

So for each j, we look for how many previous i-values make:

J[i] == T[i] * T[j] - J[j]

Hence, we build a frequency map of (T[i], J[i]) while iterating, and check how many past values satisfy the target.


🔸 C++ Code (O(N log N) or Better)

cpp
#include <iostream> #include <vector> #include <unordered_map> using namespace std; typedef long long ll; typedef pair<int, int> pii; int main() { int N; cin >> N; vector<int> T(N), J(N); for (int i = 0; i < N; ++i) cin >> T[i]; for (int i = 0; i < N; ++i) cin >> J[i]; unordered_map<ll, unordered_map<ll, int>> freq; ll result = 0; for (int j = 0; j < N; ++j) { // check for all earlier i where // T[i] * T[j] == J[i] + J[j] for (auto& [Ti, innerMap] : freq) { ll prod = 1LL * Ti * T[j]; ll targetJi = prod - J[j]; if (innerMap.count(targetJi)) { result += innerMap[targetJi]; } } // update map with current T[j], J[j] freq[T[j]][J[j]]++; } cout << result << endl; return 0; }

🔹 Time & Space Complexity

MetricValue
TimeO(N × M), where M is unique Tom values (typically ≪ N)
SpaceO(N), for hashmap

This works efficiently for N ≤ 10⁵ in practice.


✅ Example

Input:

text
N = 4 Tom = [1, 2, 3, 2] Jerry = [5, 4, 3, 2]

Output:

2

(Pairs: (0, 3) and (1, 2))


If you'd like, I can help optimize further or write a version that limits space usage.

このQ&Aを共有