【SSL】1220促销

【SSL】1220促销

Time Limit:2000MS
Memory Limit:65536K

Description

Bytelandish连锁超市委托你编写一个程序来模拟一项即将施行的促销活动,该活动的规则如下:
●想要参与的顾客,只需把他的个人资料写在帐单上,并把帐单放入投票箱;
●每天活动结束时,数额最大、最小的两张帐单被取出,付款数额最大的顾客将获得一笔奖金,价值为取出的两张帐单的数额之差;
●为了不重复计算,取出的两张帐单不再放回箱子,而剩下的帐单仍保留在箱中,进行第二天的活动。
超市每天的营业额很大,因此可假定:每天活动结束时,箱中至少有两张帐单以供取出。
你的任务是根据每天投入箱中的帐单,计算出这项促销活动期间超市付出的奖金总数额。
任务:
编写一个程序,完成下列工作:
●从文件PRO.IN读入投入箱中的帐单的信息;
●算出促销活动期间的奖金总额;
●把结果写入文件PRO.OUT。

Input

第一行是整数n(1≤n≤5000),表示促销活动持续的天数。以下n行,每行是一些用空格分开的非负整数。第i+1行上的数字表示第i天投入箱中的帐单的数额,该行第一个整数k(0≤k≤105)代表这天的帐单总数目,接下来的k个正整数表示每张帐单的数额,不超过106。
整个活动期间,投入箱中的帐单不超过10^6张。

Output

含一个整数,即整个活动中的奖金总额。

Sample Input

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

Sample Output

19

思路

暴力模拟
用一个map标记是否在箱中

代码

#include<iostream>
#include<cstdio>
#include<queue>
#include<map>
using namespace std;
map<long long,bool>d;
struct jgtmn
{
    
    
	long long s,id;
}mn;
bool operator <(const jgtmn a,const jgtmn b)
{
    
    
	return a.s>b.s;
}
priority_queue<jgtmn>qmn;
struct jgtmx
{
    
    
	long long s,id;
}mx;
bool operator <(const jgtmx a,const jgtmx b)
{
    
    
	return a.s<b.s;
}
priority_queue<jgtmx>qmx;
long long read()
{
    
    
	long long ans;
	char c;
	for(c=getchar();c>'9'||c<'0';c=getchar());
	for(ans=c-'0',c=getchar();'0'<=c&&c<='9';ans=ans*10+c-'0',c=getchar());
	return ans;
}
int main()
{
    
    
	long long ans=0,i,j,n,m,tot=-1;
	for(n=read(),i=1;i<=n;i++)
	{
    
    
		for(m=read(),j=1;j<=m;j++)
		{
    
    
			mn.s=mx.s=read();
			mn.id=mx.id=++tot;
			qmn.push(mn);
			qmx.push(mx);
		}
		for(;d[qmn.top().id];qmn.pop());
		for(;d[qmx.top().id];qmx.pop());
		ans+=qmx.top().s-qmn.top().s;
		d[qmn.top().id]=d[qmx.top().id]=1;
		qmn.pop(),qmx.pop();
	}
	printf("%lld",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_46975572/article/details/113113773
今日推荐