You are given an array of integers 𝑎 of length 𝑛. You can perform the following…
- User
You are given an array of integers 𝑎 of length 𝑛. You can perform the following operation zero or more times:
In one operation choose an index 𝑖 (1≤𝑖≤𝑛), assign 𝑎𝑖:=𝑎𝑖+1, and then move 𝑎𝑖 to the back of the array (to the rightmost position). For example, if 𝑎=[3,5,1,9], and you choose 𝑖=2, the array becomes [3,1,9,6]. Find the lexicographically smallest∗ array you can get by performing these operations.
∗An array 𝑐 is lexicographically smaller than an array 𝑑 if and only if one of the following holds: 𝑐 is a prefix of 𝑑, but 𝑐≠𝑑; or in the first position where 𝑐 and 𝑑 differ, the array 𝑐 has a smaller element than the corresponding element in 𝑑. Input Each test contains multiple test cases. The first line contains the number of test cases 𝑡 (1≤𝑡≤104). The description of the test cases follows.
The first line contains a single integer 𝑛 (1≤𝑛≤105), the length of the array.
The second line contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109), the elements of the array.
It is guaranteed that the sum of 𝑛 over all test cases does not exceed 105.
Output For each test case, print the lexicographically smallest array you can get.
Example inputCopy 3 3 2 1 3 5 1 2 2 1 4 6 1 2 3 6 5 4 outputCopy 1 3 3 1 1 3 3 5 1 2 3 4 6 7 . please use my template with 2 space tab: #include #include #include
using namespace std;
// const int N = 2e5 + 5; // int a[N]; typedef long long ll; typedef vector vi; #define endl '\n'
void solve() { //your code here
}
int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while(t--) solve(); }
- Assistant
#include <iostream> #include <vector> #include <algorithm> #include <deque> using namespace std; // const int N = 2e5 + 5; // int a[N]; typedef long long ll; typedef vector<int> vi; #define endl '\n' void solve() { int n; cin >> n; vi a(n); for(auto &x: a) cin >> x; deque<int> dq; for(auto x: a) dq.push_back(x); vi res; while(!dq.empty()){ // Find the minimum in the current deque int min_val = *min_element(dq.begin(), dq.end()); // Find the first occurrence of min_val int pos = 0; while(pos < dq.size() && dq[pos] != min_val) pos++; // Move all elements before pos to the end, incremented by1 for(int i=0;i<pos;i++){ int val = dq.front(); dq.pop_front(); dq.push_back(val + 1); } // Now the front is min_val res.push_back(dq.front()); dq.pop_front(); } // Print the result for(auto x: res){ cout << x << ' '; } cout << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while(t--) solve(); }