fork download
  1. #include<iostream>
  2. #include<queue>
  3. #include<cstdio>
  4. #include<cmath>
  5. #include<functional>
  6. using namespace std;
  7. priority_queue<int> maxi;
  8. priority_queue <int,vector<int>,greater<int> > mini;
  9.  
  10. int main()
  11. {
  12. int n,median,ele,s1,s2,c;
  13. scanf("%d",&n);
  14. int arr[n+1];
  15. for(int i=0;i<n;i++)
  16. scanf("%d",&arr[i]);
  17. maxi.push(arr[0]);
  18. median=maxi.top();
  19. printf("%d\n",median);
  20. for(int i=1;i<n;i++)
  21. {
  22.  
  23. if(arr[i]<median)
  24. maxi.push(arr[i]);
  25. else
  26. mini.push(arr[i]);
  27. s1=(int)maxi.size();
  28. s2=(int)mini.size();
  29. c = abs(s1-s2);
  30. if(c>1)
  31. {
  32. if((s1-s2)>1)
  33. {
  34. ele=maxi.top();
  35. maxi.pop();
  36. mini.push(ele);
  37. }
  38. else if((s2-s1)>1)
  39. {
  40. ele=mini.top();
  41. mini.pop();
  42. maxi.push(ele);
  43. }
  44.  
  45. }
  46. s1=(int)maxi.size();
  47. s2=(int)mini.size();
  48. if(s1<s2)
  49. median=mini.top();
  50. else
  51. median=maxi.top();
  52. printf("%d\n",median);
  53. }
  54. return 0;
  55. }
  56.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:29: error: call of overloaded ‘abs(int)’ is ambiguous
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/cmath:99: note: candidates are: double std::abs(double)
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/cmath:103: note:                 float std::abs(float)
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/cmath:107: note:                 long double std::abs(long double)
prog.cpp:13: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
prog.cpp:16: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
stdout
Standard output is empty