SSLOJ1220 promotion

Description

Bytelandish supermarket chain entrusts you to write a program to simulate an upcoming promotional activity. The rules of the activity are as follows:
●Customers who want to participate, only need to write his personal information on the bill, and put the bill into it Ballot box;
●At the end of each day, the two bills with the largest and smallest amounts will be taken out, and the customer with the largest payment will receive a bonus, the value of which is the difference between the two bills taken out;
●In order to avoid double counting , The two bills taken out are no longer put back in the box, and the remaining bills are still kept in the box for the next day’s activities.
The supermarket has a large daily turnover, so it can be assumed that at the end of each day, there are at least two bills in the box for withdrawal.
Your task is to calculate the total amount of bonuses paid by the supermarket during this promotion based on the bills put into the box every day.
Task:
Write a program to complete the following tasks:
●Read the information of the bill in the input box from the file PRO.IN;
●Calculate the total bonus during the promotion period;
●Write the result into the file PRO.OUT.

Input

The first line is an integer n (1≤n≤5000), indicating the number of days the promotion will last. The following n lines, each line is a number of non-negative integers separated by spaces. The number on line i+1 represents the amount of bills put into the box on day i. The first integer k (0≤k≤10 5) in this line represents the total number of bills on that day, and the next k positive integers Indicates the amount of each bill, which does not exceed 106.
During the entire event, no more than 10^6 bills are put in the box.

Output

Including an integer, that is, the total amount of bonuses in the entire event.

Sample Input

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

Sample Output

19

Ideas

Obviously, two priority queues can handle the maximum and minimum, and then open an array to judge the weight. Repeatedly remove the
three-year OI empty. If you don’t open longlong, see the ancestor\color{orange}{3 years OI empty, don’t open the long long to see ancestors}3 years O the I a field empty , without opening L O n- G L O n- G See progenitors were
vertical shock death disease sit without fast read TLE \ color {blue} {dying disease shock sit without fast read TLE}Hanging dead disease in shock sit play , do not use quick read T L E
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;
}

Guess you like

Origin blog.csdn.net/weixin_49843717/article/details/115016817