Boats Competition 赛艇
来源codeforses #1399c
链接: [link](链接: link.
贪心,双指针
题面
time limit per test 2 seconds
memory limit per test 256 megabytes
There are n people who want to participate in a boat compettion. The weight of the i-th partcipant is u; . Only teams consisting of twopeople can participate in this competition.As an organizer , you think that its fair to allow only teams with the same total weight
有许多人想参加一艘船的比赛。第一批的重量是u;只有由两人组成的队伍才能参加这场比赛。作为组织者,你认为只允许总重量相同的队伍参加是公平的。
So,if there are ke teams(a,b).(ag,b )… .(QR O ). where a; is the weight of the first participant of the i-th team and by is the weightof the second participant of the i-th team, then the condition a +b =+b=…=aR +b = s, where s is the total weight ofeach team, should be satisfied.
所以,如果有KE队(a,b).(AG,b)…(Qr O)其中a;是第一组的第一名参与者的权重,by是第一组的第二名参与者的权重,则条件a+b=+b=…。=ar+b=s,其中s是队伍的总重量,应该满足。
Your task is to choose such s that the number of teams people can create is the maximum possible.Note that each paricipant can be inno 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.
输入的第一行包含一个整数t(1<t<1000)–测试用例的数量。然后是测试用例。
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 containsn integers wu1, 2,. . .,wn (1≤wi≤n), where w is the weight of the i-th participant.
测试用例的第一行包含一个整数n(1<n<50),一是参与者的数目。测试用例的第二行包含n个整数wu 1,2,。。,w(1≤wi≤n),其中w是第一参与者的权重.
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 soptimally.
对于每个测试用例,打印一个整数k:如果您选择最优的话,人们可以组合的团队的最大数量与总权重为s。
即
选取一个重量k使重量和等于k的两人小组数量最多,输出最多组数
两两分组等于某个值可以使用双指针
而k的取值可以二分(每个人重量在1-u,u<50),
也可从双指针序列中取待处理区间的首尾
最后贪心更新最大组数
main()
{
int n;//an li
int m;//ge shu
cin >> n;
int a[55];
while (n--)
{
cin >> m;
for (int i = 0; i < m; ++i)
cin >> a[i];
sort(a, a + m);
int max=0;
for(char i=0;i<m;++i)
for (char j = m - 1; j > i; --j)
{
char he=i+1, la=j-1;
char num = 1;
int sum = a[i] + a[j];
while (he < la)
{
if ((a[he] + a[la]) == sum)
{
++num;
++he, --la;
}
else if (a[he] + a[la] < sum)
++he;
else
--la;
}
max = max > num ? max : num;
}
cout << max << endl;
}
return 0;
}