Codeforces C. Boats Competition (枚举 / 思维) (Round #661 Div.3)

传送门

题意: 现有很多能力值的成员,要求你选择一个团队能力值w,使得两两匹配得到的团队能力值刚好为w的队伍最多,输出最多队伍数。
在这里插入图片描述
思路:

  • 因为数据范围比较小,可以之间枚举w。
  • 再用桶mh[]统计每个能力值成员的数量,挨个组队判断改w下的成队数量。

代码实现:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 105;

int t, n, a[N], mh[N];

signed main()
{
    IOS;

	cin >> t;
	while(t --){
		cin >> n;
		int 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 ++){
			int cnt = 0;
			me(mh);
			for(int i = 1; i <= n; i++) mh[a[i]] ++;
			for(int i = 1; i <= n; i++){
				if(w-a[i] >= 1 && mh[w-a[i]] && mh[a[i]]){
					if(w-a[i] == a[i] && mh[w-a[i]] < 2) continue;
					cnt ++;
					mh[w-a[i]] --, mh[a[i]] --;
				}
			}
			ans = max(ans, cnt);
		}
		cout << ans << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Satur9/article/details/107921038
今日推荐