UVa - 11136 Hoax or what (水題 最大最小堆 set)

UVA - 11136
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

Description

Download as PDF

Problem H: Hoax or what

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.

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 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

Output for sample input

19
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 MB

Source

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

[]   [Go Back]   [Status]  




題意:

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;
}







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章