Div3 1399 C 暴力尺取

4 篇文章 0 订阅

C. Boats Competition

There are n n n people who want to participate in a boat competition. The weight of the i i i-th participant is w i w_i 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 k k teams ( a 1 , b 1 ) (a_1, b_1) (a1,b1), ( a 2 , b 2 ) (a_2, b_2) (a2,b2), … \dots , ( a k , b k ) (a_k, b_k) (ak,bk), where a i a_i ai is the weight of the first participant of the i i i-th team and b i b_i bi is the weight of the second participant of the i i i-th team, then the condition a 1 + b 1 = a 2 + b 2 = ⋯ = a k + b k = s a_1 + b_1 = a_2 + b_2 = \dots = a_k + b_k = s a1+b1=a2+b2==ak+bk=s, where s s s is the total weight of each team, should be satisfied.

Your task is to choose such s s 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 t t independent test cases.

Input

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

The first line of the test case contains one integer n n n ( 1 ≤ n ≤ 50 1 \le n \le 50 1n50) — the number of participants. The second line of the test case contains n n n integers w 1 , w 2 , … , w n w_1, w_2, \dots, w_n w1,w2,,wn ( 1 ≤ w i ≤ n 1 \le w_i \le n 1win), where w i w_i wi is the weight of the i i i-th participant.

Output

For each test case, print one integer k k k: the maximum number of teams people can compose with the total weight s s s, if you choose s s 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 s=6 s=6. Then the first boat is used by participants 1 1 1 and 5 5 5 and the second boat is used by participants 2 2 2 and 4 4 4 (indices are the same as weights).

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

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

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

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

题意

要求从给出的 n n n个数里面抽取多对数 [ a , b ] [a,b] [a,b],最后满足每一对数的和一样 a + b = t a + b = t a+b=t

注意:每一对数中的 a , b a,b a,b不能重复选取

解题思路

看到这道题,看到 n < = 50 n <= 50 n<=50 我啪的一下!很快啊,想要尝试暴力枚举

然后我左写写右写写,没写明白,最后又想试试哈希,结果也没整明白

于是我满不情愿的看了题解,闻所未闻的尺取法映入眼帘

如果不会尺取法的同学可能和我一样就要寄咯,不排除有的同学还有别的方法

幽默一下。。。。

怎么用尺取法解决这道问题呢?

尺取法 和暴力很相似,我们可以遍历所有中 a + b a + b a+b的情况

接着就是 a , b a,b a,b该如何选值了,就拿样例为例

5
1 2 3 4 5

首先我们取最大、最小值

mx = 5,mn = 1;

暴力查找的范围

mn + 1 <= i <= mx * 2 //一对

定义头尾指针

int l = 0,r = n - 1; //取决于你自己设置的起点

讨论 a [ l ] + a [ r ] a[l] + a[r] a[l]+a[r] i i i的值

如果相等,同时向中间移动

while(a[l] + a[r] == t && l < r) {
    l++; r--;
    tmp++;
}

如果左边大,说明我们应该让值小一点, r − − r-- r

while(a[l] + a[r] > t && l < r) r--;

如果右边大,说明我们应该让值大一点, l + + l++ l++

while(a[l] + a[r] < t && l < r) l++;

终止条件

if(l >= r) {
    ans = max(ans,tmp);
    break;
}

哦对了!如果只有一个人的话,说明是凑不成组的,那么团队的最大数量是0

C o d e Code Code

My Sumbit

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
#define str string
#define pb push_back
#define all(x) begin(x),end(x)
const int N = 60;

inline void solve() {
    int n; cin >> n; 
    vector<int> a(n);
    for(auto &it : a) cin >> it;
    sort(all(a));

    if(n == 1) return cout << 0 << endl,void();
    int mn = a[0],mx = a[n - 1];

    int ans = 1;
    for(int t = mn + 1;t <= mx * 2;t++) {
        int l = 0,r = n - 1,tmp = 0;
        while(1) {
            while(a[l] + a[r] == t && l < r) {
                l++; r--;
                tmp++;
            }

            while(a[l] + a[r] > t && l < r) r--;

            while(a[l] + a[r] < t && l < r) l++;

            if(l >= r) {
                ans = max(ans,tmp);
                break;
            }
        }        
    }

    cout << ans << endl;

    
}


int main(){
    // freopen("input.txt","r",stdin);
    int t; cin >> t;
    while(t--) solve();
    // solve();
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值