CF1399C Boats Competition

原文链接:http://codeforces.com/problemset/problem/1399/C

Boats Competition

There are n people who want to participate in a boat competition. The weight of the i-th participant is wi. Only teams consisting of two people can participate in this competition. As an organizer, you think that it’s fair to allow only teams with the same total weight.

So, if there are k teams (a1,b1), (a2,b2), …, (ak,bk), where ai is the weight of the first participant of the i-th team and bi is the weight of the second participant of the i-th team, then the condition a1+b1=a2+b2=⋯=ak+bk=s, where s is the total weight of each team, should be satisfied.

Your task is to choose such s that the number of teams people can create is the maximum possible. Note that each participant can be in no more than one team.

You have to answer t independent test cases.

Input

The first line of the input contains one integer t (1≤t≤1000) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤50) — the number of participants. The second line of the test case contains n integers w1,w2,…,wn (1≤wi≤n), where wi is the weight of the i-th participant.

Output

For each test case, print one integer k: the maximum number of teams people can compose with the total weight s, if you choose s optimally.

Example
input

5
5
1 2 3 4 5
8
6 6 6 6 6 6 8 8
8
1 2 2 1 2 1 1 2
3
1 3 3
6
1 1 3 4 2 2

output

2
3
4
1
2

Note

In the first test case of the example, we can reach the optimal answer for s=6. Then the first boat is used by participants 1 and 5 and the second boat is used by participants 2 and 4 (indices are the same as weights).

In the second test case of the example, we can reach the optimal answer for s=12. Then first 6 participants can form 3 pairs.

In the third test case of the example, we can reach the optimal answer for s=3. The answer is 4 because we have 4 participants with weight 1 and 4 participants with weight 2.

In the fourth test case of the example, we can reach the optimal answer for s=4 or s=6.

In the fifth test case of the example, we can reach the optimal answer for s=3. Note that participant with weight 3 can’t use the boat because there is no suitable pair for him in the list.

题目大意

t t t组询问,每组给一个数 n n n以及由 n n n个数组成的数列。

要求使尽可能多的数两两配对,且每组配对的两个数的和相等,求最大的可能组数。

题解

因为给的 w i ≤ n ≤ 50 w_i\le n\le 50 win50所以我们可以直接暴力枚举最后的和,再依次检验取最大值。

代码
#include
using namespace std;
int wei[105];
bool vis[105];
int t,n,ans,mn,mx;
void in()
{
	int w;
	mn=55,mx=0;
	memset(wei,0,sizeof(wei));
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&w);
		++wei[w];
		mn=min(mn,w);
		mx=max(mx,w);
	}
}
int check(int s)
{
	int re=0,h=(s-1)/2;
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=h;++i)
	{
		if(vis[s-i])break;
		if(!wei[i])continue;
		re+=min(wei[i],wei[s-i]);
		vis[i]=1;
	}
	if(s%2==0)re+=wei[s/2]/2;
	return re;
}
void ac()
{
	ans=0,mn+=mn,mx+=mx;
	for(int i=mn;i<=mx;++i)ans=max(ans,check(i));
	printf("%d\n",ans);
}
int main()
{
	scanf("%d",&t);
	for(int i=1;i<=t;++i)
	in(),ac();
}

你可能感兴趣的:(杂============)