题目大意
有n组数字序列,每次从一组序列中取出最大值,最小值,累加最大值与最小值的差,同时该组的剩下的数归入下一组序列,问最后累加和
思路
使用multiset来存放数字序列,从中取出最大值与最小值,累加其之差
代码如下:
#include<iostream>
#include <fstream>
#include <set>
using namespace std;
void fastio()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
int main()
{
fastio();
#ifndef ONLINE_JUDGE
ifstream fin("f:\\OJ\\uva_in.txt");
streambuf* back = cin.rdbuf(fin.rdbuf());
#endif
int n;
while ((cin >> n) && n) {
unsigned long long res = 0;
multiset<int> bills;
for (int i = 0; i < n; i++) {
int k;
cin >> k;
for (int j = 0; j < k; j++) {
int tmp;
cin >> tmp;
bills.insert(tmp);
}
auto begin = bills.begin();
auto end = bills.end();
--end;
res += *end - *begin;
bills.erase(begin); bills.erase(end);
}
cout << res << endl;
}
#ifndef ONLINE_JUDGE
cin.rdbuf(back);
#endif
return 0;
}