A

观察到本题 的范围较小,考虑枚举 的值,然后统计序列 的长度,取所有情况的最大值即可,枚举范围1~10000
点击查看代码
#include <bits/stdc++.h>
using namespace std;
int main()
{int n;cin >> n;vector<int> a(10001);int x = 0;int ans;for (int i = 0; i < n; i++){cin >> x;a[x]++;}for (int i = 2; i <= 10000; i++){int res = 0;for (int j = 1; j <= i - j; j++){if (j == i - j){res += a[j] / 2;}elseres += min(a[j], a[i - j]) * 2;}ans = max(ans, res);}cout << ans << endl;return 0;
}
点击查看代码
for (int i = 2; i <= 10000; i++){int res = 0;for (int j = 1; j <= i - j; j++){if (j == i - j){res += a[j] / 2;}elseres += min(a[j], a[i - j]) * 2;}ans = max(ans, res);}cout << ans << endl;
再找最大值,此时ans即为求
