Codeforces Round #570 (Div. 3)--F - Topforces Strikes Back

题意:给定一个数组, 要求在这数组里面选n(1 <= n <= 3)个数, S.T 这三个数的和最大, 且两两互质

我感觉这次题意写的还可以

Strategy: 贪心, 先排序, 找到一个最大的数,然后在删掉数组中与该数不互质的数,得到一个数组, 重复上述操作。。。。

然后样例都没过,,,

其实大体上是对的,但是后来才知道,要加一个特判

Let’s imagine that the maximum element is divisible by 2, 3 and 5 and there are three following numbers in the array: maximum divided by 2, by 3 and by 5. Then their sum is greater than the maximum (and may be greater than the answer we have!) because 1/2 + 1/3 + 1 / 5 > 1

@_@ 别问我为啥,我啥也不知道

#include<bits/stdc++.h>
#include<bits/extc++.h>
#define oo INT_MAX
#define maxn 10000009 
#define _rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define _rev(i, a, b) for(int i = (a); i >= (b); --i)
#define _for(i, a, b) for(int i = (a); i < (b) ;++i)
#define met(a, b) memset(a, b, sizeof(a))
using namespace std;
using namespace __gnu_pbds;
int main(){
	ios::sync_with_stdio(0);
	int t;
	cin >> t;
	while(t --){
		int n;
		cin >> n;
		set<int> s;
		_rep(i, 1, n){
			int xx;
			cin >> xx;
			s.insert(xx);
		}
		int ans = 0;
		int max_val = *s.rbegin();
		if(max_val % 5 == 0 && max_val % 2 == 0 && max_val % 3 == 0 && s.count(max_val / 5) && s.count(max_val / 3) && s.count(max_val / 2)){
			ans = max(max_val, max_val / 3 + max_val / 5 + max_val / 2);
		}
		vector<int> res;
		while (!s.empty() && int(res.size()) < 3)
		{
			int max_val = *s.rbegin();
			s.erase(prev(s.end()));
			bool isnt_devided = 1;
			for(auto x : res) isnt_devided &= (x % max_val != 0);
			if(isnt_devided) res.push_back(max_val);
		}
		cout << max(ans, accumulate(res.begin(), res.end(), 0)) << endl;
	}
	//system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值