Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2019
4,940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. /// author: Mr.Hakimov
  2.  
  3. #include <bits/stdc++.h>
  4. #include <chrono>
  5.  
  6. #define all(x) (x).begin(), (x).end()
  7. #define fin(s) freopen(s, "r", stdin);
  8. #define fout(s) freopen(s, "w", stdout);
  9.  
  10. using namespace std;
  11.  
  12. typedef long long LL;
  13. typedef long double LD;
  14.  
  15. int TN = 1;
  16.  
  17. void showDeque(deque<int> d) {
  18. int n = d.size();
  19. for (int i = 0; i < n; i++) {
  20. cout << d.front() << " ";
  21. d.push_back(d.front());
  22. d.pop_front();
  23. }
  24. cout << endl << "==" << endl;
  25. }
  26.  
  27. void solve() {
  28. int n;
  29. int q;
  30. cin >> n >> q;
  31. deque<int> d;
  32. int maxValue = -1;
  33. for (int i = 0; i < n; i++) {
  34. int a_i;
  35. cin >> a_i;
  36. d.push_back(a_i);
  37. maxValue = max(maxValue, a_i);
  38. }
  39.  
  40. map<int, pair<int, int>> answer;
  41.  
  42. int maxIndex = 0;
  43. while (true) {
  44. /// showDeque(d);
  45. int first = d.front();
  46. d.pop_front();
  47. int second = d.front();
  48. d.pop_front();
  49.  
  50. if (first == maxValue) {
  51. d.push_front(second);
  52. d.push_front(first);
  53. break;
  54. }
  55.  
  56. maxIndex++;
  57. answer[maxIndex] = {first, second};
  58.  
  59. if (second > first) {
  60. swap(first, second);
  61. }
  62.  
  63. d.push_front(first);
  64. d.push_back(second);
  65. }
  66.  
  67. int a[n];
  68. /// showDeque(d);
  69. for (int i = 0; i < n; i++) {
  70. a[i] = d.front();
  71. d.pop_front();
  72. }
  73.  
  74. for (int i = 0; i < q; i++) {
  75. LL m_j;
  76. cin >> m_j;
  77. if (m_j <= maxIndex) {
  78. cout << answer[m_j].first << " " << answer[m_j].second << '\n';
  79. } else {
  80. cout << maxValue << " " << a[(m_j - (maxIndex + 1)) % (n - 1) + 1] << '\n';
  81. }
  82. }
  83. }
  84.  
  85. int main() {
  86. auto start = chrono::steady_clock::now();
  87. ios_base::sync_with_stdio(0);
  88. cin.tie(nullptr); cout.tie(nullptr);
  89. /// =========================================
  90. /// fin("input.txt"); fout("output.txt");
  91. /// fin("file.in"); fout("file.out");
  92. /// cin >> TN;
  93. /// =========================================
  94. while (TN--) solve();
  95. auto finish = chrono::steady_clock::now();
  96. auto elapsed_ms = chrono::duration_cast<chrono::milliseconds>(finish - start);
  97. cerr << endl << "Time: " << elapsed_ms.count() << " ms\n";
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement