1037. Magic Coupon (25)

两数组之间操作事件的模拟

#include<iostream>
#include<set>

struct myComp
{
	bool operator()(const int& a, const int& b)
	{
		return a > b;
	}
};
int main()
{
	int nc, np;
	while(scanf("%d",&nc)!=EOF)
	{
		std::multiset<int, myComp>c1, p1;
		std::multiset<int> c2, p2;//negtive
		while(nc--)
		{
			int tmp;
			scanf("%d",&tmp);
			if(tmp>=0) c1.insert(tmp);
			else c2.insert(tmp);
		}
		scanf("%d",&np);
		while(np--)
		{
			int tmp;
			scanf("%d",&tmp);
			if(tmp>=0) p1.insert(tmp);
			else p2.insert(tmp);
		}
		//get the max
		int max = 0;
		while(!c1.empty() && !p1.empty())
		{
			std::multiset<int, myComp>::iterator it1, it2;
			it1 = c1.begin();
			it2 = p1.begin();
			int a = (*it1);
			int b = (*it2);
			max += a*b;
			c1.erase(it1);
			p1.erase(it2);
		}
		while(!c2.empty() && !p2.empty())
		{
			std::multiset<int>::iterator it1, it2;
			it1 = c2.begin();
			it2 = p2.begin();
			int a = (*it1);
			int b = (*it2);
			max += a*b;
			c2.erase(it1);
			p2.erase(it2);
		}
		//output
		printf("%d\n", max);
	}
	return 0;
}


 

你可能感兴趣的:(pat,ZJU)