zoukankan      html  css  js  c++  java
  • Codeforces Round #661 (Div. 3) 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

    ~~

    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

    Copy

    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

    Copy

    2
    3
    4
    1
    2
    

    这个题关键的地方就是注意到数据范围都很小(不管是n还是(a_i)),这就启示我们可以暴力。首先利用桶的思想预处理出来每个位置有多少个数,然后直接枚举每一个可能的重量和w,第二层循环枚举数对的其中一个数,然后直接判断桶里和这个数和为w的另一个数的个数,如果个数大于1的话说明能凑出来一对,累加到答案后更新桶里位置的个数。特别注意如果是a+a=w这种情况的话需要特判一下。

    #include <bits/stdc++.h>
    using namespace std;
    int a[55], ans, match[105];
    bool vis[55];
    int main()
    {
    	int t;
    	cin >> t;
    	while(t--)
    	{
    		int n;
    		cin >> n;
    		ans = 0;
    		for(int i = 1; i <= n; i++)
    		{
    			cin >> a[i];
    		}
    		sort(a + 1, a + n +1);
    		for(int w = 1; w <= a[n] + a[n - 1]; w++)//枚举重量和为w
    		{
    			int cnt = 0;
    			memset(match, 0, sizeof(match));//重新初始化一遍桶 
    			for(int i = 1; i <= n; i++)
    			{
    				match[a[i]]++;
    			}
    			for(int i = 1; i <= n; i++)
    			{
    				if(w - a[i] >= 1 && match[w - a[i]] && match[a[i]])//这两个数必须都得存在,因为当前枚举到的数可能已经被用过了,因此也得判断桶 
    				{
    					if(w - a[i] == a[i] && match[w - a[i]] <= 1) continue; 
    					cnt++;
    					match[w - a[i]]--;//更新桶 
    					match[a[i]]--;
    				}
    			} 
    			ans = max(ans, cnt);
    		}
    		cout << ans << endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    iOS开发实用干货——强化你的Xcode控制台
    Android App 性能优化实践
    AFNetworking 之于 https 认证
    点击 Run 之后发生了什么?
    happypack 原理解析
    JavaScript 笔记 ( Prototype )
    成立快两年的安卓绿色联盟,现在怎么样了?
    盘点20款主流应用FPS,最Skr帧率测试方法都在这里!
    探寻百度AI3.0背后的技术实践
    流畅购物哪家强?购物类应用“页面过度绘制”情况调查
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/13452960.html
Copyright © 2011-2022 走看看