text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long q;
cin >> q;
long long sqrtq = floor(sqrt(q)), cnt = 0, ps[2], k = 2;
while (k <= sqrtq && cnt < 2 && q > 1)
if (q % k == 0)
ps[cnt++] = k, q /= k;
else
++k;
if (!cnt)
cout << 1 << endl << 0 << endl;
else if (q == 1 || cnt == 1)
cout << 2 << endl;
else
cout << 1 << endl << ps[0] * ps[1] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> y;
void prime_factorization(long long x) {
long long i;
long long c;
c = x;
while ((c % 2) == 0) {
y.push_back(2LL);
c = c / 2;
}
i = 3;
while (i <= (sqrt(c) + 1)) {
if ((c % i) == 0) {
y.push_back(i);
c = c / i;
} else
i = i + 2;
}
if (c > 1) y.push_back(c);
}
bool isprime(long long p) {
if (p < 2) return false;
long long d;
for (d = 2; d * d <= p; d++)
if (p % d == 0) return false;
return true;
}
int main() {
long long n;
cin >> n;
if (n == 1LL)
cout << 1 << endl << 0 << endl;
else if (isprime(n)) {
cout << 1 << endl << 0 << endl;
return 0;
} else {
prime_factorization(n);
if ((int)(y.size()) <= 2)
cout << 2 << endl;
else {
cout << 1 << endl;
cout << (long long)(y[0] * y[1]) << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> pfactors;
void first_div(long long num) {
for (int i = 2; i * (long long)i <= num; i++) {
while (num % i == 0) {
pfactors.push_back(i);
num /= i;
}
}
if (num > 1) pfactors.push_back(num);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
first_div(n);
if (pfactors.size() <= 1) {
cout << 1 << "\n0\n";
} else if (pfactors.size() == 2) {
cout << 2;
} else {
cout << 1 << "\n" << pfactors[0] * pfactors[1] << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, tmp;
cin >> n;
tmp = n;
long long int cnt = 0, prod = 1;
bool isprime = true;
for (long long int i = 2; i * i <= tmp; i++) {
while (tmp % i == 0) {
isprime = false;
cnt++;
prod *= i;
if (cnt == 2 && prod != n) {
cout << 1 << endl << prod << endl;
return 0;
}
tmp /= i;
}
}
if (isprime) {
cout << 1 << endl << 0 << endl;
} else {
cout << 2 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
long long a;
cin >> a;
long long counter = 0;
long long num = 1;
for (long long i = 2; i * i <= a; i++) {
while (a % i == 0) {
a /= i;
counter++;
if (counter <= 2) num *= i;
if (counter > 2) break;
}
if (counter > 2) break;
}
if (a != 1) counter++;
if (a != 1 && counter <= 2) num *= a;
if (counter <= 1) {
cout << 1 << endl << 0 << endl;
return 0;
}
if (counter == 2)
cout << 2 << endl;
else
cout << 1 << endl << num << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int q;
void solve(long long int x) {
for (long long int i = 2; i * i <= x; i++) {
if (x % i == 0) {
long long int temp = x / i;
for (long long int j = 2; j * j <= temp; j++) {
if (temp % j == 0) {
cout << 1 << "\n";
cout << i * j;
return;
}
}
cout << 2;
return;
}
}
cout << 1 << "\n";
cout << 0;
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> q;
if (q == 1) {
cout << 1 << "\n";
cout << 0;
return 0;
}
solve(q);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
bool flag = false;
long long sqr = sqrt((double)n) + 1;
vector<long long> del;
del.reserve(100);
while (n % 2 == 0) {
del.push_back(2);
n /= 2;
}
for (long long i = 3; i <= sqr; i += 2) {
while (n % i == 0) {
del.push_back(i);
n /= i;
}
}
if (n > 1) del.push_back(n);
if (del.size() <= 1) {
cout << 1 << "\n0";
}
if (del.size() == 2) {
cout << 2;
}
if (del.size() > 2) {
long long res = del[0] * del[1];
cout << 1 << "\n" << res;
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, p, a[111], k;
int main() {
scanf("%I64d", &n);
p = 2;
while (p * p <= n) {
while (n % p == 0) {
k++;
a[k] = p;
n /= p;
}
p++;
}
if (k > 0 && n != 1) {
k++;
a[k] = n;
}
if (k == 2 || k == 1)
printf("2");
else {
if (k == 0)
printf("1\n0");
else
printf("1\n%I64d", a[1] * a[2]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int q, n, m, a[10000];
double x;
bool cmp(long long int x, long long int y) { return x < y; }
bool IsPrime(long long int qwe) {
long long int s;
double d;
d = qwe;
s = floor(sqrt(d));
for (int i = 2; i <= s; i++)
if (qwe % i == 0) {
i = s + 1;
return false;
}
return true;
}
int main() {
cin >> q;
if (IsPrime(q))
cout << 1 << endl << 0;
else {
x = q;
n = floor(sqrt(x));
for (int i = 2; i <= n; i++) {
if (q % i == 0) {
if (IsPrime(i)) {
a[m] = i;
m++;
}
if (IsPrime(q / i)) {
a[m] = q / i;
m++;
}
}
}
sort(a, a + m, cmp);
if (m == 1 && a[0] * a[0] == q)
cout << 2;
else if (m == 1 && a[0] * a[0] != q)
cout << 1 << endl << a[0] * a[0];
else if (m == 2 && a[0] * a[1] != q && q % (a[0] * a[0]) == 0)
cout << 1 << endl << a[0] * a[0];
else if (m == 2 && a[0] * a[1] != q && q % (a[1] * a[1]) == 0)
cout << 1 << endl << a[1] * a[1];
else if (m == 2 && a[0] * a[1] == q)
cout << 2;
else
cout << 1 << endl << a[0] * a[1];
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long _sieve_size;
bitset<10000010> bs;
vector<int> primes;
void sieve(long long upperbound) {
_sieve_size = upperbound + 1;
bs.set();
bs[0] = bs[1] = 0;
for (long long i = 2; i <= _sieve_size; i++) {
if (bs[i]) {
for (long long j = i * i; j <= _sieve_size; j += i) bs[j] = 0;
primes.push_back((int)i);
}
}
}
int main() {
sieve(3200000);
std::ios::sync_with_stdio(0);
cin.tie(0);
long long q;
cin >> q;
long long result = 1;
long long aux;
bool flag = false;
if (q == 1) flag = true;
int div = 0;
for (int i = 0; i < primes.size(); i++) {
aux = (long long)primes[i];
while (q % aux == 0) {
q /= aux;
if (div <= 1) result *= aux;
div++;
}
if (aux > q) break;
}
if (q != 1) {
if (div <= 1) result *= aux;
div++;
}
if (div == 1) flag = true;
if (flag) {
cout << "1\n0\n";
} else {
if (div == 2) {
cout << "2\n";
} else {
cout << "1\n" << result << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100009;
const int sq = 3200009;
int vis[sq + 5];
long long sieve(long long n) {
int k = 0;
long long ans = 1;
for (int i = 4; i <= sq; i += 2) vis[i] = 1;
if (n % 2 == 0 && n != 2) {
k++;
ans = ans * 2;
}
if (n % 4 == 0) {
ans = 4;
return ans;
}
for (long long i = 3; i <= sq; i += 2) {
if (vis[i] == 0) {
if (n % (i * i) == 0) return i * i;
if (n % i == 0 && n != i) {
k++;
ans = ans * i;
if (k == 2) return ans;
}
for (long long j = i + i; j <= sq; j += i) {
vis[j] = 1;
}
}
}
if (k == 0) return 0;
return -1;
}
int main() {
long long n, num;
scanf("%I64d", &n);
num = sieve(n);
if (num == -1 || num == n) {
printf("2\n");
} else {
printf("1\n%I64d\n", num);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long n1 = n;
vector<long long> div;
for (long long i = 2; i * i <= n; i++) {
while (n % i == 0) {
div.push_back(i);
n = n / i;
}
}
if (n > 1) div.push_back(n);
if (div.size() == 1 || n1 == 1) {
cout << 1 << endl << 0;
} else if (div[0] * div[1] == n1) {
cout << 2;
} else {
cout << 1 << endl << div[0] * div[1];
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long q;
cin >> q;
long long org = q;
vector<int> f;
int i;
for (i = 2; i <= (int)sqrt((double)(q + 1)); ++i) {
while (q % i == 0) {
f.push_back(i);
q /= i;
}
if (f.size() >= 3) break;
if (q == 1) break;
}
if (q != org && q != 1) f.push_back(q);
if (f.size() == 0) {
printf("1\n");
printf("0\n");
} else if (f.size() == 2) {
printf("2\n");
} else {
printf("1\n");
cout << f[0] * f[1] << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool prime[10000005] = {1, 1};
long long p[10000005] = {0};
long long num[105];
void get_prime() {
int i, j;
for (i = 2; i * i <= 10000005; i++)
if (!prime[i])
for (j = 2 * i; j < 10000005; j += i) prime[j] = 1;
for (i = 2; i < 10000005; i++)
if (!prime[i]) p[++p[0]] = i;
return;
}
int fenjie(long long n) {
int res = 0, i;
for (i = 1; i <= p[0] & p[i] * p[i] <= n; i++)
while (n % p[i] == 0) {
num[res++] = p[i];
n /= p[i];
}
if (n > 1) num[res++] = n;
return res;
}
int main() {
get_prime();
long long n, res;
int i, many;
while (cin >> n) {
many = fenjie(n);
if (many > 2) {
cout << 1 << endl;
res = 1;
for (i = 0; i < 2; i++) res *= num[i];
cout << res << endl;
} else if (many == 2)
cout << 2 << endl;
else
cout << 1 << endl << 0 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long q;
bool zhi(long long now) {
if (now < 2) return false;
if (now == 2) return true;
for (long long a = 2; a * a <= now; a++)
if (now % a == 0) return false;
return true;
}
int main() {
cin >> q;
if (q == 1 || zhi(q)) {
printf("1\n0\n");
return 0;
}
for (long long a = 2; a * a <= q; a++) {
if (q % a == 0) {
long long half = q / a;
if (zhi(half)) {
printf("2\n");
} else {
printf("1\n");
for (long long b = 2; b * b <= half; b++)
if (half % b == 0) {
cout << b * a << endl;
break;
}
}
return 0;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
vector<long long> prime;
long long vis[10000000];
void sieve() {
long long i, j;
memset(vis, 0, sizeof(vis));
for (i = 2; i * i <= 10000000000000; i++) {
if (vis[i] == 0) {
vis[i] = 1;
prime.push_back(i);
for (j = i * i; j <= sqrt(10000000000000); j += i) {
vis[j] = 1;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t = 1, i, j, k, n, m, a, b, sz;
char ch;
string s;
bool ok;
while (t--) {
cin >> n;
m = n;
sieve();
a = b = -1;
sz = prime.size();
i = 0;
while (i != sz) {
if (n % prime[i] == 0) {
n = n / prime[i];
if (a == -1)
a = prime[i];
else if (b == -1) {
b = prime[i];
break;
}
} else
i++;
}
if (i == sz) {
if (a == -1) {
a = 1;
b = -1;
} else if (b == -1 && n != 1) {
b = n;
}
}
if (a * b < 0) {
cout << 1 << endl;
cout << 0 << endl;
} else if (a * b == m)
cout << 2 << endl;
else {
cout << 1 << endl;
cout << a * b << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, nn, ans = 1;
bool isprime(long long n) {
for (long long i = 2; i * i <= n; i++)
if (n % i == 0) return 0;
return 1;
}
int main() {
ios::sync_with_stdio(), cin.tie(0), cout.tie(0);
cin >> n, nn = n;
for (int i = 2; i < 1e5; i++) {
while (n % i == 0 && ans * i < nn) {
n /= i, ans *= i;
if (!isprime(ans)) return cout << 1 << endl << ans, 0;
}
}
if (isprime(nn)) return cout << 1 << endl << 0, 0;
cout << 2, 0;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5;
const long long MOD = 1e9 + 7;
const long long INF = 1e9 + 7;
vector<long long> get_factor(long long q) {
vector<long long> factor;
for (long long i = 2; i <= (sqrt(q)); i++) {
if (q % i == 0) {
factor.push_back(i);
if (q / i != i) factor.push_back(q / i);
}
}
sort(factor.begin(), factor.end());
return factor;
}
void solve() {
long long q;
cin >> q;
vector<long long> factor = get_factor(q);
if (factor.size() == 0)
cout << 1 << "\n" << 0 << "\n";
else if (factor.size() == 1)
cout << 2 << "\n";
else if (get_factor(factor[1]).size() != 0)
cout << 1 << "\n" << factor[1] << "\n";
else {
if (factor[1] * factor[0] == q)
cout << 2 << "\n";
else
cout << 1 << "\n" << factor[1] * factor[0] << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
long long n;
vector<long long> v;
int main() {
cin >> n;
if (n == 1) {
cout << "1" << endl << "0";
return 0;
}
for (long long i = 2; i * i <= n; i++) {
while (n % i == 0) {
v.push_back(i);
n /= i;
}
}
if (n > 1) v.push_back(n);
if (v.size() == 1)
cout << "1" << endl << "0";
else if (v.size() == 2)
cout << "2";
else {
cout << "1" << endl;
cout << v[0] * v[1];
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int q;
vector<int> prime, fector;
bool ara[10000000];
void makevector(int q) {
for (int i = 0; i < q; i++) {
if (ara[i]) prime.push_back(i);
}
}
void makeprime(int q) {
int n = sqrt(q) + 1;
ara[0] = false;
ara[1] = false;
for (int i = 4; i < q; i += 2) {
ara[i] = false;
}
for (int i = 3; i < n; i += 2) {
if (ara[i]) {
for (int j = i * i; j < q; j += i) ara[j] = false;
}
}
makevector(q);
}
bool isprime(long long int q) {
for (int i = 0; i < prime.size(); i++) {
if (q % prime[i] == 0) return false;
}
return true;
}
void makefector(long long int q) {
for (int i = 0; i < prime.size() || q > 1;) {
if (q % prime[i] == 0) {
q = q / prime[i];
fector.push_back(prime[i]);
} else if (i == prime.size() - 1 && q != 1) {
fector.push_back(q);
q = 1;
} else
i++;
}
return;
}
int main() {
memset(ara, true, sizeof(ara));
cin >> q;
makeprime(2 * sqrt(q) + 1);
if (isprime(q) || q == 1 || q == 2 || q == 3) {
cout << "1" << endl;
cout << "0" << endl;
return 0;
}
makefector(q);
if (fector.size() == 2)
cout << "2" << endl;
else {
cout << "1" << endl;
cout << fector[0] * fector[1] << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long q, c;
cin >> q;
long long ans = 1, p = 0;
c = q;
for (long long int i = 2; i * i <= q; i += 2) {
while (c % i == 0) {
if (p < 2) ans *= i;
p++;
c /= i;
}
if (i == 2) i--;
}
if (c != 1) p++;
if (p >= 3) {
cout << "1\n" << ans << endl;
} else if (p == 2) {
cout << 2 << endl;
} else if (p == 1) {
cout << "1\n0" << endl;
} else
cout << "1\n0" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 100000000LL;
const long long MAX = 100010LL;
template <class T>
T abs(T x) {
return x > 0 ? x : -x;
}
template <typename T>
T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <typename T>
T power(T x, T y, long long m = MOD) {
T ans = 1;
while (y > 0) {
if (y & 1LL) ans = (ans * x) % m;
y >>= 1LL;
x = (x * x) % m;
}
return ans % m;
}
int main() {
long long q;
cin >> q;
int cnt = 0;
long long start = q;
long long rem = 1;
for (long long i = 2; i * i <= q; ++i) {
while (q % i == 0) {
q /= i;
++cnt;
if (cnt <= 2) rem *= i;
}
}
if (q > 1) {
++cnt;
if (cnt <= 2) rem *= q;
}
if (cnt == 2)
cout << "2";
else {
cout << "1" << '\n';
if (cnt <= 1)
cout << "0";
else
cout << rem;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long i;
long long q;
vector<long long> v;
while (cin >> q) {
v.clear();
for (i = 2; i * i <= q; i++)
while (q % i == 0) {
v.push_back(i);
q /= i;
}
if (q > 1) v.push_back(q);
if (v.size() < 2)
printf("1\n0\n");
else if (v.size() == 2)
printf("2\n");
else
printf("1\n%d\n", v[0] * v[1]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int a[111111], cnt;
int main() {
long long n;
cin >> n;
for (long long i = 2; i * i <= n; ++i)
if (n % i == 0)
while (n % i == 0) {
a[++cnt] = i;
n /= i;
}
if (n != 1) a[++cnt] = n;
if (cnt < 2)
cout << "1\n0";
else if (cnt == 2)
cout << "2";
else
cout << "1\n" << a[2] * a[1];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long int n;
cin >> n;
long long int tmp = n;
if (n == 1) {
cout << 1 << '\n' << 0;
return 0;
}
long long int d = 3;
int cnt = 0;
vector<pair<long long int, int> > prime_div;
if (n % 2 == 0) {
while (n % 2 == 0) {
cnt++;
n >>= 1;
}
prime_div.push_back({2, cnt});
cnt = 0;
}
for (; d * d <= n; d += 2) {
if (n % d == 0) {
while (n % d == 0) {
cnt++;
n /= d;
}
prime_div.push_back({d, cnt});
cnt = 0;
}
}
if (n > 2) prime_div.push_back({n, 1});
if (prime_div.size() == 1) {
if (prime_div[0].second == 2)
cout << 2;
else {
cout << 1 << '\n';
if (prime_div[0].second == 1)
cout << 0;
else {
long long int p = prime_div[0].first;
cout << p * 1ll * p;
}
}
} else {
long long int ans = 1;
ans = prime_div[0].first * 1ll * prime_div[1].first;
if (ans == tmp)
cout << 2;
else
cout << 1 << '\n' << ans;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long p[100005], pn[100005];
long long n, m;
int main() {
cin >> n;
if (n == 1) {
cout << 1 << endl << 0 << endl;
return 0;
}
m = n;
int k = 0;
long long i;
int cnt = 0;
int sum = 0;
for (i = 2; i * i <= n; i++) {
if (n % i == 0) {
int cnt = 0;
while (n % i == 0) {
n /= i;
cnt++;
sum++;
}
pn[k] = cnt;
p[k++] = i;
}
}
if (n != 1) {
p[k] = n;
pn[k++] = 1;
sum++;
}
if (k == 1 && pn[0] == 1) {
cout << 1 << endl << 0 << endl;
} else {
if (sum > 2) {
cout << 1 << endl;
if (k == 1)
cout << p[0] * p[0] << endl;
else
cout << p[0] * p[1] << endl;
} else
cout << 2 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<long long int> v;
long long int i, j, n, resp;
while (cin >> n) {
j = n;
while (n > 1 && (n % 2) == 0) {
n /= 2;
v.push_back(2);
}
for (i = 3; (i * i) <= n; i += 2) {
while (n > 1 && (n % i) == 0) {
n /= i;
v.push_back(i);
}
}
if (n > 1) v.push_back(n);
if (j == 1 || (v.size() == 1)) {
cout << "1\n0\n";
} else if (v.size() == 2) {
cout << "2\n";
} else {
sort(v.begin(), v.end());
resp = v[0] * v[1];
cout << "1\n" << resp << endl;
}
v.clear();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double EPS = 1e-9;
const int BIG_PRIME7 = 1000000007;
const int BIG_PRIME9 = 1000000009;
char ch;
long long i, j, n, m, k;
long long N, C;
long long arr[1000];
long long q;
vector<long long> p;
bool isPrime(long long x) {
long long i = 0;
if (x == 2) return true;
if (x % 2 == 0) return false;
for (i = 3; i * i <= x; i += 2) {
if (x % i == 0) return false;
}
return true;
}
bool hasAllprimes(long long x) {
if (isPrime(x)) return false;
for (long long i = 2; i * i <= x; i++) {
if (x % i == 0) {
if (!isPrime(i) || !isPrime(x / i)) return false;
}
}
return true;
}
int main() {
cin >> q;
if (q == 1 || isPrime(q)) {
cout << "1" << endl;
cout << "0" << endl;
return 0;
}
for (long long i = 2; i * i <= q; i++) {
if (q % i == 0) {
p.push_back(i);
p.push_back(q / i);
}
}
for (long long i = 0; i < p.size(); i++) {
if (hasAllprimes(p[i])) {
cout << "1" << endl;
cout << p[i] << endl;
return 0;
}
}
cout << "2" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool num[4000000];
long long primes[4000000];
void siv() {
long long d = 0;
primes[d++] = 2;
for (long long i = 3; i < 4000000; i += 2) {
if (!num[i]) {
primes[d++] = i;
for (long long j = i * i; j < 4000000; j += (2 * i)) num[j] = 1;
}
}
}
vector<long long> s;
void prime_fact(long long x) {
long long lim = sqrt(x);
for (long long i = 0; primes[i] <= lim; i++) {
if (x % primes[i] == 0) {
long long j = 0;
while (x % primes[i] == 0) {
x /= primes[i];
j++;
}
s.push_back(primes[i]);
s.push_back(j);
lim = sqrt(x);
}
}
if (x > 1) {
s.push_back(x);
s.push_back(1);
}
}
int main() {
siv();
long long q;
scanf("%I64d", &q);
prime_fact(q);
if (q == 1)
printf("1\n0\n");
else {
if (s.size() == 2) {
if (s[1] == 1) {
printf("1\n0\n");
} else if (s[1] == 2) {
printf("2\n");
} else {
printf("1\n");
printf("%I64d\n", s[0] * s[0]);
}
} else if (s.size() == 4) {
if (s[1] == 1 && s[3] == 1)
printf("2\n");
else {
printf("1\n");
printf("%I64d\n", s[0] * s[2]);
}
} else {
printf("1\n");
printf("%I64d\n", s[0] * s[2]);
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, j = 0, i;
long long arr[100000];
cin >> n;
m = n;
for (i = 2; i * i <= n; i++) {
while (n % i == 0) {
n /= i;
arr[j++] = i;
}
}
if (n != m && n > 1) arr[j++] = n;
if (j == 2)
cout << "2\n";
else
cout << "1\n" << arr[0] * arr[1] << endl;
;
}
|
#include <bits/stdc++.h>
using namespace std;
int const MX = 1e7;
bool is_prime[MX];
int main() {
long long q;
cin >> q;
vector<long long> primes;
memset(is_prime, 1, sizeof is_prime);
for (long long i = 2; i * i <= q; i++) {
if (is_prime[i]) {
for (long long j = i * i; j < MX; j += i) {
is_prime[j] = 0;
}
if (q % i == 0) primes.emplace_back(i);
if (primes.size() == 2) break;
}
}
if (primes.size() == 0)
cout << 1 << '\n' << 0;
else {
long long nxt = primes[0] * primes[0];
if (nxt < q) {
if (q % nxt == 0)
cout << 1 << '\n' << nxt;
else if (primes.size() == 2)
cout << 1 << '\n' << primes[0] * primes[1];
else
cout << 2;
} else
cout << 2;
}
}
|
#include <bits/stdc++.h>
using namespace std;
bool isPrime(long long x) {
for (int i = 2; i * i <= x; i++)
if (x % i == 0) return false;
return true;
}
int main() {
long long n, N, cnt = 0, first = 0;
cin >> n;
N = n;
while (n % 2 == 0) cnt++, n /= 2;
for (int i = 3; i <= sqrt(n); i += 2)
while (n % i == 0) cnt++, n /= i;
if (!cnt || isPrime(N)) return cout << 1 << '\n' << (long long)0, 0;
if (n != 1) cnt++;
if (cnt == 2) return cout << 2, 0;
for (long long i = 2; i * i <= N; i++)
if (N % i == 0 && !first) {
first = i;
N /= i;
if (N % i == 0) return cout << "1\n" << first * i, 0;
} else if (N % i == 0)
return cout << "1\n" << first * i, 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long divi = 1;
int main() {
long long n;
cin >> n;
long long i = 2;
int c = 0;
long long nn = n;
while (i * i <= nn) {
while (n % i == 0) {
if (c < 2) {
divi *= i;
}
n = n / i;
c++;
}
i++;
}
if (n != nn && n > 1) {
if (c < 2) {
divi *= n;
}
c++;
}
if (c <= 0) {
cout << 1 << endl;
cout << 0 << endl;
} else if (c > 2) {
cout << 1 << endl;
cout << divi << endl;
} else {
cout << 2 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxcnt_prime = 500000 + 5;
long long q;
long long prime[maxcnt_prime];
int pcnt[maxcnt_prime];
int cnt_prime;
int main() {
cin >> q;
long long qt = q;
for (long long i = 2; i * i <= q; ++i)
if (qt % i == 0) {
prime[++cnt_prime] = i;
while (qt % i == 0) qt /= i, ++pcnt[cnt_prime];
}
if (qt > 1) prime[++cnt_prime] = qt, pcnt[cnt_prime] = 1;
if (q == 1 || prime[cnt_prime] == q) {
cout << 1 << endl << 0 << endl;
return 0;
} else if (prime[1] * prime[1] != q && prime[1] * prime[2] != q) {
long long ret = 1;
if (pcnt[1] >= 2)
ret = prime[1] * prime[1];
else
ret = prime[1] * prime[2];
cout << 1 << endl << ret << endl;
} else
cout << 2 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long q;
cin >> q;
long long numOfPrimes = 0;
long long ans1 = -1, ans2 = -1;
long long num = q;
while (q % 2 == 0) {
if (numOfPrimes > 2) break;
numOfPrimes++;
q /= 2;
if (ans1 == -1)
ans1 = 2;
else if (ans2 == -1)
ans2 = 2;
}
for (long long i = 3; i <= sqrt(q); i++) {
if (numOfPrimes > 2) break;
while (q % i == 0) {
if (numOfPrimes > 2) break;
numOfPrimes++;
q /= i;
if (ans1 == -1)
ans1 = i;
else if (ans2 == -1)
ans2 = i;
}
}
if (q > 2) numOfPrimes++;
if (numOfPrimes <= 1) {
cout << 1 << endl << 0 << endl;
} else if (numOfPrimes == 2)
cout << 2 << endl;
else {
cout << 1 << endl << ans1 * ans2 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> primes;
void prime(long long n) {
primes.push_back(2);
long long k = 3;
while (k * k <= n) {
bool is_prime = true;
for (int i = 0; i < primes.size() && primes[i] * primes[i] <= k && is_prime;
i++)
if (k % primes[i] == 0) is_prime = false;
if (is_prime) primes.push_back(k);
k += 2;
}
}
int main() {
long long int q, t;
cin >> q;
t = q;
prime(q);
vector<int> factors;
for (int i = 0; i < primes.size() && q > 1; i++) {
while (q % primes[i] == 0) {
factors.push_back(primes[i]);
q /= primes[i];
}
}
if (q > 1) factors.push_back(q);
if (factors.size() <= 1)
cout << "1\n0" << endl;
else if (factors.size() == 2)
cout << "2" << endl;
else
cout << "1\n" << factors[0] * factors[1] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long q;
cin >> q;
vector<long long> vec;
for (long long i = 2; i * i <= q && vec.size() < 3; i++) {
while (q % i == 0) {
q /= i;
vec.push_back(i);
if (vec.size() == 3) break;
}
}
if (q != 1 && vec.size() != 3) vec.push_back(q);
if (vec.size() == 3)
cout << "1\n" << vec[0] * vec[1] << endl;
else if (vec.size() == 2)
cout << 2 << endl;
else
cout << "1\n" << 0 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> factorization(long long n) {
vector<long long> first_primes;
if (n == 1) return first_primes;
for (long long i = 2; i * i <= n && first_primes.size() <= 3; i++) {
while (n % i == 0) first_primes.push_back(i), n /= i;
}
if (n > 1) first_primes.push_back(n);
return first_primes;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
long long n;
cin >> n;
vector<long long> first_primes = factorization(n);
if (first_primes.size() <= 1)
cout << "1\n0\n";
else if (first_primes.size() == 2)
cout << "2\n";
else
cout << "1\n" << first_primes[0] * first_primes[1] << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool check(long long x) {
long long s = (long long)sqrt(x + .0);
for (long long i = 2; i <= s + 10 && i < x; i++)
if (x % i == 0) return 0;
return 1;
}
int main() {
long long n;
cin >> n;
if (check(n) || n == 2) {
printf("1\n0\n");
return 0;
}
long long x = n;
long long s = (long long)sqrt(x + .0);
int cnt = 0;
for (int i = 2; i <= s && i < n; i++) {
while (x % i == 0) {
x /= i;
cnt++;
}
}
if (x > 1) cnt++;
if (cnt == 2) {
printf("2\n");
return 0;
}
cout << 1 << endl;
x = n;
cnt = 0;
long long p = 1;
for (int i = 2; i <= s && i < n; i++)
while (x % i == 0) {
x /= i;
p *= i;
cnt++;
if (cnt == 2) {
cout << p << endl;
return 0;
}
}
if (x > 1) {
cnt++;
if (cnt == 2) {
p = p * x;
cout << p << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
unsigned long long arr[4000001];
int main() {
arr[0] = 0, arr[1] = 0;
for (int i = 2; i <= 4000000; i++) arr[i] = 1;
for (int i = 2; i * i <= 4000000; i++) {
if (arr[i]) {
for (int p = i * i; p <= 4000000; p += i) {
arr[p] = 0;
}
}
}
unsigned long long q, x = 0, y = 0, z, c = 1, tmp = 0, mn = INT_MAX;
cin >> q;
bool b = 0, bl = 0;
while (1) {
b = 0, z = 0, mn = INT_MAX;
for (unsigned long long i = 2; i * i <= q; i++) {
if (q % i == 0 && !arr[i]) {
b = 1;
z = i;
break;
} else if (q % i == 0 && arr[i]) {
b = 1;
z = q / i;
}
}
if (q != 4 && q % 2 == 0) {
if (z > q / 2) z = q / 2;
}
if (c == 1) x = z;
q = z;
c++;
if (!b) break;
}
if (c % 2 == 0)
cout << "1\n" << x << "\n";
else
cout << 2 << "\n";
return 0;
}
|
#include <bits/stdc++.h>
const long long int INF = 1ll << 60;
using namespace std;
namespace number_theory {
bool prime[15000105];
void sieve(int n) {
for (long long int i = 0; i <= n; i++) prime[i] = 1;
for (long long int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (long long int i = p * p; i <= n; i += p) prime[i] = false;
}
}
prime[1] = prime[0] = 0;
}
vector<long long int> primelist;
bool __primes_generated__ = 0;
void genprimes(int n) {
__primes_generated__ = 1;
sieve(n + 1);
for (long long int i = 2; i <= n; i++)
if (prime[i]) primelist.push_back(i);
}
vector<long long int> factors(long long int n) {
if (!__primes_generated__) {
cerr << "Bruh, Call genprimes" << '\n';
exit(1);
}
vector<long long int> facs;
for (long long int i = 0;
primelist[i] * primelist[i] <= n && i < primelist.size(); i++) {
if (n % primelist[i] == 0) {
while (n % primelist[i] == 0) {
n /= primelist[i];
facs.push_back(primelist[i]);
}
}
}
if (n > 1) {
facs.push_back(n);
}
return facs;
}
} // namespace number_theory
using namespace number_theory;
long long int n, m, k, q, l, r, x, y, z;
const long long int template_array_size = 1e6 + 3862;
long long int a[template_array_size];
long long int b[template_array_size];
long long int c[template_array_size];
string s, t;
long long int ans = 0;
long long int bp(long long int a, long long int b) {
long long int res = 1;
while (b > 0) {
if (b & 1) {
res = (res * a);
b--;
}
a = (a * a);
b >>= 1;
}
return res;
}
long long int ncr(long long int n, long long int k) {
long long int ans = 1;
if (k > n - k) k = n - k;
for (long long int i = 1; i <= k; i++) ans *= (n - i + 1), ans /= i;
return ans;
}
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int modInverse(long long int n, long long int p) {
return power(n, p - 2, p);
}
long long int ncrModPFermat(long long int n, long long int r, long long int p) {
if (r == 0) return 1;
long long int fac[n + 1];
fac[0] = 1;
for (long long int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % p;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
p;
}
long long int gcd(long long int a, long long int b) {
return (b == 0) ? a : gcd(b, a % b);
}
bool isprime(long long int x) {
long long int i = 2;
bool f = false;
for (i = 2; i * i <= x; i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n;
cin >> n;
if (n == 1) {
cout << 1 << '\n';
cout << 0 << '\n';
} else {
genprimes(10000000);
vector<long long int> v = factors(n);
if (v.size() == 1) {
cout << 1 << '\n';
cout << 0 << '\n';
} else {
if (v.size() == 2) {
cout << 2 << '\n';
} else {
cout << 1 << '\n';
ans = v[0] * v[1];
cout << ans << '\n';
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
if (n == 1) {
printf("1\n0\n");
return 0;
}
long long p1 = -1, p2 = -1;
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) {
p1 = i;
n /= i;
break;
}
}
if (p1 == -1) {
printf("1\n0\n");
return 0;
}
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) {
p2 = i;
n /= i;
break;
}
}
if (p2 == -1) {
printf("2\n");
return 0;
}
printf("1\n%lld\n", p1 * p2);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
string tostr(const T& t) {
ostringstream os;
os << t;
return os.str();
}
long long N;
vector<long long> getfactors(long long n) {
set<long long> s;
for (long long i = 1; i * i <= n; ++i) {
if ((n % i) == 0) {
if (i != 1 && i != n) s.insert(i);
long long x = n / i;
if (x != 1 && x != n) s.insert(x);
}
}
return vector<long long>((s).begin(), (s).end());
}
map<long long, bool> memo;
bool go(int x) {
vector<long long> f = getfactors(x);
if ((int)f.size() == 0) {
return true;
}
if (memo.count(x)) return memo[x];
bool& ref = memo[x];
bool allWin = true;
for (int i = (int)(0); i <= (int)((int)f.size() - 1); ++i) {
bool win = go(f[i]);
if (!win) {
allWin = false;
}
}
if (allWin)
ref = false;
else
ref = true;
return ref;
}
int main() {
cin >> N;
vector<long long> f = getfactors(N);
if ((int)f.size() == 0) {
cout << "1" << endl;
cout << "0" << endl;
} else {
bool found = false;
for (int i = (int)(0); i <= (int)((int)f.size() - 1); ++i) {
memo.clear();
bool lose = !go(f[i]);
if (lose) {
found = true;
cout << "1" << endl;
cout << f[i] << endl;
break;
}
}
if (!found) cout << "2" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5000007;
int tot;
long long n, ans;
long long a[maxn];
void work() {
cin >> n;
if (n == 1) {
puts("1");
puts("0");
return;
}
tot = 0;
long long i = 2;
while (i * i <= n) {
if (n % i == 0) {
while (n % i == 0) a[++tot] = i, n /= i;
}
i++;
}
if (n > 1) a[++tot] = n;
if (tot == 1) {
puts("1");
puts("0");
} else if (tot > 2) {
puts("1");
cout << a[1] * a[2] << endl;
} else
puts("2");
}
int main() { work(); }
|
#include <bits/stdc++.h>
using namespace std;
long long x, n, a, b, i;
int main() {
scanf("%lld", &n);
x = n;
a = 0;
b = 0;
for (i = 2; i <= sqrt(n) && x > 1; ++i) {
while (x % i == 0) {
if (a == 0)
a = i;
else if (b == 0)
b = i;
else {
printf("1\n");
printf("%lld\n", a * b);
return 0;
}
x /= i;
}
}
if (x > 1) {
if (a == 0)
a = x;
else if (b == 0)
b = x;
else {
printf("1\n");
printf("%lld\n", a * b);
return 0;
}
}
if (a * b == 0)
printf("1\n0\n");
else
printf("2\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int prime[10000000], que[10000000], vis[10000000];
int main() {
long long i, j, n, cnt = 0, tmp, flag = 0, besti = -1;
long long ans;
memset(prime, 0, sizeof(prime));
memset(vis, 0, sizeof(vis));
for (i = 2; i * i < 10000000; i++)
if (prime[i] == 0) {
for (j = i * 2; j < 10000000; j += i) prime[j] = 1;
}
for (i = 2; i < 10000000; i++)
if (prime[i] == 0) que[cnt++] = i;
cin >> n;
tmp = n;
if (n < 10000000 - 2 && prime[n] == 0) {
printf("1\n0\n");
return 0;
}
for (i = 0; i < cnt; i++) {
while (tmp >= que[i] && tmp % que[i] == 0) {
vis[que[i]]++;
tmp /= que[i];
flag++;
besti = que[i];
}
}
if (tmp == n) {
cout << "1" << endl;
cout << "0" << endl;
return 0;
}
if (tmp != 1 && flag > 1) {
printf("1\n");
cout << tmp * besti << endl;
return 0;
}
if (flag >= 3) {
printf("1\n");
ans = 1, flag = 0;
for (i = 0; i * i <= n && flag < 2; i++)
while (flag < 2 && vis[i]) {
ans *= i, flag++;
vis[i]--;
}
cout << ans << endl;
} else
printf("2\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100;
long long fac[N];
int cnt[N];
int main() {
long long a;
cin >> a;
if (a == 1) {
cout << 1 << endl << 0 << endl;
return 0;
}
int num = 0;
long long i;
for (i = 2; i * i <= a; i++) {
if (a % i == 0) {
int cunt = 0;
while (a % i == 0) {
a /= i;
cunt++;
}
fac[num] = i;
cnt[num++] = cunt;
}
}
if (a > 1) {
fac[num] = a;
cnt[num++] = 1;
}
int count = 0;
for (int i = 0; i < num; i++) count += cnt[i];
if (count == 1) {
cout << 1 << endl;
cout << 0 << endl;
} else if (count == 2) {
cout << 2 << endl;
} else {
long long num = 1;
int count = 0;
int j = 0;
while (count < 2) {
for (int i = 0; count < 2 && i < cnt[j]; i++) {
num *= fac[j];
count++;
}
j++;
}
cout << 1 << endl;
cout << num << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
void fact(long long q) {
int g = 1;
long long n = q;
long long count = 0;
long long k = 1;
int flag = 0;
long long i;
for (i = 2; i * i <= n; i++) {
while (q % i == 0) {
flag = 1;
q = q / i;
count = count + 1;
if (count <= 2) k = k * i;
}
}
if (q != 1) count = count + 1;
if (flag == 0) {
cout << 1 << endl;
cout << 0 << endl;
return;
}
if (count > 2) {
cout << 1 << endl;
cout << k << endl;
return;
} else
cout << 2 << endl;
}
int main() {
long long q;
cin >> q;
fact(q);
return 0;
}
|
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1e5 + 10;
int t, n, m, k, tot;
int a[N], b[N], ans[N];
template <typename T>
inline void read(T &x){
x = 0;
char c = getchar();
T op = 1;
for(; c < '0' || c > '9'; c = getchar())
if(c == '-') op = -1;
for(; c <= '9' && c >= '0'; c = getchar())
x = (x << 3) + (x << 1) + c - '0';
x *= op;
}
int main(){
read(t);
while(t--){
tot = 0;
read(k), read(n), read(m);
for(int i = 1; i <= n; ++i) read(a[i]);
for(int i = 1; i <= m; ++i) read(b[i]);
int i = 1, j = 1;
while(i <= n || j <= m){
while(a[i] == 0 && i <= n) ++k, ans[++tot] = 0, ++i;
while(b[j] == 0 && j <= m) ++k, ans[++tot] = 0, ++j;
if(a[i] <= k && i <= n) ans[++tot] = a[i], ++i;
else if(b[j] <= k && j <= m) ans[++tot] = b[j], ++j;
else break;
}
if(i <= n || j <= m) printf("-1");
else for(int i = 1; i <= tot; ++i) printf("%d ", ans[i]);
putchar('\n');
}
return 0;
}
|
#include<iostream>
#include<queue>
using namespace std;
int n, m, k;
int a[310], b[310];
int main() {
int t;
cin>>t;
while (t--) {
queue<int> q;
cin>>k>>n>>m;
for (int i = 1; i <= n; i++) cin>>a[i];
for (int i = 1; i <= m; i++) cin>>b[i];
int i = 1, j = 1;
bool st = true;
while (i + j != n + m + 2) {
if (i <= n && a[i] == 0) {
q.push(0);
i++;
k++;
continue;
}
if (j <= m && b[j] == 0) {
q.push(0);
j++;
k++;
continue;
}
if (i <= n && a[i] <= k) {
q.push(a[i]);
i++;
continue;
}
if (j <= m && b[j] <= k) {
q.push(b[j]);
j++;
continue;
}
st = false;
break;
}
if (st) {
while (!q.empty()) {
int t = q.front();
q.pop();
printf("%d ", t);
}
printf("\n");
} else
printf("-1\n");
}
return 0;
}
|
/*
Author : notreallystatic
github:https://github.com/notreallystatic
linkedin : https://www.linkedin.com/in/notreallystatic/
*/
#include <iostream>
#include <stdlib.h>
#include <numeric>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <list>
#include <functional>
#include <cctype>
#include <sstream>
#include <cmath>
#include <limits.h>
#include <bitset>
#include <random>
using namespace std;
vector<int> merge(int lines, vector<int> &arr1, vector<int> &arr2)
{
vector<int> result;
while (arr1.size() && arr2.size())
{
if (arr1.front() <= lines)
{
if (arr1.front() == 0)
++lines;
result.push_back(arr1.front());
arr1.erase(arr1.begin());
}
else if (arr2.front() <= lines)
{
if (arr2.front() == 0)
++lines;
result.push_back(arr2.front());
arr2.erase(arr2.begin());
}
else
return {-1};
}
if (arr2.size())
arr1 = arr2;
while (arr1.size())
{
if (arr1.front() <= lines)
{
result.push_back(arr1.front());
if (arr1.front() == 0)
++lines;
}
else
return {-1};
arr1.erase(arr1.begin());
}
return result;
}
void solve()
{
int lines, n, m;
cin >> lines >> n >> m;
vector<int> arr1(n), arr2(m);
for (int &x : arr1)
{
cin >> x;
}
for (int &x : arr2)
{
cin >> x;
}
vector<int> result = merge(lines, arr1, arr2);
for (auto x : result)
{
cout << x << " ";
}
cout << endl;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
string temp;
getline(cin, temp);
getline(cin, temp);
while (t--)
{
solve();
getline(cin, temp);
getline(cin, temp);
}
return 0;
}
|
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef pair<int,int> p;
#define N 110
#define pi pair<int,int>
#define pb push_back
#define test \
int t; \
scanf("%d",&t);\
while(t--)
ll llmax = numeric_limits<long long>::max();
ll llmin= numeric_limits<long long>::min();
int arr[N], grr[N];
int mat[1030][11];
char grid[60][60];
char gl[60][60];
char gn[60][60];
vector<int> v;
vector<string> srr;
vector<int> vy;
vector< vector<int> > g;
vector<pair<int,pair<int,int> > >pv;
map<ll,int>mp;
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >pqr;
int main()
{
test
{
cout<<endl;
int k,n,m;
cin>>k>>n>>m;
v.clear();
for(int i=0; i<n; i++)
cin>>arr[i];
for(int i=0; i<m; i++)
cin>>grr[i];
int as = 0, gs =0;
int tt = n+m;
while(tt--)
{
if(arr[as]==0 && as<n)
{
k++;
v.push_back(arr[as]);
//cerr<<arr[as]<<endl;
as++;
}
else if(arr[as]<=k && as<n)
{
v.push_back(arr[as]);
//cerr<<arr[as]<<endl;
as++;
}
if(grr[gs]==0 && gs<m)
{
k++;
v.push_back(grr[gs]);
//cerr<<grr[gs]<<endl;
gs++;
}
else if(grr[gs]<=k && gs<m)
{
v.push_back(grr[gs]);
//cerr<<grr[gs]<<endl;
gs++;
}
}
if(v.size()==n+m)
{
int sz=v.size();
for(int i=0; i<sz; i++)
cout<<v[i]<<" ";
cout<<endl;
}
else
{
cout<<"-1"<<endl;
}
}
}
|
#include <bits/stdc++.h>
#include <deque>
using namespace std;
#define ll long long int
#define tab " "
#define endl "\n"
ll m = 1000000007;
bool iseven(ll n)
{
return n%2==0;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t;
cin>>t;
while(t--)
{
ll k,m,n;
cin>>k>>n>>m;
ll a[n],b[m],ans[m+n];
for (int i = 0; i < n; ++i)
{
cin>>a[i];
}
for (int i = 0; i < m; ++i)
{
cin>>b[i];
}
ll flag=0,i=0,j=0,it=0,l=0;
while(l<m+n)
{
if (a[i]==0&&i<n)
{
ans[l++]=a[i++];
k++;
flag=0;
}
else if (b[j]==0&&j<m)
{
ans[l++]=b[j++];
k++;
flag=0;
}
else if (a[i]<=k&&a[i]>0&&i<n)
{
ans[l++]=a[i++];
flag=0;
}
else if (b[j]<=k&&b[j]>0&&j<m)
{
ans[l++]=b[j++];
flag=0;
}
else if (flag==1)
{
break;
}
else
{
flag=1;
}
}
if (l<m+n)
{
cout<<-1<<endl;
}
else
{
for (int i = 0; i < m+n; ++i)
{
cout<<ans[i]<<tab;
}
cout<<endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MAX=1e18+1;
const int NN=1e5+5;
const int mod=1e9+7;
#define pb push_back
//s1=string(3,'a')
//s1="aaa"
// use long double for precision not double
void solve(){
int k,n,m;
cin>>k>>n>>m;
int a[n],b[m];
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<m;i++)
cin>>b[i];
vector<int> v;
int ai=0,bi=0,turn=1;
while(ai<n || bi<m){
if(turn){
if(ai<n && a[ai]==0){
v.pb(0);
k++,ai++;
}
else if(ai<n && a[ai]<=k){
v.pb(a[ai]);
ai++;
}
else if(bi>=m || b[bi]>k){
cout<<-1<<endl;
return;
}
}
else{
if(bi<m && b[bi]==0){
v.pb(0);
k++,bi++;
}
else if(bi<m && b[bi]<=k){
v.pb(b[bi]);
bi++;
}
else if(ai>=n || a[ai]>k){
cout<<-1<<endl;
return;
}
}
turn ^= 1;
}
if(v.size()!=n+m){
cout<<-1<<endl;
}
else{
for(auto it: v)
cout<<it<<" ";
cout<<endl;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
cin>>t;
while(t--) solve();
return 0;
}
//#ifndef ONLINE_JUDGE
//cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n";
//#endif
// always check for long long
// vector<int> v[30]; -> vector inside array of fixed sized 30
// 2LL
//cout << fixed << setprecision(6) << pi <<" "<<npi<<endl;
|
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
int main(){
int t; cin >> t;
while(t--){
int k, n, m; cin >> k >> n >> m;
queue<int> a, b;
int size = k;
for(int i = 0; i < n; i++){
int x; cin >> x;
a.push(x);
}
for(int j = 0; j < m; j++){
int x; cin >> x;
b.push(x);
}
bool ok = true;
vector<int> ans;
while(!a.empty() && !b.empty()){
int op;
if(a.front() < b.front()){
op = a.front();
a.pop();
}
else{
op = b.front();
b.pop();
}
if(op == 0)size++;
else{
if(op > size){
ok = false;
break;
}
}
ans.push_back(op);
}
if(ok){
int op;
while(!a.empty()){
op = a.front();
a.pop();
if(op == 0)size++;
else{
if(op > size){
ok = false;
break;
}
}
ans.push_back(op);
}
while(!b.empty()){
op = b.front();
b.pop();
if(op == 0)size++;
else{
if(op > size){
ok = false;
break;
}
}
ans.push_back(op);
}
}
if(ok){
for(int i = 0; i < ans.size(); i++)cout << ans[i] << " ";
cout << endl;
}
else{
cout << -1 << endl;
}
}
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define dd double
#define MOD 1000000007
#define MAX LONG_LONG_MAX
#define MIN LONG_LONG_MIN
#define nl cout << endl
#define mp make_pair
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define frr(i, b, a) for (ll i = b - 1; i >= a; i--)
#define pb push_back
#define ff first
#define max3(a, b, c) max(c, max(a, b))
#define min3(a, b, c) min(c, min(a, b))
#define pre(p) cout.precision(p)
#define in(n) cin >> n
#define fo(i, n) for (ll i = 0; i < n; i++)
#define Fo(i, k, n) for (ll i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define clr(x) memset(x, 0, sizeof(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define all(v) v.begin(), v.end()
#define s(ar, n) sort(ar, ar + n)
#define rs(ar, n) sort(ar, ar + n, greater<ll>())
#define cn(a, n) \
for (ll i = 0; i < n; i++) \
cin >> a[i];
#define cn1(a, n) \
for (ll i = 1; i <= n; i++) \
cin >> a[i];
//#define reverse(a, n) for(ll i=0;i<n/2;i++){ll temp = a[i];a[i]=a[n-i-1];a[n-i-1]=temp;}
#define ov(a, n) \
for (ll i = 0; i < n; i++) \
cout << a[i] << " "; \
nl
#define ovv(v) \
for (ll i = 0; i < v.size(); i++) \
cout << v[i] << " "; \
nl
#define ovp(a, n) \
for (ll i = 0; i < n; i++) \
cout << a[i].ff << " " << a[i].ss << " ";
#define maxa(ar, N) *max_element(ar, ar + N)
#define mina(ar, N) *min_element(ar, ar + N)
#define fastio() \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
typedef pair<ll, ll> pii;
#define vl vector<ll>
ll gcd(ll a, ll b)
{
while (b)
{
a %= b;
swap(a, b);
}
return a;
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll powe(ll n, ll m)
{
if (m == 0)
return 1;
ll t = powe(n, m / 2);
if (m % 2 == 0)
return (t * t);
return (((t * t)) * n);
}
ll mpowe(ll n, ll m)
{
if (m == 0)
return 1;
ll t = powe(n, m / 2);
t %= MOD;
if (m % 2 == 0)
return (t * t) % MOD;
return (((t * t) % MOD) * n) % MOD;
}
ll logtwo(ll n)
{
if (n == 1)
return 0;
return logtwo(n / 2) + 1;
}
ll binpow(ll a, ll b, ll m)
{
a %= m;
ll res = 1;
while (b > 0)
{
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
ll calculateNcR(ll n, ll r)
{
if (n < r)
return 0;
ll p = 1, k = 1;
if (n - r < r)
r = n - r;
if (r != 0)
{
while (r)
{
p *= n;
k *= r;
ll m = gcd(p, k);
p /= m;
k /= m;
n--;
r--;
}
}
else
p = 1;
return p;
}
ll stringToInt(string s)
{
ll num = 0;
fr(i, 0, s.length()) { num = num * 10 + (s[i] - '0'); }
return num;
}
void output(ll n) { cout << "Case #" << n << ": "; }
ll A[10000005];
void vecIn(vector<ll> &v, ll n)
{
for (ll i = 0; i < n; i++)
{
cin >> v[i];
}
}
void print(vector<ll> v)
{
for (ll i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
}
bool cmp(vl &a, vl &b)
{
if (a[1] != b[1])
return a[1] < b[1];
return a[0] > b[0];
}
int main()
{
fastio();
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll t;
cin >> t;
while (t--)
{
ll k,n,m;
cin >> k>>n>>m;
queue<ll> arr;
queue<ll> brr;
for(int i=0;i<n;i++)
{
ll temp; cin >> temp;
arr.push(temp);
}
for(int i=0;i<m;i++)
{
ll temp; cin >> temp;
brr.push(temp);
}
queue<ll> ans;
bool flag=true;
ll cs=k;
while(!arr.empty() and !brr.empty())
{
if(arr.front()==0)
{
ans.push(0);
arr.pop();
cs++;
}
else if(brr.front()==0)
{
ans.push(0);
brr.pop();
cs++;
}
else
{
if(min(arr.front(), brr.front()) > cs)
{
flag = false;
break;
}
else if(arr.front()<=brr.front())
{
ans.push(arr.front());
arr.pop();
}
else
{
ans.push(brr.front());
brr.pop();
}
}
}
while(!arr.empty())
{
if(arr.front()==0)
{
ans.push(0);
arr.pop();
cs++;
}
else if(arr.front()>cs)
{
flag = false;
break;
}
else
{
ans.push(arr.front());
arr.pop();
}
}
while(!brr.empty())
{
if(brr.front()==0)
{
ans.push(0);
brr.pop();
cs++;
}
else if(brr.front()>cs)
{
flag = false;
break;
}
else
{
ans.push(brr.front());
brr.pop();
}
}
if(!flag)
cout<<-1<<endl;
else
{
while(!ans.empty())
{
cout<<ans.front()<<" ";
ans.pop();
}
cout<<endl;
}
}
}
|
#include <bits/stdc++.h>
#include <string>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define pb push_back
#define fi first
#define se second
const ll mod = 1e9+7;
#define loop(i,a,b) for (ll i = a; i < b; ++i)
#define revloop(i,a,b) for (ll i = a; i > b ; i--)
const ll MAX=100005;
int32_t main()
{
IOS
ll t;
cin>>t;
while(t--)
{
int k , n, m;
cin>>k>>n>>m;
vector<int> a(n);
vector<int> b(m);
loop(i,0,n) cin>>a[i];
loop(i,0,m) cin>>b[i];
int x=0;
int y=0;
bool f =true;
vector<int> res;
loop(z,0,n+m)
{
if (x<n and a[x]==0)
{
k++;
res.pb(a[x]);
x++;
}
else if (y<m and b[y]==0)
{
k++;
res.pb(b[y]);
y++;
}
else if (y==m or (x<n and a[x]<b[y]))
{
if(a[x]<=k)
{
res.pb(a[x]);
x++;
}
else
{
f=false;
break;
}
}
else
{
if(b[y]<=k)
{
res.pb(b[y]);
y++;
}
else
{
f=false;
break;
}
}
}
if(f)
{
for(int i : res)
{
cout<<i<<' ';
}
cout<<endl;
}
else
{
cout<<-1<<endl;
}
}
return 0;
} |
/**
You just can't beat the one who never gives up
/T /I
/ |/ | .-~/
T\ Y I |/ / _
/T | \I | I Y.-~/
I l /I T\ | | l | T /
T\ | \ Y l /T | \I l \ ` l Y
__ | \l \l \I l __l l \ ` _. |
\ -l `\ `\ \ \ ~\ \ `. .- |
\ ~-. "-. ` \ ^._ ^. "-. / \ |
.--~-._ ~- ` _ ~-.-"-." . /._ ." ./
>--. ~-. ._ ~>-" "\ 7 7 ]
^._"--._ ~-{ .- . `\ Y . / |
<_ ~"-. ~ // \ \I Y : |
^-._ ~(/ \ >.: | l___
^--.,_.-" /_/ ! `-."--l_ / ~"-.
(_/ . ( /' ""--,Y -=b-. _)
(_/ . \ : / l c"~o \
\ / `. . .^ \_.-"--. )
(_/ . ` / / ! )/
/ / _. '. .': / '
~(/ . / _ ` .-<
/_/ . ' .-~" `. / \ \ ,z=.
( / ' : | K "-.-.__//
"-,. l I/ \_ _{--->.(==.
//( \ < "" //
/' /\ \ \ ,v=. ((
.^. / /\ " }__ //===- `
/ / ' ' "-.,__ {---(==-
.^ ' : T ~" ll -Sparrow
/ . . . : | :! \
(_/ / | | j-" ~^
**/
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define endl '\n'
#define fo(i, a) for (int i = 0; i < a; i++)
#define fod(i, a, b) for (int i = a; i >= b; i--)
#define ff first
#define ss second
#define FAST_IO \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define ip(v) \
for (auto &x : v) \
cin >> x
#define op(v) \
for (auto x : v) \
cout << x << " "; \
cout << endl
#define tr(v) for (auto x : v)
#define srt(v) sort(v.begin(), v.end())
#define rev(v) reverse(v.begin(), v.end())
#define clr(x) memset(x, 0, sizeof(x))
typedef long long ll;
typedef vector<ll> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef map<int, int> mii;
const int Mod = 1e9 + 2;
// &*!=
// ()@
//Vikram1234@
void solve()
{
ll k, n, m;
cin >> k >> n >> m;
vi a(n), b(m), ans;
ip(a);
ip(b);
//dd a line is beneficial
int i = 0, j = 0;
ll lc = k;
while (i < n and j < m)
{
if (a[i] == 0)
{
ans.push_back(a[i]);
i++;
lc++;
}
else if (b[j] == 0)
{
ans.push_back(b[j]);
j++;
lc++;
}
else
{
if (a[i] <= b[j])
{
if (a[i] <= lc)
{
ans.push_back(a[i]);
i++;
}
else
{
cout << "-1\n";
return;
}
}
else
{
if (b[j] <= lc)
{
ans.push_back(b[j]);
j++;
}
else
{
cout << "-1\n";
return;
}
}
}
}
while (i < n)
{
if (a[i] == 0)
{
lc++;
ans.push_back(a[i]);
i++;
}
else
{
if (a[i] <= lc)
{
ans.push_back(a[i]);
i++;
}
else
{
cout << "-1\n";
return;
}
}
}
while (j < m)
{
if (b[j] == 0)
{
lc++;
ans.push_back(b[j]);
j++;
}
else
{
if (b[j] <= lc)
{
ans.push_back(b[j]);
j++;
}
else
{
cout << "-1\n";
return;
}
}
}
op(ans);
}
int main()
{
FAST_IO
ll t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
int k, n, m;
cin >> k >> n >> m;
int i, j, a[n], b[m];
for(i=0; i<n; i++)
cin >> a[i];
for(i=0; i<m; i++)
cin >> b[i];
vector<int> ans;
int flag;
flag = 0;
j=0;
for(i=0; i<n; i++)
{
if(flag==2)
break;
if(a[i]==0)
{
k++;
flag=0;
ans.push_back(a[i]);
}
else if(a[i]<=k)
{
ans.push_back(a[i]);
flag=0;
}
else
{
i--;
flag+=1;
if(flag==2)
break;
for(j=j; j<m; j++)
{
if(b[j]==0)
{
k++;
flag=0;
ans.push_back(b[j]);
}
else if(b[j]<=k)
{
ans.push_back(b[j]);
flag=0;
}
else
{
flag++;
break;
}
}
}
}
if(flag==2)
{
cout << "-1" << endl;
}
else
{
for(j=j; j<m; j++)
{
if(b[j]==0)
{
k++;
flag=0;
ans.push_back(b[j]);
}
else if(b[j]<=k)
{
ans.push_back(b[j]);
flag=0;
}
else
{
flag=5;
break;
}
}
if(flag==5)
cout << "-1" << endl;
else
{
for(i=0; i<n+m; i++)
{
cout << ans[i] << " ";
}
cout <<endl;
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector <int>
#define vl vector <ll>
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define ff first
#define ss second
#define MOD 1000000007
ll power(ll a, ll b) { //a^b
ll res = 1;
a = a % MOD;
while (b > 0) {
if (b & 1) {res = (res * a) % MOD; b--;}
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
ll gcd(ll a, ll b) {return (b == 0) ? a : gcd(b, a % b);}
int main() {
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin >> t;
while (t--) {
ll k, n, m;
cin >> k >> n >> m;
vl a(n);
for (auto &i : a)
cin >> i;
vl b(m);
for (auto &i : b)
cin >> i;
int f = 0;
vl ans;
ll i, j;
i = j = 0;
while (i < n || j < m) {
while (i < n && a[i] == 0) {
ans.pb(0);
i++;
k++;
}
while (j < m && b[j] == 0) {
ans.pb(0);
j++;
k++;
}
if (i < n && j < m) {
if (a[i] <= b[j]) {
if (a[i] > k) {
f = 1;
break;
}
ans.pb(a[i]);
i++;
continue;
}
else {
if (b[j] > k) {
f = 1;
break;
}
ans.pb(b[j]);
j++;
continue;
}
}
if (i < n) {
if (a[i] > k) {
f = 1;
break;
}
ans.pb(a[i]);
i++;
continue;
}
if (j < m) {
if (b[j] > k) {
f = 1;
break;
}
ans.pb(b[j]);
j++;
continue;
}
}
if (f)
cout << "-1\n";
else {
for (auto i : ans)
cout << i << " ";
cout << "\n";
}
}
}
|
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<ctime>
#define ll long long
using namespace std;
const int maxx=600010;
const double pi=acos(-1.0);
const double eps=1e-15;
int t,n,m,k;
int ans=0,total=0,sum=0,cnt=0,l1=0,l2=0;
int a[maxx],b[maxx],c[maxx];
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&k,&n,&m);
l1=l2=1;
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=m;i++)scanf("%d",&b[i]);
ans=0,cnt=0;
while(l1<=n||l2<=m)
{
while(l1<=n)
{
if(!a[l1])k++,c[++cnt]=a[l1++];
else if(a[l1]<=k)c[++cnt]=a[l1++];
else break ;
}
while(l2<=m)
{
if(!b[l2])k++,c[++cnt]=b[l2++];
else if(b[l2]<=k)c[++cnt]=b[l2++];
else break ;
}
if(l1>n&&l2>m)break ;
else if(l1>n)
{
if(b[l2]>k)
{
ans=1;
break ;
}
}
else if(l2>m)
{
if(a[l1]>k)
{
ans=1;
break ;
}
}
else if(a[l1]>k&&b[l2]>k)
{
ans=1;
break ;
}
}
if(ans)printf("-1\n");
else
{
for(int i=1;i<=cnt;i++)printf("%d ",c[i]);
printf("\n");
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int MX = 2e3+5;
#define ll long long
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
cin>>t;
while(t--)
{
int k,n,m;
cin>>k>>n>>m;
int a[n],b[m];
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<m;i++)
cin>>b[i];
int i=0,j=0;
vector<int>ans;
while(i<n&&j<m)
{
if(a[i]<b[j])
ans.push_back(a[i++]);
else
ans.push_back(b[j++]);
}
while(i<n)ans.push_back(a[i++]);
while(j<m)ans.push_back(b[j++]);
bool flag=0;
for(int i=0;i<ans.size();i++)
{
if(ans[i]==0)k++;
else if(ans[i]>k)
flag=1;
}
if(flag)
cout<<-1;
else
{
for(int i=0;i<ans.size();i++)
cout<<ans[i]<<" ";
}
cout<<"\n";
}
}
|
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <deque>
#include <random>
#include <climits>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef vector <ll> vec;
typedef vector <bool> vecb;
typedef vector <char> vecc;
typedef vector <pll> vecpll;
typedef vector <vec> matrix;
typedef vector <vecpll> matrixpll;
#define fast_io ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fixed_out(a) fixed << setprecision(a)
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep1(i, n) for (ll i = 1; i < n; ++i)
#define repv1(i, n) for (ll i = 1; i <= n; ++i)
#define repb(i, n) for (ll i = n - 1; i >= 0; --i)
#define rsz resize
#define sz(a) (ll)(a.size())
#define val(a) a.begin(), a.end()
#define pf push_front
#define pb push_back
#define pob pop_back
#define pof pop_front
#define f first
#define s second
#define en '\n'
const ld PI = 3.14159265358979323846;
const ld EPS = 0.0000000000001;
const ll MD_7 = 1000000007;
const ll MD_9 = 1000000009;
const ll MD = 228228227;
const ll INF = INT64_MAX;
void solve() {
ll k, n, m; cin >> k >> n >> m;
vec a(n), b(m), rs;
rep(i, n) cin >> a[i];
rep(i, m) cin >> b[i];
ll fit = 0, sit = 0;
while (fit < n || sit < m) {
if (fit < n && a[fit] == 0) { ++k; rs.pb(0); ++fit; }
else if (sit < m && b[sit] == 0) { ++k; rs.pb(0); ++sit; }
else if (fit == n || (sit != m && a[fit] > b[sit])) {
if (b[sit] <= k)
rs.pb(b[sit++]);
else {
cout << -1 << en;
return;
}
}
else if (a[fit] <= k)
rs.pb(a[fit++]);
else {
cout << -1 << en;
return;
}
}
for (auto el : rs) cout << el << ' ';
cout << en;
}
int main() {
fast_io;
ll t; cin >> t;
while (t--)
solve();
return 0;
}
/**/ |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define logarr(a) for (auto i : a) cout << i << " "; cout << endl;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define getarr(a) rep(i, 0, a.size()) cin >> a[i];
const int INF = 1e9+7;
struct fastIO {
fastIO() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(12);
}
} fio;
template <typename... T>
void read(T&... args) {
((cin >> args), ...);
}
void test_case()
{
int k, n, m;
read(k, n, m);
vector<int> a(n), b(m), ans;
getarr(a);
getarr(b);
int i = 0, j = 0;
while(i < n && j < m) {
if (a[i] == 0) {
ans.push_back(a[i]);
i++;
k++;
continue;
}
if (b[j] == 0) {
ans.push_back(b[j]);
j++;
k++;
continue;
}
if (a[i] > k && b[j] > k) {
cout << "-1\n";
return;
}
if (a[i] > k) {
ans.push_back(b[j]);
j++;
continue;
}
if (b[j] > k) {
ans.push_back(a[i]);
i++;
continue;
}
ans.push_back(a[i]);
i++;
}
rep(ind,i,n) {
if (a[ind] == 0) k++;
if (a[ind] > k) {
cout << "-1\n";
return;
}
ans.push_back(a[ind]);
}
rep(ind,j,m) {
if (b[ind] == 0) k++;
if (b[ind] > k) {
cout << "-1\n";
return;
}
ans.push_back(b[ind]);
}
logarr(ans);
}
int32_t main()
{
int T = 1;
cin >> T;
while(T--) {
test_case();
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false)
#define maxn 5005
int a[maxn],b[maxn];
int main()
{
IOS;
int t;
cin>>t;
while(t--)
{
int k,n,m;
cin>>k>>n>>m;
for(int i=1; i<=n; i++)
cin>>a[i];
for(int i=1; i<=m; i++)
cin>>b[i];
vector<int>V;
int pos1=1,pos2=1;
while(pos1<=n||pos2<=m)
{
if(pos1<=n && pos2<=m)
{
if(a[pos1]==0)
{
V.push_back(0);
pos1++;
}
else if(b[pos2]==0)
{
V.push_back(0);
pos2++;
}
else if(a[pos1]<b[pos2])
{
V.push_back(a[pos1]);
pos1++;
}
else
{
V.push_back(b[pos2]);
pos2++;
}
}
else if(pos1<=n && pos2>m)
{
V.push_back(a[pos1]);
pos1++;
}
else
{
V.push_back(b[pos2]);
pos2++;
}
}
bool flag=true;
for(int i=0; i<V.size(); i++)
{
if(V[i]==0)
k++;
else
{
if(V[i]>k)
{
flag=false;
break;
}
}
}
if(flag)
{
for(int i=0;i<V.size();i++)
cout<<V[i]<<" ";
}
else
cout<<-1;
cout<<"\n";
}
return 0;
}
|
#include<bits/stdc++.h>
#define lint long long int
using namespace std;
int main()
{
int t;
scanf("%d",&t);
for(int lops = 0;lops<t;lops++)
{
int k,n,m;
scanf("%d%d%d",&k,&n,&m);
vector<int> A;
vector<int> B;
for(int i = 0;i<n;i++)
{
int a;
scanf("%d",&a);
A.push_back(a);
}
for(int i = 0;i<m;i++)
{
int a;
scanf("%d",&a);
B.push_back(a);
}
vector<int> P;
int flag = 0;
int limit = k;
int i = 0,j = 0;
while(i<n && j<m)
{
if(A[i]<B[j])
{
if(A[i]==0) limit++;
if(A[i]>limit)
{
flag = 1;
break;
}
P.push_back(A[i]);
i++;
continue;
}
else
{
if(B[j]==0) limit++;
if(B[j]>limit)
{
flag = 1;
break;
}
P.push_back(B[j]);
j++;
continue;
}
}
while(i<n)
{
if(A[i]==0) limit++;
if(A[i]>limit)
{
flag = 1;
break;
}
P.push_back(A[i]);
i++;
}
while(j<m)
{
if(B[j]==0) limit++;
if(B[j]>limit)
{
flag = 1;
break;
}
P.push_back(B[j]);
j++;
}
if(flag) printf("-1\n");
else
{
for(int l = 0;l<m+n;l++)
{
printf("%d ",P[l]);
}
printf("\n");
}
}
return 0;
}
|
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void solve(int k, int n, int m, vector<int> M, vector<int> P){
int i=0,j=0;
vector<int> ans;
while(true){
if(i==n && j==m){
// done
for(auto a: ans){
cout << a << " ";
}
cout << endl;
return;
}
if(i<n && M[i] == 0){
ans.push_back(0);
i++;
k++;
}
else if(j<m && P[j]==0){
ans.push_back(0);
j++;
k++;
}
else if( i<n && M[i] <= k){
ans.push_back(M[i]);
i++;
}
else if(j<m && P[j] <=k){
ans.push_back(P[j]);
j++;
}
else{
cout << -1 << endl;
return;
}
}
}
int main(){
int t;
cin>>t;
int k,n,m;
string temp;
while(t--){
getline(cin, temp);
scanf("%d %d %d",&k, &n, &m);
vector<int> M(n,0);
vector<int> P(m,0);
int temp=0;
for(int i=0;i<n;i++){
cin>>temp;
M[i] = temp;
}
for(int i=0;i<m;i++){
cin>>temp;
P[i] = temp;
}
// for(auto a: M){
// cout << a << " ";
// }
// cout << endl;
// for(auto b: P){
// cout << b << " ";
// }
// cout << endl;
solve(k,n,m,M,P);
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
void solve()
{
int k,n,m;
cin >> k >> n >> m;
vector<int> a(n);
for(auto &i:a)
{
cin >> i;
}
vector<int> b(m);
for(auto &i:b)
{
cin >> i;
}
vector<int> res(n+m);
int i=0,j=0,ind=0;
while(i!=n && j!=m)
{
if(a[i]==0)
{
res[ind]=0;
ind++;
i++;
k++;
}
else if(b[j]==0)
{
res[ind]=0;
ind++;
j++;
k++;
}
else if(a[i]<=b[j] && a[i]<=k)
{
res[ind]=a[i];
ind++;
i++;
}
else if(a[i]>=b[j] && b[j]<=k)
{
res[ind]=b[j];
ind++;
j++;
}
else
{
cout << -1 << endl;
return;
}
}
while(i!=n)
{
if(a[i]==0)
{
res[ind]=0;
ind++;
i++;
k++;
}
else{
if(a[i]>k)
{
cout << -1 << endl;
return;
}
res[ind]=a[i];
ind++;
i++;
}
}
while(j!=m)
{
if(b[j]==0)
{
res[ind]=0;
ind++;
j++;
k++;
}
else{
if(b[j]>k)
{
cout << -1 << endl;
return;
}
res[ind]=b[j];
ind++;
j++;}
}
for(auto x:res)
cout << x << " ";
cout << endl;
}
int main()
{
int t;
cin >> t;
while(t--)
{
solve();
}
}
|
/* AUTHOR
(_|_)
\_/
_______________________ÃMIT SÃRKER KISHÕR_______________________
----------------------------------------------------------------
_____DEPARTMENT OF CSE, CITY UNIVERSITY, DHAKA, BANGLADESH______
----------------------------------------------------------------
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ci std ::cin
#define co std ::cout
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sl(n) scanf("%lld",&n)
#define sll(n,m) scanf("%lld%lld",&n,&m)
#define pd(n) printf("%d",n)
#define pdd(n,m) printf("%d%d",n,m)
#define pl(n) printf("%lld",n)
#define pll(n,m) printf("%lld%lld",n,m)
#define p_line printf("\n")
#define line cout<<"\n"
#define cas(n) printf("Case %d: ",n++)
#define task return
#define loop(x,n) for(int x = 0 ; x < n ; x++)
#define constloop(x,a,n) for(int x = a ; x < n ; x++)
#define revloop(x,a,n) for(int x = a ; x >= n ; x--)
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define F first
#define S second
#define pb push_back
#define mkp make_pair
#define pi acos(-1)
const ll mx=100000;
const ll mod=1e9+7;
/*
ll P[mx+5],cnt=1;
bool nP[mx+5];
void sieve(void)
{
P[0]=2;
nP[0]=true;
nP[1]=true;
for(ll i=3 ; i<=mx ; i+=2)
{
if(nP[i]==true)
continue;
for(ll j=i+i ; j<=mx ; j+=i)
nP[j]=true;
P[cnt++]=i;
}
}
*/
/*
ll bigMod(ll a, ll b)
{
if(b<=0)
{
return 1;
}
ll value=bigMod(a,b/2);
value=(value*value)%mod;
if(b%2==1)
value=(value*a)%mod;
return value;
}
*/
/*
struct st
{
int value;
int pos;
};
*/
/*
bool cmp(st n,st m)
{
return n.a>m.a;
}
*/
/*
bool cmp(int n,int m)
{
return m>n;
}
*/
void solve()
{
//Code here
int t=1;
ci>>t;
while(t--)
{
int k,n,m,a[105],b[105];
cin>>k>>n>>m;
for(int i=0 ; i<n ; i++)
cin>>a[i];
for(int j=0 ; j<m ; j++)
{
cin>>b[j];
}
int i=0,j=0,ans[500],ck=1,pos=0;
while(i<n || j<m)
{
if(i<n && a[i]==0)
{
ans[pos]=0;
i++,pos++;
k++;
}
else if(j<m && b[j]==0)
{
ans[pos]=0;
j++,pos++;
k++;
}
else
{
if(i<n && a[i]<=k)
{
ans[pos]=a[i];
i++,pos++;
}
else if(j<m && b[j]<=k)
{
ans[pos]=b[j];
j++,pos++;
}
else
{
ck=0;
break;
}
}
}
if(ck)
{
for(int ii=0 ; ii<pos ; ii++)
{
cout<<ans[ii]<<" ";
}
}
else
cout<<-1;
line;
}
}
void I_O()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
I_O();
solve();
//line;
return 0;
} |
#include <bits/stdc++.h>
#include <cstdio>
#define FAST_IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define pb push_back
#define all(x) begin(x), end(x)
#define umap unordered_map
#define TEST_CASES int a; cin >> a; for (int i = 0; i < a; i++) {solve(); cout << endl;}
using namespace std;
typedef long long ll;
typedef long double ld;
template<typename T>
int binary_search(vector<T> &vec, T n) {
int l = vec.size();
int left = 0;
int right = l - 1;
while (left != right) {
int mid = (left + right + 1) / 2;
if (vec[mid] > n) right = mid - 1;
else left = mid;
}
return vec[left] == n ? left : -1;
}
ll bs_lowest(const function<bool(ll)> &f, ll lo, ll hi) {
while (lo < hi) {
ll m = (lo+hi)/2;
f(m) ? hi = m : lo = m+1;
}
return lo;
}
ll bs_highest(const function<bool(ll)> &f, ll lo, ll hi) {
while (lo < hi) {
ll m = (lo+hi+1)/2;
f(m) ? lo = m : hi = m-1;
}
return lo;
}
vector<string> split(const string& str, const string& delim) {
vector<string> tokens;
size_t prev = 0, pos = 0;
do {
pos = str.find(delim, prev);
if (pos == string::npos) pos = str.length();
string token = str.substr(prev, pos-prev);
if (!token.empty()) tokens.push_back(token);
prev = pos + delim.length();
}
while (pos < str.length() && prev < str.length());
return tokens;
}
struct cow {
int l, r;
};
bool cmp(const cow&x, const cow&y){
if (x.r != y.r){
return x.r < y.r;
}
return x.l < y.l;
}
void solve() {
int b, c, d; cin >> b >> c >> d;
vector<int> vec(c);
vector<int> vect(d);
for (int i = 0; i < c; i++){
cin >> vec[i];
}
for (int i = 0; i < d; i++){
cin >> vect[i];
}
int first = 0; int second = 0;
vector<int> ans;
while (first < c && second < d){
if (vec[first] == 0){
b++; ans.pb(vec[first]); first++;
}
else if (vect[second] == 0){
b++; ans.pb(vect[second]); second++;
}
else if (vec[first] <= b){
ans.pb(vec[first]); first++;
}
else if (vect[second] <= b){
ans.pb(vect[second]); second++;
}
else{
cout << -1; return;
}
}
if (first < c){
for (int i = first; i < c; i++){
if (vec[i] == 0){
b++; ans.pb(vec[i]);
}
else if (vec[i] <= b){
ans.pb(vec[i]);
}
else{
cout << -1; return;
}
}
}
if (second < d){
for (int i = second; i < d; i++){
if (vect[i] == 0){
b++; ans.pb(vect[i]);
}
else if (vect[i] <= b){
ans.pb(vect[i]);
}
else{
cout << -1; return;
}
}
}
for (int i = 0; i < ans.size(); i++){
cout << ans[i] << " ";
}
}
int main() {
FAST_IO;
//freopen("helpcross.in", "r", stdin);
//freopen("helpcross.out", "w", stdout);
TEST_CASES;
//solve(); cout << endl;
} |
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
ll t,ans,k,n,m;
ll a[505];
ll b[505];
ll c[1005];
int main()
{
cin>>t;
while(t--)
{
bool f=true;
cin>>k>>n>>m;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=m;i++)
cin>>b[i];
int x=1,y=1;
for(int i=1;i<=n+m;i++)
{
if(k>=a[x]&&x<=n)
{
if(a[x]==0) k++;
c[i]=a[x];
x++;
continue;
}
if(k>=b[y]&&y<=m)
{
if(b[y]==0) k++;
c[i]=b[y];
y++;
continue;
}
f=false;
break;
}
if(f==true)
{
for(int i=1;i<=n+m;i++)
{
cout<<c[i]<<' ';
}
cout<<endl;
}
else
{
cout<<-1<<endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
#define l long
#define ll long long
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define M pow(10,9)+7
#define endl "\n"
#define rep(i,a,n) for(int i = a;i<n;i++)
#define REP(i,k,n) for (int i = k; i <= n; ++i)
#define repr(i,k,n) for (int i = k; i >= n; --i)
#define each(i,v) for(auto i:v)
using namespace std;
void solve(){
int k,n,m;
cin >> k >> n >> m;
int a[n],b[m],ans[n+m];
rep(i,0,n) cin >> a[i];
rep(i,0,m) cin >> b[i];
int i=0,j=0,p=0;
bool f=true;
while(i<n && j<m){
if(a[i] > k && b[j] > k){
f=false;break;
}
if(a[i]==0 || b[j]==0){
if(a[i]==0)
{
ans[p++]=a[i];
i++;
}
else{
ans[p++]=b[j];
j++;
}
k++;
}
else if(a[i] <= k){
ans[p++]=a[i];
i++;
}
else if(b[j] <= k){
ans[p++]=b[j];
j++;
}
}
if(f){
while(i<n){
if(a[i]==0){
ans[p++]=0;
i++;
k++;
}
else if(a[i] <= k){
ans[p++]=a[i];
i++;
}
else {
f=false;break;
}
}
while(j<m){
if(b[j]==0){
ans[p++]=0;
j++;
k++;
}
else if(b[j] <= k){
ans[p++]=b[j];
j++;
}
else {
f=false;break;
}
}
}
if(!f){
cout << -1;return;
}
rep(i,0,n+m){
cout << ans[i] << " ";
}
}
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while(t--){
solve();
cout << endl;
}
cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << "\n";
return 0;
} |
// https://codeforces.com/contest/19/problem/B
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll mod = 1000000007;
#define FOR(i, s, e) for (ll i = s; i < e; i++)
#define FOr(i, s, e) for (int i = s; i >= e; i--)
#define PI acos(-1)
#define INF 1e18
void fastio()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void solve()
{
cin.ignore();
int k, n, m;
cin >> k >> n >> m;
int a[n], b[m];
vector<int> ans;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
for (int i = 0; i < m; i++)
{
cin >> b[i];
}
int p = 0, q = 0;
for (int i = 0; i < n + m; i++)
{
if (p < n && a[p] == 0)
{
ans.push_back(0);
p++;
k++;
}
else if (q < m && b[q] == 0)
{
ans.push_back(0);
q++;
k++;
}
else if (q < m && k >= b[q])
{
ans.push_back(b[q]);
q++;
}
else if (p < n && k >= a[p])
{
ans.push_back(a[p]);
p++;
}
else
{
cout << -1 << '\n';
return;
}
}
for (int i = 0; i < n + m; i++)
{
cout << ans[i] << ' ';
}
cout << '\n';
}
int main()
{
fastio();
int t = 1; cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define ll long long
#define ld long double
#define fori(i,a,n,p) for(int i=a;i<n;i+=p)
#define forl(i,a,n,p) for(ll i=a;i<n;i+=p)
#define f first
#define s second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define PI 3.14159265358979323846
#define veci vector<int>
#define vecl vector<long long>
#define vecs vector<string>
#define vec2d vector<pair<long long,long long>>
void solve(){
int k,n,m;cin>>k>>n>>m;
int a[n],b[m];
fori(i,0,n,1){
cin>>a[i];
}fori(i,0,m,1){
cin>>b[i];
}
veci v;
int x=0,y=0;
while (x!=n || y!=m){
if (x!=n && a[x]==0){
v.pb(0);
k++;
x++;
}else if(y!=m && b[y]==0){
v.pb(0);
k++;
y++;
}else if(x!=n && a[x]<=k){
v.pb(a[x]);
x++;
}else if(y!=m && b[y]<=k){
v.pb(b[y]);
y++;
}else{
cout<<"-1\n";
return;
}
}
fori(i,0,v.size(),1){
cout<<v[i]<<" ";
}cout<<endl;
}
int main(){
int t;cin>>t;
fori(m,0,t,1){
solve();
}return 0;
} |
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define endl '\n'
int dx[] = { 0, -1, 0, 1, -1, 1, -1, 1 };
int dy[] = { -1, 0, 1, 0, 1, -1, -1, 1 };
const int N = 2e5+ 5, mod = 1e9 + 7;
using namespace std;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t; cin>>t;
while(t--)
{
int k, n, m; cin>>k>>n>>m;
int arr[n + 5], brr[m + 5];
for(int i = 0; i < n; i++) cin>>arr[i];
for(int i = 0; i < m; i++) cin>>brr[i];
int i = 0, j = 0;
vector<int> vec;
while(i < n || j < m)
{
bool change = false;
if(i < n)
{
if(!arr[i]) i++, k++, change = true, vec.push_back(arr[i - 1]);
else if(arr[i] <= k) i++, change = true, vec.push_back(arr[i - 1]);
}
if(j < m && !change)
{
if(!brr[j]) j++, k++, change = true, vec.push_back(brr[j - 1]);
else if(brr[j] <= k) j++, change = true, vec.push_back(brr[j - 1]);
}
if(!change)
{
cout<<-1<<endl;
goto done;
}
}
for(auto it : vec) cout<<it<<' ';
cout<<endl;
done:;
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
ll MOD=1000000007;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin>>t;
while(t--) {
ll k,n,m,f=1;
cin>>k>>n>>m;
ll a[n],b[m],c[n+m];
ll max1=0,c0=0;
for(auto &y:a){cin>>y;}
for(auto &y:b){cin>>y;}
ll i=0,j=0,p=0;
while(f && p<n+m){
if(i<n && j<m && a[i]>k && b[j]>k)f=0;
if((i==n && j<m && b[j]>k) or (j==m && i<n && a[i]>k))f=0;
while(i<n && a[i] <= k){if(!a[i])k++;c[p++]=a[i++];}
while(j<m && b[j] <= k){if(!b[j])k++;c[p++]=b[j++];}
if(p<n+m && a[i]>k && b[j]>k )f=0;
}
// for(auto y:c)cout<<y<<" ";
if(f==0)cout<<-1;
else for(auto y:c)cout<<y<<" ";
cout<<endl;
}
}
|
#include <iostream>
#include <vector>
using namespace std;
void solve() {
int k, n, m;
cin >> k >> n >> m;
vector<int> v1, v2;
for (int i = 0; i < n; i++) {
int p;
cin >> p;
v1.push_back(p);
}
for (int i = 0; i < m; i++) {
int p;
cin >> p;
v2.push_back(p);
}
vector<int> ans;
int a = 0, b = 0;
for (int i = 0; i < n + m; i++) {
if (a < n && v1[a] == 0) {
ans.push_back(v1[a]);
k++;
a++;
continue;
}
if (b < m && v2[b] == 0) {
ans.push_back(v2[b]);
k++;
b++;
continue;
}
if (a < n && b < m) {
if (min(v1[a], v2[b]) > k) {
cout << -1 << endl;
return;
}
if (v1[a] < v2[b]) {
ans.push_back(v1[a]);
a++;
continue;
}
else {
ans.push_back(v2[b]);
b++;
continue;
}
}
if (a < v1.size()) {
if(v1[a]>k) {
cout << -1 << endl;
return;
}
ans.push_back(v1[a]);
a++;
continue;
}
else {
if (v2[b] > k) {
cout << -1 << endl;
return;
}
ans.push_back(v2[b]);
b++;
continue;
}
}
for (auto i : ans) {
cout << i << ' ';
}
cout << endl;
}
int main()
{
int t;
cin >> t;
while (t--) {
solve();
}
} |
#include<bits/stdc++.h>
using namespace std;
int T,n,m,flag,k,a[110],b[110],ans[300];
int main()
{
scanf("%d",&T);
while (T--) {
scanf("%d%d%d",&k,&n,&m);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
for (int i=1;i<=m;i++) scanf("%d",&b[i]);
flag=1;
for (int i=1,j=1;i<=n||j<=m;) {
if ((a[i]>k||i>n)&&(b[j]>k||j>m)) {
flag=0;
break;
}
while (a[i]<=k&&i<=n) {
ans[i+j-1]=a[i];
if (a[i]==0) k++;
i++;
}
while (b[j]<=k&&j<=m) {
ans[i+j-1]=b[j];
if (b[j]==0) k++;
j++;
}
}
if (flag) {
for (int i=1;i<=n+m;i++) printf("%d ",ans[i]);
printf("\n");
} else {
printf("-1\n");
}
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int main() {
int q; cin >> q;
while (q--) {
int k, n, m; cin >> k >> n >> m;
vector<int>mono;
vector<int>poly;
vector<int>res;
int maxl = 0;
int al = 0;
int i = 0, j = 0;
while (i < n) {
int a; cin >> a;
mono.push_back(a);
if (a == 0)
al++;
if (a > maxl)
maxl = a;
i++;
}
i = 0, j = 0;
while (j < m) {
int a; cin >> a;
if (a == 0)
al++;
if (a > maxl)
maxl = a;
if (i < n && a >= mono[i]) {
while (i < n && mono[i] <= a) {
res.push_back(mono[i]);
i++;
}
}
res.push_back(a);
j++;
}
while (i < n) {
res.push_back(mono[i]);
i++;
}
if (maxl <= k + al) {
i = 0;
string s = "";
while (i < res.size()) {
s += to_string(res[i]) + " ";
if (res[i] == 0)
k++;
else if (res[i] > k) {
cout << -1 << endl;
break;
}
i++;
}
if(i==res.size())
cout << s << endl;
}
else {
cout << -1 << endl;
}
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ld long double
#define pb push_back
#define ppb pop_back
#define ss second
#define ff first
#define pii pair<int,int>
#define pll pair<ll,ll>
#define dbg(x) cout<<(#x)<<"= {"<<x<<"}\n";
#define sz(s) (int)s.size()
#define all(v) v.begin(),v.end()
#define vi vector<int>
#define vpi vector<pii>
#define mp make_pair
//Constants
int mod= 1e9+7;
int modd=998244353;
const int MAX= 2e5+5;
const int inf= 1e9;
#define start_time() auto start = chrono::high_resolution_clock::now()
#define stop_time() auto stop = chrono::high_resolution_clock::now();
#define exec_time() auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);cout << "time:"<<duration.count()/1e6 << " seconds" << endl
//*****Functions******//
ll bipow(ll a,ll b)
{ll ans=1;
while(b)
{if(b&1)ans=(ans*1LL*a)%mod;a=(a*1LL*a)%mod;b>>=1;}
return ans;
}
bool isPrime(int x)
{
if(x==1)return 0;
else if(x==2 || x==3)return 1;
if(x%2==0 ||x%3==0)return 0;
for(int i=5;i*i<=x;i+=6){if(x%i==0 || x%(i+2)==0)return 0;}
return 1;
}
//****Modular operations
inline ll add(ll x,ll y){return (x%mod+y%mod)%mod;}
inline ll sub(ll x,ll y){
if(x<0)x+=mod;if(y<0)y+=mod;
return (x%mod-y%mod + mod)%mod;}
inline ll mul(ll x,ll y){return ((x%mod)*(y%mod))%mod;}
inline ll dvd(ll x,ll y){return mul(x,bipow(y,mod-2));}
//*****factorial and inverse_factorial*****//
/*
vector<int>fac(MAX),inv_fac(MAX);
void Factorials()
{ inv_fac[0]=inv_fac[1]=fac[0]=fac[1]=1;
for(int i=2;i<MAX;i++)fac[i]=(i*1LL*fac[i-1])%mod;
for(int i=2;i<MAX;i++)inv_fac[i]=bipow(fac[i],mod-2);
}
*/
//*****Sieve*****//
/*
int sieve[MAX];
void Sieve(){sieve[1]=1;for(int i=2;i<MAX;i++)if(sieve[i]==0){for(int j=i;j<MAX;j+=i)sieve[j]=i;}}
*/
//*****Prime Generate*****//
/*
vector<int>primes;
void PrimeGen(){bool vis[MAX]={0};for(int i=2;i<MAX;i++)if(!vis[i]){primes.pb(i);for(int j=i;j<MAX;j+=i)vis[j]=1;}}
*/
//vector<vector<int>>pf(MAX);
//*****DSU*****//
/*
int g(int x,vector<int>&p){return p[x]=p[x]==x?x:g(p[x],p);}
void unn(int a,int b,vector<int>&p,vector<int>&r)
{
a=get(a,p),b=get(b,p);
if(r[a]==r[b])r[a]++;
if(r[b]>r[a])swap(a,b);
p[b]=a;
}
*/
void solve()
{
int k,n,m;cin>>k>>n>>m;
int a[n],b[m];
for(int &x:a)cin>>x;
for(int &x:b)cin>>x;
int i=0,j=0;
int cur=k;
vector<int>ans;
while(i<n && j<m)
{
if(a[i]==0){ans.pb(0);cur++;i++;}
else if(b[j]==0){ans.pb(0);cur++;j++;}
else if(a[i]<=cur){ans.pb(a[i]);i++;}
else if(b[j]<=cur){ans.pb(b[j]);j++;}
else {cout<<-1<<endl;return;}
}
while(i<n){if(a[i]==0)cur++,ans.pb(0);
else if(a[i]<=cur)ans.pb(a[i]);
else {cout<<-1<<endl;return;}
i++;
}
while(j<m){if(b[j]==0)cur++,ans.pb(0);
else if(b[j]<=cur)ans.pb(b[j]);
else {cout<<-1<<endl;return;}
j++;
}
for(int &x:ans)cout<<x<<" ";cout<<endl;
}
int main()
{ //Factorials();
//Sieve();
//PrimeGen();
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//cout<<setprecision(15)<<fixed;
int tc=1;
cin>>tc;
while(tc--)
{
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define int64 int64_t
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define pb push_back
#define str string
#define ri(x) int x;cin>>x;
#define rl(x) ll x; cin>>x;
#define rs(x) str x; cin>>x;
#define rd(x) d x; cin>>x;
#define w(x) cout<<x
#define vec(x) std::vector<x>
#define nl '\n'
#define all(x) x.begin(),x.end()
#define debug(x) for(auto y : x) {cout<<y<<" ";} cout<<nl;
#define PI 3.14159265358979323846264338327950L
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define vi vector<int>
const unsigned int M = 1000000007;
// int n,m,k,q;
const int mxN=2e5;
const int N = 100031;
bool bad = false;
// Target 4-problems
int dirs[5] = {-1,0,1,0,-1};
bool dp[2][27];
bool dfs(string s,char lastv){
int n = s.size();
char first = s[0], last = s[n-1];
bool ans;
if(s.size()==1 && s[0]=='a')return true;
if(s.size()==1)return false;
char temp = lastv-1;
// cout<<temp<<nl;
if(s[n-1]==lastv){
ans = dfs(s.substr(0,n-1),temp);
}else if(s[0]==lastv){
ans = dfs(s.substr(1),temp);
}else{
return false;
}
return ans;
}
void solve(){
int k, n,m; cin>>k>>n>>m;
int tot = k;
vi a(n),b(m); rep(i,0,n){
cin>>a[i];
}
rep(i,0,m){
cin>>b[i];
}
int i=0,j=0;
// sort(all(a)); sort(all(b));
// for(auto x: a){
// cout<<x<<" ";
// }
// cout<<nl;
// for(auto x:b){
// cout<<x<<" ";
// }
// cout<<nl;
vi ans;
for(int l=0;l<n+m;l++){
if(i<n && a[i]<=tot && a[i]!=0){
// cout<<a[i]<<" ";
ans.pb(a[i++]);
}else if(j<m&&b[j]<=tot && b[j]!=0){
ans.pb(b[j++]);
}else if(i<n && a[i]==0){
tot++; i++;
ans.pb(0);
}else if(j<m && b[j]==0){
tot++; j++;
ans.pb(0);
}else {
cout<<-1<<nl;
return;
}
}
for(auto x: ans){
cout<<x<<" ";
}
cout<<nl;
}
signed main(){
IOS;
int t; cin>>t;
// int t = 1;
while(t--){
solve();
}
}
|
#include <bits/stdc++.h>
#define int long long
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
#define MOD 1000000007
#define nline '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define vi vector<int>
#define pii pair<int,int>
#define sz(v) v.size()
#define lcm(a,b) (a*b)/(__gcd(a,b))
#define deb(x) cout<<#x<<" = "<<x<<endl;
#define deb2(x,y) cout<<#x<<" = "<<x<<' '<<#y<<" = "<<y<<endl;
#define deb3(x,y,z) cout<<#x<<" = "<<x<<' '<<#y<<" = "<<y<<' '<<#z<<" = "<<z<<endl;
#define PR(a,n) cout<<#a<<" = ";for(int _=0;_<n;_++)cout<<a[_]<<' ';cout<<endl;
#define all(v) v.begin(),v.end()
#define sortv(v) sort(all(v));
#define sortrev(v) sort(all(v),greater<int>())
#define maxHeap priority_queue<int>
#define minHeap priority_queue<int, vector<int>, greater<int>>
int pow(int a, int b, int m){
int ans = 1;
while(b){
if (b&1) ans = (ans*a) % m;
b /= 2;
a = (a*a) % m;
}
return ans;
}
int SumOfDigits(int x)
{
if (x < 0)return -1;
int s = 0;
while(x)
{
s += x%10;
x /= 10;
}
return s;
}
//lower_bound = first element >= val, end otherwise
//upper_bound = first element > val, end otherwise
//remember 2 pointer , binary search , bits method
//always look for the edges i.e max value ad min value possible
inline void solve()
{
int k,n,m;
cin>>k>>n>>m;
int a[n],b[m];
int cnt1 = 0,cnt2=0;
vi x,y;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<m;i++)
{
cin>>b[i];
}
vi ans;
int i=0,j=0;
int total = k;
while(1)
{
if(sz(ans)==n+m)
break;
if(a[i]==0 && i<n)
{
ans.pb(0);
total++;
i++;
continue;
}
else if(b[j]==0 && j<m)
{
ans.pb(0);
total++;
j++;
continue;
}
if(( a[i]<b[j] && (i<n && j<m) ) || (i<n && j>=m))
{
if(total>=a[i])
{
ans.pb(a[i]);
i++;
}
else
{
cout<<-1<<nline;
return;
}
}
else if( (a[i]>=b[j] && (j<m && i<n)) || (j<m && i>=n) )
{
if(total>=b[j])
{
ans.pb(b[j]);
j++;
}
else
{
cout<<-1<<nline;
return;
}
}
}
if(sz(ans)==n+m)
{
for(auto x:ans)
cout<<x<<' ';
cout<<nline;
}
else
cout<<-1<<nline;
return;
}
signed main()
{
boost;
int t=1;
cin>>t;
for(int tt=1;tt<=t;tt++)
{
// cout<<"Case #"<<tt<<": ";
solve();
}
return 0;
}
|
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<bitset>
#include<unordered_map>
#include<time.h>
#include<cstdlib>
#include <chrono>
#include <random>
typedef long long ll;
typedef unsigned long long ull;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#pragma comment(linker, "/STACK:36777216") //hdu 扩栈
#define mm(a) memset(a,0,sizeof(a))
#define lr rt<<1
#define rr rt<<1|1
#define sync std::ios::sync_with_stdio(false);std::cin.tie(0);
#define inf 0x3f3f3f3f
#define eqs 1e-8
#define lb(x) (x&(-x))
#define ch(a) (int(a-'a')+1)
#define rep(i, a, b) for(int i=a;i<=b;i++)
#define mkp(a, b) make_pair(a,b)
#define re register
#define umap(a) (a).reserve(1024);(a).max_load_factor(0.25);
using namespace std;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<pii,int> piii;
//const double pi=acos(-1);
const int maxn=500005;
//const ll Mod=1000000007;
const ll Mod=998244353;
int a[maxn],b[maxn],ans[maxn];
int main()
{
sync;
int t;
cin>>t;
while(t--)
{
int n,m,k;
cin>>k>>n>>m;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=m;i++)
cin>>b[i];
int La=1,Lb=1;
int cnt=0;
while(La<=n&&Lb<=m)
{
if(a[La]<b[Lb])
{
ans[++cnt]=a[La];
La++;
}
else
{
ans[++cnt]=b[Lb];
Lb++;
}
}
while(La<=n)
{
ans[++cnt]=a[La];
La++;
}
while(Lb<=m)
{
ans[++cnt]=b[Lb];
Lb++;
}
int f=0;
for(int i=1;i<=cnt;i++)
{
if(ans[i]==0)
k++;
else
{
if(ans[i]>k)
{
f=1;
break;
}
}
}
//
// cout<<t<<' ';
// for(int i=1;i<=cnt;i++)
// cout<<ans[i]<<' ';
// cout<<endl;
if(f)
cout<<-1<<endl;
else
{
for(int i=1;i<=cnt;i++)
cout<<ans[i]<<' ';
cout<<endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define MAXN (int)(1e5 + 5)
#define MAXL 20
#define F first
#define S second
#define endl "\n"
#define MOD (lli)(1e9 + 9)
#define MOD2 (lli)(1e9 + 7)
#define lli long long int
#define sz(a) int(a.size())
#define DEBUG if (0) cout << "aqui" << endl;
#define PI 2 * acos(0.0)
typedef pair<int, lli> ii;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int dddx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dddy[] = {0, 0, 1, -1, 1, -1, 1, -1};
int t;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> t;
while(t--) {
int k, n, m;
cin >> k >> n >> m;
vector<int> vec, vec2;
for(int i=0;i<n;i++) {
int a;
cin >> a;
vec.push_back(a);
}
for(int i=0;i<m;i++) {
int a;
cin >> a;
vec2.push_back(a);
}
vector<int> ans;
int a = 0, b = 0;
bool nono = false;
while(a < sz(vec) or b < sz(vec2)) {
bool at = false;
if(b < sz(vec2)) {
if(!vec2[b] or (a < sz(vec) && vec2[b] < vec[a])) at = true;
}
if(!at && a < sz(vec)) {
ans.push_back(vec[a]);
a++;
} else if(b < sz(vec2)) {
ans.push_back(vec2[b]);
b++;
}
}
int atual = k;
for(auto i : ans) {
if(!i) atual++;
else if(i > atual) nono = true;
}
if(nono) {
cout << -1 << endl;
continue;
}
for(auto i : ans) cout << i << " ";
cout << endl;
}
return 0;
} |
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '"' << x << '"';}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]" << endl;}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define deb(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define deb(x...)
#endif
typedef long long int ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
#define ff first
#define ss second
#define pb push_back
#define MESSI ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define rep(i,a,b) for(ll i=a ; i<b ; ++i)
#define VectorInput(v,n) for(ll i=0;i<n;i++)cin>>v[i];
#define VectorPrint(v) for(ll i=0;i<v.size();i++)cout<<v[i]<<" ";
#define line "\n"
#define YES "YES"
#define NO "NO"
#define Variable(x) ll x;cin>>x;
const ll MOD = 1e9 + 7;
const ll maxn = 1e6 + 4;
const ll INF = 1e18;
void answer()
{
ll k,n,m;cin>>k>>n>>m;
vll one(n);VectorInput(one,n);
vll two(m);VectorInput(two,m);
vll ans;
bool fuckedup=false;
ll oi=0,ti=0;
ll c=0;
ll curr=k;
while(c<n+m)
{
ll newo=(oi<n?one[oi]:-1);
ll newt=(ti<m?two[ti]:-1);
if(newo>curr && newt>curr)
{
fuckedup=true;
break;
}
if(newo==-1 && newt==-1)break;
if(newo==-1)
{
if(newt==0)
{
ti++;c++;curr++;
ans.pb(0);
}
else
{
if(newt>curr)
{
fuckedup=true;
break;
}
else
{
ti++;c++;
ans.pb(newt);
}
}
continue;
}
if(newt==-1)
{
if(newo==0)
{
oi++;c++;curr++;
ans.pb(0);
}
else
{
if(newo>curr)
{
fuckedup=true;
break;
}
else
{
oi++;c++;
ans.pb(newo);
}
}
continue;
}
if(newo==0)
{
oi++;c++;curr++;
ans.pb(0);
}
if(newt==0)
{
ti++;c++;curr++;
ans.pb(0);
}
if(newo<=curr && newo!=0 && newo!=-1)
{
oi++;c++;
ans.pb(newo);
}
if(newt<=curr && newt!=0 && newo!=-1)
{
ti++;c++;
ans.pb(newt);
}
}
if(fuckedup==true || ans.size()!=n+m){cout<<-1<<line;return;}
VectorPrint(ans);cout<<line;
}
int main()
{
MESSI
int t;cin>>t;
while(t--)answer();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector <int> ve;
#define int ll
#define pb push_back
#define f first
#define s second
#define in(a) ll a;scanf(" %lld",&a)
#define out(a) printf("%lld ",a);
#define cs(name) set <ll> name
#define cm(name) multiset <type> name
#define lpi(n) for(i=0;i<n;i++)
#define NO printf("NO")
#define YES printf("YES")
#define pow bpow
#define gcd(a,b) __gcd(a,b)
#define endl '\n'
#define ret return
#define cont continue
#define br break
void solve();
signed main(){
in(t);
while(t--) solve(),cout<<endl; ret 0;
}
ull bpow(ull a,ull b){
if(b==0) return 1;
else if(b%2) return a*bpow(a,b/2)*bpow(a,b/2);
else return bpow(a,b/2)*bpow(a,b/2);
}
void solve()
{
ll a,b,c; cin>>a>>b>>c;
ll i=0,j=0,ct=0,flag=1;
ll x[b],y[c],z[b+c];
lpi(b) cin>>x[i];
lpi(c) cin>>y[i];
i=0,j=0;
while(i<b && j<c)
{
if(x[i]==0) a++,z[ct]=x[i],i++;
else if(y[j]==0) a++,z[ct]=y[j],j++;
else if(x[i]<=y[j] && a>=x[i]) z[ct]=x[i],i++;
else if(y[j]<x[i] && a>=y[j]) z[ct]=y[j],j++;
else
{
flag=0;
break;
}
ct++;
}
if(flag==0) {cout<<-1;return;}
while(i<b)
{
if(x[i]==0) z[ct]=x[i],a++,i++,ct++;
else if(x[i]<=a) z[ct]=x[i],i++,ct++;
else
{
flag=0;
break;
}
}
if(flag==0) {cout<<-1;return;}
while(j<c)
{
if(y[j]==0) z[ct]=y[j],a++,j++,ct++;
else if(y[j]<=a) z[ct]=y[j],j++,ct++;
else
{
flag=0;
break;
}
}
if(flag==0) {cout<<-1;return;}
lpi(b+c) cout<<z[i]<<' ';
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ppb pop_back
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define endl "\n"
#define fast std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD 1000000007
void solve()
{
int k,n,m;
cin >> k >> n >> m;
int a[n], b[m];
int cnt = 0;
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
for(int j = 0; j < m; j++)
{
cin >> b[j];
}
int i = 0, j = 0;
vector<int>ans;
int flag = 0;
while(i < n || j < m)
{
if(i != n && a[i] == 0)
{
ans.pb(a[i]);
k++;
i++;
continue;
}
if(j != m && b[j] == 0)
{
ans.pb(b[j]);
k++;
j++;
continue;
}
if(i != n && a[i] <= k)
{
ans.pb(a[i]);
i++;
continue;
}
if(j != m && b[j] <= k)
{
ans.pb(b[j]);
j++;
continue;
}
flag = 1;
break;
}
if(flag == 1)
cout << "-1" << endl;
else
{
for(auto it:ans)
cout << it << " ";
cout << endl;
}
}
signed main()
{
fast;
int t=1;
cin >> t;
while(t--)
{
solve();
}
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define all(v) v.begin(),v.end()
const int mod = 1e9 + 7;
bool solved[305][305], dp[305][305];
int parent[305][305];
bool recurse(vector<int>& a, vector<int>& b, int pagecount, int i, int j, int n, int m) {
if (i == n && j == m)
return true;
if (solved[i][j])
return dp[i][j];
solved[i][j] = true;
bool ans = false;
if (i < n && (a[i] == 0 || pagecount >= a[i])) {
parent[i + 1][j] = 1;
ans |= recurse(a, b, pagecount + (a[i] == 0), i + 1, j, n, m);
}
if (!ans && j < m && (b[j] == 0 || pagecount >= b[j])) {
parent[i][j + 1] = 2;
ans |= recurse(a, b, pagecount + (b[j] == 0), i, j + 1, n, m);
}
return ans;
}
int main (){
fast_io;
int t;
cin >> t;
while (t--) {
memset(solved, false, sizeof(solved));
memset(parent, 0, sizeof(parent));
int k, n, m;
cin >> k >> n >> m;
vector<int> a(n), b(m);
for (int i = 0; i < n; i++)
cin >> a[i];
for (int j = 0; j < m; j++)
cin >> b[j];
bool poss = recurse(a, b, k, 0, 0, n, m);
if (poss == false) {
cout << - 1 << endl;
continue;
}
vector<int> ans;
int i = n, j = m;
while (i > 0 || j > 0) {
if (parent[i][j] == 1) {
ans.push_back(a.back());
a.pop_back();
i--;
}
else {
ans.push_back(b.back());
b.pop_back();
j--;
}
}
reverse(all(ans));
for (auto it: ans)
cout << it << ' ';
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define forn(i,n) for(ll i = 0; i < n; i++)
typedef long long ll;
const int MOD = 1e9 + 7;
//const int MOD = 998244353;
void solve() {
int k, n, m; cin >> k >> n >> m;
vector<int> a(n), b(m);
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < m; i++) cin >> b[i];
int i = 0, j = 0;
vector<int> c(n + m);
for(int l = 0; l < m + n; l++) {
if(i < n && a[i] == 0) {
i++;
k++;
} else if(j < m && b[j] == 0) {
j++;
k++;
} else {
if(i == n) {
if(j < m && b[j] <= k) {
c[l] = b[j];
j++;
} else {
cout << -1 << endl;
return;
}
} else if(j == m) {
if(i < n && a[i] <= k) {
c[l] = a[i];
i++;
} else {
cout << -1 << endl;
return;
}
}
else if (i < n && j < m && a[i] <= b[j]) {
if(i < n && a[i] <= k) {
c[l] = a[i];
i++;
} else {
cout << -1 << endl;
return;
}
} else {
if(j < m && b[j] <= k) {
c[l] = b[j];
j++;
} else {
cout << -1 << endl;
return;
}
}
}
}
if (i == n && j == m) {
for(int l = 0; l < m + n; l++)
cout << c[l] << " ";
cout << endl;
}
else {
cout << -1 << endl;
return;
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fast_cin();
int t = 1;
cin >> t;
while(t--) {
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define int long long
#define all(x) (x).begin(), (x).end()
const int MAX_N = 1e5 + 1;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int LINF = 1e18;
void solve() {
int tc = 1;
cin >> tc;
for (int t = 1; t <= tc; t++) {
int l,n,m;
cin>>l>>n>>m;
int arr[n],brr[m];
for(int i=0;i<n;i++)
cin>>arr[i];
for(int j=0;j<m;j++)
cin>>brr[j];
int cl=0;
int ans[n+m];
int ptr1=0,ptr2=0;
bool yes=true;
memset(ans,-1,sizeof(ans));
for(int i=0;i<n+m;i++)
{
if(ptr1<n&&arr[ptr1]==0){
ans[i]=arr[ptr1];
ptr1++;
l++;
continue;
}
if(ptr2<m&&brr[ptr2]==0){
ans[i]=brr[ptr2];
l++;
ptr2++;
continue;
}
if(ptr1<n&&arr[ptr1]<=l){
ans[i]=arr[ptr1];
ptr1++;
continue;
}
if(ptr2<m&&brr[ptr2]<=l){
ans[i]=brr[ptr2];
ptr2++;
continue;
}
yes=false;
break;
}
if(yes){
for(auto i:ans) cout<<i<<" ";
}
else{
cout<<-1<<" ";
}
cout<<endl;
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
solve();
} |
#include "bits/stdc++.h"
#define ll long long
#define pb push_back
using namespace std;
void solve(){
int k, n , m; cin >> k >> n >> m;
int a[n], b[m];
for(int i=0; i<n; i++) cin >> a[i];
for(int i=0; i<m; i++) cin >> b[i];
int p1 = 0, p2 = 0;
vector<int> v;
while(p1 < n && p2 < m){
if(a[p1] == 0) { v.pb(0); p1++; k++;}
else if(b[p2] == 0){ v.pb(0); p2++; k++;}
else{
if(a[p1] > k && b[p2] > k){ cout << -1; return; }
if(a[p1] < b[p2]) { v.pb(a[p1]); p1++; }
else { v.pb(b[p2]); p2++; }
}
}
while(p1 < n){
if(a[p1] == 0) k++;
if(a[p1] > k) { cout << -1; return; }
v.pb(a[p1]); p1++;
}
while(p2 < m){
if(b[p2] == 0) k++;
if(b[p2] > k) { cout << -1; return; }
v.pb(b[p2]); p2++;
}
for(auto it : v) cout << it << " ";
}
int main(){
int t; cin >> t;
while(t--){
solve();
cout << "\n";
}
} |
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t ;
cin>>t;
while(t -- )
{
int k , n, m ;
cin>>k >> n >> m ;
vector<int>a(n + 1 ) ;
vector<int>b(m + 1 ) ;
vector<int>all(m + n + 1 ) ;
for(int i = 0 ; i <n ; i ++ ) cin>>a[i] ;
for(int i = 0 ; i < m ; i ++ ) cin>>b[i] ;
int i = 0 , j = 0 , ok = 1 , co = 0 ;
while(i < n || j < m )
{
if(a[i] <= k && i < n )
{
all[++co] = a[i ] ;
if(a[i] == 0 ) k ++ ;
i ++ ;
}
else if(b[j] <=k && j < m )
{
all[++co] = b[j] ;
if(b[j] == 0 ) k ++ ;
j ++ ;
}
else
{
ok = 0 ;
break;
}
}
if(ok == 0 ) cout<<"-1"<<endl;
else
{
for(int i = 1 ; i <= n + m ; i ++ ) cout<<all[i] <<" " ;
cout<<endl;
}
}
return 0 ;
} |
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define aprt(ns) for(auto n:ns)cout<<n<<' ';cout<<endl
#define prt(n) for(int i=0;i<n.size();i++)printf("%d%c",n[i],i==n.size()-1?'\n':' ');
#define for1(i,n) for(int i=0;i<n;i++)
#define all(t) t.begin(),t.end()
#define PQ priority_queue
#define pii pair<int,int>
#define pll pair<ll,ll>
#define dormir_11 ios::sync_with_stdio(0);cin.tie(0)
using namespace std;
const int mod = 1e9+7;
const int MAX = 1e6+5;
const double PI = 3.14159265358979323;
const double eps = 1e-8;
int gcd(int a, int b){return a%b==0? b: gcd(b, a%b);}
int fast_powr(int a,int b){ int ret=1;for(;b;b>>=1,a=1LL*a*a%mod)if(b&1)ret=1LL*ret*a%mod;return ret; }
int times;
int dir[4][2] = { {1,0},{0,-1},{0,1},{-1,0} };
char dirs[4]={'D','L','R','U'};
bool cmp(int a,int b){return a>b;}
void YorN(bool flag){if(flag)cout<<"YES"<<endl;else cout<<"NO"<<endl;}
int main(){
cin>>times;
while(times--){
int cs,n,m;
cin>>cs>>n>>m;
bool flag=true;
vector<int>ans(n+m);
vector<int>a(n),b(m);
for1(i,n)cin>>a[i];
for1(i,m)cin>>b[i];
int ai=0,bi=0,idx=0;
while(ai<n||bi<m){
if(ai<n&&a[ai]<=cs){
ans[idx++]=a[ai];
if(a[ai]==0)cs++;
ai++;
}else if(bi<m&&b[bi]<=cs){
ans[idx++]=b[bi];
if(b[bi]==0)cs++;
bi++;
}else{
flag=false;
break;
}
}
//cout<<idx<<endl;
if(flag){
prt(ans);
}else cout<<-1<<endl;
}
return 0;
}
/*
abcdefghijklmnopqrstuvwxyz
*/ |
#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define FASTIO cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
#define FILES freopen("in", "r", stdin); freopen("out", "w", stdout);
#define vec vector<int>
using namespace std;
void tc() {
int k, n, m;
cin >> k >> n >> m;
int a[n + 1], b[m + 1];
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = 1; i <= m; i++)
cin >> b[i];
vector <int> v(1);
int i = 1, j = 1;
while (i<=n && j <=m)
{
if (a[i] < b[j])
v.push_back(a[i++]);
else
v.push_back(b[j++]);
}
while (i <= n)
v.push_back(a[i++]);
while (j <= m)
v.push_back(b[j++]);
int cnt = k;
for(int i = 1; i < v.size(); i++)
if(v[i] != 0 && v[i] > cnt) {
cout << -1 << '\n';
return;
} else if(v[i] == 0)
cnt++;
for(int i = 1; i < v.size(); i++)
cout << v[i] << ' ';
cout << '\n';
}
signed main() {
int t;
cin >> t;
while(t--)
tc();
return 0;
} |
//#pragma GCC optimize(1)
//#pragma GCC optimize(2)
//#pragma GCC optimize(3,"Ofast","inline")
# include<iostream>
# include<algorithm>
# include<unordered_map>
# include<cmath>
# include<cstdio>
# include<set>
# include<stack>
# include<queue>
# include<map>
# include<string>
# include<cstring>
# include<limits.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int > PII;
const int mod=998244353;
const int MAX=3e6+10;
const int Time=86400;
const int X=131;
const int inf=0x3f3f3f3f;
const double PI = 1e-4;
double pai = 3.14159265358979323846;
int t,k,n,m;
int a[MAX],b[MAX];
int main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while(t--){
queue<int>q;
cin >> k >> n >> m;
for(int i = 1 ; i <= n ; i ++ ) cin >> a[i];
for(int i = 1 ; i <= m ; i ++ ) cin >> b[i];
bool flag = true;
int i = 1 , j = 1;
while( i + j != n + m + 2){
if(a[i] == 0 && i <= n){
q.push(a[i]);
i++;
k++;
continue;
}
if(b[j] == 0 && j <= m){
q.push(b[j]);
j++;
k++;
continue;
}
if(a[i] <= k && i <= n){
q.push(a[i]);
i++;
continue;
}
if(b[j] <= k && j <= m){
q.push(b[j]);
j++;
continue;
}
flag = false;
break;
}
if(flag){
while(!q.empty()){
cout<<q.front()<<" ";
q.pop() ;
}
cout<<"\n";
}
else cout<<"-1\n";
}
return 0;
}
|
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <utility>
#include <map>
#include <list>
#include <climits>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <bitset>
#include <string>
// #define cerr if(false)cerr
#define watch(x) cerr << "> " << #x << ": " << x << "\n";
using namespace std;
template <typename T>
std::ostream &operator <<(std::ostream &out, vector<T> &v) {
for (typename vector<T>::size_type i = 0; i < v.size(); ++i)
out << v[i] << " ";
out << "\n";
return out;
}
template <typename T>
std::ostream &operator <<(std::ostream &out, set<T> &s) {
for (auto e : s)
out << e << " ";
out << "\n";
return out;
}
template <typename T, typename N>
std::ostream &operator <<(std::ostream &out, pair<T, N> &p) {
out << "(" << p.first << ", " << p.second << ") ";
return out;
}
template <typename T, typename N>
std::ostream &operator <<(std::ostream &out, vector<pair<T, N> > &v) {
for (size_t i = 0; i < v.size(); ++i)
cout << v[i];
out << "\n";
return out;
}
template <typename T, typename N>
std::ostream &operator <<(std::ostream &out, set<pair<T, N> > &s) {
for (auto p : s)
out << p;
out << "\n";
return out;
}
template <typename T>
std::ostream &operator <<(std::ostream &out, vector<vector<T> > &v) {
for (size_t i = 0; i < v.size(); ++i) {
for (size_t j = 0; j < v[i].size(); ++j) {
out << v[i][j] << " ";
}
out << "\n";
}
return out;
}
template <typename T>
std::ostream &operator <<(std::ostream &out, vector<set<T> > &v) {
for (size_t i = 0; i < v.size(); ++i) {
out << v[i];
}
out << "\n";
return out;
}
void solve() {
int k, n, m;
cin >> k >> n >> m;
vector<int> a(n), b(m);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < m; ++i) {
cin >> b[i];
}
vector<int> seq;
int i = 0, j = 0;
while (i < n && j < m) {
if (a[i] > k && b[j] > k) {
cout << "-1\n";
return ;
}
if (b[j] == 0) {
++k;
++j;
seq.push_back(0);
} else {
if (b[j] <= k) {
seq.push_back(b[j]);
++j;
}
}
if (a[i] == 0) {
++k;
seq.push_back(0);
++i;
} else {
if (a[i] <= k) {
seq.push_back(a[i]);
++i;
}
}
}
while (i < n) {
if (a[i] == 0) {
++k;
seq.push_back(0);
++i;
} else {
if (a[i] <= k) {
seq.push_back(a[i]);
++i;
} else {
cout << "-1\n";
return ;
}
}
}
while (j < m) {
if (b[j] == 0) {
++k;
++j;
seq.push_back(0);
} else {
if (b[j] <= k) {
seq.push_back(b[j]);
++j;
} else {
cout << "-1\n";
return ;
}
}
}
cout << seq;
return ;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
template<class A, class B>void tostring(pair<A, B>p) { cerr<<"(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template<class A>void tostring(A v) {int f = 1;string r = "{";for (auto& x : v) {if (!f) {r += ", ";}f = 0;r += to_string(x);}r += "}\n";cerr<<r;}
void _out() { cerr << endl; }template<typename H, typename... T> void _out(H h, T... t) { cerr << ' ' << h; _out(t...); }
void show() { cerr << endl; }template<typename H, typename... T> void show(H h, T... t) { cerr<<h<<' '; show(t...); }
#define see(...) cerr << "(" << #__VA_ARGS__ << "):", _out(__VA_ARGS__)
#define rep(i, l, r) for(int i=(int)(l);i<=(int)(r);i++)
#define per(i, r, l) for(int i=(int)(r);i>=(int)(l);i--)
#define all(x) (x).begin(), (x).end()
#define all1(x) (x).begin()+1, (x).end()
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define int long long
typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp;
#define fi first
#define se second
//const int N = 1e5+100;
//const int mod=1e9+7;
void solve(int tase) {
int k, n, m;cin >> k >> n >> m;
vi a(n+2),b(m+2);
rep(i,1,n)cin >> a[i];
rep(i,1,m)cin >> b[i];
bool f = 1;
int i = 1, j = 1;
vi ans;
while(i<=n||j<=m){
while(i<=n&&a[i]==0){
i ++;
ans.pb(0);
k ++;
}
while(j<=m&&b[j]==0){
j ++;
ans.pb(0);
k ++;
}
if(i>n && j>m)break;
if(i<=n && a[i] > k && (j>m)){
f = 0;break;
}
if(j<=m && b[j] > k && (i>n)){
f = 0;break;
}
if(i<=n&&a[i]>k&&j<=m&&b[j]>k){
f = 0;break;
}
int ii = i;
rep(t,ii,n){
if((a[t]!=0) && a[t]<=k){
ans.pb(a[t]);i ++;
}
else break;
}
int jj = j;
rep(t,jj,m){
if((b[t]!=0) && b[t]<=k){
ans.pb(b[t]);j ++;
}
else break;
}
}
if(!f)cout << -1 << endl;
else {
for(auto i : ans) cout << i << ' ';
cout << endl;
}
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("E:\\in.txt", "r", stdin);
#endif
int T; cin >> T; rep(i, 1, T) solve(i);
} |
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define pb push_back
#define sv(v) sort(v.begin(), v.end())
#define rsv(v) sort(v.rbegin(), v.rend())
#define pv(v) \
for (auto x : v) \
cout << x << ' '
#define tv(v) \
for (auto &x : v) \
cin >> x
typedef long long int ll;
void solve()
{
int k, n, m;
cin >> k >> n >> m;
vi a(n), b(m), ans;
tv(a);
tv(b);
int i = 0, j = 0;
while (i < n && j < m)
{
if (a[i] == 0)
{
ans.pb(a[i]);
i++;
k++;
continue;
}
if (b[j] == 0)
{
ans.pb(b[j]);
j++;
k++;
continue;
}
if (a[i] <= k)
{
ans.pb(a[i]);
i++;
continue;
}
if (b[j] <= k)
{
ans.pb(b[j]);
j++;
continue;
}
cout << -1 << endl;
return;
}
while (i < n)
{
if (a[i] == 0)
{
ans.pb(a[i]);
i++;
k++;
continue;
}
if (a[i] <= k)
{
ans.pb(a[i]);
i++;
continue;
}
cout << -1 << endl;
return;
}
while (j < m)
{
if (b[j] == 0)
{
ans.pb(b[j]);
j++;
k++;
continue;
}
if (b[j] <= k)
{
ans.pb(b[j]);
j++;
continue;
}
cout << -1 << endl;
return;
}
pv(ans);
cout << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int testCase;
cin >> testCase;
while (testCase--)
{
solve();
}
return 0;
} |
/*
Author : Arvinder Singh
*/
#include <bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
void INPUT()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/arvindersingh/Desktop/Current/input.txt", "r", stdin);
freopen("C:/Users/arvindersingh/Desktop/Current/output.txt", "w", stdout);
#endif
}
#define ll long long
#define ar array
//#define PI 3.14159
const double PI=acos(-1);
const long long mod = 1e9+7;
#define vint vector<int>
#define vfloat vector<float>
#define pb push_back
#define fo(i,n) for(i=0;i<n;i++)
#define fok(i,k,n) for(i=k;i<n;i++)
int size=1000000;
//int a[size];
int k,n,m;
void solve() {
cin>>k>>n>>m;
vector<int> a(n),b(m);
for (int i = 0; i < n; ++i)
{
cin>>a[i];
}
for (int i = 0; i < m; ++i)
{
cin>>b[i];
}
/////////// logic
int itr1=0,itr2=0;
vector<int> ans;
while(itr1<n or itr2<m){
if(itr1 < n and a[itr1] == 0){
ans.push_back(a[itr1]);
k++;
itr1++;
}
else if(itr1 < n and a[itr1] <= k){
ans.push_back(a[itr1]);
itr1++;
}else if(itr2 < m and b[itr2] == 0){
ans.push_back(b[itr2]);
k++;
itr2++;
}else if(itr2 < m and b[itr2] <= k){
ans.push_back(b[itr2]);
itr2++;
}else{
cout<<"-1"<<endl;
return;
}
}
for(int i:ans)
cout<<i<<" ";
cout<<endl;
}
int main(int argc, char const *argv[])
{
FIO
INPUT();
ll t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll ;
#define f0(i,n) for(i=0;i<n;i++)
#define f1(i,n) for(i=1;i<n;i++)
#define fab(i,a,b) for(i=a;i<=b;i++)
#define fabr(i,a,b) for(i=b;i>=a;i--)
#define bs binary_search
#define memo(a,b) memset(a,b,sizeof(a))
#define fstl(i,v) for(auto i=v.begin();i!=v.end();i++) cout<<*i<<" "
#define lb lower_bound
#define ub upper_bound
#define sps(x,y) fixed<<setprecision(y)<<x
#define all(v) v.begin(),v.end()
#define allr(x) x.rbegin(),x.rend()
#define INF 1000000000000
void fast()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
}
int main()
{
fast();
ll pr;
cin>>pr;
while(pr--)
{
ll k,n,m,i,j,c=0,f=0;
cin>>k>>n>>m;
ll a[n],b[m];
f0(i,n)
{
cin>>a[i];
}
f0(i,m)
{
cin>>b[i];
}
vector<ll> ans;
deque<ll> a1,b1;
f0(i,n)
{
a1.push_back(a[i]);
}
f0(i,m)
{
b1.push_back(b[i]);
}
while(!(a1.empty())&&!(b1.empty()))
{
if(a1.front()==0)
{
while(a1.front()==0&&!(a1.empty()))
{
ans.push_back(a1.front());
a1.pop_front();
}
}
if(b1.front()==0)
{
while(b1.front()==0&&!(b1.empty()))
{
ans.push_back(b1.front());
b1.pop_front();
}
}
/* if(!(b1.empty()))
{
ans.push_back(b1.front());
b1.pop_front();
}
if(!(a1.empty()))
{
ans.push_back(a1.front());
a1.pop_front();
}*/
if(!(a1.empty())&&!(b1.empty()))
{
if(a1.front()>b1.front())
{
ans.push_back(b1.front());
b1.pop_front();
}
else
{
ans.push_back(a1.front());
a1.pop_front();
}
}
/* while(!(b1.empty()))
{
ans.push_back(b1.front());
b1.pop_front();
}
while(!(a1.empty()))
{
ans.push_back(a1.front());
a1.pop_front();
}*/
}
while(!(b1.empty()))
{
ans.push_back(b1.front());
b1.pop_front();
}
while(!(a1.empty()))
{
ans.push_back(a1.front());
a1.pop_front();
}
// fstl(pp,ans);
for(i=0;i<n+m;i++)
{
if(ans[i]==0)
{
c++;
}
else
{
if(ans[i]>c+k)
{
f=1;
break;
}
}
}
if(f==1)
{
cout<<-1<<endl;
}
else
{
fstl(pp,ans);
cout<<endl;
}
}
return 0;
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.