Thursday, December 6, 2012

UVA 11136 Hoax or what Solution

Problem Link
Problem Type : Data-structure(STL set)
Difficulty Level : Easy
Author : Imdad

Briefly problem statement
In his problem we have to insert int in sorted list and find out max and min regularly. So balanced binary tree can be a data structure to approach this problem. For this we can use STL set template.



Solution

#include <cstdio>
#include <set>
#include <iostream>

using namespace std;

int main(){
 multiset<long>s;
 multiset<long>::iterator sit,rrsit;

 long T,n,cost,tmp;

 while(scanf("%ld", &T) && T){
  cost = 0;
  while(T--){
   scanf("%ld",&n);
   while(n--){
    scanf("%ld",&tmp);
    s.insert(tmp);
   }
   sit = s.begin();

   rrsit = s.end();
   --rrsit;

   cost += (*(rrsit) - *(sit));

   s.erase(sit);
   s.erase(rrsit);
  }
  printf("%ld\n",cost);
  s.clear();
 } 
 return 0;
}

No comments:

Post a Comment