A - Mike and Children

A - Mike and Children(枚举)

Mike decided to teach programming to children in an elementary school. He knows that it is not an easy task to interest children in that age to code. That is why he decided to give each child two sweets.

Mike has n sweets with sizes a1,a2,…,an. All his sweets have different sizes. That is, there is no such pair (i,j) (1≤i,j≤n) such that i≠j and ai=aj.

Since Mike has taught for many years, he knows that if he gives two sweets with sizes ai and aj to one child and ak and ap to another, where (ai+aj)≠(ak+ap), then a child who has a smaller sum of sizes will be upset. That is, if there are two children who have different sums of sweets, then one of them will be upset. Apparently, Mike does not want somebody to be upset.

Mike wants to invite children giving each of them two sweets. Obviously, he can’t give one sweet to two or more children. His goal is to invite as many children as he can.

Since Mike is busy preparing to his first lecture in the elementary school, he is asking you to find the maximum number of children he can invite giving each of them two sweets in such way that nobody will be upset.

Input
The first line contains one integer n (2≤n≤1000) — the number of sweets Mike has.

The second line contains n integers a1,a2,…,an (1≤ai≤10^5) — the sizes of the sweets. It is guaranteed that all integers are distinct.

Output
Print one integer — the maximum number of children Mike can invite giving each of them two sweets in such way that nobody will be upset.

Examples
Input
8
1 8 3 11 4 9 2 7
Output
3
Input
7
3 1 7 11 9 2 12
Output
2
Note
In the first example, Mike can give 9+2=11 to one child, 8+3=11 to another one, and 7+4=11 to the third child. Therefore, Mike can invite three children. Note that it is not the only solution.

In the second example, Mike can give 3+9=12 to one child and 1+11 to another one. Therefore, Mike can invite two children. Note that it is not the only solution.

思路:

由于n范围小,我们可以考虑O(n^2)复杂度,将所有糖果两两之和算出来再用数组存储每种和的组合数,因为题目里有说每颗糖果重量不同,不会出现两颗重量相同而重复计数的情况。

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=1000005;
int a[N],b[N];
bool cmp(int x,int y)
{
    return x>y;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    for(int i=0;i<n;i++)cin>>a[i];
    for(int i=0;i<n-1;i++)
        for(int j=i+1;j<n;j++)
            b[a[i]+a[j]]++;
    sort(b,b+N,cmp);
    int ans=b[0];
    cout<<ans<<endl;
    return 0;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值