-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC_Game_of_Mathletes.cpp
More file actions
71 lines (56 loc) · 1.58 KB
/
Copy pathC_Game_of_Mathletes.cpp
File metadata and controls
71 lines (56 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
vector<int> optimalScore(int t, vector<pair<int, int>> &nk, vector<vector<int>> &arrays) {
vector<int> results;
for (int test = 0; test < t; ++test) {
int n = nk[test].first;
int k = nk[test].second;
vector<int> &x = arrays[test];
// Frequency map to count occurrences
unordered_map<int, int> freq;
for (int num : x) {
freq[num]++;
}
int score = 0;
for (auto &it : freq) {
int num = it.first;
int count = it.second;
int complement = k - num;
if (freq.find(complement) != freq.end()) {
int pairs = min(count, freq[complement]);
if (num == complement) {
pairs /= 2; // Special case: self-pair
}
score += pairs;
// Decrease frequencies
freq[num] -= pairs;
freq[complement] -= pairs;
}
}
results.push_back(score);
}
return results;
}
int main() {
int t;
cin >> t;
vector<pair<int, int>> nk(t);
vector<vector<int>> arrays(t);
for (int i = 0; i < t; ++i) {
int n, k;
cin >> n >> k;
nk[i] = {n, k};
arrays[i].resize(n);
for (int j = 0; j < n; ++j) {
cin >> arrays[i][j];
}
}
vector<int> results = optimalScore(t, nk, arrays);
for (int score : results) {
cout << score << "\n";
}
return 0;
}