UVA11136 Hoax or what

Each Mal-Wart supermarket has prepared a promotion scheme run by the following rules:
  • A client who wants to participate in the promotion (aka a sucker) must write down their phone number on the bill of their purchase and put the bill into a special urn.
  • Two bills are selected from the urn at the end of each day: first the highest bill is selected and then the lowest bill is selected. The client who paid the largest bill receives a monetary prize equal to the difference between his bill and the lowest bill of the day.
  • Both selected bills are not returned to the urn while all the remaining ones are kept in the urn for the next day.
  • Mal-Wart has many clients such that at the end of each day there are at least two bills in the urn.
  • It is quite obvious why Mal-Wart is doing this: they sell crappy products which break quickly and irreparably. They give a short-term warranty on their products but in order to obtain a warranty replacement you need the bill of sale. So if you are gullible enough to participate in the promotion you will regret it.
Your task is to write a program which takes information about the bills put into the urn and computes Mal-Wart's cost of the promotion.

The input contains a number of cases. The first line in each case contains an integern, 1<=n<=5000, the number of days of the promotion. Each of the subsequent n lines contains a sequence of non-negative integers separated by whitespace. The numbers in the (i+1)-st line of a case give the data for the i-th day. The first number in each of these lines, k, 0≤k≤105, is the number of bills and the subsequent k numbers are positive integers of the bill amounts. No bill is bigger than 106. The total number of all bills is no bigger than 106. The case when n = 0 terminates the input and should not be processed.

For each case of input print one number: the total amount paid to the clients by Mal-Wart as the result of the promotion.

Sample input

5
3 1 2 3
2 1 1
4 10 5 5 1
0
1 2
2
2 1 2
2 1 2
0

Output for sample input

19
2
 
刷道水题涨涨信心!!!
没什么好说的,开2个优先队列,一个小到大,一个大到小。注意取出的小票是否已经用过了。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define maxn 1000080
#define LL long long int
int cnt[maxn];//该金额小票的剩余数量

struct Node
{
	int key;
	bool operator < (const Node & a) const
	{
		return key > a.key;
	}//越小的优先级越小
};

priority_queue <int> q;
priority_queue <Node> qq;
int main()
{
	//freopen("in.txt","r",stdin);
	int n;
	while(scanf("%d",&n)!=EOF && n)
	{
		LL ans = 0;
		memset(cnt,0,sizeof(cnt));
		while(!q.empty()) q.pop();
		while(!qq.empty())	qq.pop();
		for(int i = 1;i <= n;i++)
		{
			int k;	scanf("%d",&k);
			while(k--)
			{
				int d;	scanf("%d",&d);
				cnt[d]++;
				q.push(d);
				Node node;
				node.key = d;
				qq.push(node);
			}
			while(cnt[q.top()] == 0)
				q.pop();
			ans += (LL)q.top();
			cnt[q.top()]--;
			q.pop();
			while(cnt[qq.top().key] == 0)
				qq.pop();
			ans -= (LL)qq.top().key;
			cnt[qq.top().key]--;
			qq.pop();
		}
		printf("%lld\n",ans);
	}
	return 0;
}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值