SSLOJ1220 促销

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

思路

显然可以2个优先队列处理一下最大最小,再开一个数组判重,重复的去掉
3 年 O I 一 场 空 , 不 开 l o n g l o n g 见 祖 宗 \color{orange}{3年OI一场空,不开long long见祖宗} 3OIlonglong
垂 死 病 中 惊 坐 起 , 不 用 快 读 T L E \color{blue}{垂死病中惊坐起,不用快读TLE} TLE
code:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<map>
#include<queue>
#include<vector>
using namespace std;
long long n,k,tot,x,s;
map<long long,bool> u;
struct f{
    
    
	long long x,id;
} p;
bool operator <(const f &a,const f &b)
{
    
    
	return a.x>b.x;
}
inline long long read()
{
    
    
    long long ret,c;
    while ((c=getchar())> '9'||c< '0');
	ret=c-'0';
    while ((c=getchar())>='0'&&c<='9') ret=ret*10+c-'0';
    return ret;
}
struct f2{
    
    
	long long x,id;
} p1;
bool operator <(const f2 &a,const f2 &b)
{
    
    
	return a.x<b.x;
}
priority_queue<f> b;
priority_queue<f2> c;
int main()
{
    
    
	n=read();
	for (int i=1;i<=n;i++)
	{
    
    
		k=read();
		for (int j=1;j<=k;j++)
		{
    
    
			x=read();
			p.id=tot,p1.id=tot++;
			p.x=x,p1.x=x;
			b.push(p);
			c.push(p1);
		}
		while (u[b.top().id]) b.pop();
		while (u[c.top().id]) c.pop();
		s+=c.top().x-b.top().x;
		u[b.top().id]=1,u[c.top().id]=1;
		b.pop(),c.pop();
	}
	cout<<s<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_49843717/article/details/115016817
今日推荐