首页 > 技术文章 > UVa 11136 Hoax or what (STL)

dwtfukgv 2016-07-05 23:47 原文

题意:有 n 天,每天有m个数,开始的前一天没有数据,然后每天从这个里面拿出一个最大的和最小的,求 n 天的最大的和最小的差值相加。

析:一看就知道用set啊,多简单的STL,不过要注意,开long long,和multiset,因为可能数是一样。

代码如下:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <set>
#include <cstdio>

using namespace std;
typedef long long LL;
multiset<int> sets;
multiset<int> :: iterator it1, it2;

int main(){
    int n, m, x;
    while(scanf("%d", &n) == 1 && n){
        sets.clear();
        LL ans = 0;
        while(n--){
            scanf("%d", &m);
            for(int i = 0; i < m; ++i){
                scanf("%d", &x);
                sets.insert(x);
            }
            it1 = sets.end();
            it2 = sets.begin();
            ans += (LL)*--it1;
            ans -= (LL)*it2;
            sets.erase(it1);
            sets.erase(it2);
        }
        printf("%lld\n", ans);
    }
    return 0;
}

 

推荐阅读