Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. struct opis{
  7. unsigned int npanstwa;
  8. int wspolczynnik;
  9. };
  10.  
  11.  
  12. int Hoare(vector<int> dane, int n)//n <=rozmiarowi wektora mniejszych, czyszcze clear rownych wiekszych i dane clear sharing to fit
  13. {
  14. if(dane.size()==1)
  15. {
  16. return dane.front();
  17. }
  18. vector<int> mniejsze; //return hoare vector mniejszych n
  19. vector<int> wieksze;
  20. vector<int> rowne;
  21. int i=dane.size()/2 ;
  22. for(int j=0; j < dane.size(); j++)
  23. {
  24. if(dane[i] < dane[j])
  25. {
  26. wieksze.push_back(dane[j]);
  27. }
  28. else if(dane[i] == dane[j])
  29. {
  30. rowne.push_back(dane[j]);
  31. }
  32. else
  33. {
  34. mniejsze.push_back(dane[j]);
  35. }
  36. }
  37. if(n <= mniejsze.size())
  38. {
  39. wieksze.clear();
  40. rowne.clear();
  41. dane.clear();
  42. wieksze.shrink_to_fit();
  43. rowne.shrink_to_fit();
  44. dane.shrink_to_fit();
  45. return Hoare(mniejsze, n);
  46. }
  47. if(n > mniejsze.size() && n <= (mniejsze.size()+rowne.size()))
  48. {
  49. mniejsze.shrink_to_fit();
  50. mniejsze.clear();
  51. wieksze.clear();
  52. wieksze.shrink_to_fit();
  53. dane.clear();
  54. dane.shrink_to_fit();
  55. return rowne.front();
  56. }
  57.  
  58. if(n > (mniejsze.size()+rowne.size()))
  59. {
  60. n=(n-(mniejsze.size()+rowne.size()));
  61. mniejsze.clear();
  62. rowne.clear();
  63. dane.clear();
  64. mniejsze.shrink_to_fit();
  65. rowne.shrink_to_fit();
  66. dane.shrink_to_fit();
  67. return Hoare(wieksze, n );
  68. }
  69.  
  70. }
  71.  
  72. int main()
  73. {
  74. ios_base::sync_with_stdio(false);
  75. int panstwa,i,m;
  76. opis wczytaj;
  77. cin >> panstwa;
  78. vector<opis> panstw;
  79. vector<int> wspolczynnik;
  80. for(i =0; i < panstwa; i++)
  81. {
  82. cin >> wczytaj.npanstwa >> wczytaj.wspolczynnik ;
  83. panstw.push_back(wczytaj);
  84. wspolczynnik.push_back(wczytaj.wspolczynnik);
  85. }
  86. int n,w,najmniejsze=2147483647;
  87. cin >> m;
  88. while(m > 0)
  89. {
  90. cin >> n;
  91. w=Hoare(wspolczynnik,n);
  92. for(i =0; i < panstw.size(); i++)
  93. {
  94. if(najmniejsze > panstw[i].npanstwa && (w == panstw[i].wspolczynnik))
  95. najmniejsze = panstw[i].npanstwa;
  96. }
  97. cout << najmniejsze << endl;
  98. m--;
  99. }
  100. return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement