Advertisement
Guest User

operators

a guest
Mar 7th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. /* NAME: PANAGIOTIS KOSTOPANAGIOTIS
  2.  * TASK: OPERATORS
  3.  * LANG: C++
  4.  */
  5. #include <cstdio>  
  6. #include <vector>  
  7. #include <cmath>  
  8.  
  9. int main()  
  10. {  
  11.   freopen("operators.in","r",stdin);  
  12.   freopen("operators.out","w",stdout);  
  13.   long N,current=0,num[2],min=1000000001;  
  14.   scanf("%d\n",&N);  
  15.   std::vector<long> neg;  
  16.   for(int i=0; scanf("%d ",&current) != EOF && i<N; ++i) {  
  17.     if(current>0 && !i) {  
  18.       num[0]=current;  
  19.       scanf("%d ", &num[1]);  
  20.       break;  
  21.     }
  22.     if(current<0)  
  23.       neg.push_back(current);  
  24.     while (current>0 && neg.size()) {  
  25.       if(fabs(neg.back()+current)<min) {  
  26.     num[0]=neg.back();  
  27.     num[1]=current;  
  28.     min=fabs(num[0]+num[1]);  
  29.       }  
  30.       if(fabs(neg.back())>current)  
  31.     break;  
  32.       neg.pop_back();  
  33.     }
  34.     if(neg.size()==0 || num[0]+num[1]==0)
  35.       break;
  36.   }
  37.   if(neg.size()==N) {  
  38.     for(int i=0; i<2; ++i) {  
  39.       num[1-i]=neg.back();  
  40.       neg.pop_back();  
  41.     }  
  42.   }  
  43.   printf("%d %d\n", num[0],num[1]);  
  44.   return 0;  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement