1180C - Valeriy and Deque - CodeForces Solution


data structures implementation *1500

Please click on ads to support us..

Python Code:

from collections import deque

info = input().split()
n = int(info[0])
q = int(info[1])
dq = deque(list(map(int, input().split())))
e_max = max(dq)
ans = list()

while True:
    A = dq.popleft()
    B = dq.popleft()
    ans.append(str(A)+ ' ' + str(B))
    if A > B:
        dq.appendleft(A)
        dq.append(B)
        if A == e_max:
            break
    else:
        dq.appendleft(B)
        dq.append(A)
        if B == e_max:
            break

l = len(ans)
for i in range(q):
    aux = int(input())
    if aux  <=  l:
        print(ans[aux - 1])
    else:
        pos = (aux - 1 - l) % (n - 1) + 1
        print(str(e_max) + ' ' + str(dq[pos]))
 
 	   	  	  	   	  	  		 	 	 		

C++ Code:

 
#include <bits/stdc++.h>
#include <chrono>
 
#define all(x) (x).begin(), (x).end()
#define fin(s) freopen(s, "r", stdin);
#define fout(s) freopen(s, "w", stdout);
 
using namespace std;
 
typedef long long LL;
typedef long double LD;
 
int TN = 1;
 
void showDeque(deque<int> d) {
int n = d.size();
for (int i = 0; i < n; i++) {
cout << d.front() << " ";
d.push_back(d.front());
d.pop_front();
}
cout << endl << "==" << endl;
}
 
void solve() {
int n;
int q;
cin >> n >> q;
deque<int> d;
int maxValue = -1;
for (int i = 0; i < n; i++) {
int a_i;
cin >> a_i;
d.push_back(a_i);
maxValue = max(maxValue, a_i);
}
 
map<int, pair<int, int>> answer;
 
int maxIndex = 0;
while (true) {
/// showDeque(d);
int first = d.front();
d.pop_front();
int second = d.front();
d.pop_front();
 
if (first == maxValue) {
d.push_front(second);
d.push_front(first);
break;
}
 
maxIndex++;
answer[maxIndex] = {first, second};
 
if (second > first) {
swap(first, second);
}
 
d.push_front(first);
d.push_back(second);
}
 
int a[n];
/// showDeque(d);
for (int i = 0; i < n; i++) {
a[i] = d.front();
d.pop_front();
}
 
for (int i = 0; i < q; i++) {
LL m_j;
cin >> m_j;
if (m_j <= maxIndex) {
cout << answer[m_j].first << " " << answer[m_j].second << '\n';
} else {
cout << maxValue << " " << a[(m_j - (maxIndex + 1)) % (n - 1) + 1] << '\n';
}
}
}
 
int main() {
auto start = chrono::steady_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(nullptr); cout.tie(nullptr);
/// =========================================
/// fin("input.txt"); fout("output.txt");
/// fin("file.in"); fout("file.out");
/// cin >> TN;
/// =========================================
while (TN--) solve();
auto finish = chrono::steady_clock::now();
auto elapsed_ms = chrono::duration_cast<chrono::milliseconds>(finish - start);
cerr << endl << "Time: " << elapsed_ms.count() << " ms\n";
return 0;
}


Comments

Submit
0 Comments
More Questions

507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm