Uva-11136-Hoax or what

这个题算水题吧,直接2个优先队列就可以过,注意需要处理下删除元素的问题哈~

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=1000001;
priority_queue<int,vector<int>,greater<int> > qa;
priority_queue<int,vector<int>,less<int> > qb;
int numa[maxn],numb[maxn];
int main()
{
    int n;
    while(scanf("%d",&n)&&n)
    {
	int k;
	long long ans=0;
	while(!qa.empty())
	    qa.pop();
	while(!qb.empty())
	    qb.pop();
	memset(numa,0,sizeof(numa));
	memset(numb,0,sizeof(numb));
	for(int i=0;i<n;i++)
	{
	    scanf("%d",&k);
	    for(int j=0;j<k;j++)
	    {
		int ita;
		scanf("%d",&ita);
		qa.push(ita);
		qb.push(ita);
	    }
	    while(1)
	    {
		if(numa[qb.top()])
		{
		    numa[qb.top()]--;
		    qb.pop();
		}
		else
		    break;
	    }
	    while(1)
	    {
		if(numb[qa.top()])
		{
		    numb[qa.top()]--;
		    qa.pop();
		}
		else
		    break;
	    }
	    numa[qa.top()]++;
	    numb[qb.top()]++;
	    ans+=qb.top()-qa.top();
	    qa.pop();
	    qb.pop();
	}
	cout<<ans<<endl;
    }
    return 0;
}


你可能感兴趣的:(优先队列)