#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "sorting.h"
#define ANSWER {\
rep(i, ans.size()) {\
P[i] = ans[i].ff;\
Q[i] = ans[i].ss;\
}\
return ans.size();\
}
VI bit;
void add(int i, int s) {
while (i < bit.size()) bit[i] += s, i += i&-i;
}
int get(int i) {
int ret = 0;
while (i > 0) ret += bit[i], i -= i&-i;
return ret;
}
ll inversions(VI a) {
int n = a.size();
ll ret = 0;
bit = VI(n+1);
rep(i, n) {
ret += i - get(a[i]+1);
add(a[i]+1, +1);
}
return ret;
}
VI operator*(VI a, PII s) {
swap(a[s.ff], a[s.ss]);
return a;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
VPI ans;
int n = N;
int m = M;
VI a(n);
rep(i, n) a[i] = S[i];
VPI op(m);
rep(i, M) op[i] = mkp(X[i], Y[i]);
int step = 0;
while (inversions(a)) {
PII best;
ll bestinv = 1e18;
rep(u, n) rep(v, n) {
ll curinv = inversions(a * op[step] * mkp(u, v));
if (curinv < bestinv) {
best = mkp(u, v);
bestinv = curinv;
}
}
ans.pb(best);
a = a * op[step] * best;
step++;
}
ANSWER
return 0;
/*bool sub3 = false;*/
/*if (X[0] == 0 && Y[0] == 1) sub3 = true;*/
/*if (issorted()) ANSWER;*/
/*int ptr = 0;*/
/*UPDATE(X[ptr], Y[ptr])*/
/*ptr++;*/
/*if (sub3) {*/
/* if (ind[0] > 1) {*/
/* if (ind[1] == 0) MYSTEP(ind[0], 1)*/
/* else MYSTEP(ind[0], 0)*/
/* }*/
/* if (ind[1] > 1) {*/
/* if (ind[0] == 1) MYSTEP(ind[1], 0)*/
/* else MYSTEP(ind[1], 1)*/
/* }*/
/* replr(i, 2, n-1) {*/
/* MYSTEP(ind[i], i)*/
/* }*/
/* MYSTEP(0, 0);*/
/*} else {*/
/* rep(i, n) {*/
/* MYSTEP(ind[i], i)*/
/* }*/
/*}*/
/*ANSWER;*/
}
Compilation message
sorting.cpp: In function 'void add(int, int)':
sorting.cpp:44:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | while (i < bit.size()) bit[i] += s, i += i&-i;
| ~~^~~~~~~~~~~~
sorting.cpp: In function 'll inversions(VI)':
sorting.cpp:52:19: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
52 | int n = a.size();
| ~~~~~~^~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:39:24: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
39 | return ans.size();\
| ~~~~~~~~^~
sorting.cpp:89:5: note: in expansion of macro 'ANSWER'
89 | ANSWER
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
436 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
436 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
732 ms |
480 KB |
Output is correct |
11 |
Correct |
830 ms |
348 KB |
Output is correct |
12 |
Correct |
724 ms |
480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Execution timed out |
1091 ms |
348 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
436 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
732 ms |
480 KB |
Output is correct |
11 |
Correct |
830 ms |
348 KB |
Output is correct |
12 |
Correct |
724 ms |
480 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Execution timed out |
1091 ms |
348 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1028 ms |
600 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1028 ms |
600 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |