#include<iostream> #include<fstream> #include<set> #include<unordered_set> #include <bits/stdc++.h> using namespace std; // //------------------------------------------------------------------------ // //for POLICY BASED DATA STRUCTURE // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // //stores unique integers in increasing order // typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // //stores unique integers in descreasing order // typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // //store integers in increasing order(can store duplicate integers also like multiset) // typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set1; // //store integers in decreasing order(can store duplicate integers also like multiset) // typedef tree<int, null_type, greater_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set2; // // Declaring ordered_set for pair<int,int> // typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update> ordered_set_pair; // //------------------------------------------------------------------------ #define forn(i, n) for (int i = 0; i < n; i++) #define um unordered_map #define us unordered_set #define sz(v) (int)v.size() #define all(x) x.begin(), x.end() #define pb push_back #define rep(i, a, b) for (int i = a; i <=b; ++i) #define pii pair<int, int> #define fi first #define se second typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vii; const ll md = 1e9 + 7; const ll inf = LLONG_MAX; // Graph Grid // int dirs[] = {0, 1, 0, -1, 0}; int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1}; int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1}; // print matrix void print(vector<vector<int>> &v) { for (auto &c : v) { for (auto i : c) cout << i << " "; cout << "\n"; } } // print 1-D vector template <typename T> void pri(vector<T> &v) { for (T i : v) cout << i << ' '; cout<<'\n'; } // pow(x,n), INT_MIN <=n<= INT_MAX, 'n' is an int and 'x' is a double double myPow(double x, int n) { double ans = 1.0; long long nn = n; if (nn < 0) nn *= (-1); while (nn) { if (nn % 2) // if n is odd { ans *= x; nn -= 1; } else // n is even { x *= x; nn /= 2; } } if (n < 0) // power is negative ans = 1 / ans; return ans; } // pow(x,n)%mod int binaryExponentiation(int x, int n, int mod) { int res = 1; while (n > 0) { if (n & 1) res = (res* 1LL * x) % mod; x = (x * 1LL * x) % mod; n >>= 1; } return res; } // bool sieve[1e8]; // void createSieve(int N) // { // for(int i=0;i<1e8;i++) // sieve[i]=true; // initially assuming all numbers are prime // sieve[1]=false; // for(int i=2;i*i<=N;i++) // if(sieve[i]) // for(int j=i*i;j<=N;j+=i) // sieve[j]=false; // } void helper(ll curr,set<ll>& s,vector<ll>& res,int n) { if(!s.size()) return; if(curr%3==0 and s.find(curr/3)!=s.end()) { s.erase(curr/3); res.pb(curr/3); helper(curr/3,s,res,n); if(res.size()==n) return; res.pop_back(); } if(s.find(curr*2)!=s.end()) { s.erase(curr*2); res.pb(curr*2); helper(curr*2,s,res,n); if(sz(res)==n) return; res.pop_back(); } } void solve() { // cout<<"hwf"; int n; cin>>n; vector<ll> arr(n); set<ll>s; int t; forn(i,n) { cin>>t; cout<<t; arr[i]=t; s.insert(t); } vector<ll>ans; for(ll i:arr) { s.erase(s.find(i)); ans.push_back(i); helper(i,s,ans,n); s.insert(i); if(ans.size()==n) { pri<ll>(ans); return; } ans.pop_back(); } cout<<"heruj\n"; } int main() { // ios_base::sync_with_stdio(false); // cin.tie(NULL); // cout.tie(NULL); // #ifndef ONLINE_JUDGE // std::ifstream input("input.txt"); // std::freopen("input.txt", "r", stdin); // std::ofstream output("output.txt"); // std::freopen("output.txt", "w", stdout); // std::ofstream error("err.txt"); // std::freopen("err.txt", "w", stderr); // #endif int t; cin >> t; // cout<<"asas"; while (t--) solve(); return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}