0
点赞
收藏
分享

微信扫一扫

E. Best Pair

我是小瘦子哟 2022-02-13 阅读 44
https://codeforces.com/contest/1637/problem/E
#include<bits/stdc++.h>
using namespace std;

#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define per(i,l,r) for(int i=(l);i>=(r);i--)
#define ll long long
#define pii pair<int, int>
#define mset(s,t) memset(s,t,sizeof(t))
#define mcpy(s,t) memcpy(s,t,sizeof(t))
#define fir first
#define pb push_back
#define sec second
#define sortall(x) sort((x).begin(),(x).end())
inline int read () {
	int x = 0, f = 0;
	char ch = getchar();
	while (!isdigit(ch)) f |= (ch=='-'),ch= getchar();
	while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
	return f?-x:x;
}
template<typename T> void print(T x) {
	if (x < 0) putchar('-'), x = -x;
	if (x >= 10) print(x/10);
	putchar(x % 10 + '0');
}
#define int long long
const int N = 1e4 + 10;


void solve() {
	map<int, int> cnt;
	
	int n, m;
	cin >> n >> m;
	vector<int> a(n);
	for (auto &tt : a) {
		cin >> tt;
		cnt[tt] ++;
	}
	vector<pii> bad(m * 2);
	rep(i, 1, m) {
		int x, y;
		cin>> x >> y;
		bad.pb({x, y});
		bad.pb({y, x}); 
	} 
	sort(bad.begin(), bad.end());
	vector<vector<int>> occ(n); 
	for (auto &[num, ct] : cnt) {
		occ[ct].pb(num);
	}
	for (auto &t : occ)
		reverse(t.begin(), t.end());
	int ans = 0;
	for (int cx = 1; cx < n; cx ++) {
		for (auto x : occ[cx]) {
			for (int cy = 1; cy <= cx; cy ++) {
				for (auto y : occ[cy]) {
					if (y != x && !binary_search(bad.begin(), bad.end(), pair<int, int>{x, y})) {
						ans = max (ans, (cx + cy) * (x + y));
						break;
					}
				}
			}
		}
	}
	cout << ans << endl;
}
signed main () {
    int t;
    cin >> t;
    while (t --) solve();
    
}


这题之前没思路是在想有什么简便的思想可以帮助我寻找这样的答案。实际这题就是对stl的运用,因为是找最大值,所以把所有的数反转过来我们通过枚举次数,可以让复杂度降低,如果枚举,先把次数算出来,然后枚举枚举次数, 更换枚举对象

举报

相关推荐

0 条评论