原题链接:Codeforces1121B
题目大意:给定 $n$ 颗大小严格不同的糖,给孩子们每人分 $2$ 颗,要求每人分得的糖大小之和相同,求最多能给几个孩子这样分配。
暴力
把所有情况枚举一遍,unordered_map
存两数之和的次数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| #include <iostream>
#include <unordered_map>
using namespace std;
const int N = 1010;
int n;
int a[N];
int main() {
cin >> n;
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
unordered_map<int, int> mp;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++)
mp[a[i] + a[j]]++;
}
int res = 0;
for (auto x : mp) res = max(res, x.second);
printf("%d\n", res);
return 0;
}
|