UVA - 11136
Description Problem H: Hoax or what
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. 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 input5 3 1 2 3 2 1 1 4 10 5 5 1 0 1 2 2 2 1 2 2 1 2 0 Output for sample input19 2 T. Walen, adapted by P. Rudnicki Warning: Time limit is too tight to get accepted using STL. The input file size is around 16 MBSource
Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Data Structures and Libraries :: Non Linear Data Structures with Built-in Libraries :: C++
STL map/set (Java TreeMap/TreeSet)
Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Data Structures and Libraries :: Non Linear Data Structures with Built-in Libraries :: C++ STL set (Java TreeSet) Root :: Competitive Programming: Increasing the Lower Bound of Programming Contests (Steven & Felix Halim) :: Chapter 2. Data Structures and Libraries :: Data Structures With Built-in Libraries :: STL map/set Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 3. Data Structures :: Fundamental Data Structures :: Exercises: Beginner |
題意:
n天,每天有ki張單據,每張單據上面有金額。第一天早上箱子是空的,每天晚上都把當天的單據放進箱子裏,然後取出兩張最大,最小的單據max,min,然後爲最大單據所有者提供max - min的獎品。問n天一共要提供多少價值獎品。
直接multiset/優先隊列就行了
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif
#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const double PI = (4.0*atan(1.0));
multiset<int> S;
int main() {
int n;
while(scanf("%d", &n) != EOF && n) {
LL ans = 0;
S.clear();
for(int i=0; i<n; i++) {
int k;
scanf("%d", &k);
for(int j=0; j<k; j++) {
int t;
scanf("%d", &t);
S.insert(t);
}
set<int>::iterator eit = S.end();
set<int>::iterator sit = S.begin();
--eit;
ans += *eit - *sit;
S.erase(eit);
S.erase(sit);
}
P64I(ans);
}
return 0;
}