标签:
用multiset模拟一下就可以了,需要注意的是erase(val)会将值等于val的全部删掉,而erase(iterator)只会删去该iterator指向的一个值。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <set> 5 using namespace std; 6 7 multiset<int> s; 8 multiset<int>::iterator it; 9 10 int main () 11 { 12 int n; 13 while ( scanf("%d", &n), n ) 14 { 15 long long sum = 0; 16 s.clear(); 17 for ( int i = 0; i < n; i++ ) 18 { 19 int m; 20 scanf("%d", &m); 21 while ( m-- ) 22 { 23 int tmp; 24 scanf("%d", &tmp); 25 s.insert(tmp); 26 } 27 it = s.begin(); 28 int lb = (*it); 29 s.erase(it); 30 it = s.end(); 31 it--; 32 int ub = (*it); 33 s.erase(it); 34 sum += ub - lb; 35 } 36 printf("%lld\n", sum); 37 } 38 return 0; 39 }
标签:
原文地址:http://www.cnblogs.com/huoxiayu/p/4768430.html