text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
int f[505], n, ans[1000000];
int main() {
int tt = 0, ttt = 0;
scanf("%d", &tt);
while (tt--) {
scanf("%d", &n);
ans[0] = 0;
for (int i = 1; i <= n; i++) scanf("%d", &f[i]);
for (int i = 1, j; i + 2 <= n; i++) {
j = i;
for (int k = i + 1; k <= n; k++) {
if (f[k] < f[j]) j = k;
}
for (int k = j - 2, kk; k >= i; k -= 2) {
kk = f[k + 2];
f[k + 2] = f[k + 1];
f[k + 1] = f[k];
f[k] = kk;
j -= 2;
ans[++ans[0]] = k;
}
if (i + 1 == j) {
ans[++ans[0]] = i;
ans[++ans[0]] = i;
int kk = f[i];
f[i] = f[i + 1];
f[i + 1] = f[i + 2];
f[i + 2] = kk;
}
}
bool tag = false;
if (f[n] < f[n - 1]) {
tag = true;
for (int i = n - 2, kk; i > 0; i--) {
kk = f[i + 2];
f[i + 2] = f[i + 1];
f[i + 1] = f[i];
f[i] = kk;
ans[++ans[0]] = i;
if (f[i] <= f[i + 1]) {
tag = false;
break;
}
}
}
if (tag)
puts("-1");
else {
printf("%d\n", ans[0]);
for (int i = 1; i < ans[0]; i++) {
printf("%d ", ans[i]);
}
if (ans[0] > 0)
printf("%d\n", ans[ans[0]]);
else
puts("");
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 2;
const int M = 4e6 + 2;
const int mod = 998244353;
const long long inf = 1e15;
const long long INF = 2e18;
void data() {}
int T, n, a[N], b[N];
int main() {
data();
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
int r = n;
vector<int> ans;
while (r >= 3) {
int mx = 0;
for (int i = 1; i <= r; ++i) {
if (a[mx] <= a[i]) mx = i;
}
while (mx != r) {
if (mx == r - 1) {
ans.push_back(mx - 1);
swap(a[mx + 1], a[mx - 1]);
swap(a[mx], a[mx + 1]);
mx++;
} else {
ans.push_back(mx);
swap(a[mx + 2], a[mx]);
swap(a[mx + 1], a[mx + 2]);
mx++;
}
}
r--;
}
bool f = 0;
if (a[1] <= a[2])
f = 1;
else if (a[1] == a[3]) {
f = 1;
ans.push_back(1);
ans.push_back(1);
swap(a[1 + 2], a[1]);
swap(a[1 + 1], a[1 + 2]);
swap(a[1 + 2], a[1]);
swap(a[1 + 1], a[1 + 2]);
}
if (f) {
printf("%d\n", (int)ans.size());
for (auto it : ans) printf("%d ", it);
printf("\n");
} else {
int pos = 0;
for (int i = 4; i <= n; ++i) {
if (a[i] == a[i - 1]) {
pos = i - 1;
break;
}
}
if (!pos) {
printf("-1\n");
} else {
for (int i = 1; i <= pos - 3; ++i) {
ans.push_back(i);
swap(a[i + 2], a[i]);
swap(a[i + 1], a[i + 2]);
}
pos--;
ans.push_back(pos);
swap(a[pos + 2], a[pos]);
swap(a[pos + 1], a[pos + 2]);
pos--;
ans.push_back(pos);
swap(a[pos + 2], a[pos]);
swap(a[pos + 1], a[pos + 2]);
pos += 2;
for (int i = pos - 3; i >= 1; --i) {
ans.push_back(i);
ans.push_back(i);
swap(a[i + 2], a[i]);
swap(a[i + 1], a[i + 2]);
swap(a[i + 2], a[i]);
swap(a[i + 1], a[i + 2]);
}
printf("%d\n", (int)ans.size());
for (auto it : ans) {
printf("%d ", it);
}
printf("\n");
}
}
for (int i = 1; i <= n; ++i) {
cerr << a[i] << " ";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 510;
long long a[N];
void rot1(long long& first, long long& second, long long& z) {
tie(first, second, z) = make_tuple(z, first, second);
}
void rot2(long long ind) { rot1(a[ind], a[ind + 1], a[ind + 2]); }
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
map<long long, long long> pos;
long long pos1 = 0, pos2 = 0;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
if (pos.count(a[i])) pos1 = pos[a[i]], pos2 = i;
pos[a[i]] = i;
a[i] *= N;
a[i] += i;
}
bool even = true;
for (long long i = 1; i <= n; i++)
for (long long j = i + 1; j <= n; j++)
if (a[i] > a[j]) even = !even;
if (!even)
if (pos1)
swap(a[pos1], a[pos2]);
else {
cout << -1 << "\n";
continue;
}
vector<long long> idx;
while (true) {
long long i = 1;
while (i <= n && min_element(a + i, a + n + 1) == a + i) i++;
if (i > n - 2) break;
long long j = min_element(a + i, a + n + 1) - a;
while (j - i >= 2) rot2(j - 2), idx.push_back(j - 2), j -= 2;
if (j - i == 1) rot2(i), idx.push_back(i), rot2(i), idx.push_back(i);
}
if (is_sorted(a + 1, a + n + 1)) {
cout << idx.size() << "\n";
for (long long i : idx) cout << i << " ";
cout << "\n";
} else
cout << -1 << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int T, n, mk, a[N];
vector<int> res;
void flip(int x) {
int t = a[x + 2];
a[x + 2] = a[x + 1];
a[x + 1] = a[x];
a[x] = t;
}
void solve(int x, int y) {
if (a[x] == a[x + 2]) {
flip(x);
flip(x);
res.push_back(x);
res.push_back(x);
return;
}
if (y >= n - 1) {
mk = 0;
return;
}
if (a[x + 2] == a[y + 2]) {
flip(y);
flip(x);
res.push_back(y);
res.push_back(x);
return;
}
solve(x + 1, y + 1);
flip(x);
res.push_back(x);
}
int main() {
scanf("%d", &T);
while (T--) {
res.clear();
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = n; i >= 3; i--) {
int pos = 0, imax = 0;
for (int k = 1; k <= i; k++) {
if (a[k] > imax) {
imax = a[k];
pos = k;
}
}
if (pos == i) continue;
for (int k = 0; k < i - pos - 1; k++) {
res.push_back(pos + k);
flip(pos + k);
}
flip(i - 2);
res.push_back(i - 2);
}
if (a[1] > a[2]) {
mk = 1;
solve(1, 2);
if (0 == mk) {
printf("-1\n");
continue;
}
}
printf("%d\n", res.size());
for (int i = 0; i < res.size(); i++) {
printf("%d", res[i]);
printf(i == res.size() - 1 ? "\n" : " ");
}
if (0 == res.size()) printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void culc(int& a, int& b, int& c) { swap(b, c), swap(a, b); }
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> ans;
for (int i = 0; i < n - 2; i++) {
auto it = min_element(a.begin() + i, a.end()) - a.begin();
if (i % 2 != it % 2) {
if (it < n - 1) {
culc(a[it - 1], a[it], a[it + 1]);
ans.push_back(it - 1);
it++;
} else {
culc(a[it - 2], a[it - 1], a[it]);
culc(a[it - 2], a[it - 1], a[it]);
ans.push_back(it - 2);
ans.push_back(it - 2);
it--;
}
}
while (it != i) {
culc(a[it - 2], a[it - 1], a[it]);
ans.push_back(it - 2);
it -= 2;
}
}
bool flag = true;
if (a[n - 1] < a[n - 2]) {
flag = false;
for (int i = n - 3; i >= 0; i--) {
culc(a[i], a[i + 1], a[i + 2]);
ans.push_back(i);
if (a[i] <= a[i + 1]) {
flag = true;
break;
}
}
}
if (!flag) {
cout << -1 << endl;
return;
} else {
cout << ans.size() << endl;
for (auto u : ans) cout << u + 1 << " ";
cout << endl;
}
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
c b, e;
};
template <class c>
rge<c> range(c i, c j) {
return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
template <class c>
debug& operator<<(const c&) {
return *this;
}
};
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> ans;
bool ok = false;
int cnt = 0, prev = -1;
while (cnt < n * n && !ok) {
prev = cnt;
for (int i = 0; i < n - 2; i++) {
if (a[i + 1] <= a[i] and a[i + 1] <= a[i + 2]) {
if (n * n - cnt < 2) {
break;
}
cnt += 2;
swap(a[i], a[i + 1]);
swap(a[i + 1], a[i + 2]);
ans.push_back(i);
ans.push_back(i);
} else if (a[i + 2] <= a[i] && a[i + 2] <= a[i + 1]) {
if (n * n - cnt < 1) {
break;
}
cnt += 1;
swap(a[i], a[i + 2]);
swap(a[i + 1], a[i + 2]);
ans.push_back(i);
}
}
ok = true;
for (int i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
ok = false;
break;
}
}
if (!ok && prev == cnt) {
break;
}
}
if (ok) {
cout << cnt << "\n";
for (int x : ans) {
cout << x + 1 << " ";
}
cout << "\n";
} else {
cout << "-1\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
int T;
std::cin >> T;
while (T--) {
int n;
std::cin >> n;
std::vector<int> a(n), cnt(500, 0), mem;
for (int i = 0; i < n; i++) {
std::cin >> a[i];
--a[i];
++cnt[a[i]];
}
mem = a;
for (int i = 1; i < 500; i++) {
cnt[i] += cnt[i - 1];
}
std::vector<int> b = a;
for (int i = 0; i < n; i++) b[i] = --cnt[a[i]];
a = b;
std::vector<int> answer;
auto solve = [&]() {
for (int k = 0; k < n - 2; k++) {
int mn = 1e9, pos;
for (int i = k; i < n; i++)
if (a[i] < mn) {
mn = a[i];
pos = i;
}
for (int i = pos; i > k;) {
if (i == k + 1) {
answer.push_back(k);
answer.push_back(k);
--i;
std::swap(a[i], a[i + 1]);
std::swap(a[i], a[i + 2]);
std::swap(a[i], a[i + 1]);
std::swap(a[i], a[i + 2]);
break;
} else {
answer.push_back(i - 2);
i -= 2;
std::swap(a[i], a[i + 1]);
std::swap(a[i], a[i + 2]);
}
}
}
bool flag = true;
for (int i = 1; i < n; i++)
if (a[i] < a[i - 1]) {
flag = false;
}
return flag;
};
if (solve()) {
std::cout << answer.size() << '\n';
for (auto i : answer) {
std::cout << i + 1 << ' ';
}
std::cout << '\n';
} else {
answer.clear();
cnt.assign(500, -1);
a = mem;
for (int i = 0; i < n; i++) {
if (~cnt[a[i]]) {
std::swap(b[cnt[a[i]]], b[i]);
break;
} else {
cnt[a[i]] = i;
}
}
a = b;
if (solve()) {
std::cout << answer.size() << '\n';
for (auto i : answer) {
std::cout << i + 1 << ' ';
}
std::cout << '\n';
} else {
std::cout << -1 << '\n';
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void cyc(vector<int>& v, int i) {
swap(v[i], v[i + 2]);
swap(v[i + 2], v[i + 1]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
vector<pair<int, int>> p(n);
map<int, int> kde;
pair<int, int> con = make_pair(-1, -1);
for (int i = 0; i < n; i++) {
cin >> a[i];
if (kde.count(a[i])) {
con = make_pair(kde[a[i]], i);
}
kde[a[i]] = i;
p[i] = make_pair(a[i], i);
}
sort(p.begin(), p.end());
for (int i = 0; i < n; i++) {
a[p[i].second] = i;
}
int inv = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++)
if (a[j] > a[i]) inv++;
}
if (inv % 2 == 1 && con.first > -1) swap(a[con.first], a[con.second]);
vector<int> v;
if (n > 2)
for (int i = n - 1; i >= 1; i--) {
int in = 0;
for (int j = 0; j < i + 1; j++)
if (a[j] >= a[in]) {
in = j;
}
if (in == 0) {
cyc(a, 0);
v.push_back(0);
in++;
}
for (int j = in - 1; j < i - 1; j++) {
cyc(a, j);
v.push_back(j);
}
}
vector<int> b = a;
sort(b.begin(), b.end());
if (b == a) {
cout << v.size() << endl;
for (int i : v) cout << i + 1 << " ";
cout << endl;
} else {
cout << -1 << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int T, n, m, k;
int a[555];
pair<int, int> b[555];
vector<int> ans;
inline void op(int pos) {
int t = a[pos + 2];
a[pos + 2] = a[pos + 1];
a[pos + 1] = a[pos];
a[pos] = t;
ans.push_back(pos);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> T;
while (T--) {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
b[i] = make_pair(a[i], i);
}
sort(b + 1, b + n + 1);
int same = -1, cnt = 0;
for (int i = 1; i < n; i++) {
if (b[i].first == b[i + 1].first) same = b[i].first;
for (int j = i + 1; j <= n; j++) {
if (b[i].second > b[j].second) cnt++;
}
}
if (cnt % 2 != 0 && same == -1) {
cout << -1 << endl;
continue;
}
if (cnt % 2 != 0) {
for (int i = 1; i < n; i++) {
if (b[i].first == b[i + 1].first) {
swap(b[i].second, b[i + 1].second);
break;
}
}
}
for (int i = 1; i <= n; i++) a[b[i].second] = i;
for (int i = 1; i <= n - 2; i++) {
int pos = 0;
for (int j = i; j <= n; j++) {
if (a[j] == i) {
pos = j;
break;
}
}
while (pos > i + 1) op(pos - 2), pos -= 2;
if (pos == i + 1) op(i), op(i);
}
cout << ans.size() << endl;
for (auto x : ans) cout << x << ' ';
cout << endl;
ans.clear();
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma optimise GCC(-O2)
using namespace std;
mt19937_64 rang(
chrono::high_resolution_clock::now().time_since_epoch().count());
long long rng(long long lim) {
uniform_int_distribution<long long> uid(0, lim - 1);
return uid(rang);
}
long long INF = LLONG_MAX;
const long long M = 1000000007, M2 = 998244353, M3 = 2013265921;
long long powm(long long, long long, long long);
long long modI(long long, long long);
long long PrimRoot(long long p, long long x);
void fft(vector<long long>& a, long long n, bool invert, long long m,
long long x);
void PolyMult(vector<long long>& a, vector<long long>& b, vector<long long>& v,
long long m, long long x);
void solve() {
long long n;
cin >> n;
long long a[n];
for (long long i = 0; i < n; i++) cin >> a[i];
vector<long long> ans;
long long b[n];
for (long long i = 0; i < n; i++) b[i] = a[i];
sort(b, b + n);
for (long long i = 0; i < n - 2; i++) {
for (long long j = i; j < n; j++) {
if (a[j] == b[i]) {
while (j != i) {
ans.push_back(j - 2);
swap(a[j - 2], a[j - 1]);
swap(a[j - 2], a[j]);
j -= 2;
}
goto meow;
}
j++;
}
for (long long j = i + 1; j < n; j++) {
if (a[j] == b[i]) {
while (j != i + 1) {
ans.push_back(j - 2);
swap(a[j - 2], a[j - 1]);
swap(a[j - 2], a[j]);
j -= 2;
}
ans.push_back(i);
ans.push_back(i);
swap(a[i], a[i + 1]);
swap(a[i + 2], a[i + 1]);
goto meow;
}
j++;
}
meow:;
}
if (a[n - 1] != b[n - 1]) {
map<long long, long long> ct;
bool ALLMIGHT = 0;
for (long long i = 0; i < n; i++) {
ct[a[i]]++;
if (ct[a[i]] == 2) ALLMIGHT = 1;
}
if (ALLMIGHT == 0) {
cout << -1 << endl;
return;
}
long long curram_mf = n - 3;
while (ALLMIGHT) {
ans.push_back(curram_mf);
swap(a[curram_mf], a[curram_mf + 1]);
swap(a[curram_mf], a[curram_mf + 2]);
curram_mf--;
bool DETROITSMASH = 0;
for (long long i = 0; i < n; i++)
if (a[i] != b[i]) DETROITSMASH = 1;
ALLMIGHT = DETROITSMASH;
}
}
cout << ans.size() << endl;
for (auto x : ans) cout << x + 1 << " ";
cout << endl;
return;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
while (t--) {
solve();
}
}
long long powm(long long a, long long b, long long c = M) {
long long res = 1;
while (b) {
if (b & 1) res = (res * a) % c;
a = (a * a) % c;
b >>= 1;
}
return res;
}
long long modI(long long a, long long m = M) {
long long m0 = m, y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long q = a / m;
long long t = m;
m = a % m;
a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) x += m0;
return x;
}
long long PrimRoot(long long p, long long x) {
vector<long long> v;
long long t = p - 1;
for (long long i = 2; i * i <= t; i++)
if (t % i == 0) {
v.push_back((p - 1) / i);
while (t % i == 0) t /= i;
}
if (t > 1) v.push_back((p - 1) / t);
for (long long i = x + 1; i < p; i++) {
long long flag = 0;
for (auto x : v)
if (powm(i, x, p) == 1) {
flag = 1;
break;
}
if (flag == 0) return i;
}
return 0;
}
void fft(vector<long long>& a, long long n, bool invert, long long m,
long long x) {
long long lg_n = __builtin_ctzll(n);
for (long long i = 0, y = 0; i < n; i++) {
for (long long j = 0; j < lg_n; j++)
if ((1LL << j) & i) y |= (1LL << (lg_n - j - 1));
if (y > i) swap(a[i], a[y]);
}
if (invert) x = modI(x, m);
for (long long s = 2; s <= n; s <<= 1) {
long long y = powm(x, (n / s), m);
for (long long j = 0; j < (n / s); j++) {
long long r = 1;
for (long long i = 0; i < s / 2; i++) {
long long u = a[i + j * s], v = (r * a[i + j * s + (s / 2)]) % m;
a[i + j * s] = (u + v) % m;
a[i + j * s + (s / 2)] = (u - v + m) % m;
r = r * y % m;
}
}
}
if (invert) {
long long invn = modI(n, m);
for (long long i = 0; i < n; i++) a[i] = (a[i] * invn) % m;
}
return;
}
void PolyMult(vector<long long>& a, vector<long long>& b, vector<long long>& v,
long long m, long long x) {
long long n = 1;
while (n < ((long long)a.size()) + ((long long)b.size())) n <<= 1;
vector<long long> fa(a.begin(), a.end());
fa.resize(n, 0);
vector<long long> fb(b.begin(), b.end());
fb.resize(n, 0);
long long y = powm(x, (m - 1) / n, m);
fft(fa, n, false, m, y);
fft(fb, n, false, m, y);
v.resize(n, 0);
for (long long i = 0; i < n; i++) v[i] = ((fa[i] * fb[i]) % m);
fft(v, n, true, m, y);
return;
}
|
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
template <typename T>
T sqr(T a) {
return a * a;
}
template <typename T>
int sign(T a) {
if (a == 0) return 0;
return (a > 0 ? 1 : -1);
}
template <typename T>
bool uax(T& a, const T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T>
bool uin(T& a, const T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& p) {
in >> p.first >> p.second;
return in;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2>& p) {
out << p.first << ' ' << p.second;
return out;
}
mt19937_64 rnd(0);
const int LOG = 64;
const int N = (int)2e5 + 7;
const int MAXN = (int)1e6 + 7;
const int MOD = (int)1e9 + 7;
const int INF = (int)2e9 + 7;
const int CONST = 450;
const long long LINF = (int64_t)1e18;
const long double PI = 3.1415926535897932384626433832795;
const long double EPS = 1;
vector<int> act;
int n, a[N], need[N];
bool check_parity() {
int inv = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) inv += need[i] > need[j];
}
return inv % 2 == 0;
}
void perform(int pos) {
act.push_back(pos + 1);
swap(need[pos], need[pos + 1]);
swap(need[pos], need[pos + 2]);
}
void solve() {
cin >> n;
act.clear();
vector<pair<int, int>> v;
for (int i = 0; i < n; i++) {
cin >> a[i];
v.push_back(make_pair(a[i], i));
}
sort(v.begin(), v.end());
pair<int, int> eq = make_pair(-1, -1);
for (int i = 0; i < n; i++) {
need[v[i].second] = i;
if (i && v[i - 1].first == v[i].first)
eq = make_pair(v[i - 1].second, v[i].second);
}
if (!check_parity()) {
if (eq.first == -1) {
cout << "-1\n";
return;
}
swap(need[eq.first], need[eq.second]);
}
for (int i = 0; i < n; i++) {
int pos;
for (int j = 0; j < n; j++) {
if (need[j] == i) pos = j;
}
if (pos % 2 != i % 2) {
if (pos == n - 1) {
perform(n - 3);
perform(n - 3);
pos = n - 2;
} else {
perform(pos - 1);
pos++;
}
}
while (pos != i) {
perform(pos - 2);
pos -= 2;
}
}
cout << (int)act.size() << "\n";
for (int x : act) cout << x << ' ';
cout << "\n";
}
int main() {
double start = clock();
cout << fixed << setprecision(20);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
rnd.seed(time(0));
int t = 1;
cin >> t;
for (int i = 0; i < t; i++) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v, ans;
void op(int i) {
int x = v[i + 2];
v[i + 2] = v[i + 1];
v[i + 1] = v[i];
v[i] = x;
if (ans.size() >= 2 && ans[ans.size() - 1] == i + 1 &&
ans[ans.size() - 2] == i + 1)
ans.pop_back(), ans.pop_back();
else
ans.push_back(i + 1);
}
void reach(int x, int y) {
while (y > x + 1) op(y - 2), y -= 2;
if (y == x + 1) op(y - 1), op(y - 1);
}
int main(int argc, char **argv) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, st = 0, temp = 0;
ans.clear();
cin >> n;
v.resize(n);
vector<int> ar(501, -1);
int val = -1, x, y;
for (int i = 0; i < n; i++) {
cin >> v[i];
if (ar[v[i]] != -1) val = v[i], x = ar[v[i]], y = i;
ar[v[i]] = i;
}
if (val != -1) v[x] = v[y] = 2000;
while (n - st > 3) {
int mi = 1000, in;
for (int j = st; j < n; j++)
if (v[j] < mi) mi = v[j], in = j;
reach(st, in);
st++;
}
if (val == -1) {
while (!(v[st] <= v[st + 1] && v[st + 1] <= v[st + 2])) {
op(st), temp++;
if (temp > 5) break;
}
if (temp > 5) {
cout << -1 << endl;
continue;
}
} else {
while (v[st + 1] != 2000 || v[st + 2] != 2000) op(st);
while (st >= 0 && val < v[st]) op(st), op(st), st--;
}
cout << ans.size() << endl;
for (auto x : ans) cout << x << " ";
cout << endl;
}
}
|
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using namespace std;
inline int read() {
int x = 0, neg = 1;
char op = getchar();
while (!isdigit(op)) {
if (op == '-') neg = -1;
op = getchar();
}
while (isdigit(op)) {
x = 10 * x + op - '0';
op = getchar();
}
return neg * x;
}
inline void print(int x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
const int N = 505;
pair<int, int> a[N];
int p[N], n;
void rotate(int x) {
swap(p[x], p[x + 2]);
swap(p[x + 1], p[x + 2]);
}
bool check() {
for (register int i = 1; i < n; i++) {
if (p[i] > p[i + 1]) {
return 0;
}
}
return 1;
}
void solve() {
n = read();
for (register int i = 1; i <= n; i++) {
int x = read();
a[i] = make_pair(x, i);
}
sort(a + 1, a + n + 1);
for (register int i = 1; i <= n; i++) {
p[a[i].second] = i;
}
int cnt = 0;
for (register int i = 1; i <= n; i++) {
for (register int j = i + 1; j <= n; j++) {
if (p[i] > p[j]) {
cnt++;
}
}
}
if (cnt & 1) {
for (register int i = 1; i < n; i++) {
if (a[i].first == a[i + 1].first) {
swap(p[a[i].second], p[a[i + 1].second]);
break;
}
}
}
vector<int> ans;
for (register int i = 1; i <= n - 2; i++) {
int pos = min_element(p + i, p + n + 1) - p;
while (pos > i + 1) {
rotate(pos - 2), ans.push_back(pos - 2);
pos -= 2;
}
if (pos > i) {
rotate(i), ans.push_back(i);
rotate(i), ans.push_back(i);
}
}
for (register int i = 0; i < 3; i++) {
if (check()) break;
rotate(n - 2), ans.push_back(n - 2);
}
if (!check()) {
puts("-1");
} else {
printf("%d\n", (int)ans.size());
for (auto v : ans) {
printf("%d ", v);
}
puts("");
}
}
int main() {
int T = read();
while (T--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> ans;
void tros(int i, vector<int> &v) {
int t = v[i + 2];
v[i + 2] = v[i + 1];
v[i + 1] = v[i];
v[i] = t;
}
void sort(vector<int> &v) {
int n = v.size();
for (int i = 0; i < n - 2; i++) {
int mn = 600, id = -1;
for (int j = i; j < n; j++) {
if (mn <= v[j]) continue;
id = j;
mn = v[j];
}
while (id >= i + 2) {
ans.push_back(id - 2);
tros(id - 2, v);
id -= 2;
}
if (id == i + 1) {
ans.push_back(id - 1);
ans.push_back(id - 1);
tros(id - 1, v);
tros(id - 1, v);
}
}
}
bool sorted(vector<int> &v) {
for (int i = 1; i < v.size(); i++)
if (v[i] < v[i - 1]) return 0;
return 1;
}
void fix(vector<int> &v) {
int dup = -1, n = v.size();
for (int i = n - 1; i > 0; i--) {
if (v[i] == v[i - 1]) {
dup = i - 1;
break;
}
}
if (~dup) {
int id = n - 1;
while (id >= dup + 2) {
ans.push_back(id - 2);
tros(id - 2, v);
id -= 2;
}
ans.push_back(dup);
tros(dup, v);
}
}
void fix2(vector<int> &v) {
int dup = -1, n = v.size();
for (int i = n - 1; i > 0; i--) {
if (v[i] == v[i - 1]) {
dup = i - 1;
break;
}
}
if (~dup) {
int id = n - 1;
while (id >= dup + 2) {
ans.push_back(id - 2);
tros(id - 2, v);
id -= 2;
}
ans.push_back(dup);
tros(dup, v);
ans.push_back(dup);
tros(dup, v);
}
}
void fix3(vector<int> &v) {
int dup = -1, n = v.size();
for (int i = n - 1; i > 0; i--) {
if (v[i] == v[i - 1]) {
dup = i - 1;
break;
}
}
if (~dup) {
int id = n - 1;
while (id >= dup + 2) {
ans.push_back(id - 2);
tros(id - 2, v);
id -= 2;
}
ans.push_back(dup);
tros(dup, v);
ans.push_back(dup);
tros(dup, v);
ans.push_back(dup);
tros(dup, v);
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n, mx = 0, id = -1;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
if (v[i] > mx) mx = v[i], id = i;
}
sort(v);
if (!sorted(v)) fix(v), sort(v);
if (v[n - 1] == v[n - 3] and v[n - 2] > v[n - 1]) {
ans.push_back(n - 3);
tros(n - 3, v);
}
if (sorted(v)) {
cout << ans.size() << endl;
assert(ans.size() <= n * n);
for (int i : ans) cout << i + 1 << ' ';
cout << endl;
} else
cout << -1 << endl;
ans.clear();
}
}
|
#include <bits/stdc++.h>
const int N = 502;
void sw(float* x) {
float tmp = *x;
*x = *(x + 2);
*(x + 2) = *(x + 1);
*(x + 1) = tmp;
}
int main() {
int _t;
scanf("%d", &_t);
while (_t--) {
static float arr[N];
static int anss[N * N];
static int tm[N];
memset(tm, 0, sizeof(tm));
int n;
scanf("%d", &n);
int f2 = -1;
for (int i = 1; i <= n; i++) {
int a;
scanf("%d", &a);
tm[a]++;
if (tm[a] == 2) f2 = a;
arr[i] = a + 0.0001 * tm[a];
}
int nx = 0;
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++)
if (arr[i] > arr[j]) nx++;
if (nx & 1) {
if (f2 != -1) {
int f21 = -1, f22 = -1;
for (int i = 1; i <= n; i++)
if (((int)arr[i]) == f2) {
if (f21 == -1)
f21 = i;
else if (f22 == -1)
f22 = i;
else
break;
}
std::swap(arr[f21], arr[f22]);
nx++;
} else {
printf("-1\n");
continue;
}
}
int ans = 0;
while (nx) {
for (int i = 1; i <= n - 2; i++) {
if (arr[i] < arr[i + 2] && arr[i + 2] < arr[i + 1] ||
arr[i] > arr[i + 2] && arr[i + 2] > arr[i + 1]) {
sw(arr + i);
anss[++ans] = i;
}
if (arr[i] < arr[i + 1] && arr[i] > arr[i + 2] ||
arr[i] > arr[i + 1] && arr[i + 1] > arr[i + 2]) {
sw(arr + i);
nx -= 2;
anss[++ans] = i;
}
}
}
printf("%d\n", ans);
for (int i = 1; i <= ans; i++) printf("%d ", anss[i]);
printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long MOD(long long x, long long m) {
long long r = x % m;
if (r < 0) {
r += m;
}
return r;
}
ostream& operator<<(ostream& str, const pair<long long, long long>& p) {
str << p.first << " " << p.second;
return str;
}
template <typename T>
void join(T& arr, string sep) {
bool first = true;
for (auto t : arr) {
if (first) {
first = false;
cout << t;
} else {
cout << sep << t;
}
}
cout << "\n";
}
void solve() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < (long long)(n); ++i) {
cin >> a[i];
}
function<bool(long long)> tswap = [&](long long i) {
long long mx = max(a[i], a[i + 1]);
mx = max(mx, a[i + 2]);
if (a[i + 2] == mx) {
return false;
}
swap(a[i + 2], a[i + 1]);
swap(a[i + 1], a[i]);
return true;
};
vector<long long> ops;
bool move = true;
while (move) {
move = false;
for (long long i = (long long)(0); i < (long long)(n - 2); ++i) {
if (tswap(i)) {
move = true;
ops.push_back(i + 1);
if (tswap(i)) ops.push_back(i + 1);
}
}
}
if (a[0] > a[1] && a[0] != a[2]) {
long long i = 1;
while (i < n - 1) {
if (a[i] == a[i + 1]) break;
i++;
}
if (i == n - 1) {
cout << "-1\n";
return;
}
i--;
while (i >= 0) {
swap(a[i + 2], a[i + 1]);
swap(a[i + 1], a[i]);
ops.push_back(i + 1);
i--;
}
}
if (a[0] == a[2]) {
ops.push_back(1);
ops.push_back(1);
}
cout << ops.size() << "\n";
join(ops, " ");
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt = 1;
cin >> tt;
for (int t = 1; t <= tt; t++) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 510;
vector<int> ans;
int a[N];
int n;
void move(int x, int t) {
while (t--) {
ans.push_back(x);
int tmp = a[x + 2];
a[x + 2] = a[x + 1];
a[x + 1] = a[x];
a[x] = tmp;
}
if (0)
for (int i = 1; i <= n; ++i) printf("%d%c", a[i], " \n"[i == n]);
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", a + i);
int tot = 0;
ans.clear();
bool flag = true;
for (; tot < n;) {
int num = 1e9;
for (int i = tot + 1; i <= n; ++i) num = min(num, a[i]);
if (0) printf("deal %d:\n", num);
for (int i = tot + 1; i <= n; ++i) {
if (a[i] != num) continue;
if (0) printf("found a[%d]=%d\n", i, a[i]);
tot++;
if ((i - tot) % 2 == 0) {
for (int j = i; j - 2 >= tot; j -= 2) move(j - 2, 1);
continue;
}
if (tot >= 2 && a[tot - 1] == num) {
for (int j = i; j - 2 >= tot; j -= 2) move(j - 2, 1);
move(tot - 1, 1);
continue;
}
if (tot + 1 < n) {
for (int j = i; j - 2 >= tot; j -= 2) move(j - 2, 1);
move(tot, 2);
continue;
}
int same = n - 2;
for (; same > 1 && a[same] != a[same - 1]; --same)
;
if (same <= 1) {
flag = false;
break;
}
int rep = (tot - same) % 2 == 0 ? tot : n;
for (int j = rep; j - 2 >= same; j -= 2) move(j - 2, 1);
move(same - 1, 1);
for (int j = same + 1; j + 2 <= n; j += 2) move(j, 2);
}
}
if (!flag)
puts("-1");
else {
int ans_size = ans.size();
printf("%d\n", ans_size);
for (int i = 0; i < ans_size; ++i)
printf("%d%c", ans[i], " \n"[i == ans_size - 1]);
if (ans_size == 0) puts("");
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
mt19937 gen(time(0));
bool IS_ONE_TEST = 0;
vector<int> res;
int total = 0;
bool proc(int idx, vector<int> &a) {
if (!total) return 0;
res.push_back(idx + 1);
int temp = a[idx + 2];
a[idx + 2] = a[idx + 1];
a[idx + 1] = a[idx];
a[idx] = temp;
--total;
return 1;
}
void solve() {
int n;
cin >> n;
res.clear();
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
vector<int> b = a;
total = n * n;
sort(a.begin(), a.end());
for (int z = 0; z < n; ++z) {
for (int i = 0; i + 2 < n; ++i) {
int mn = min({b[i], b[i + 1], b[i + 2]});
while (b[i] != mn && proc(i, b)) {
}
}
for (int i = n - 1; i > 1; --i) {
int mx = max({b[i], b[i - 1], b[i - 2]});
while (b[i] != mx && proc(i - 2, b)) {
}
}
}
if (a == b) {
cout << res.size() << endl;
for (auto &c : res) cout << c << " ";
cout << endl;
} else
cout << -1 << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << fixed << setprecision(15);
int t;
if (IS_ONE_TEST)
t = 1;
else
cin >> t;
while (t-- > 0) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 555;
int ori[maxn];
vector<int> ans;
int n;
inline void show() {
for (int i = 1; i <= n; i++) cout << ori[i] << " ";
cout << endl;
}
inline void move(int x) {
if (ans.size() >= 2 && ans[ans.size() - 1] == x && ans[ans.size() - 2] == x) {
ans.pop_back();
ans.pop_back();
} else
ans.push_back(x);
int temp = ori[x + 2];
ori[x + 2] = ori[x + 1];
ori[x + 1] = ori[x];
ori[x] = temp;
}
void go(int head, int pos) {
while (head <= pos - 2) {
move(pos - 2);
pos -= 2;
}
while (head < pos) {
move(pos - 1);
move(pos - 1);
pos--;
}
}
void gor(int head, int pos) {
while (pos < head) {
move(pos);
move(pos);
pos += 2;
}
}
void solve() {
ans.clear();
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &ori[i]);
for (int head = 1; head <= n - 2; head++) {
int pos = head;
for (int i = head + 1; i <= n; i++) {
if (ori[pos] > ori[i]) pos = i;
}
go(head, pos);
}
if (ori[n - 1] > ori[n]) {
if (ori[n] == ori[n - 2])
go(n - 2, n);
else {
int head = -1;
for (int i = 2; i <= n - 2; i++) {
if (ori[i - 1] == ori[i]) {
head = i;
break;
}
}
if (head == -1) {
puts("-1");
return;
}
if ((n - head) % 2 == 0) {
go(head, n);
move(head - 1);
move(head - 1);
gor(n - 1, head - 1);
} else {
go(head, n - 1);
move(head - 1);
gor(n, head + 1);
}
}
}
cout << ans.size() << endl;
for (int aa : ans) cout << aa << " ";
cout << endl;
}
signed main() {
int T;
cin >> T;
while (T--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500;
const int INF = 1e9;
int n, a[MAXN + 5], ans[MAXN * MAXN + 5], cnt_ans;
void work(int i) {
assert(i <= n - 2);
ans[++cnt_ans] = i;
int x = a[i];
int y = a[i + 1];
int z = a[i + 2];
a[i] = z;
a[i + 1] = x;
a[i + 2] = y;
}
void print_ans() {
cout << cnt_ans << endl;
for (int i = 1; i <= cnt_ans; ++i) cout << ans[i] << " ";
cout << endl;
}
void fail() { cout << -1 << endl; }
void solve(int st) {
for (int i = st; i <= n - 2; ++i) {
int minval = INF, minpos = 0;
for (int j = i; j <= n; ++j) {
if (a[j] < minval) {
minval = a[j];
minpos = j;
}
}
if (minpos == i) continue;
if ((minpos - i) % 2 == 0) {
for (int j = minpos - 2; j >= i; j -= 2) {
work(j);
}
assert(a[i] == minval);
} else {
if (minpos > n - 2) {
if (minpos == i + 1) {
work(i);
work(i);
continue;
}
work(minpos - 2);
minpos -= 2;
}
work(minpos);
for (int j = minpos - 1; j >= i; j -= 2) {
work(j);
}
assert(a[i] == minval);
}
}
}
int main() {
int T;
cin >> T;
while (T--) {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
cnt_ans = 0;
solve(1);
if (a[n - 1] > a[n]) {
if (a[n - 2] == a[n - 1] || a[n - 2] == a[n]) {
work(n - 2);
print_ans();
continue;
}
bool ok = 0;
for (int i = n - 3; i >= 1; --i) {
if (a[i] == a[i + 1]) {
work(i + 1);
work(i);
solve(i + 2);
ok = 1;
break;
}
}
if (!ok)
fail();
else
print_ans();
} else
print_ans();
}
return 0;
}
|
#include <bits/stdc++.h>
using i32 = int;
using i64 = long long;
i32 main() {
std::ios::sync_with_stdio(false);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
i32 T;
std::cin >> T;
while (T--) {
i32 n;
std::cin >> n;
std::vector<i32> a(n);
for (i32 i = 0; i < n; i++) std::cin >> a[i];
std::vector<i32> ans;
auto flip = [&](i32 i) {
std::swap(a[i], a[i + 2]);
std::swap(a[i + 1], a[i + 2]);
ans.push_back(i + 1);
};
for (i32 i = 0; i + 2 < n; i++) {
i32 j = std::min_element(a.begin() + i, a.end()) - a.begin();
for (; 1 < j - i; j -= 2) flip(j - 2);
if (j - i == 1) {
flip(i);
flip(i);
}
}
if (a[n - 1] < a[n - 2]) {
i32 i = 1;
while (i + 2 < n && a[i - 1] != a[i]) i++;
if (i + 2 == n) {
if (a[n - 3] != a[n - 1]) {
std::cout << "-1\n";
continue;
}
flip(n - 3);
} else {
flip(i - 1);
flip(i - 1);
flip(i);
flip(i++);
for (; i + 2 < n; i++) {
flip(i);
flip(i);
}
}
}
assert(std::is_sorted(a.begin(), a.end()));
std::cout << ans.size() << "\n";
for (i32 i = 0; i < ans.size(); i++)
std::cout << ans[i] << " \n"[i + 1 == ans.size()];
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <typename T>
using mat = vector<vector<T>>;
template <int mod>
struct modint {
int x;
modint() : x(0) {}
modint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
modint &operator+=(const modint &p) {
if ((x += p.x) >= mod) x -= mod;
return *this;
}
modint &operator-=(const modint &p) {
if ((x += mod - p.x) >= mod) x -= mod;
return *this;
}
modint &operator*=(const modint &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
modint &operator/=(const modint &p) {
*this *= p.inverse();
return *this;
}
modint operator-() const { return modint(-x); }
modint operator+(const modint &p) const { return modint(*this) += p; }
modint operator-(const modint &p) const { return modint(*this) -= p; }
modint operator*(const modint &p) const { return modint(*this) *= p; }
modint operator/(const modint &p) const { return modint(*this) /= p; }
bool operator==(const modint &p) const { return x == p.x; }
bool operator!=(const modint &p) const { return x != p.x; }
modint inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return modint(u);
}
modint pow(int64_t n) const {
modint ret(1), mul(x);
while (n > 0) {
if (n & 1) ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }
friend istream &operator>>(istream &is, modint &a) {
int64_t t;
is >> t;
a = modint<mod>(t);
return (is);
}
static int get_mod() { return mod; }
};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <typename T>
T mypow(T x, ll n) {
T ret = 1;
while (n > 0) {
if (n & 1) (ret *= x);
(x *= x);
n >>= 1;
}
return ret;
}
ll modpow(ll x, ll n, const ll mod) {
ll ret = 1;
while (n > 0) {
if (n & 1) (ret *= x);
(x *= x);
n >>= 1;
x %= mod;
ret %= mod;
}
return ret;
}
uint64_t my_rand(void) {
static uint64_t x = 88172645463325252ULL;
x = x ^ (x << 13);
x = x ^ (x >> 7);
return x = x ^ (x << 17);
}
ll popcnt(ull x) { return __builtin_popcountll(x); }
template <typename T>
struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
bool operator<(const edge<T> &r) const { return cost < r.cost; }
operator int() const { return to; }
};
template <typename T>
using Edges = vector<edge<T>>;
template <typename T>
using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
struct Timer {
clock_t start_time;
void start() { start_time = clock(); }
int lap() { return (clock() - start_time) * 1000 / CLOCKS_PER_SEC; }
};
const long double eps = 0.000001;
const long double PI = 3.141592653589793;
template <typename T>
struct BIT {
vector<T> data;
BIT(int sz) { data.assign(++sz, 0); }
T sum(int k) {
T ret = 0;
for (; k > 0; k -= k & -k) ret += data[k];
return (ret);
}
void add(int k, T x) {
for (++k; k < data.size(); k += k & -k) data[k] += x;
}
int search(long long k) {
++k;
int res = 0;
int N = 1;
while (N < (int)data.size()) N *= 2;
for (int i = N / 2; i > 0; i /= 2) {
if (res + i < (int)data.size() && data[res + i] < k) {
k = k - data[res + i];
res = res + i;
}
}
return res;
}
void show() {
for (ll i = (0), max_i = (((ll)(data).size()) - 1); i < max_i; i++)
cout << sum(i + 1) - sum(i) << ' ';
cout << '\n';
}
};
ll inversion_number(vector<int> &x, int n) {
BIT<int> bit(n);
ll res = 0;
for (int &y : x) {
res += bit.sum(n - 1 - y);
bit.add(n - 1 - y, 1);
}
return res;
}
void move(vector<int> &x, vector<int> &y, int i) {
assert(x.size() - 2 > i);
int a = x[i], b = x[i + 1], c = x[i + 2];
swap(y[a], y[b]);
swap(y[c], y[b]);
swap(x[i + 2], x[i + 1]);
swap(x[i], x[i + 1]);
}
void solve() {
int n;
cin >> n;
vector<int> a(n);
vector<pair<int, int>> inv(n);
for (ll i = (0), max_i = (n); i < max_i; i++)
cin >> a[i], inv[i] = make_pair(a[i], i);
sort((inv).begin(), (inv).end());
vector<int> id(n);
for (ll i = (0), max_i = (n); i < max_i; i++) id[i] = inv[i].second;
int num = inversion_number(id, n);
if (num % 2 == 1) {
bool flag = false;
for (ll i = (0), max_i = (n - 1); i < max_i; i++) {
if (inv[i].first == inv[i + 1].first) {
swap(inv[i].second, inv[i + 1].second);
swap(id[i], id[i + 1]);
flag = true;
break;
}
}
if (not flag) {
cout << -1 << '\n';
return;
}
}
vector<int> now(n), now_inv(n);
iota((now).begin(), (now).end(), 0);
iota((now_inv).begin(), (now_inv).end(), 0);
vector<int> ans;
for (ll i = (0), max_i = (n); i < max_i; i++) {
int a = id[i];
while (now_inv[a] != i) {
if (now_inv[a] == n - 1) {
ans.push_back(now_inv[a] - 1);
move(now, now_inv, now_inv[a] - 2);
continue;
}
int tmp = now_inv[a] - 1;
ans.push_back(tmp + 1);
ans.push_back(tmp + 1);
move(now, now_inv, tmp);
move(now, now_inv, tmp);
}
}
cout << ans.size() << '\n';
for (ll i = (0), max_i = (ans.size()); i < max_i; i++) cout << ans[i] << ' ';
cout << '\n';
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << setprecision(20);
int t;
cin >> t;
for (ll _ = (0), max_i = (t); _ < max_i; _++) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y) {
long long int res = 1;
while (y) {
if (y % 2) res = (res * x) % 998244353;
y /= 2;
x = (x * x) % 998244353;
}
return res;
}
void cycshift(long long int pos, long long int* a) {
long long int temp = a[pos];
a[pos] = a[pos + 2];
a[pos + 2] = a[pos + 1];
a[pos + 1] = temp;
}
int main() {
long long int t, i, j, n;
cin >> t;
while (t--) {
cin >> n;
long long int a[n];
vector<long long int> v;
for (i = 0; i < n; i++) {
cin >> a[i];
}
for (i = 0; i < n; i++) {
long long int mn = 1000000000000, pos = -1;
for (j = i; j < n; j++) {
if (mn > a[j]) {
mn = a[j];
pos = j;
}
}
if ((pos - i) == 1 && pos == n - 1) break;
for (j = pos - 2; j >= i; j -= 2) {
v.push_back(j);
cycshift(j, a);
}
if ((pos - i) % 2) {
for (j = 0; j < 2; j++) {
v.push_back(i);
cycshift(i, a);
}
}
}
long long int z = -1;
if (a[n - 3] == a[n - 1]) {
cycshift(n - 3, a);
v.push_back(n - 3);
i = n - 1;
}
for (j = 0; j < n - 1; j++)
if (a[j] == a[j + 1]) z = j;
if (i < n - 1 && z == -1)
cout << -1 << endl;
else {
if (i == n - 2) {
for (j = n - 3; j >= z; j -= 2) {
cycshift(j, a);
v.push_back(j);
}
cycshift(z, a);
v.push_back(z);
if ((n - 1 - z) % 2) z++;
for (i = z + 1; i <= n - 4; i += 2) {
for (j = 0; j < 2; j++) {
v.push_back(i);
cycshift(i, a);
}
}
}
cout << v.size() << endl;
for (j = 0; j < v.size(); j++) cout << v[j] + 1 << " ";
cout << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
const long long inf = 1e18;
long long i, j, k, n, m, x, y, ma = -inf, mi = inf, qq = 1, w, frr, to, t, ppp;
char ch;
vector<long long> a;
void solve() {
cin >> n;
a.resize(n);
vector<pair<long long, long long> > b(n);
for (i = 0; i < n; i++) {
cin >> a[i];
b[i] = {a[i], i};
}
sort((b).begin(), (b).end());
long long cnt = -1;
for (i = 0; i < n; i++) {
if (i == 0 || b[i].first != b[i - 1].first) cnt++;
a[b[i].second] = cnt;
}
for (i = 0; i < n; i++) {
b[i].first = a[i];
}
sort((b).begin(), (b).end());
vector<long long> ans;
long long t1, t2, t3;
for (i = 0; i < n - 2; i++) {
if (a[i] == b[i].first) continue;
long long it;
for (j = i + 1; j < n; j++)
if (a[j] == b[i].first) {
it = j;
break;
}
while (it - i > 1) {
ans.push_back(it - 2);
t1 = a[it - 2];
t2 = a[it - 1];
t3 = a[it];
a[it] = t2;
a[it - 1] = t1;
a[it - 2] = t3;
it -= 2;
}
if (it != i) {
ans.push_back(i);
ans.push_back(i);
swap(a[i], a[i + 2]);
swap(a[i], a[i + 1]);
}
}
if (a[n - 1] == a[n - 3]) {
ans.push_back(n - 3);
swap(a[n - 2], a[n - 1]);
}
if (a[n - 1] == b[n - 1].first) {
cout << ans.size() << '\n';
for (auto v : ans) cout << v + 1 << ' ';
cout << '\n';
} else {
if (cnt == n - 1 || n < 4) {
cout << "-1\n";
return;
}
long long it;
for (i = n - 3; i > 0; i--) {
if (b[i].first == b[i - 1].first) {
it = i - 1;
break;
}
}
long long fit = it;
while (it < n - 4) {
ans.push_back(it);
swap(a[it], a[it + 2]);
it++;
}
ans.push_back(n - 4);
ans.push_back(n - 4);
ans.push_back(n - 3);
ans.push_back(n - 3);
it = n - 5;
while (it >= fit) {
ans.push_back(it);
ans.push_back(it);
it--;
}
cout << ans.size() << '\n';
for (auto v : ans) cout << v + 1 << ' ';
cout << '\n';
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> qq;
while (qq--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
template <typename A>
ostream &operator<<(ostream &cout, vector<A> const &v);
template <typename A, typename B>
ostream &operator<<(ostream &cout, pair<A, B> const &p) {
return cout << "(" << p.first << ", " << p.second << ")";
}
template <typename A>
ostream &operator<<(ostream &cout, vector<A> const &v) {
cout << "[";
for (int i = 0; i < v.size(); i++) {
if (i) cout << ", ";
cout << v[i];
}
return cout << "]";
}
template <typename A, typename B>
istream &operator>>(istream &cin, pair<A, B> &p) {
cin >> p.first;
return cin >> p.second;
}
mt19937 rng(61378913);
void usaco(string filename) {
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
const long double pi = 3.14159265358979323846;
const long long mod = 1000000007;
long long n, m, k, q, l, r, x, y, z;
long long a[1000005];
long long b[1000005];
long long c[1000005];
string second, t;
long long ans = 0;
void cyc(long long pos) {
b[pos + 1] = a[pos];
b[pos + 2] = a[pos + 1];
b[pos] = a[pos + 2];
for (long long i = (pos); i < (pos + 3); ++i) a[i] = b[i];
}
void solve(int tc) {
cin >> n;
for (int ele = 0; ele < n; ele++) cin >> a[ele];
;
vector<long long> ops;
for (long long i = 0; i < (n + 5); ++i) {
if (i == n + 1) {
long long p = -1;
for (long long i = (3); i < (n - 1); ++i)
if (a[i] == a[i + 1]) {
p = i;
break;
}
if (p != -1) {
--p;
while (p >= 3) {
for (long long i = 0; i < (2); ++i) {
ops.push_back(p + 1);
cyc(p);
}
--p;
}
for (long long i = 0; i < (2); ++i) {
ops.push_back(2);
cyc(1);
}
for (long long i = 0; i < (2); ++i) {
ops.push_back(3);
cyc(2);
}
p = 3;
while (p + 2 < n) {
if (a[p + 1] <= a[p + 2]) break;
ops.push_back(p + 1);
cyc(p);
++p;
}
} else {
if (n > 3) {
for (long long i = 0; i < (3); ++i)
if (a[i] == a[3]) {
for (long long j = 0; j < (2 - i); ++j) {
ops.push_back(1);
cyc(0);
}
for (long long i = 0; i < (2); ++i) {
ops.push_back(1);
cyc(0);
}
for (long long i = 0; i < (2); ++i) {
ops.push_back(2);
cyc(1);
}
break;
}
}
}
}
long long wpos = -1;
for (long long j = (n - 2); j >= 0; --j) {
if (a[j] > a[j + 1]) {
if (wpos == -1 || a[j] > a[wpos]) wpos = j;
}
}
if (wpos == -1) break;
long long st = wpos - 1;
if (st == -1) {
ops.push_back(1);
cyc(0);
st = 0;
}
while (st + 1 < n - 1) {
if (a[st + 1] <= a[st + 2]) break;
ops.push_back(st + 1);
cyc(st);
++st;
}
}
if (((long long)ops.size()) > n * n) {
cout << -1 << '\n';
return;
}
bool is = 1;
for (long long i = 0; i < (n - 1); ++i)
if (a[i] > a[i + 1]) is = 0;
if (!is) {
cout << -1 << '\n';
} else {
cout << ops.size() << '\n';
{
for (int ele = 0; ele < (ops.size()); ele++) {
if (ele) cout << " ";
cout << ops[ele];
}
cout << '\n';
};
}
}
int main() {
{ ios_base::sync_with_stdio(false); }
{
cin.tie(NULL);
cout.tie(NULL);
}
int tc = 1;
cin >> tc;
for (int t = 0; t < tc; t++) solve(t);
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v, ans;
void op(int i) {
int x = v[i + 2];
v[i + 2] = v[i + 1];
v[i + 1] = v[i];
v[i] = x;
if (ans.size() >= 2 && ans[ans.size() - 1] == i + 1 &&
ans[ans.size() - 2] == i + 1)
ans.pop_back(), ans.pop_back();
else
ans.push_back(i + 1);
}
void reach(int x, int y) {
while (y > x + 1) op(y - 2), y -= 2;
if (y == x + 1) op(y - 1), op(y - 1);
}
int main(int argc, char **argv) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, st = 0, temp = 0;
ans.clear();
cin >> n;
v.resize(n);
vector<int> ar(501, -1);
int val = -1, x, y;
for (int i = 0; i < n; i++) {
cin >> v[i];
if (ar[v[i]] != -1) val = v[i], x = ar[v[i]], y = i;
ar[v[i]] = i;
}
if (val != -1) v[x] = v[y] = 2000;
while (n - st > 3) {
int mi = 1000, in;
for (int j = st; j < n; j++)
if (v[j] < mi) mi = v[j], in = j;
reach(st, in);
st++;
}
if (val == -1) {
while (!(v[st] <= v[st + 1] && v[st + 1] <= v[st + 2])) {
op(st), temp++;
if (temp > 5) {
cout << -1 << endl;
goto brk;
}
}
} else {
while (v[st + 1] != 2000 || v[st + 2] != 2000) op(st);
while (st >= 0 && val < v[st]) op(st), op(st), st--;
}
cout << ans.size() << endl;
for (auto x : ans) cout << x << " ";
cout << endl;
brk:;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int a[512];
int main() {
int n, t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
vector<int> ans;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < n - 1; j++) {
if (a[j] > a[j + 1]) {
swap(a[j], a[j + 2]);
swap(a[j], a[j + 1]);
ans.emplace_back(j);
ans.emplace_back(j);
} else if (a[j + 1] > a[j + 2]) {
swap(a[j], a[j + 1]);
swap(a[j], a[j + 2]);
ans.emplace_back(j);
}
}
}
int i;
for (i = 2; i <= n; i++)
if (a[i] < a[i - 1]) break;
if (i <= n)
printf("-1\n");
else {
printf("%d\n", ans.size());
for (int x : ans) printf("%d ", x);
;
printf("\n");
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int M = 110000;
const unsigned long long inf = 9e18;
int n, m, k;
int a[M];
int cal(int s) {
int p = s;
for (int i = (s + 1); i <= (n); ++i)
if (a[i] < a[p]) p = i;
return p;
}
vector<int> ans;
void mv(int x) {
int t = a[x];
a[x] = a[x + 2];
a[x + 2] = a[x + 1];
a[x + 1] = t;
ans.push_back(x);
}
int b[555];
void solve() {
ans.clear();
int num[555] = {0};
cin >> n;
for (int i = (1); i <= (n); ++i) {
cin >> a[i];
b[i] = a[i];
}
sort(b + 1, b + 1 + n);
for (int i = (1); i <= (n); ++i) {
int pos = lower_bound(b + 1, b + 1 + n, a[i]) - b;
a[i] = pos + num[pos];
++num[pos];
}
int inv = 0;
for (int i = (1); i <= (n); ++i) {
for (int j = (i + 1); j <= (n); ++j) {
if (a[j] < a[i]) ++inv;
}
}
if (inv & 1) {
for (int i = (1); i <= (n - 1); ++i) {
if (b[i] == b[i + 1]) {
for (int j = (1); j <= (n); ++j) {
if (a[j] == i)
++a[j];
else if (a[j] == i + 1)
--a[j];
}
break;
}
}
}
for (int c = (1); c <= (n - 2); ++c) {
int t = cal(c);
while (t > c + 1) {
t -= 2;
mv(t);
}
if (t > c) {
mv(c);
mv(c);
}
}
if (a[n - 1] > a[n])
cout << -1 << endl;
else {
cout << (ans.size()) << endl;
for (int x : ans) cout << x << ' ';
puts("");
}
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long mod = 1000000007;
const long long linf = 0x3f3f3f3f3f3f3f3f;
const int N = 300100;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void bubsort(vector<int>& a, int i) {
swap(a[i + 1], a[i + 2]);
swap(a[i], a[i + 1]);
}
void solve() {
int n;
cin >> n;
vector<int> a(n);
vector<pair<int, int> > v(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
v[i] = {a[i], i};
}
sort(v.begin(), v.end());
for (int i = 0; i < n; i++) {
a[v[i].second] = i;
}
int inv = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
inv += a[i] > a[j];
}
}
if (inv & 1) {
for (int i = 0; i < n - 1; i++) {
if (v[i].first == v[i + 1].first) {
swap(a[v[i].second], a[v[i + 1].second]);
break;
}
}
}
vector<int> ans;
for (int i = 0; i < n - 2; i++) {
int ind = -1;
int mx = n;
for (int j = i; j < n; j++) {
if (mx > a[j]) mx = a[j], ind = j;
}
while (ind > i + 1) {
ind -= 2;
bubsort(a, ind);
ans.push_back(ind);
}
if (ind == i + 1) {
bubsort(a, i);
bubsort(a, i);
ans.push_back(i);
ans.push_back(i);
}
}
for (int i = 0; i < 3; i++) {
if (is_sorted(a.begin(), a.end())) {
break;
}
bubsort(a, n - 3);
ans.push_back(n - 3);
}
if (!is_sorted(a.begin(), a.end()))
cout << "-1\n";
else {
cout << int(ans.size()) << "\n";
for (auto p : ans) cout << p + 1 << " ";
cout << "\n";
}
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
;
int t_ = 1;
cin >> t_;
while (t_--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
scanf("%d", &T);
while (T--) {
int n;
cin >> n;
vector<int> a(n);
for (int &t : a) cin >> t;
vector<int> ans;
auto apply_rot = [&](int id) {
rotate(a.begin() + id, a.begin() + id + 2, a.begin() + id + 3);
ans.push_back(id);
};
for (int i = 0; i + 2 < n; i++) {
int p = min_element(a.begin() + i, a.end()) - a.begin();
int v = a[p];
for (; p >= i + 2; p -= 2) {
apply_rot(p - 2);
}
while (a[i] != v) apply_rot(i);
}
if (a[n - 2] > a[n - 1]) {
for (int i = n - 1; i > 0; i--) {
if (a[i] == a[i - 1]) {
int v = a[i];
for (int j = i; j + 2 < n; j += 2) {
while (a[j + 2] != v) apply_rot(j);
}
int p = n - 1;
if (a[n - 1] != v)
apply_rot(n - 3);
else {
apply_rot(n - 3);
apply_rot(n - 3);
p = n - 2;
}
for (; p >= i + 2; p -= 2) {
apply_rot(p - 2);
}
if (p != i) {
apply_rot(p - 2);
}
break;
}
}
}
if (a[n - 3] == a[n - 1]) apply_rot(n - 3);
if (is_sorted(begin(a), end(a))) {
cout << int(ans.size()) << endl;
for (int t : ans) cout << t + 1 << " ";
cout << endl;
} else {
puts("-1");
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 501;
int a[N];
int as[N * N], ac = 0;
void f(int i) {
as[++ac] = i;
swap(a[i], a[i + 1]);
swap(a[i], a[i + 2]);
}
int main(void) {
int T;
scanf("%d", &T);
while (T--) {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
bool fail = 0;
int p = 0;
for (int i = 1; i <= n; ++i) {
int j = min_element(a + i, a + n + 1) - a;
while (j > i + 1) f(j - 2), j -= 2;
if (j == i + 1) {
if (j <= n - 1)
f(i), f(i);
else if (p) {
f(p - 1), f(p - 1), i = p - 1;
} else {
f(i - 1);
if (!is_sorted(a + i - 1, a + n + 1)) {
ac = -1;
break;
}
}
}
if (a[i] == a[i - 1]) p = i;
}
printf("%d\n", ac);
if (ac == 0) printf("\n");
if (ac > 0)
for (int i = 1; i <= ac; ++i) printf("%d%c", as[i], " \n"[i == ac]);
ac = 0;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1e6 + 8;
const long long MOD = 1e9 + 7;
const long long sz = 1e5 + 5;
void f_io() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
}
int32_t main() {
f_io();
long long tc = 1;
cin >> tc;
auto rotate = [](vector<long long> &arr, long long index) {
swap(arr[index], arr[index + 1]);
swap(arr[index], arr[index + 2]);
};
while (tc--) {
long long n;
cin >> n;
vector<long long> v(n);
vector<long long> ret;
for (auto &el : v) cin >> el;
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < n - 2; j++) {
if (v[j] > v[j + 1]) {
rotate(v, j);
rotate(v, j);
ret.push_back(j);
ret.push_back(j);
} else if (v[j + 1] > v[j + 2]) {
rotate(v, j);
ret.push_back(j);
}
}
}
if (!is_sorted(begin(v), end(v)))
cout << "-1"
<< "\n";
else {
cout << ret.size() << "\n";
for (auto el : ret) cout << el + 1 << " ";
cout << "\n";
}
}
}
|
#include <bits/stdc++.h>
int ans[252000], a[510], tot;
void move(int u) {
int t1, t2, t3;
t1 = a[u], t2 = a[u + 1], t3 = a[u + 2];
a[u] = t3, a[u + 1] = t1, a[u + 2] = t2;
}
int main() {
int t, n, i, j, mn, mx, u;
scanf("%d", &t);
while (t--) {
tot = 0;
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
for (i = 1; i <= n - 2; i++) {
mn = 1000;
for (j = i; j <= n; j++)
if (a[j] < mn) mn = a[j], u = j;
while (u >= i + 2) ans[++tot] = u - 2, move(u - 2), u -= 2;
if (u == i + 1) move(i), move(i), ans[++tot] = i, ans[++tot] = i;
}
for (i = n; i >= 3; i--) {
mx = -1;
for (j = i; j >= 1; j--)
if (a[j] > mx) mx = a[j], u = j;
if (u <= i - 2) puts("0");
while (u <= i - 2) ans[++tot] = u, move(u), u += 2;
if (u == i - 1) move(i - 2), ans[++tot] = i - 2;
}
for (i = 1; i < n; i++)
if (a[i] > a[i + 1]) tot = -1;
if (tot == -1)
printf("-1\n");
else {
printf("%d\n", tot);
for (i = 1; i <= tot; i++) printf("%d ", ans[i]);
printf("\n");
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
inline long long sbt(long long x) { return __builtin_popcountll(x); }
inline long long iceil(double a) { return (long long)(ceil(a)); }
inline long long mul(long long a, long long b,
long long m = (long long)(1e9 + 7)) {
return ((a % m) * (b % m)) % m;
}
inline long long add(long long a, long long b,
long long m = (long long)(1e9 + 7)) {
return (a + b) % m;
}
inline long long sub(long long a, long long b,
long long m = (long long)(1e9 + 7)) {
return (a - b + m) % m;
}
long long fastpow(long long a, long long b,
long long m = (long long)(1e9 + 7)) {
long long res = 1;
while (b > 0) {
if (b & 1) res = mul(res, a, m);
a = mul(a, a, m);
b >>= 1;
}
return res;
}
long long modinv(long long a, long long m = (long long)(1e9 + 7)) {
return fastpow(a, m - 2, m);
}
long long n;
long long a[510], ranks[510];
vector<long long> v;
long long count() {
long long ct = 0;
for (long i = 0; i < n; i++) {
for (auto j = i + 1; j <= n - 1; j++) {
if (ranks[i] > ranks[j]) {
ct++;
}
}
}
return ct;
}
long long moves(long long i) {
long long maxi = -1;
for (auto j = i - 2; j <= i; j++) {
maxi = max(maxi, ranks[j]);
}
long long c = 0, temp;
while (ranks[i] != maxi) {
v.push_back(i - 1);
temp = ranks[i];
ranks[i] = ranks[i - 1];
ranks[i - 1] = ranks[i - 2];
ranks[i - 2] = temp;
c++;
}
return c;
}
void get_ac() {
v.clear();
cin >> n;
set<long long> s;
for (long i = 0; i < n; i++) {
cin >> a[i];
s.insert(a[i]);
}
map<long long, vector<long long> > ind;
for (long i = 0; i < n; i++) {
ind[a[i]].push_back(i);
}
long long rank = 0;
for (auto i : ind) {
for (auto j : i.second) {
ranks[j] = rank++;
}
}
long long inv = count();
if (inv & 1) {
bool f = 0;
for (auto i : ind) {
if ((long long)(i.second.size()) > 1) {
swap(ranks[i.second[0]], ranks[i.second[1]]);
f = 1;
break;
}
}
if (!f) {
cout << -1;
return;
}
}
long long ans = 0;
for (auto i = n - 1; i >= 2; i--) {
for (auto j = 2; j <= i; j++) {
long long c = moves(j);
ans += c;
}
}
if (ans <= n * n) {
cout << ans;
cout << "\n";
for (auto i : v) {
cout << i << " ";
}
} else {
cout << -1;
}
}
int main() {
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
cin.tie(NULL);
long long testcases;
cin >> testcases;
while (testcases--) {
get_ac();
cout << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int sqr(long long int x) { return x * x; }
inline long long int add(long long int a, long long int b) {
long long int c = a + b;
if (c >= 1000000007) c -= 1000000007;
return c;
}
inline long long int mul(long long int a, long long int b) {
return (a * 1ll * b) % 1000000007;
}
inline long long int sub(long long int a, long long int b) {
long long int c = a - b;
if (c < 0) c += 1000000007;
return c;
}
const int nax = 1000 * 1000 + 10;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
auto ok = [](vector<int> &v, int pos) {
swap(v[pos + 1], v[pos + 2]);
swap(v[pos], v[pos + 1]);
};
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> v(n);
vector<pair<int, int>> x(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
x[i] = {v[i], i};
}
sort(x.begin(), x.end());
for (int i = 0; i < n; ++i) {
v[x[i].second] = i;
}
int cnt = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
cnt += (v[j] < v[i]);
}
}
if (cnt & 1) {
for (int i = 0; i < n - 1; ++i) {
if (x[i].first == x[i + 1].first) {
swap(v[x[i].second], v[x[i + 1].second]);
break;
}
}
}
vector<int> ans;
for (int i = 0; i < n - 2; ++i) {
int pos = min_element(v.begin() + i, v.end()) - v.begin();
while (pos > i + 1) {
ok(v, pos - 2);
ans.push_back(pos - 2);
pos -= 2;
}
if (pos != i) {
ok(v, i);
ok(v, i);
ans.push_back(i);
ans.push_back(i);
}
}
for (int i = 0; i < 3; ++i) {
if (is_sorted(v.begin(), v.end())) break;
ok(v, n - 3);
ans.push_back(n - 3);
}
if (!is_sorted(v.begin(), v.end())) {
cout << -1 << "\n";
} else {
cout << ans.size() << "\n";
for (int i = 0; i < ans.size(); ++i) cout << ans[i] + 1 << " ";
cout << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
return f == 1 ? x : -x;
}
inline void print(int x) {
if (x < 0) x = ~x + 1, putchar('-');
if (x >= 10) print(x / 10);
putchar((x % 10) | 48);
}
int T;
int n;
int a[510];
int pos[510];
struct node {
int val, id;
} b[510];
bool cmp(node x, node y) {
return x.val == y.val ? x.id < y.id : x.val < y.val;
}
int m;
int ans[250010];
map<int, int> vis;
int main() {
T = read();
while (T--) {
n = read();
m = 0;
for (int i = 1; i <= n; ++i) a[i] = read(), b[i].val = a[i], b[i].id = i;
sort(b + 1, b + n + 1, cmp);
for (int i = 1; i <= n; ++i) a[b[i].id] = i;
for (int i = 1; i <= n; ++i) pos[a[i]] = i;
for (int i = 1; i <= n - 2; ++i) {
int p = pos[i];
while (p >= i + 2) {
pos[a[p - 1]] = p, pos[a[p - 2]] = p - 1;
pos[a[p]] = p - 2;
swap(a[p - 2], a[p]);
swap(a[p - 1], a[p]);
ans[++m] = p - 2;
p -= 2;
}
if (p == i + 1) {
ans[++m] = i, ans[++m] = i;
pos[a[p]] = p - 1;
pos[a[p - 1]] = p + 1, pos[a[p + 1]] = p;
int t1 = a[p - 1], t2 = a[p + 1];
a[p - 1] = a[p], a[p] = t2, a[p + 1] = t1;
}
}
if (b[a[n - 1]].val > b[a[n]].val) {
vis.clear();
bool flag = 0;
for (int i = 1; i <= n; ++i) {
if (!vis.count(b[i].val)) {
vis[b[i].val] = b[i].id;
a[b[i].id] = i;
} else {
a[b[i].id] = i;
if (!flag) swap(a[vis[b[i].val]], a[b[i].id]);
flag = 1;
vis.erase(b[i].val);
}
}
for (int i = 1; i <= n; ++i) pos[a[i]] = i;
m = 0;
for (int i = 1; i <= n - 2; ++i) {
int p = pos[i];
while (p >= i + 2) {
pos[a[p - 1]] = p, pos[a[p - 2]] = p - 1;
pos[a[p]] = p - 2;
swap(a[p - 2], a[p]);
swap(a[p - 1], a[p]);
ans[++m] = p - 2;
p -= 2;
}
if (p == i + 1) {
ans[++m] = i, ans[++m] = i;
pos[a[p]] = p - 1;
pos[a[p - 1]] = p + 1, pos[a[p + 1]] = p;
int t1 = a[p - 1], t2 = a[p + 1];
a[p - 1] = a[p], a[p] = t2, a[p + 1] = t1;
}
}
if (b[a[n - 1]].val > b[a[n]].val) {
printf("-1\n");
} else {
printf("%d\n", m);
for (int i = 1; i <= m; ++i) printf("%d ", ans[i]);
printf("\n");
}
} else {
printf("%d\n", m);
for (int i = 1; i <= m; ++i) printf("%d ", ans[i]);
printf("\n");
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
void Print_a(vector<int>& a) {
for (int& c : a) {
cout << c << " ";
}
cout << endl;
}
int main() {
int k;
cin >> k;
for (int l = 0; l < k; ++l) {
int n;
cin >> n;
vector<int> a(n), b(n);
vector<int> ans;
int num_op = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
b = a;
sort(b.begin(), b.end());
for (int i = 0; i <= n - 3; i++) {
int j = i;
while (a[j] != b[i]) {
j++;
}
int temp;
while (j != i) {
if (j - i >= 2) {
ans.push_back(j - 2 + 1);
num_op += 1;
temp = a[j - 2];
a[j - 2] = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
j -= 2;
} else if (j - i == 1) {
ans.push_back(j - 1 + 1);
ans.push_back(j - 1 + 1);
num_op += 2;
temp = a[j - 1];
a[j - 1] = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
break;
}
}
}
int i_doub = -1;
if (a[n - 2] > a[n - 1]) {
int doub = b[n - 2];
for (int i = n - 3; i >= 0; --i) {
if (doub == b[i]) {
i_doub = i;
break;
}
doub = b[i];
}
if (i_doub == -1) {
cout << -1 << endl;
} else {
int temp;
for (int i = n - 3; i >= i_doub; --i) {
ans.push_back(i + 1);
num_op += 1;
temp = a[i];
a[i] = a[i + 2];
a[i + 2] = a[i + 1];
a[i + 1] = temp;
}
}
} else {
i_doub = -2;
}
if (i_doub != -1) {
cout << num_op << endl;
for (int& c : ans) {
cout << c << " ";
}
cout << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int T, N;
pair<int, int> arr[500];
int ct[501];
vector<int> mvs;
vector<pair<int, int> > elems;
void rot(int ind) {
pair<int, int> tmp = arr[ind + 2];
arr[ind + 2] = arr[ind + 1];
arr[ind + 1] = arr[ind];
arr[ind] = tmp;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> T;
while (T--) {
cin >> N;
memset(ct, 0, sizeof(int) * 501);
elems.clear();
mvs.clear();
for (int i = (0); i < (N); i++) {
cin >> arr[i].first;
arr[i].second = ct[arr[i].first]++;
elems.emplace_back(arr[i]);
}
sort(elems.begin(), elems.end());
long long inv = 0;
bool dup = 0;
for (int i = (0); i < (N); i++) {
for (int j = (i + 1); j < (N); j++) {
if (arr[j].first < arr[i].first) inv++;
if (arr[j].first == arr[i].first) dup = 1;
}
}
if (dup || !(inv & 1)) {
if (inv & 1) {
for (int i = (0); i < (N - 1); i++)
if (elems[i].first == elems[i + 1].first) {
elems[i].second++;
elems[i + 1].second--;
break;
}
}
for (int i = (N - 1); i >= (0); i--) {
int ind;
for (int j = (0); j < (N); j++)
if (arr[j] == elems[i]) {
ind = j;
break;
}
for (int j = (ind); j < (i); j++) {
mvs.emplace_back(max(0, j - 1));
rot(max(0, j - 1));
}
}
cout << mvs.size() << '\n';
for (int mv : mvs) cout << mv + 1 << ' ';
cout << '\n';
} else
cout << "-1\n";
}
}
|
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using namespace std;
inline int read() {
int x = 0, neg = 1;
char op = getchar();
while (!isdigit(op)) {
if (op == '-') neg = -1;
op = getchar();
}
while (isdigit(op)) {
x = 10 * x + op - '0';
op = getchar();
}
return neg * x;
}
inline void print(int x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
const int N = 505;
pair<int, int> a[N];
int p[N], n;
void rotate(int x) {
swap(p[x], p[x + 2]);
swap(p[x + 1], p[x + 2]);
}
bool check() {
for (register int i = 1; i < n; i++) {
if (p[i] > p[i + 1]) {
return 0;
}
}
return 1;
}
void solve() {
n = read();
for (register int i = 1; i <= n; i++) {
int x = read();
a[i] = make_pair(x, i);
}
sort(a + 1, a + n + 1);
for (register int i = 1; i <= n; i++) {
p[a[i].second] = i;
}
int cnt = 0;
for (register int i = 1; i <= n; i++) {
for (register int j = i + 1; j <= n; j++) {
if (p[i] > p[j]) {
cnt++;
}
}
}
if (cnt & 1) {
for (register int i = 1; i < n; i++) {
if (a[i].first == a[i + 1].first) {
swap(p[a[i].second], p[a[i + 1].second]);
break;
}
}
}
vector<int> ans;
for (register int i = 1; i <= n - 2; i++) {
int pos = min_element(p + i, p + n + 1) - p;
while (pos > i + 1) {
rotate(pos - 2), ans.push_back(pos - 2);
pos -= 2;
}
if (pos > i) {
rotate(i), ans.push_back(i);
rotate(i), ans.push_back(i);
}
}
if (!check()) {
puts("-1");
} else {
printf("%d\n", (int)ans.size());
for (auto v : ans) {
printf("%d ", v);
}
puts("");
}
}
int main() {
int T = read();
while (T--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int const inf = 1e8;
int n;
int a[1 << 10];
void shift(int pos) {
int b[3];
b[0] = a[pos + 2];
b[1] = a[pos];
b[2] = a[pos + 1];
for (int j = 0; j < 3; ++j) a[pos + j] = b[j];
}
void print(vector<int> &ans) {
printf("%d\n", int(ans.size()));
for (int i : ans) {
printf("%d ", i);
}
printf("\n");
}
void solve() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
}
int inv = 0;
vector<int> ans;
for (int i = 1; i < n - 1; ++i) {
int mn = a[i];
int pos = -1;
for (int j = i + 1; j <= n; ++j) {
if (a[j] < mn) {
mn = a[j];
pos = j;
}
}
if (pos == -1) continue;
while (pos - i >= 2) {
ans.push_back(pos - 2);
shift(pos - 2);
pos -= 2;
}
if (pos > i) {
ans.push_back(i);
ans.push_back(i);
shift(i);
shift(i);
}
}
if (is_sorted(a + 1, a + 1 + n)) {
print(ans);
return;
}
if (a[n] == a[n - 2]) {
shift(n - 2);
ans.push_back(n - 2);
return print(ans);
}
int pos = -1;
for (int i = n - 2; i >= 1; --i) {
if (a[i] == a[i - 1]) {
pos = i - 1;
break;
}
}
if (pos == -1) {
puts("-1");
return;
}
for (int i = n - 2; i >= pos; --i) {
shift(i);
ans.push_back(i);
}
assert(is_sorted(a + 1, a + 1 + n));
print(ans);
}
int main() {
int T;
for (scanf("%d", &T); T--;) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
int a[505];
int b[505];
int opt[250050];
int optnum;
void triple_swap(int k) {
swap(a[k + 1], a[k + 2]);
swap(a[k], a[k + 1]);
}
void print() {
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
b[i] = a[i];
}
sort(b, b + n);
optnum = 0;
for (int i = n; i >= 3; i--) {
int maxIdx = 0;
for (int j = 1; j < i; j++)
if (a[j] >= a[maxIdx]) maxIdx = j;
if (maxIdx == 0) {
maxIdx = 1;
opt[optnum++] = 0;
triple_swap(0);
}
for (int j = maxIdx; j < i - 1; j++) {
opt[optnum++] = j - 1;
triple_swap(j - 1);
}
}
bool flag = true;
if (a[0] > a[1]) {
flag = false;
for (int i = 0; i < n - 2; i++) {
opt[optnum++] = i;
opt[optnum++] = i;
triple_swap(i);
triple_swap(i);
if (a[i + 1] == a[i + 2]) break;
}
if (a[n - 2] <= a[n - 1]) flag = true;
}
if (!flag)
printf("-1\n");
else {
printf("%d\n", optnum);
for (int i = 0; i < optnum; i++) {
printf("%d ", opt[i] + 1);
}
printf("\n");
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int N;
pair<int, int> elements[500];
vector<int> freq[501];
vector<int> ans;
void swap(int i) {
ans.emplace_back(i);
pair<int, int> p1 = elements[i], p2 = elements[i + 1], p3 = elements[i + 2];
elements[i + 1] = p1;
elements[i + 2] = p2;
elements[i] = p3;
}
void solve(int size) {
if (size <= 2) {
return;
}
int max_ind = 0;
for (int i = 1; i < size; ++i) {
if (elements[max_ind] < elements[i]) {
max_ind = i;
}
}
for (int i = max_ind; i < size - 1; ++i) {
swap(min(i, size - 3));
}
solve(size - 1);
}
int main(void) {
int T;
scanf("%d", &T);
while (T--) {
ans.clear();
fill(freq, freq + 500, vector<int>());
scanf("%d", &N);
for (int u, i = 0; i < N; ++i) {
scanf("%d", &u);
elements[i] = {u, freq[u].size()};
freq[u].emplace_back(i);
}
int num_inv = 0;
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < N; ++j) {
num_inv += (elements[i] > elements[j]);
}
}
bool poss = false;
if (num_inv % 2 == 0) {
poss = true;
} else {
for (int i = 1; i <= 500; ++i) {
if (freq[i].size() > 1) {
elements[freq[i][0]].second = 1;
elements[freq[i][1]].second = 0;
poss = true;
break;
}
}
}
if (!poss) {
printf("-1\n");
continue;
}
solve(N);
printf("%d\n", (int)ans.size());
for (int i = 0; i < ans.size(); ++i) {
printf("%d ", ++ans[i]);
}
printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v, ans;
void op(int i) {
swap(v[i], v[i + 1]);
swap(v[i], v[i + 2]);
if (ans.size() >= 2 && ans[ans.size() - 1] == i + 1 &&
ans[ans.size() - 2] == i + 1)
ans.pop_back(), ans.pop_back();
else
ans.push_back(i + 1);
}
void reach(int x, int y) {
while (y > x + 1) op(y - 2), y -= 2;
if (y == x + 1) op(y - 1), op(y - 1);
}
int main(int argc, char **argv) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, st = 0, temp = 0;
ans.clear();
cin >> n;
v.resize(n);
vector<int> ar(501, -1);
int val = -1, x, y;
for (int i = 0; i < n; i++) {
cin >> v[i];
if (ar[v[i]] != -1) val = v[i], x = ar[v[i]], y = i;
ar[v[i]] = i;
}
if (val != -1) v[x] = v[y] = 2000;
while (n - st > 3) {
int mi = 1000, in;
for (int j = st; j < n; j++)
if (v[j] < mi) mi = v[j], in = j;
reach(st, in);
st++;
}
if (val == -1) {
while (!(v[st] <= v[st + 1] && v[st + 1] <= v[st + 2])) {
op(st), temp++;
if (temp > 5) {
cout << -1 << endl;
goto brk;
}
}
} else {
while (v[st + 1] != 2000 || v[st + 2] != 2000) op(st);
while (st >= 0 && val < v[st]) op(st), op(st), st--;
}
cout << ans.size() << endl;
for (auto x : ans) cout << x << " ";
cout << endl;
brk:;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n), ans;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
auto cyc = [&](int idx) {
swap(a[idx + 2], a[idx]);
swap(a[idx + 1], a[idx + 2]);
ans.push_back(idx);
};
if (n == 3) {
if (!is_sorted(a.begin(), a.end())) cyc(0);
if (!is_sorted(a.begin(), a.end())) cyc(0);
}
for (int i = 0; i + 2 < n; i++) {
vector<pair<int, int>> b(n);
for (int j = 0; j < n; j++) {
b[j] = {a[j], j};
}
sort(b.begin(), b.end());
if (i < b[i].second) {
for (int k = b[i].second - 2; k >= i; k -= 2) {
cyc(k);
}
if ((b[i].second - i) & 1) {
cyc(i);
cyc(i);
}
}
}
if (a[n - 2] > a[n - 1]) {
int x = -1;
for (int i = 0; i + 2 < n; i++) {
if (a[i] == a[i + 1]) {
x = i;
}
}
if (x != -1) {
for (int i = x; i < n - 4; i++) {
cyc(i);
}
cyc(n - 3);
cyc(n - 4);
for (int i = n - 5; i >= x; i--) {
cyc(i);
cyc(i);
}
}
}
if (a[n - 2] > a[n - 1]) cyc(n - 3);
if (is_sorted(a.begin(), a.end()) && ans.size() <= n * n) {
cout << ans.size() << '\n';
for (auto i : ans) cout << i + 1 << " ";
cout << '\n';
} else {
cout << -1 << '\n';
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500;
pair<int, int> a[MAXN + 1];
int where[MAXN + 1];
vector<int> pos[MAXN + 1];
vector<int> ans;
bool solve(int n) {
vector<pair<int, int>> v(n + 1);
for (int i = 1; i <= n; i++) {
v[i] = a[i];
}
int right = n;
for (int i = MAXN; i >= 1; i--) {
for (int j : pos[i]) {
if (right <= 3) break;
for (int k = where[j]; k <= right - 2; k++) {
ans.push_back(k);
swap(v[k + 1], v[k + 2]);
swap(v[k], v[k + 1]);
where[v[k].second] = k;
where[v[k + 1].second] = k + 1;
where[v[k + 2].second] = k + 2;
}
if (where[j] < right) {
ans.push_back(right - 2);
swap(v[right - 1], v[right]);
swap(v[right - 2], v[right - 1]);
where[v[right].second] = right;
where[v[right - 1].second] = right - 1;
where[v[right - 2].second] = right - 2;
}
right--;
}
}
for (int i = 0; i < 4; i++) {
if (v[1].first <= v[2].first && v[2].first <= v[3].first) break;
ans.push_back(1);
swap(v[2], v[3]);
swap(v[1], v[2]);
if (i == 3) return false;
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
ans.clear();
for (int i = 1; i <= MAXN; i++) {
pos[i].clear();
}
for (int i = 1; i <= n; i++) {
cin >> a[i].first;
a[i].second = i;
pos[a[i].first].push_back(i);
where[i] = i;
}
if (solve(n)) {
cout << (int)ans.size() << "\n";
for (int x : ans) cout << x << " ";
cout << "\n";
} else {
for (int i = 1; i <= n; i++) where[i] = i;
ans.clear();
bool valid = false;
for (int i = 1; i <= MAXN; i++) {
if ((int)pos[i].size() > 1) {
valid = true;
int temp1 = pos[i].back();
pos[i].pop_back();
int temp2 = pos[i].back();
pos[i].pop_back();
pos[i].push_back(temp1);
pos[i].push_back(temp2);
break;
}
}
if (valid) {
solve(n);
cout << (int)ans.size() << "\n";
for (int x : ans) cout << x << " ";
cout << "\n";
} else {
cout << "-1\n";
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n;
cin >> n;
long long a[n], b[n];
set<long long> s;
for (long long i = 0; i < n; i++) {
cin >> a[i];
s.insert(a[i]);
b[i] = a[i];
}
vector<long long> v;
sort(b, b + n);
for (long long i = 0; i < n; i++) {
if (a[i] == b[i])
;
else {
long long in, cnt = 1;
for (long long j = i + 1; j < n; j++) {
if (a[j] == b[i]) {
in = j;
break;
} else {
cnt++;
}
}
if (in == n - 1 && i == n - 2) {
if (s.size() == n) {
cout << -1;
return;
}
if (a[n - 1] == a[n - 3]) {
v.push_back(n - 2);
break;
}
for (long long j = n - 3; j >= 1; j -= 2) {
v.push_back(j);
v.push_back(j);
v.push_back(j + 1);
v.push_back(j + 1);
if (a[j] == a[j - 1]) {
break;
}
if (a[j - 1] == a[j - 2]) {
v.push_back(j - 1);
break;
}
}
break;
}
while (cnt > 1) {
swap(a[in], a[in - 2]);
swap(a[in], a[in - 1]);
in -= 2;
cnt -= 2;
v.push_back(in + 1);
}
if (cnt == 1) {
swap(a[i], a[i + 1]);
swap(a[i + 1], a[i + 2]);
v.push_back(i + 1);
v.push_back(i + 1);
}
}
}
cout << v.size() << endl;
for (long long i = 0; i < v.size(); i++) cout << v[i] << " ";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long tt = 1;
cin >> tt;
while (tt--) {
solve();
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int t, n, olda[505], a[505];
void rot(int ind) {
swap(a[ind], a[ind + 1]);
swap(a[ind], a[ind + 2]);
}
int main() {
cin >> t;
for (int amount = 0; amount < t; amount++) {
cin >> n;
vector<int> ans;
vector<pair<int, int> > v;
for (int i = 1; i <= n; i++) {
cin >> olda[i];
a[i] = olda[i];
v.push_back({a[i], i});
}
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++) a[v[i].second] = i + 1;
for (int i = 1; i <= n - 2; i++) {
int ind = i;
for (int j = i; j <= n; j++) {
if (a[j] == i) {
ind = j;
break;
}
}
while (ind != i) {
if (ind - i >= 2) {
ans.push_back(ind - 2);
rot(ind - 2);
ind -= 2;
} else {
ans.push_back(ind - 1);
ans.push_back(ind - 1);
rot(ind - 1);
rot(ind - 1);
ind--;
}
}
}
if (a[n] == n) {
cout << ans.size() << '\n';
for (int i = 0; i < ans.size(); i++) cout << ans[i] << ' ';
cout << '\n';
continue;
}
v.clear();
ans.clear();
for (int i = 1; i <= n; i++) {
a[i] = olda[i];
v.push_back({a[i], i});
}
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++) a[v[i].second] = i + 1;
for (int i = 1; i < v.size(); i++) {
if (v[i].first == v[i - 1].first) {
swap(a[v[i].second], a[v[i - 1].second]);
break;
}
}
for (int i = 1; i <= n - 2; i++) {
int ind = i;
for (int j = i; j <= n; j++) {
if (a[j] == i) {
ind = j;
break;
}
}
while (ind != i) {
if (ind - i >= 2) {
ans.push_back(ind - 2);
rot(ind - 2);
ind -= 2;
} else {
ans.push_back(ind - 1);
ans.push_back(ind - 1);
rot(ind - 1);
rot(ind - 1);
ind--;
}
}
}
if (a[n] == n) {
cout << ans.size() << '\n';
for (int i = 0; i < ans.size(); i++) cout << ans[i] << ' ';
cout << '\n';
} else
cout << "-1\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> b;
void rotate(int i) {
swap(b[i], b[i + 1]);
swap(b[i], b[i + 2]);
}
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> ans;
bool flag = true;
b = a;
sort(a.begin(), a.end());
int sind = -1;
for (int i = 0; i < n - 1; i++) {
if (a[i] == a[i + 1]) sind = i;
}
for (int i = 0; i < n - 2; i++) {
while (a[i] != b[i]) {
int j;
for (j = i + 1; j < n; j++) {
if (b[j] == a[i]) break;
}
if (i + 2 >= j) {
ans.emplace_back(i + 1);
rotate(i);
} else {
ans.emplace_back(j - 1);
rotate(j - 2);
}
}
}
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) flag = false;
}
if (!flag && sind != -1) {
for (int i = n - 3; i >= sind; i--) {
rotate(i);
ans.emplace_back(i + 1);
}
}
if (flag == false && sind == -1) {
cout << -1 << '\n';
return;
} else {
cout << ans.size() << '\n';
for (int i = 0; i < int(ans.size()); i++) {
cout << ans[i] << " ";
}
cout << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t = 1;
cin >> t;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> v(n), temp, move;
for (int i = 0; i < n; ++i) cin >> v[i];
temp = v;
sort(temp.begin(), temp.end());
for (int i = 0; i < n - 2; ++i) {
int ind;
for (int j = i; j < n; ++j) {
if (v[j] == temp[i]) {
ind = j;
break;
}
}
if (ind == i) continue;
ind -= 2;
while (ind >= i) {
int a = v[ind], b = v[ind + 1], c = v[ind + 2];
v[ind] = c;
v[ind + 1] = a;
v[ind + 2] = b;
move.push_back(ind);
ind -= 2;
}
ind += 2;
if (ind != i) {
--ind;
int a = v[ind], b = v[ind + 1], c = v[ind + 2];
v[ind] = c;
v[ind + 1] = a;
v[ind + 2] = b;
a = v[ind];
b = v[ind + 1];
c = v[ind + 2];
v[ind] = c;
v[ind + 1] = a;
v[ind + 2] = b;
move.push_back(ind);
move.push_back(ind);
}
}
if (v[n - 2] > v[n - 1]) {
if (v[n - 1] == v[n - 3])
move.push_back(n - 3);
else {
int ind = -1;
for (int i = n - 4; i >= 0; --i) {
if (v[i] == v[i + 1]) {
ind = i;
break;
}
}
if (ind == -1) {
cout << -1 << "\n";
return;
}
for (int i = ind; i <= n - 5; ++i) {
move.push_back(i);
}
move.push_back(n - 3);
move.push_back(n - 4);
for (int i = n - 5; i >= ind; --i) {
move.push_back(i);
move.push_back(i);
}
}
cout << move.size() << "\n";
for (auto i : move) cout << i + 1 << " ";
cout << "\n";
} else {
cout << move.size() << "\n";
for (auto i : move) cout << i + 1 << " ";
cout << "\n";
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
void change_stream(char* in, char* out) {
ifstream fin;
fin.open(in);
ofstream fout;
fout.open(out);
if (fin && fout) {
freopen(in, "r", stdin);
freopen(out, "w", stdout);
}
}
long long a[500], renum[500];
void shift(long long i) {
long long p = a[i];
a[i] = a[i + 2];
a[i + 2] = a[i + 1];
a[i + 1] = p;
renum[a[i]] = i;
renum[a[i + 1]] = i + 1;
renum[a[i + 2]] = i + 2;
}
void solve() {
long long n;
cin >> n;
pair<long long, long long> s[n];
for (long long i = 0; i < n; i++) {
cin >> a[i];
s[i] = {a[i], i};
}
sort(s, s + n);
for (long long i = 0; i < n; i++) {
renum[i] = s[i].second;
a[renum[i]] = i;
}
long long sdv = 0;
for (long long i = 0; i < n; i++)
for (long long j = i + 1; j < n; j++)
if (a[i] > a[j]) sdv++;
if (sdv % 2) {
bool flag = false;
for (long long i = 0; i < n - 1; i++)
if (s[i].first == s[i + 1].first) {
swap(a[s[i].second], a[s[i + 1].second]);
renum[a[s[i].second]] = s[i].second;
renum[a[s[i + 1].second]] = s[i + 1].second;
flag = true;
break;
}
if (!flag) {
cout << "-1\n";
return;
}
}
vector<long long> ans;
for (long long i = n - 1; i > 1; i--) {
while (renum[i] < i) {
long long l = max(0ll, renum[i] - 1);
ans.push_back(l);
shift(l);
}
}
cout << ans.size() << "\n";
for (long long i : ans) cout << i + 1 << ' ';
cout << "\n";
}
signed main() {
change_stream("in", "out");
ios::sync_with_stdio(0);
cin.tie(0);
long long test_count = 1;
cin >> test_count;
while (test_count--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> arr, z;
void rotate(int i) {
z.push_back(i + 1);
int x = arr[i + 2];
arr[i + 2] = arr[i + 1];
arr[i + 1] = arr[i];
arr[i] = x;
}
void solve() {
cin >> n;
arr.resize(n);
for (auto &x : arr) cin >> x;
z.clear();
for (int i = 0; i < n - 2; ++i) {
int k = i;
for (int j = i + 1; j < n; ++j)
if (arr[j] < arr[k]) k = j;
for (; k - 2 >= i; k -= 2) rotate(k - 2);
if (k > i) {
rotate(i);
rotate(i);
}
}
for (int i = n - 2; arr[i] > arr[i + 1]; rotate(--i))
if (i == 0) {
cout << -1 << "\n";
return;
}
cout << z.size() << "\n";
for (auto x : z) cout << x << " ";
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int t, a[MAX], n;
vector<int> list1;
void solve() {
scanf("%d", &t);
while (t--) {
list1.clear();
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
for (int i = 1; i < n; ++i) {
for (int j = 1; j <= n - 2; ++j) {
if (a[j] > a[j + 1]) {
swap(a[j], a[j + 2]);
swap(a[j], a[j + 1]);
list1.push_back(j);
list1.push_back(j);
} else if (a[j + 1] > a[j + 2]) {
swap(a[j], a[j + 1]);
swap(a[j], a[j + 2]);
list1.push_back(j);
}
}
}
bool flag = true;
for (int i = 1; i < n; ++i) {
if (a[i] > a[i + 1]) {
flag = false;
break;
}
}
if (!flag)
printf("-1\n");
else {
int len = list1.size();
printf("%d\n", len);
for (int i = 0; i < len; ++i) {
printf("%d ", list1[i]);
}
printf("\n");
}
}
}
int main(void) {
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
auto cycle = [&](int p) {
int x = a[p + 2];
a[p + 2] = a[p + 1];
a[p + 1] = a[p];
a[p] = x;
};
vector<int> ans;
for (int st = 1; st + 2 < n; ++st) {
int pos = st;
for (int i = st + 1; i <= n; ++i) {
if (a[i] < a[pos]) {
pos = i;
}
}
while (pos - 2 >= st) {
ans.push_back(pos - 2);
cycle(pos - 2);
pos = pos - 2;
}
if (pos == st) {
continue;
}
if (pos == st + 1) {
ans.push_back(st);
ans.push_back(st);
cycle(st);
cycle(st);
continue;
}
}
int delta = 0;
while (delta < 3) {
if (a[n - 2] <= a[n - 1] && a[n - 1] <= a[n]) {
break;
}
ans.push_back(n - 2);
cycle(n - 2);
++delta;
}
if (delta == 3) {
if (n == 3) {
cout << "-1\n";
return;
}
ans.pop_back();
ans.pop_back();
ans.pop_back();
while (true) {
if (a[n - 2] <= a[n - 1] && a[n - 2] <= a[n]) {
break;
}
ans.push_back(n - 2);
cycle(n - 2);
}
int same = -1;
for (int i = 1; i <= n - 3; ++i) {
if (a[i] == a[i + 1]) {
same = i;
}
}
if (same == -1) {
cout << "-1\n";
return;
}
int d = 0;
while (same < n - 3) {
ans.push_back(same);
cycle(same);
++same;
++d;
}
ans.push_back(same);
ans.push_back(same);
cycle(same);
cycle(same);
++same;
ans.push_back(same);
ans.push_back(same);
cycle(same);
cycle(same);
while (d > 0) {
ans.push_back(same - 2);
ans.push_back(same - 2);
cycle(same - 2);
cycle(same - 2);
--same;
--d;
}
}
cout << ans.size() << "\n";
for (auto i : ans) {
cout << i << " ";
}
cout << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
inline long long sbt(long long x) { return __builtin_popcountll(x); }
inline long long iceil(double a) { return (long long)(ceil(a)); }
inline long long mul(long long a, long long b,
long long m = (long long)(1e9 + 7)) {
return ((a % m) * (b % m)) % m;
}
inline long long add(long long a, long long b,
long long m = (long long)(1e9 + 7)) {
return (a + b) % m;
}
inline long long sub(long long a, long long b,
long long m = (long long)(1e9 + 7)) {
return (a - b + m) % m;
}
long long fastpow(long long a, long long b,
long long m = (long long)(1e9 + 7)) {
long long res = 1;
while (b > 0) {
if (b & 1) res = mul(res, a, m);
a = mul(a, a, m);
b >>= 1;
}
return res;
}
long long modinv(long long a, long long m = (long long)(1e9 + 7)) {
return fastpow(a, m - 2, m);
}
long long n;
long long a[510], ranks[510];
vector<long long> v;
long long count() {
long long ct = 0;
for (long i = 0; i < n; i++) {
for (auto j = i + 1; j <= n - 1; j++) {
if (ranks[i] > ranks[j]) {
ct++;
}
}
}
return ct;
}
long long moves(long long i) {
long long maxi = -1;
for (auto j = i - 2; j <= i; j++) {
maxi = max(maxi, ranks[j]);
}
long long c = 0, temp;
while (ranks[i] != maxi) {
v.push_back(i - 1);
temp = ranks[i];
ranks[i] = ranks[i - 1];
ranks[i - 1] = ranks[i - 2];
ranks[i - 2] = temp;
c++;
}
return c;
}
void get_ac() {
v.clear();
cin >> n;
set<long long> s;
for (long i = 0; i < n; i++) {
cin >> a[i];
s.insert(a[i]);
}
map<long long, vector<long long> > ind;
for (long i = 0; i < n; i++) {
ind[a[i]].push_back(i);
}
long long rank = 0;
for (auto i : ind) {
for (auto j : i.second) {
ranks[j] = rank++;
}
}
long long inv = count();
if (inv & 1) {
bool f = 0;
for (auto i : ind) {
if ((long long)(i.second.size()) > 1) {
swap(ranks[i.second[0]], ranks[i.second[1]]);
f = 1;
break;
}
}
if (!f) {
cout << -1;
return;
}
}
long long ans = 0;
for (auto i = n - 1; i >= 2; i--) {
for (auto j = 2; j <= i; j++) {
long long c = moves(j);
ans += c;
}
}
if (ans <= n * n) {
cout << ans;
cout << "\n";
for (auto i : v) {
cout << i << " ";
}
} else {
cout << -1;
}
}
int main() {
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
cin.tie(NULL);
long long testcases;
cin >> testcases;
while (testcases--) {
get_ac();
cout << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = (0); ((1) > 0 ? i < (n) : i > (n)); i += (1)) cin >> a[i];
vector<int> p(n);
iota(p.begin(), p.end(), 0);
sort(p.begin(), p.end(), [&](int i, int j) { return a[i] < a[j]; });
vector<int> vis(n);
int cnt = n;
for (int i = (0); ((1) > 0 ? i < (n) : i > (n)); i += (1)) {
if (vis[i]) continue;
for (int j = i; !vis[j]; j = p[j]) vis[j] = 1;
cnt--;
}
if (cnt & 1) {
bool ok = false;
for (int i = (0); ((1) > 0 ? i < (n - 1) : i > (n - 1)); i += (1)) {
if (a[p[i]] == a[p[i + 1]]) {
ok = true;
swap(p[i], p[i + 1]);
break;
}
}
if (!ok) {
cout << -1 << '\n';
return;
}
}
for (int i = (0); ((1) > 0 ? i < (n) : i > (n)); i += (1)) a[p[i]] = i;
vector<int> t;
auto shift = [&](int x) {
t.push_back(x);
swap(a[x], a[x + 2]);
swap(a[x + 1], a[x + 2]);
};
for (int i = (0); ((1) > 0 ? i < (n - 2) : i > (n - 2)); i += (1)) {
int j = i;
while (a[j] != i) j++;
while (j != i) {
if (j - 2 >= i) {
shift(j - 2);
j -= 2;
} else {
shift(j - 1);
j++;
}
}
}
cout << (int)t.size() << '\n';
for (int i = (0); ((1) > 0 ? i < ((int)t.size()) : i > ((int)t.size()));
i += (1))
cout << t[i] + 1 << " \n"[i == (int)t.size() - 1];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for (int i = (0); ((1) > 0 ? i < (t) : i > (t)); i += (1)) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
mt19937 gen(time(0));
bool IS_ONE_TEST = 0;
vector<int> res;
int total = 0;
bool proc(int idx, vector<int> &a) {
if (!total) return 0;
res.push_back(idx + 1);
int temp = a[idx + 2];
a[idx + 2] = a[idx + 1];
a[idx + 1] = a[idx];
a[idx] = temp;
--total;
return 1;
}
void solve() {
int n;
cin >> n;
res.clear();
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
vector<int> b = a;
total = n * n;
sort(a.begin(), a.end());
for (int z = 0; z < n; ++z) {
for (int i = 0; i + 2 < n; ++i) {
int mn = min({b[i], b[i + 1], b[i + 2]});
while (b[i] != mn && proc(i, b))
;
}
for (int i = n - 1; i > 1; --i) {
int mx = max({b[i], b[i - 1], b[i - 2]});
while (b[i] != mx && proc(i - 2, b))
;
}
}
if (a == b) {
cout << res.size() << endl;
for (auto &c : res) cout << c << " ";
cout << endl;
} else
cout << -1 << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << fixed << setprecision(15);
int t;
if (IS_ONE_TEST)
t = 1;
else
cin >> t;
while (t-- > 0) {
solve();
}
}
|
#include <bits/stdc++.h>
inline char nc() {
static char buf[1000000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)
? EOF
: *p1++;
}
inline void read(int &x) {
static char c;
for (c = getchar(); !('0' <= c && c <= '9'); c = getchar())
;
for (x = 0; ('0' <= c && c <= '9'); c = getchar()) x = x * 10 + c - 48;
}
void write(int x) {
if (x < 10) {
putchar('0' + x);
return;
}
write(x / 10);
putchar('0' + x % 10);
}
inline void writeln(int x) {
if (x < 0) putchar('-'), x *= -1;
write(x);
putchar('\n');
}
inline void writel(int x) {
if (x < 0) putchar('-'), x *= -1;
write(x);
putchar(' ');
}
using namespace std;
int T, a[1007], sb, ans[1007 * 1007], n, tot;
void play(int x) {
ans[++tot] = x;
a[x] ^= a[x + 2], a[x + 2] ^= a[x], a[x] ^= a[x + 2];
a[x + 1] ^= a[x + 2], a[x + 2] ^= a[x + 1], a[x + 1] ^= a[x + 2];
}
signed main() {
read(T);
while (T--) {
read(n);
tot = 0;
for (int i = 1; i <= n; i++) read(a[i]);
for (int i = 1; i <= n - 2; i++) {
sb = i;
for (int j = i + 1; j <= n; j++)
if (a[j] < a[sb]) sb = j;
while (sb >= i + 2) play(sb - 2), sb -= 2;
if (sb == i + 1) {
play(i), play(i);
continue;
}
}
for (int i = n; i >= 3; i--) {
sb = i;
for (int j = i - 1; j; j--)
if (a[j] > a[sb]) sb = j;
while (sb <= i - 2) play(sb), play(sb), sb += 2;
if (sb == i - 1) {
play(sb - 1);
continue;
}
}
for (int i = 1; i < n; i++)
if (a[i] > a[i + 1]) tot = -1;
writeln(tot);
if (tot <= 0) continue;
for (int i = 1; i < tot; i++) writel(ans[i]);
writeln(ans[tot]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int ans[255000], a[510], tot;
void move(int u) {
int t1, t2, t3;
t1 = a[u], t2 = a[u + 1], t3 = a[u + 2];
a[u] = t3, a[u + 1] = t1, a[u + 2] = t2;
}
int t, n, mn, mx, u;
int main() {
scanf("%d", &t);
while (t--) {
tot = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n - 2; i++) {
mn = 1000;
for (int j = i; j <= n; j++)
if (a[j] < mn) mn = a[j], u = j;
while (u >= i + 2) ans[++tot] = u - 2, move(u - 2), u -= 2;
if (u == i + 1) move(i), move(i), ans[++tot] = i, ans[++tot] = i;
}
for (int i = n; i >= 3; i--) {
mx = -1;
for (int j = i; j >= 1; j--)
if (a[j] > mx) mx = a[j], u = j;
while (u <= i - 2) ans[++tot] = u, move(u), u += 2;
if (u == i - 1) move(i - 2), ans[++tot] = i - 2;
}
for (int i = 1; i < n; i++)
if (a[i] > a[i + 1]) tot = -1;
if (tot == -1)
printf("-1\n");
else {
printf("%d\n", tot);
for (int i = 1; i <= tot; i++) printf("%d ", ans[i]);
printf("\n");
}
}
return 0;
}
|
#include <bits/stdc++.h>
inline char nc() {
static char buf[1000000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)
? EOF
: *p1++;
}
inline void read(int &x) {
static char c;
for (c = getchar(); !('0' <= c && c <= '9'); c = getchar())
;
for (x = 0; ('0' <= c && c <= '9'); c = getchar()) x = x * 10 + c - 48;
}
void write(int x) {
if (x < 10) {
putchar('0' + x);
return;
}
write(x / 10);
putchar('0' + x % 10);
}
inline void writeln(int x) {
if (x < 0) putchar('-'), x *= -1;
write(x);
putchar('\n');
}
inline void writel(int x) {
if (x < 0) putchar('-'), x *= -1;
write(x);
putchar(' ');
}
using namespace std;
int T, a[1007], sb, ans[1007 * 1007], n, tot;
void play(int x) {
ans[++tot] = x;
a[x] ^= a[x + 2], a[x + 2] ^= a[x], a[x] ^= a[x + 2];
a[x + 1] ^= a[x + 2], a[x + 2] ^= a[x + 1], a[x + 1] ^= a[x + 2];
}
signed main() {
read(T);
while (T--) {
read(n);
tot = 0;
for (int i = 1; i <= n; i++) read(a[i]);
for (int i = 1; i <= n - 2; i++) {
sb = i;
for (int j = i + 1; j <= n; j++)
if (a[j] < a[sb]) sb = j;
while (sb >= i + 2) play(sb - 2), sb -= 2;
if (sb == i + 1) {
play(i), play(i);
}
}
for (int i = n; i >= 3; i--) {
sb = i;
for (int j = i - 1; j; j--)
if (a[j] > a[sb]) sb = j;
while (sb <= i - 2) play(sb), play(sb), sb += 2;
if (sb == i - 1) {
play(sb - 1);
}
}
for (int i = 1; i < n; i++)
if (a[i] > a[i + 1]) tot = -1;
writeln(tot);
if (tot <= 0) continue;
for (int i = 1; i < tot; i++) writel(ans[i]);
writeln(ans[tot]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin >> t;
while (t--) {
long long n, n1, flag2 = 0;
cin >> n;
n1 = n;
vector<long long> vect(n + 1), ans;
set<pair<long long, long long>, greater<pair<long long, long long> > > set1;
map<long long, long long> map1;
for (int i = 1; i <= n; i++) {
cin >> vect[i];
map1[vect[i]] += 1;
if (map1[vect[i]] > 1) flag2 = vect[i];
set1.insert({vect[i], i});
}
while (n1 >= 3) {
pair<long long, long long> p = *set1.begin();
set1.erase(p);
int i;
for (i = p.second; i <= n1 - 2; i += 2) {
ans.push_back(i);
ans.push_back(i);
set1.erase({vect[i + 1], i + 1});
set1.erase({vect[i + 2], i + 2});
set1.insert({vect[i + 1], i});
set1.insert({vect[i + 2], i + 1});
swap(vect[i + 1], vect[i]);
swap(vect[i + 2], vect[i + 1]);
}
if (i == n1 - 1) {
ans.push_back(i - 1);
set1.erase({vect[i - 1], i - 1});
set1.erase({vect[i + 1], i + 1});
set1.insert({vect[i + 1], i - 1});
set1.insert({vect[i - 1], i});
swap(vect[i], vect[i + 1]);
swap(vect[i], vect[i - 1]);
}
n1--;
}
long long val = flag2;
for (int i = 3; i <= n; i++) {
if (vect[i] == flag2) {
flag2 = i;
break;
}
}
if (flag2 != 0 || vect[1] <= vect[2]) {
if (vect[3] == vect[1]) {
ans.push_back(1);
ans.push_back(1);
} else if (vect[1] > vect[2]) {
for (int i = 1; i <= flag2 - 3; i++) {
ans.push_back(i);
}
ans.push_back(flag2 - 2);
ans.push_back(flag2 - 2);
{
ans.push_back(flag2 - 1);
ans.push_back(flag2 - 1);
}
for (int i = flag2 - 3; i >= 1; i--) {
ans.push_back(i);
ans.push_back(i);
}
}
cout << ans.size() << "\n";
for (int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
cout << "\n";
} else
cout << "-1\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
const long long mod = 1e9 + 7;
const long long linf = 1LL << 62;
const double EPS = 1e-7;
template <class T>
void chmin(T& x, T y) {
if (x > y) x = y;
}
template <class T>
void chmax(T& x, T y) {
if (x < y) x = y;
}
void culc(int& a, int& b, int& c) {
swap(b, c);
swap(a, b);
}
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> ans;
for (int i = 0; i < n - 2; i++) {
auto it = min_element(a.begin() + i, a.end()) - a.begin();
if (i % 2 != it % 2) {
if (it < n - 1)
culc(a[it - 1], a[it], a[it + 1]), ans.push_back(it - 1), it++;
else {
culc(a[it - 2], a[it - 1], a[it]);
culc(a[it - 2], a[it - 1], a[it]);
ans.push_back(it - 2);
ans.push_back(it - 2);
it--;
}
}
while (it != i) {
culc(a[it - 2], a[it - 1], a[it]);
ans.push_back(it - 2);
it -= 2;
}
}
bool flag = true;
if (a[n - 1] < a[n - 2]) {
flag = false;
for (int i = n - 3; i >= 0; i--) {
culc(a[i], a[i + 1], a[i + 2]);
ans.push_back(i);
if (a[i] <= a[i + 1]) {
flag = true;
break;
}
}
}
if (!flag) {
cout << -1 << endl;
return;
}
cout << ans.size() << endl;
for (auto u : ans) cout << u + 1 << " ";
cout << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
string ys = "Yes\n", no = "No\n";
string imp = "IMPOSSIBLE\n";
long long mod = 1e9 + 7, inf = 1e9;
long long maxn = 501;
vector<long long> a(maxn);
void d(long long ind) {
swap(a[ind + 1], a[ind + 2]);
swap(a[ind], a[ind + 1]);
return;
}
void print(long long t) {
for (long long i = 0; i < t; i++) {
cout << a[i] << " ";
}
cout << '\n';
return;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
cin >> t;
for (; t; t--) {
long long n;
cin >> n;
vector<long long> b(n);
for (long long i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b.begin(), b.end());
if (n == 3) {
if (a[0] == b[0] && a[1] == b[1] && a[2] == b[2]) {
cout << 0 << '\n' << '\n';
continue;
}
if (a[2] == b[0] && a[0] == b[1] && a[1] == b[2]) {
cout << 1 << '\n' << 1 << '\n';
continue;
}
if (a[1] == b[0] && a[2] == b[1] && a[0] == b[2]) {
cout << 2 << '\n' << 1 << " " << 1 << '\n';
continue;
}
cout << -1 << '\n';
continue;
}
vector<long long> ans;
bool ok = true;
for (long long i = 0; i < n && ok; i++) {
if (a[i] == b[i]) continue;
long long j = i + 1;
for (; j < n; j++)
if (a[j] == b[i]) break;
if (j == i + 1 && j != n - 1) {
ans.push_back(j);
ans.push_back(j);
d(i);
d(i);
continue;
}
if (j == i + 1 && j == n - 1) {
bool fl = true;
long long k = i - 1;
for (; k >= 0; k--) {
if (b[k] == b[k + 1]) {
break;
}
}
if (k == -1) {
ok = false;
cout << -1 << '\n';
break;
}
while (i != k) {
ans.push_back(i);
i--;
}
swap(a[n - 2], a[n - 1]);
continue;
}
if ((j - i) % 2 == 1) {
if (j != n - 1) {
ans.push_back(j);
ans.push_back(j);
d(j - 1);
d(j - 1);
j--;
} else {
ans.push_back(j - 1);
ans.push_back(j - 2);
d(j - 2);
j -= 2;
d(j - 1);
j++;
}
}
while (j != i) {
ans.push_back(j - 1);
d(j - 2);
j -= 2;
}
}
if (ok) {
cout << ans.size() << '\n';
for (auto el : ans) {
cout << el << " ";
}
cout << '\n';
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int a[505];
int sorted_a[505];
void rot(int i) {
i--;
int temp = a[i + 2];
a[i + 2] = a[i + 1];
a[i + 1] = a[i];
a[i] = temp;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int cases;
cin >> cases;
while (cases--) {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
sorted_a[i] = a[i];
}
sort(sorted_a, sorted_a + n);
vector<int> ans;
for (int i = n - 1; i > 1; i--) {
int j = i;
while (a[j] != sorted_a[i]) j--;
if (j == 0) {
ans.push_back(1);
rot(1);
j++;
}
for (; j < i; j++) {
ans.push_back(j);
rot(j);
}
}
if (a[0] != sorted_a[0]) {
if (a[0] == a[2]) {
ans.push_back(1);
ans.push_back(1);
rot(1);
rot(1);
} else {
for (int i = 2; i < n - 1; i++) {
if (a[i] == a[i + 1]) {
for (int j = i; j >= 1; j--) {
ans.push_back(j);
rot(j);
}
break;
}
}
}
}
if (a[0] == sorted_a[0]) {
cout << ans.size() << "\n";
for (int i : ans) cout << i << " ";
cout << "\n";
} else
cout << "-1\n";
}
return 0;
}
|
#include <bits/stdc++.h>
const int N = 502;
void sw(float* x) {
float tmp = *x;
*x = *(x + 2);
*(x + 2) = *(x + 1);
*(x + 1) = tmp;
}
int main() {
int _t;
scanf("%d", &_t);
while (_t--) {
static float arr[N];
static int anss[N * N];
static int tm[N];
memset(tm, 0, sizeof(tm));
int n;
scanf("%d", &n);
int f2 = -1;
for (int i = 1; i <= n; i++) {
int a;
scanf("%d", &a);
tm[a]++;
if (tm[a] == 2) f2 = a;
arr[i] = a + 0.0001 * tm[a];
}
int nx = 0;
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++)
if (arr[i] > arr[j]) nx++;
if (nx & 1) {
if (f2 != -1) {
int f21, f22;
for (int i = 1; i <= n; i++)
if (((int)arr[i]) == f2) {
f21 = i;
break;
}
for (int i = n; i >= 1; i--)
if (((int)arr[i]) == f2) {
f22 = i;
break;
}
std::swap(arr[f21], arr[f22]);
} else {
printf("-1\n");
continue;
}
}
nx = 0;
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++)
if (arr[i] > arr[j]) nx++;
int ans = 0;
while (nx) {
for (int i = 1; i <= n - 2; i++) {
if (arr[i] < arr[i + 2] && arr[i + 2] < arr[i + 1] ||
arr[i] > arr[i + 2] && arr[i + 2] > arr[i + 1]) {
sw(arr + i);
anss[++ans] = i;
}
if (arr[i] < arr[i + 1] && arr[i] > arr[i + 2] ||
arr[i] > arr[i + 1] && arr[i + 1] > arr[i + 2]) {
sw(arr + i);
nx -= 2;
anss[++ans] = i;
}
}
}
printf("%d\n", ans);
for (int i = 1; i <= ans; i++) printf("%d ", anss[i]);
printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
const int N = 502;
void sw(float *x) {
float tmp = *x;
*x = *(x + 2);
*(x + 2) = *(x + 1);
*(x + 1) = tmp;
}
int nxs(float *p, int n) {
int res = 0;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (p[i] > p[j]) res++;
return res;
}
int diff(float *p) {
static float tmp[3];
for (int i = 0; i < 3; i++) tmp[i] = p[i];
sw(tmp);
return nxs(tmp, 3) - nxs(p, 3);
}
int main() {
int _t;
scanf("%d", &_t);
while (_t--) {
static float arr[N];
static int anss[N * N];
static int tm[N];
memset(tm, 0, sizeof(tm));
int n;
scanf("%d", &n);
int f2 = -1;
for (int i = 1; i <= n; i++) {
int a;
scanf("%d", &a);
tm[a]++;
if (tm[a] == 2) f2 = a;
arr[i] = a + 0.0001 * tm[a];
}
int nx = 0;
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++)
if (arr[i] > arr[j]) nx++;
if (nx & 1) {
if (f2 != -1) {
int f21 = -1, f22 = -1;
for (int i = 1; i <= n; i++)
if (((int)arr[i]) == f2) {
if (f21 == -1)
f21 = i;
else if (f22 == -1)
f22 = i;
else
break;
}
std::swap(arr[f21], arr[f22]);
nx++;
} else {
printf("-1\n");
continue;
}
}
int ans = 0;
while (nx)
for (int i = 1; i <= n - 2; i++)
for (int df = diff(arr + i); df <= 0;
sw(arr + i), df = diff(arr + i), anss[++ans] = i)
nx += df;
printf("%d\n", ans);
for (int i = 1; i <= ans; i++) printf("%d ", anss[i]);
printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
const double eps = 1e-9;
const long double pi = acos(-1.0);
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n);
vector<pair<int, int>> b(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = {a[i], i};
}
sort(b.begin(), b.end());
for (int i = 0; i < n; i++) {
a[b[i].second] = i;
}
int kol = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (j > i && a[j] < a[i]) kol++;
}
}
if (kol & 1) {
for (int i = 0; i < n - 1; i++)
if (b[i].first == b[i + 1].first) {
swap(a[b[i].second], a[b[i + 1].second]);
kol ^= 1;
break;
}
}
if (kol & 1) {
cout << -1 << endl;
return;
}
vector<int> ans;
for (int i = 0; i < n - 3; i++) {
int mn = n + 1;
int ind = -1;
for (int j = i; j < n; j++)
if (a[j] < mn) mn = a[j], ind = j;
while (1) {
if (ind - 2 >= i) {
int tmp = a[ind];
a[ind] = a[ind - 1];
a[ind - 1] = a[ind - 2];
a[ind - 2] = tmp;
ind -= 2;
ans.push_back(ind + 1);
} else
break;
}
if (ind != i) {
ans.push_back(i + 1);
ans.push_back(i + 1);
int tmp = a[i];
a[i] = a[i + 1];
a[i + 1] = a[i + 2];
a[i + 2] = tmp;
}
}
if (a[n - 2] > a[n - 1] && a[n - 2] > a[n - 3] && a[n - 3] > a[n - 1]) {
ans.push_back(n - 3 + 1);
} else if (a[n - 3] > a[n - 2] && a[n - 3] > a[n - 1] &&
a[n - 1] > a[n - 2]) {
ans.push_back(n - 3 + 1);
ans.push_back(n - 3 + 1);
}
cout << ans.size() << endl;
for (int x : ans) cout << x << " ";
cout << endl;
}
int main() {
ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
cout.setf(ios::fixed), cout.precision(10);
int step;
step = 1;
cin >> step;
for (int i = 1; i <= step; i++) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 505;
int arr[MAXN];
int arr_copy[MAXN];
void rotate(int ind) {
int num1 = arr[ind], num2 = arr[ind + 1], num3 = arr[ind + 2];
arr[ind] = num3;
arr[ind + 1] = num1;
arr[ind + 2] = num2;
}
void swap_back(int ind, int distance, vector<int>& actions) {
if (distance == 0) return;
if (distance == 1) {
actions.push_back(ind - 1);
actions.push_back(ind - 1);
rotate(ind - 1);
rotate(ind - 1);
return;
}
actions.push_back(ind - 2);
rotate(ind - 2);
swap_back(ind - 2, distance - 2, actions);
}
void swap_back2(int ind, int distance, vector<int>& actions) {
if (distance <= 1) return;
actions.push_back(ind - 2);
rotate(ind - 2);
swap_back2(ind - 2, distance - 2, actions);
}
int main() {
ios_base::sync_with_stdio(0);
int t;
cin >> t;
for (int test = 1; test <= t; test++) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
arr_copy[i] = arr[i];
}
vector<int> actions;
for (int i = 1; i <= n - 2; i++) {
int min_ind = -1, min_val = 1e9;
for (int j = i; j <= n; j++) {
if (arr[j] < min_val) min_val = arr[j], min_ind = j;
}
swap_back(min_ind, min_ind - i, actions);
}
if (arr[n - 1] <= arr[n]) {
cout << actions.size() << '\n';
for (int i : actions) cout << i << ' ';
cout << '\n';
continue;
}
for (int i = 1; i <= n; i++) arr[i] = arr_copy[i] * 501;
int done = 0;
for (int i = 1; i <= n; i++) {
int num = arr[i] + 1;
for (int j = i + 1; j <= n; j++) {
if (arr[i] == arr[j]) {
arr[j] = num;
num++;
}
}
}
actions.clear();
for (int i = 1; i <= n - 2; i++) {
int min_ind = -1, min_val = 1e9;
for (int j = i; j <= n; j++) {
if (arr[j] < min_val) min_val = arr[j], min_ind = j;
}
swap_back(min_ind, min_ind - i, actions);
}
if (arr[n - 1] / 501 <= arr[n] / 501) {
cout << actions.size() << '\n';
for (int i : actions) cout << i << ' ';
cout << '\n';
continue;
}
for (int i = 1; i <= n; i++) arr[i] = arr_copy[i] * 501;
done = 0;
for (int i = 1; i <= n; i++) {
int num = arr[i] + 1;
for (int j = i + 1; j <= n; j++) {
if (arr[i] == arr[j]) {
arr[j] = num;
num++;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (arr[j] == arr[i] + 1) {
swap(arr[i], arr[j]);
done = 1;
break;
}
}
if (done) break;
}
actions.clear();
for (int i = 1; i <= n - 2; i++) {
int min_ind = -1, min_val = 1e9;
for (int j = i; j <= n; j++) {
if (arr[j] < min_val) min_val = arr[j], min_ind = j;
}
swap_back(min_ind, min_ind - i, actions);
}
if (arr[n - 1] / 501 <= arr[n] / 501) {
cout << actions.size() << '\n';
for (int i : actions) cout << i << ' ';
cout << '\n';
continue;
} else
cout << -1 << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
const int N = 502;
void sw(float *x) {
float tmp = *x;
*x = *(x + 2);
*(x + 2) = *(x + 1);
*(x + 1) = tmp;
}
int nxs(float *p, int n) {
int res = 0;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (p[i] > p[j]) res++;
return res;
}
int diff(float *p) {
static float tmp[3];
for (int i = 0; i < 3; i++) tmp[i] = p[i];
sw(tmp);
return nxs(tmp, 3) - nxs(p, 3);
}
int main() {
int _t;
scanf("%d", &_t);
while (_t--) {
static float arr[N];
static int anss[N * N];
static int tm[N];
memset(tm, 0, sizeof(tm));
int n;
scanf("%d", &n);
int f2 = -1;
for (int i = 1; i <= n; i++) {
int a;
scanf("%d", &a);
tm[a]++;
if (tm[a] == 2) f2 = a;
arr[i] = a + 0.0001 * tm[a];
}
int nx = nxs(arr + 1, n);
if (nx & 1) {
if (f2 != -1) {
int f21 = -1, f22 = -1;
for (int i = 1; i <= n; i++)
if (((int)arr[i]) == f2) {
if (f21 == -1)
f21 = i;
else if (f22 == -1)
f22 = i;
else
break;
}
std::swap(arr[f21], arr[f22]);
nx++;
} else {
printf("-1\n");
continue;
}
}
int ans = 0;
while (nx)
for (int i = 1; i <= n - 2; i++)
for (int df = diff(arr + i); df <= 0;
sw(arr + i), df = diff(arr + i), anss[++ans] = i)
nx += df;
printf("%d\n", ans);
for (int i = 1; i <= ans; i++) printf("%d ", anss[i]);
printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n;
cin >> n;
;
string str;
cin >> str;
;
map<long long, long long> mp;
mp[0] = 1;
long long sum = 0;
long long ans = 0;
for (long long i = 0; i < str.size(); i++) {
sum += (str[i] - '0') - 1;
ans += mp[sum];
mp[sum]++;
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long func(long long x) { return (x * (x - 1)) / 2; }
int32_t main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
string s;
cin >> s;
long long a[n];
for (long long i = 0; i < n; i++) a[i] = (long long)(s[i] - '0');
long long prefix[n];
prefix[0] = a[0];
for (long long i = 1; i < n; i++) prefix[i] = prefix[i - 1] + a[i];
long long aux[n];
for (long long i = 0; i < n; i++) aux[i] = (i + 1) - prefix[i];
map<long long, long long> freq;
for (long long i = 0; i < n; i++) freq[aux[i]]++;
long long res = 0;
for (auto s : freq) res += func(s.second);
for (long long i = 0; i < n; i++) {
if (prefix[i] == i + 1) res++;
}
cout << res << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int tt;
cin >> tt;
while (tt--) {
int n;
cin >> n;
vector<int> arr(n), pr(n + 1);
pr[0] = 0;
for (int i = 0; i < n; i++) {
char c;
cin >> c;
arr[i] = c - '0';
pr[i + 1] = pr[i] + arr[i];
}
vector<int> i_minus_pr(n + 1);
map<int, long long> mp;
long long ans = 0;
for (int i = 0; i < n + 1; i++) {
ans += mp[i - pr[i]]++;
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve();
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long _ = 1;
cin >> _;
while (_--) solve();
return 0;
}
void solve() {
long long n;
cin >> n;
string s;
cin >> s;
long long sum = 0, res = 0;
map<long long, long long> mp;
mp[0] = 1;
for (long long i = 1; i <= n; i++) {
sum += s[i - 1] - '0';
res += mp[sum - i];
mp[sum - i]++;
}
cout << res << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
string s;
cin >> s;
vector<long long> sum(n);
sum[0] = s[0] - '0';
map<long long, long long> m;
m[1] = 1;
long long ans = 0;
for (int i = 0; i < n; i++) {
if (i > 0) sum[i] = sum[i - 1] + s[i] - '0';
ans += m[sum[i] - i];
m[sum[i] - i]++;
}
cout << ans << endl;
return;
}
int main() {
long long t;
cin >> t;
while (t--) {
solve();
}
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 t;
cin >> t;
while (t--) {
long long n, ans = 0, p = 0;
map<long long, long long> mp;
mp[0] = 1;
string s;
cin >> n;
cin >> s;
for (long long i = 0; i < (long long)(s).size(); i++) {
if (s[i] == '0')
p--;
else
p += (long long)(s[i] - '0') - 1;
ans += (long long)mp[p];
mp[p]++;
}
cout << ans << '\n';
}
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#pragma GCC optimize("unroll-loops")
using namespace std;
struct fhash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long randint(long long a, long long b) {
return uniform_int_distribution<long long>(a, b)(rng);
}
template <class T>
int fd(const T& n) {
return (int)(n / pow(10, (int)log10(n)));
}
long long fstTrue(function<bool(long long)> f, long long lower_bound,
long long rb) {
while (lower_bound < rb) {
long long mb = (lower_bound + rb) / 2;
f(mb) ? rb = mb : lower_bound = mb + 1;
}
return lower_bound;
}
long long lstTrue(function<bool(long long)> f, long long lower_bound,
long long rb) {
while (lower_bound < rb) {
long long mb = (lower_bound + rb + 1) / 2;
f(mb) ? lower_bound = mb : rb = mb - 1;
}
return lower_bound;
}
bool pow2(int i) { return i && (i & -i) == i; }
constexpr int pct(int x) { return __builtin_popcount(x); }
constexpr int bits(int x) { return 31 - __builtin_clz(x); }
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long cdiv(long long a, long long b) {
return a / b + ((a ^ b) > 0 && a % b);
}
template <class T>
bool chmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
bool chmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
template <class T>
void remdup(vector<T>& v) {
sort((v).begin(), (v).end());
v.erase(unique((v).begin(), (v).end()), end(v));
}
template <class A, class B, class C>
string to_string(tuple<A, B, C> p);
template <class A, class B, class C, class D>
string to_string(tuple<A, B, C, D> p);
string to_string(char c) { return string(1, c); }
string to_string(bool b) { return to_string((int)b); }
string to_string(const char* s) { return (string)s; }
string to_string(string s) { return s; }
template <class A>
string to_string(complex<A> c) {
stringstream ss;
ss << c;
return ss.str();
}
string to_string(vector<bool> v) {
string res = "{";
for (int i = (0); i < (((int)(v).size())); ++i) res += char('0' + v[i]);
res += "}";
return res;
}
template <size_t SZ>
string to_string(bitset<SZ> b) {
string res = "";
for (int i = (0); i < (((int)(b).size())); ++i) res += char('0' + b[i]);
return res;
}
template <class A, class B>
string to_string(pair<A, B> p);
template <class T>
string to_string(T v) {
bool fst = 1;
string res = "";
for (const auto& x : v) {
if (!fst) res += " ";
fst = 0;
res += to_string(x);
}
return res;
}
template <class A, class B>
string to_string(pair<A, B> p) {
return to_string(p.first) + " " + to_string(p.second);
}
template <class A, class B, class C>
string to_string(tuple<A, B, C> p) {
return to_string(get<0>(p)) + " " + to_string(get<1>(p)) + " " +
to_string(get<2>(p));
}
template <class A, class B, class C, class D>
string to_string(tuple<A, B, C, D> p) {
return to_string(get<0>(p)) + " " + to_string(get<1>(p)) + " " +
to_string(get<2>(p)) + " " + to_string(get<3>(p));
}
template <class A>
void re(complex<A>& c);
template <class A, class B>
void re(pair<A, B>& p);
template <class A>
void re(vector<A>& v);
template <class A, size_t SZ>
void re(array<A, SZ>& a);
template <class T>
void re(T& x) {
cin >> x;
}
void re(double& d) {
string t;
re(t);
d = stod(t);
}
void re(long double& d) {
string t;
re(t);
d = stold(t);
}
template <class H, class... T>
void re(H& h, T&... t) {
re(h);
re(t...);
}
template <class A>
void re(complex<A>& c) {
A a, b;
re(a, b);
c = {a, b};
}
template <class A, class B>
void re(pair<A, B>& p) {
re(p.first, p.second);
}
template <class A>
void re(vector<A>& x) {
for (auto& a : x) re(a);
}
template <class A, size_t SZ>
void re(array<A, SZ>& x) {
for (auto& a : x) re(a);
}
template <class A>
void pr(A x) {
cout << to_string(x);
}
template <class H, class... T>
void pr(const H& h, const T&... t) {
pr(h);
pr(t...);
}
void ps() { pr("\n"); }
template <class H, class... T>
void ps(const H& h, const T&... t) {
pr(h);
if (sizeof...(t)) pr(" ");
ps(t...);
}
void DBG() { cerr << "]" << endl; }
template <class H, class... T>
void DBG(H h, T... t) {
cerr << to_string(h);
if (sizeof...(t)) cerr << ", ";
DBG(t...);
}
const int di[4] = {-1, 0, 1, 0}, dj[4] = {0, 1, 0, -1};
const int di8[8] = {-1, -1, 0, 1, 1, 1, 0, -1},
dj8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void IOS(int n) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout.precision(n);
cerr.precision(n);
cerr << fixed;
cout << fixed;
}
void solve() {
int n;
string a;
re(n, a);
long long ans = 0, s = 0;
map<int, int> m;
m[0]++;
for (int i = (0); i < (n); ++i) {
s += a[i] - '0';
ans += m[s - (i + 1)];
m[s - (i + 1)]++;
}
ps(ans);
}
int main() {
IOS(10);
int t = 1;
re(t);
for (int i = (0); i < (t); ++i) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long t;
cin >> t;
while (t--) {
long long n, i, j, a, count1 = 0, sum = 0;
cin >> n;
string s, temp;
cin >> s;
long long arr[n];
for (i = 0; i < n; i++) {
temp = s[i];
arr[i] = stoi(temp);
}
for (i = 0; i < n; i++) {
arr[i] = arr[i] - 1;
}
map<long long, long long> mm;
sum = 0;
mm[0] = 1;
count1 = 0;
for (i = 0; i < n; i++) {
sum = sum + arr[i];
count1 = count1 + mm[sum];
mm[sum]++;
}
cout << count1 << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
const int N = 100005;
int n, ara[N];
int main() {
int t;
cin >> t;
while (t--) {
cin >> n;
string str;
cin >> str;
for (int i = 1; i <= n; i++) {
char ch = str[i - 1];
int d = ch - '0';
ara[i] = d - 1;
}
map<int, int> mp;
mp[0] = 1;
int s = 0;
long long ans = 0;
for (int i = 1; i <= n; i++) {
s += ara[i];
ans += mp[s];
mp[s]++;
}
cout << ans << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const double PI = 3.14159265359;
vector<long long> primes;
void GeneratingPrimes(int top) {
vector<bool> p(top + 1, 1);
p[0] = p[1] = 0;
for (int i = 2; i <= sqrt(top); i++) {
if (p[i]) {
for (int j = i * i; j <= top; j += i) {
p[j] = 0;
}
}
}
for (int i = 0; i < top + 1; i++)
if (p[i]) primes.push_back(i);
}
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
void EXTgcd(long long a, long long b, long long& x0, long long& y0) {
if (b == 0) {
x0 = 1, y0 = 0;
return;
}
EXTgcd(b, a % b, x0, y0);
long long x1 = y0;
long long y1 = x0 - (a / b) * (y0);
y1 = y1;
x0 = x1;
y0 = y1;
}
long long power(long long n, long long p, long long mod) {
if (!p) return 1;
long long ans = 1;
while (p > 1) {
if (p & 1) ans = (ans * n) % mod;
n = (n * n) % mod;
p /= 2;
}
return (ans * n) % mod;
}
double dist(double a, double b, double x, double y) {
return sqrt(pow(a - x, 2) + pow(b - y, 2));
}
double dist3(double a, double b, double c, double x, double y, double z) {
return sqrt(pow(a - x, 2) + pow(b - y, 2) + pow(c - z, 2));
}
int xadd[9] = {1, -1, 0, 1, -1, 0, 1, -1, 0};
int yadd[9] = {1, -1, 0, -1, 0, 1, 0, 1, -1};
int xx[4] = {0, 1, 0, -1};
int yy[4] = {1, 0, -1, 0};
long long const N = 1e5 + 10;
long long const INF = 1e18;
long long const mod = 998244353;
int t, n, a[N];
int main() {
cin >> t;
while (t--) {
long long curSum = 0, ans = 0;
cin >> n;
for (int i = 0; i < n; i++) {
char c;
cin >> c;
a[i] = c - '0' - 1;
}
map<int, long long> memoSum;
memoSum[0]++;
for (int i = 0; i < n; i++) {
curSum += a[i];
ans += memoSum[curSum];
memoSum[curSum]++;
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
string a;
cin >> a;
unordered_map<int, int> mp2;
mp2[0]++;
long long ans = 0, sum = 0;
for (int i = 0; i < n; i++) {
if (a[i] - '0' == 0) {
sum--;
} else {
sum = sum + (a[i] - '0' - 1);
}
if (mp2.find(sum) != mp2.end()) {
ans = ans + mp2[sum];
}
mp2[sum]++;
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string a;
cin >> a;
int b[n + 2];
long long int s = 0;
for (int i = 0; i < n; i++) {
s += (a[i] - '0');
b[i] = s - i;
}
int an = 0;
s = 0;
for (int i = 0; i < n; i++) {
s += a[i] - '0';
if (s == i + 1) an++;
}
sort(b, b + n);
vector<long long int> c;
int ss = 1;
for (int i = 1; i < n; i++) {
if (b[i] != b[i - 1]) {
c.push_back(ss);
ss = 1;
} else {
ss++;
}
}
c.push_back(ss);
long long int ans = 0;
for (int i = 0; i < c.size(); i++) {
if (c[i] > 1) {
ans += (c[i] * (c[i] - 1)) / 2;
}
}
cout << ans + an << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
while (cin >> n) {
for (int x = 0; x < n; x++) {
int a;
cin >> a;
int num[a + 1];
int sum[a + 1];
char word[a + 1];
int res[a + 1];
for (int i = 1; i <= a; i++) {
cin >> word[i];
num[i] = int(word[i]) - 48;
}
sum[0] = 0;
res[0] = 0;
for (int i = 1; i <= a; i++) {
sum[i] = sum[i - 1] + num[i];
res[i] = sum[i] - i;
}
sort(res, res + a + 1);
long long int ans = 0;
long long int tmp = 1;
for (int i = 1; i <= a; i++) {
if (res[i] == res[i - 1] && i != a) {
tmp = tmp + 1;
} else if (res[i] == res[i - 1] && i == a) {
tmp = tmp + 1;
ans = ans + ((tmp - 1) * tmp) / 2;
} else {
ans = ans + ((tmp - 1) * tmp) / 2;
tmp = 1;
}
}
cout << ans << "\n";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
void pvec(vector<long long int> &inp) {
long long int n;
for (n = 0; n < inp.size(); ++n) cout << inp[n] << " ";
cout << endl;
}
void parr(long long int inp[], long long int size) {
long long int n;
for (n = 0; n < size; ++n) cout << inp[n] << " ";
cout << endl;
}
int main() {
long long int t, n, i, k, inp, j;
string s;
cin >> t;
for (i = 0; i < t; ++i) {
cin >> n;
cin >> s;
long long int arr[n], ans = 0;
map<long long int, long long int> mp;
map<long long int, long long int>::iterator itr;
mp.insert(make_pair(0, 1));
for (k = 0; k < n; ++k) arr[k] = (int)s[k] - 48;
for (k = 0; k < n; ++k) {
if (k == 0)
arr[k] = arr[k];
else {
arr[k] = arr[k] + arr[k - 1];
}
}
for (k = 0; k < n; ++k) {
itr = mp.find(arr[k] - (k + 1));
if (itr == mp.end())
mp.insert(make_pair(arr[k] - (k + 1), 1));
else
itr->second += 1;
}
for (itr = mp.begin(); itr != mp.end(); ++itr) {
long long int val = itr->second;
ans += val * (val - 1) / 2;
}
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
void _print(long long t) { cerr << t; }
void _print(int t) { cerr << t; }
void _print(string t) { cerr << t; }
void _print(char t) { cerr << t; }
void _print(long double t) { cerr << t; }
void _print(double t) { cerr << t; }
void _print(unsigned long long t) { cerr << t; }
template <class T, class V>
void _print(pair<T, V> p);
template <class T>
void _print(vector<T> v);
template <class T>
void _print(set<T> v);
template <class T, class V>
void _print(map<T, V> v);
template <class T>
void _print(multiset<T> v);
template <class T, class V>
void _print(pair<T, V> p) {
cerr << "{";
_print(p.first);
cerr << ",";
_print(p.second);
cerr << "}";
}
template <class T>
void _print(vector<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T>
void _print(set<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T>
void _print(multiset<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T, class V>
void _print(map<T, V> v) {
cerr << "[ ";
for (auto i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
void solve() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
string s;
cin >> s;
vector<long long> v(n);
for (long long i = 0; i < n; ++i) {
v[i] = s[i] - '0' - 1;
}
map<long long, long long> m;
m[0] = 1;
long long ans = 0, sum = 0;
for (long long i = 0; i < n; ++i) {
sum += v[i];
if (m.find(sum) != m.end()) ans += m[sum];
m[sum]++;
}
cout << ans << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
string s;
cin >> s;
int a[n];
unordered_map<int, int> mp;
for (int i = 0; i < n; i++) {
a[i] = s[i] - '0';
}
mp[0]++;
long long int curr_sum = 0;
long long int res = 0;
for (int i = 0; i < n; i++) {
curr_sum += a[i];
res += mp[curr_sum - (i + 1)]++;
}
cout << res << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
for (int i = 0; i < t; i++) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int t;
string s;
int main() {
cin >> t;
while (t--) {
int n;
cin >> n;
cin >> s;
vector<int> p(n + 1);
for (int i = 0; i < n; i++) {
p[i + 1] = p[i] + s[i] - '1';
}
map<int, int> cnt;
long long ans = 0;
for (int i = 0; i <= n; ++i) {
ans += cnt[p[i]]++;
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long tc, a;
char b;
int main() {
cin >> tc;
while (tc--) {
cin >> a;
map<long long, long long> m;
long long ps[a + 3];
long long maks = 0;
long long mins = 9999999999;
long long jaw = 0;
m[0]++;
memset(ps, 0, sizeof(ps));
for (int k = 1; k <= a; k++) {
cin >> b;
ps[k] = ps[k - 1] + b - '0';
m[ps[k] - k]++;
maks = max(maks, ps[k] - k);
mins = min(mins, ps[k] - k);
}
for (int k = mins; k <= maks; k++) {
jaw += (m[k] * (m[k] - 1) / 2);
}
cout << jaw << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long t, n, has;
string s;
map<long long, long long> dp;
int main() {
cin >> t;
while (t--) {
dp.clear();
has = 0;
long long now = 0;
dp[0] = 1;
cin >> n >> s;
for (int i = 0; i < n; i++) {
now += s[i] - '0';
has += dp[now - i - 1];
dp[now - i - 1]++;
}
cout << has << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int test;
cin >> test;
while (test--) {
int n;
cin >> n;
vector<int> arr(n);
string str;
cin >> str;
for (int i = 0; i < n; i++) arr[i] = str[i] - '0';
long long count = 0;
long long sum = 0;
long long len = 0;
map<long long, long long> m;
m[0] = 1;
long long t;
for (int i = 0; i < n; i++) {
sum += arr[i];
t = sum - i - 1;
m[t]++;
count += m[t] - 1;
}
cout << count << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200100;
int T, n, a[N];
char s[N];
map<int, int> m;
int main() {
scanf("%d", &T);
for (int t = 1; t <= T; t++) {
scanf("%d %s", &n, s);
a[0] = 0;
for (int i = 0; i < n; i++) {
a[i + 1] = a[i] + s[i] - '0';
}
long long res = 0;
for (int i = 0; i <= n; i++) {
int v = a[i] - i;
if (m.count(v) != 0) {
int k = m[v];
res += k;
m[v] = k + 1;
} else {
m[v] = 1;
}
}
printf("%lld\n", res);
m.clear();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
void _print(long long t) { cerr << t; }
void _print(int t) { cerr << t; }
void _print(string t) { cerr << t; }
void _print(char t) { cerr << t; }
void _print(long double t) { cerr << t; }
void _print(double t) { cerr << t; }
void _print(unsigned long long t) { cerr << t; }
template <class T, class V>
void _print(pair<T, V> p);
template <class T>
void _print(vector<T> v);
template <class T>
void _print(set<T> v);
template <class T, class V>
void _print(map<T, V> v);
template <class T>
void _print(multiset<T> v);
template <class T, class V>
void _print(pair<T, V> p) {
cerr << "{";
_print(p.first);
cerr << ",";
_print(p.second);
cerr << "}";
}
template <class T>
void _print(vector<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T>
void _print(set<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T>
void _print(multiset<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T, class V>
void _print(map<T, V> v) {
cerr << "[ ";
for (auto i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T, class V>
void _print(unordered_map<T, V> v) {
cerr << "[ ";
for (auto i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T, class V>
void _print(unordered_set<T, V> v) {
cerr << "[ ";
for (auto i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
long long gcd(long long a, long long b) {
if (b > a) {
return gcd(b, a);
}
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
long long expo(long long a, long long b, long long mod) {
long long res = 1;
while (b > 0) {
if (b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b = b >> 1;
}
return res;
}
void extendgcd(long long a, long long b, long long *v) {
if (b == 0) {
v[0] = 1;
v[1] = 0;
v[2] = a;
return;
}
extendgcd(b, a % b, v);
long long x = v[1];
v[1] = v[0] - v[1] * (a / b);
v[0] = x;
return;
}
long long mminv(long long a, long long b) {
long long arr[3];
extendgcd(a, b, arr);
return arr[0];
}
long long mminvprime(long long a, long long b) { return expo(a, b - 2, b); }
bool revsort(long long a, long long b) { return a > b; }
void swap(int &x, int &y) {
int temp = x;
x = y;
y = temp;
}
long long combination(long long n, long long r, long long m, long long *fact,
long long *ifact) {
long long val1 = fact[n];
long long val2 = ifact[n - r];
long long val3 = ifact[r];
return (((val1 * val2) % m) * val3) % m;
}
void google(int t) { cout << "Case #" << t << ": "; }
vector<long long> sieve(int n) {
int *arr = new int[n + 1]();
vector<long long> vect;
for (int i = 2; i <= n; i++)
if (arr[i] == 0) {
vect.push_back(i);
for (int j = 2 * i; j <= n; j += i) arr[j] = 1;
}
return vect;
}
long long mod_add(long long a, long long b, long long m) {
a = a % m;
b = b % m;
return (((a + b) % m) + m) % m;
}
long long mod_mul(long long a, long long b, long long m) {
a = a % m;
b = b % m;
return (((a * b) % m) + m) % m;
}
long long mod_sub(long long a, long long b, long long m) {
a = a % m;
b = b % m;
return (((a - b) % m) + m) % m;
}
long long mod_div(long long a, long long b, long long m) {
a = a % m;
b = b % m;
return (mod_mul(a, mminvprime(b, m), m) + m) % m;
}
long long phin(long long n) {
long long number = n;
if (n % 2 == 0) {
number /= 2;
while (n % 2 == 0) n /= 2;
}
for (long long i = 3; i <= sqrt(n); i += 2) {
if (n % i == 0) {
while (n % i == 0) n /= i;
number = (number / i * (i - 1));
}
}
if (n > 1) number = (number / n * (n - 1));
return number;
}
void solve() {
int n;
cin >> n;
vector<int> v(n);
string s;
cin >> s;
for (int i = 0; i < n; i++) {
int a = s[i] - '0' - 1;
v[i] = a;
}
unordered_map<int, int> m;
m[0] = 1;
int curSum = 0;
long long ans = 0;
for (int i = 0; i < n; i++) {
curSum += v[i];
ans += m[curSum];
m[curSum] += 1;
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
auto start1 = high_resolution_clock::now();
long long T = 1;
cin >> T;
for (long long t = 1; t <= T; t++) {
solve();
}
auto stop1 = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop1 - start1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> v(n);
string s;
cin >> s;
for (int i = 0; i < n; i++) {
v[i] = s[i] - '0';
}
unordered_map<long long int, long long int> m;
m[0] = 1;
long long int count = 0;
long long int sum = 0;
for (int i = 0; i < n; i++) {
v[i]--;
sum += v[i];
count += m[sum];
m[sum]++;
}
cout << count << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int p = 0;
map<long long int, long long int> occur;
string s;
set<long long int> occurance;
cin >> s;
long long int ans = 0;
for (long long int i = 0; i < n; i++) {
long long int d = s[i] - '0';
p += d;
if (occur.count(p - (i + 1)) == 0) {
occur[p - (i + 1)] = 0;
}
if (p == i + 1) {
ans++;
}
occur[p - (i + 1)]++;
occurance.insert(p - (i + 1));
};
for (auto x : occurance) {
ans += ((occur[x] * (occur[x] - 1)) / 2);
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int countP(vector<long long int> arr, long long int n) {
unordered_map<long long int, long long int> map;
for (long long int i = 0; i < n; i++) map[arr[i] - i]++;
long long int res = 0;
for (auto x : map) {
long long int cnt = x.second;
res += ((cnt * (cnt - 1)) / 2);
}
return res;
}
void solve() {
long long int n, ans = 0, sm = 0, x;
cin >> n;
string s;
cin >> s;
vector<long long int> v;
for (long long int i = 0; i < s.length(); i++) {
x = (long long int)s[i] - 48;
v.push_back(x);
sm += x;
}
vector<long long int> sum(n);
sum[0] = v[0];
for (long long int i = 1; i <= n - 1; i++) {
sum[i] = sum[i - 1] + v[i];
}
sum.insert(sum.begin(), 0);
ans += countP(sum, n + 1);
cout << ans << "\n";
}
int main() {
int t = 1;
scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, t;
map<long long, long long> mp;
char s[100010];
signed main() {
cin >> t;
while (t--) {
cin >> n;
scanf("%s", s + 1);
long long ans = 0, now = 0;
mp.clear();
mp[0] = 1;
for (long long i = 1; i <= n; i++) {
now += (s[i] - '0');
now--;
ans += mp[now];
mp[now]++;
}
cout << ans << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 20;
long long int ans;
int A[maxn];
void DAQ(int l, int r) {
if (r - l < 2) {
if (A[l] == 1) ans++;
return;
}
int mid = (l + r) >> 1;
vector<int> R;
R.push_back(A[mid]);
for (int i = mid + 1; i < r; i++) R.push_back(R.back() + A[i]);
for (int i = 0; i < R.size(); i++) R[i] -= i + 1;
sort(R.begin(), R.end());
int sum = 0;
for (int i = mid - 1; i >= l; i--) {
sum += A[i];
int x = mid - i - sum;
ans +=
upper_bound(R.begin(), R.end(), x) - lower_bound(R.begin(), R.end(), x);
}
DAQ(l, mid);
DAQ(mid, r);
}
void solve() {
int n;
cin >> n;
string s;
cin >> s;
for (int i = 0; i < n; i++) {
A[i] = s[i] - 48;
}
DAQ(0, n);
cout << ans << "\n";
ans = 0;
}
int main() {
ios_base::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;
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n;
string str;
cin >> n >> str;
long long int i, j, c = 0;
long long int a[n], b[n], sum[n];
for (i = 0; i < n; i++) {
a[i] = str[i] - '0';
}
sum[0] = a[0];
for (i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
}
for (i = 0; i < n; i++) {
b[i] = sum[i] - (i + 1);
if (b[i] == 0) c++;
}
map<long long int, long long int> make_pair;
vector<long long int> v;
for (i = 0; i < n; i++) {
if (make_pair[b[i]] == 0) {
v.push_back(b[i]);
}
make_pair[b[i]]++;
}
for (i = 0; i < v.size(); i++) {
if (make_pair[v[i]] > 1) {
long long int x = make_pair[v[i]];
c += ((x * (x - 1)) / 2);
}
}
cout << c << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long INF = 1e9 + 7;
int MOD = 998244353;
ostream& operator<<(ostream& out, vector<int>& v) {
for (int num : v) out << num << ' ';
return out;
}
istream& operator>>(istream& in, vector<int>& v) {
for (int i = 0; i < v.size(); i++) in >> v[i];
return in;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
vector<int> sums(1e6);
long long ans = 0, sum = 0;
for (int i = 0; i < n; i++) {
int a = s[i] - '1';
sum += a;
if (sum == 0) ans++;
ans += sums[sum + 1e5];
sums[sum + 1e5]++;
}
cout << ans << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
void _R(T &x) {
cin >> x;
}
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template <class T, class... U>
void R(T &head, U &...tail) {
_R(head);
R(tail...);
}
template <class T>
void _W(const T &x) {
cout << x;
}
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) {
_W(x.first);
putchar(' ');
_W(x.second);
}
template <class T>
void _W(const vector<T> &x) {
for (auto i = x.begin(); i != x.end(); _W(*i++))
if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &...tail) {
_W(head);
putchar(sizeof...(tail) ? ' ' : '\n');
W(tail...);
}
int MOD = 1e9 + 7;
void ADD(long long &x, long long v) {
x = (x + v) % MOD;
if (x < 0) x += MOD;
}
const int SIZE = 1 << 20;
void solve() {
int n;
R(n);
string s;
R(s);
map<int, int> AA;
AA[0]++;
int now = 0;
for (int i = 0; i < (((int)(s).size())); ++i) {
now += s[i] - '0' - 1;
AA[now]++;
}
long long an = 0;
for (auto &[x, y] : AA) an += y * (y - 1LL) / 2;
W(an);
}
int main() {
int ___T;
scanf("%d", &___T);
for (int cs = 1; cs <= ___T; cs++) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve();
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int t = 1;
cin >> t;
while (t--) solve();
return 0;
}
void solve() {
long long int i, j;
long long int n;
cin >> n;
string s;
cin >> s;
vector<long long int> pref(n);
for (long long int i = 0; i < s.length(); i++) {
long long int x = s[i] - '0';
x--;
pref[i] = x;
}
long long int sum = 0, ans = 0;
map<long long int, long long int> mp;
mp[0] = 1;
for (long long int i = 0; i < s.length(); i++) {
sum += pref[i];
ans += mp[sum];
mp[sum]++;
}
cout << ans << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
char a[100001];
long long b[2000001];
long long c[100001];
long long qh[100001];
long long ans = 0;
template <typename T>
inline void read(T &x) {
x = 0;
int f = 1;
char c = getchar();
while (c > '9' || c < '0') {
if (c == '-') f = -1;
c = getchar();
}
while (c <= '9' && c >= '0') {
x *= 10;
x = x + c - '0';
c = getchar();
}
x *= f;
}
int main() {
int T;
cin >> T;
for (int i = 1; i <= 100000; i++) {
qh[i] = qh[i - 1] + i;
}
while (T--) {
ans = 0;
int n;
cin >> n;
memset(b, 0, sizeof(b));
for (int i = 1; i <= n; i++) {
cin >> a[i];
c[i] = a[i] - '1';
}
for (int i = 1; i <= n; i++) {
c[i] += c[i - 1];
b[c[i] + 1000000]++;
if (c[i] == 0) {
ans += qh[b[c[i] + 1000000]];
ans -= qh[b[c[i] + 1000000] - 1];
}
if (c[i] != 0) {
if (b[c[i] + 1000000] >= 2) {
ans += qh[b[c[i] + 1000000] - 1];
ans -= qh[b[c[i] + 1000000] - 2];
}
}
}
cout << ans << endl;
}
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.