Advertisement
Guest User

Untitled

a guest
May 14th, 2010
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5.  
  6.  
  7. using namespace std;
  8.  
  9. struct dbl {
  10.   unsigned long x,y;    
  11. };
  12.  
  13.  
  14.  
  15. bool mysort_byy (dbl i,dbl j) {
  16. double ratio1 = ((double)i.x)/(i.y);    
  17. double ratio2 = ((double)j.x)/(j.y);
  18.  
  19. return (ratio1 > ratio2);
  20. }
  21.  
  22. int main() {
  23.  
  24.   freopen("calatorie.in","r",stdin);
  25.  freopen("calatorie.out","w",stdout);
  26.  
  27.  vector<dbl> vec;
  28.  vector<dbl>::iterator it;
  29.  dbl tmp;
  30.  int n, m;
  31.  int i;
  32.  unsigned long maxsum = 0, minsum = 0, tmpsum, lastsum, othersum;
  33.  cin >> n;
  34.  while (n--) {
  35.     cin >> m;
  36.     if (m == 1) {
  37.      
  38.        cout << "Consumul minim = 0." << endl;
  39.        continue;
  40.     }
  41.     while (--m) {
  42.           cin >> tmp.x >> tmp.y;
  43.           vec.push_back(tmp);      
  44.     }    
  45.     sort (vec.begin(), vec.end(), mysort_byy);    
  46.     maxsum = 0;
  47.     for (it=vec.begin(); it!=vec.end(); ++it) {
  48.          maxsum+=(*it).x;            
  49.     }
  50.  
  51.     minsum = 0;
  52.     tmpsum = maxsum;
  53.     lastsum = maxsum;
  54.     i = 0;
  55.     it = vec.begin();
  56.     while (it != vec.end()) {
  57.         minsum += (*it).y;
  58.         tmpsum -= (*it).x;
  59.         othersum = lastsum;
  60.         lastsum = (minsum*minsum*minsum*minsum + tmpsum);
  61.         if (lastsum > othersum) {
  62.  
  63.            lastsum = othersum;
  64.            break;            
  65.         }
  66.         it++;
  67.    
  68.     }
  69.     if (lastsum < othersum) {
  70.  
  71.            othersum = lastsum;
  72.     }
  73.    
  74.  
  75.     cout << "Consumul minim = " << othersum << "." << endl;
  76.  
  77.    
  78.     vec.clear();
  79.  }
  80.  //getch();
  81.  return 0;  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement