Codeforces Round #543(Div.2) B. Mike and Children解题报告(暴力)

题目链接

B. Mike and Children
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

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 n n sweets with sizes a 1 a_1 a1 , , , a 2 a_2 a2 , , , … , …, , a n a_n an. All his sweets have different sizes. That is, there is no such pair ( i , j ) (i,j) (i,j) (1 ≤ \leq i , j i,j i,j ≤ \leq n n n) such that i i i j j j and a i a_i ai= a j a_j aj.

Since Mike has taught for many years, he knows that if he gives two sweets with sizes a i a_i ai and a j a_j aj to one child and a k a_k ak and a p a_p ap to another, where ( a i + a j ) (a_i+a_j) (ai+aj) ( a k + a p ) (a_k+a_p) (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 n n (2 ≤ \leq n n n ≤ \leq 1000) — the number of sweets Mike has.

The second line contains n n n integers a 1 , a 2 , … , a n a_1,a_2,…,a_n a1,a2,,an (1 ≤ \leq a i a_i ai ≤ \leq 1 0 5 10^5 105) — 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 9+2=11 9+2=11 to one child, 8 + 3 = 11 8+3=11 8+3=11 to another one, and 7 + 4 = 11 7+4=11 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 3+9=12 3+9=12 to one child and 1 + 11 1+11 1+11 to another one. Therefore, Mike can invite two children. Note that it is not the only solution.

题目意思

有n个不同的数,给每个人分两个数,最后使得任意两个人的两个数之和都相同,问最多能分给多少个人?

解题思路

刚开始拿到这道题的时候,就在想最后两个数之和是不确定的,到底应该是多少呢?因为1 ≤ \leq a i a_i ai ≤ \leq 1 0 5 10^5 105,那么两个数之和最大才是 2 ∗ 1 0 5 2*10^5 2105 ,那我可不可以从 1 1 1 2 ∗ 1 0 5 2*10^5 2105依次去遍历一遍,看一下哪个最大?但仔细一想又发现两数之差不好判断在数组中是否存在(或者说会增加不必要的开销)。最后才想到因为 n n n个数是不相同的,那么肯定不会存在 a i + a j = = a j + a k a_i+a_j==a_j+a_k ai+aj==aj+ak的情况,现在我就可以用两重循环(时间复杂度是 O ( n 2 ) O(n^2) On2,因为n的范围比较小,不会超时)计算所有可能情况下的两数之和,然后从大到小排一下序,让数组的第一个数除以 2 2 2便是答案。

AC代码
#include <bits/stdc++.h>
#define INF 0x3f3f3f
using namespace std;
const int mod=1e9+7;
const int Max_N=1e3+4;
const int Max_M=2e5+4;
typedef pair<int,int>P;
typedef long long ll;
typedef unsigned long long ull;
int ans[Max_M];
bool cmp(int a,int b)
{
    return a>b;
}
int main(int argc, char const *argv[])
{
    int n,a[Max_N];
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i==j)
                continue;
            ans[a[i]+a[j]]++;
        }
    }
    sort(ans,ans+Max_M,cmp);
    printf("%d\n",ans[0]/2);
    return 0;
}
总结

读题的时候,数据范围也是一个很重要的信息,数据范围的不同决定着如何选择解题方案。有时候暴力解题也挺不错的,咕咕咕。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值