codeforces day 4

problem tags:greedy,brute force

题目:

C. Boats Competition

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 twopeople 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 (𝑎1,𝑏1)(a1,b1), (𝑎2,𝑏2)(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 𝑎1+𝑏1=𝑎2+𝑏2=⋯=𝑎𝑘+𝑏𝑘=𝑠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≤𝑡≤10001≤t≤1000) — the number of test cases. Then 𝑡t test cases follow.

The first line of the test case contains one integer 𝑛n (1≤𝑛≤501≤n≤50) — the number of participants. The second line of the test case contains 𝑛n integers 𝑤1,𝑤2,…,𝑤𝑛w1,w2,…,wn (1≤𝑤𝑖≤𝑛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 𝑠soptimally.

解析:题意n个人,n个体重,两个一组,每组的体重都要一样,问这n个人最多能够有多少个组,因为数据很小直接暴力就完事

#include <iostream>
using   namespace std;
int w[52],a[52];
int main(int argc, const char * argv[]) {
    // insert code here...、
    int t;
    cin>>t;
    while (t--)//题意n个人,n个体重,两个一组,每组的体重都要一样,问这n个人最多能够有多少个组
    {
        int n,Max=0;
        cin>>n;
        for (int i=1; i<=n; i++) {
            cin>>w[i];
        }
        for (int k=2; k<=100; k++)
        {
            memset(a, 0, sizeof a);//将数组a全部初始化成0,
 
            int num=0;
            for (int i=1; i<=n; i++)
            {
                for (int j=1; j<=n; j++)
                {
                    if (j!=i&&k==w[j]+w[i]&&!a[j]&&!a[i])
                    {
                        a[j]=1;
                        a[i]=1;
                        num++;
                    }
                }
            }
            if (num>Max)
            {
                Max=num;
            }
        }
        cout<<Max<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值