A1037 Magic Coupon

A1037 Magic Coupon_第1张图片
A1037 Magic Coupon_第2张图片

solution

#include 
#include 
using namespace std;
int main(){
	int n1, n2, a1[100005], a2[100005], i = 0, j, ans = 0;
	scanf("%d", &n1);
	for(int k = 0; k < n1; k++)
		scanf("%d", a1 + k);
	scanf("%d", &n2);
	for(int k = 0; k < n2; k++)
		scanf("%d", a2 + k);
	sort(a1, a1 + n1);
	sort(a2, a2 + n2);
	while(i < n1 && i < n2 && a1[i] < 0 && a2[i] < 0){
		ans += a1[i]* a2[i++];
	} 
	i = n1 - 1;
	j = n2 - 1;
	while( i >= 0 && j >= 0 && a1[i] > 0 && a2[j] > 0){
		ans += a1[i--]*a2[j--];
	}
	printf("%d", ans);
	return 0;
} 

你可能感兴趣的:(pat,甲级,算法)