目录
题目描述
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.
Input
The input contains a number of cases. The first line in each case contains an integer n, 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.
Output
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
Sample Output
19
2
题意分析
题意:去超市,其有活动。有N天,每天从购买的顾客中抽取两位消费最多的和最小的
给最多的二者之差值。可以使用 mutiset 集合 ,其可以自动排序 和 存储重复元素
AC代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
using namespace std;
multiset <int> s; //允许重复元素
int main()
{
int n,i,j,p,q; //days
long long sum,minone,maxone;
while(cin>>n && n)
{
s.clear();
sum=0;
for(i=0;i<n;i++)
{
cin>>p;
for(j=0;j<p;j++)
{
cin>>q;
s.insert(q);
}
minone = *(s.begin());
maxone = *(s.rbegin());
sum += maxone-minone;
s.erase(s.find(minone));
s.erase(s.find(maxone));
}
cout<<sum<<endl;
}
return 0;
}