1. 程式人生 > >UVA 11136 Hoax or what 【multiset】

UVA 11136 Hoax or what 【multiset】

題意:每天放進箱子裡一些數字,之後取出箱子中最大和最小的兩個數,得分為兩者的差,求n天后的得分總和。
思路:使用multiset,因為其內部是排好序的,直接取,然後刪除即可。

#pragma warning(disable:4996)
#include <stdio.h>
#include <algorithm>
#include <set>
#include <iostream>
#include <stdlib.h>
#define maxn 100005
using namespace std;
multiset<int>
s; int main() { int n; while (cin>>n && n) { int m; long long ans=0; s.clear(); multiset<int>::iterator ll,rr; for (int i = 0;i<n;i++) { scanf("%d",&m); int tmp; for (int j = 0;j<m;j++) { scanf
("%d",&tmp); s.insert(tmp); } ll = s.begin(); rr = (--s.end()); ans += *rr-*ll; s.erase(rr); s.erase(ll); } printf("%lld\n",ans); } return 0; }