fork(2) download
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<math.h>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9.  
  10. int ile, n, *tab, kand, t;
  11. cin >> t;
  12. while(t--){
  13.  
  14. cin >> n;
  15. int *tab = new int [n];
  16.  
  17. for(int i=0; i<n; i++)
  18. cin>>tab[i];
  19.  
  20. kand = tab[0];
  21. ile = 0;
  22. for(int i = 1; i < n; i++)
  23. {
  24. if(tab[i] == kand)
  25. {
  26. ile += 1;
  27. }
  28. else
  29. if(ile > 0)
  30. ile -= 1;
  31. else
  32. kand = tab[i];
  33. ile = 1;
  34. }
  35. ile = 0;
  36. for(int i = 0; i < n; i++){
  37. if(tab[i] == kand){
  38. ile += 1;
  39. }
  40. else
  41. continue;
  42. }
  43. if(ile >= floor(n/2) + 1)
  44. cout << kand << endl;
  45. else
  46. cout << -1 << endl;
  47. }
  48. return 0;
  49. }
  50.  
Time limit exceeded #stdin #stdout 5s 4400KB
stdin
Standard input is empty
stdout
Standard output is empty