【1037 25 贪心】 Magic Coupon

传送门

题意

给定长度为 \(n,m\) 的两个序列 \(a_{i},b_{j}\),求 \(max\; \sum a_{i}\times b_{j}\)

数据范围

\(n,m\leq 10^{5}\)

题解

  • 只有同号才计算,实现排序正数从大到小,负数和 0 从小到大,0 需要跳过所以排到最后

Code

#include <bits/stdc++.h>
using namespace std;

#define ll long long 

bool cmp(ll a, ll b) {
	if (a <= 0 and b <= 0) return a < b;
	return a > b;
}
int main() {
	int nc; cin >> nc;
	vector<ll> a(nc);
	for (auto& it : a) cin >> it;
	sort(a.begin(), a.end(), cmp);
	int np; cin >> np;
	vector<ll> b(np);
	for (auto& it : b) cin >> it;
	sort(b.begin(), b.end(), cmp);

	ll ans = 0;
	for (int i = 0, j = 0; i < nc and j < np; i++, j++) {
		while (a[i] < 0 and b[j] > 0 and j < np) j++;
		while (a[i] > 0 and b[j] < 0 and i < nc) i++;
		ans += 1ll * a[i] * b[j];
	}
	cout << ans;
}
posted @ 2021-02-20 23:36  Hyx'  阅读(53)  评论(0)    收藏  举报