Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 5th, 2012  |  syntax: C++  |  size: 0.73 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <set>
  3. #include <utility>
  4.  
  5. using namespace std;
  6.  
  7. set < pair <int,int> > s;
  8. set< pair <int,int> >::iterator itl,ith;
  9.  
  10. int main()
  11. {
  12.     int i,j,k,n,p,id,ret;
  13.  
  14.     while(scanf("%d",&n)==1)
  15.     {
  16.         s.clear();
  17.  
  18.         for(id=1,ret=i=0;i<n;i++)
  19.         {
  20.             scanf("%d",&k);
  21.  
  22.             for(j=0;j<k;j++)
  23.             {
  24.                 scanf("%d",&p);
  25.                 s.insert(make_pair(p,id++));
  26.             }
  27.  
  28.             itl=s.upper_bound (make_pair(-1,1));
  29.             ith=s.end();
  30.             ith--;
  31.  
  32.             ret+=(*ith).first-(*itl).first;
  33.  
  34.             s.erase(itl);
  35.             s.erase(ith);
  36.         }
  37.  
  38.         printf("%d\n",ret);
  39.  
  40.     }
  41.  
  42.     return 0;
  43. }