# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
352260 | spike1236 | Arranging Shoes (IOI19_shoes) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ll long long
#define ld long double
#define all(_v) _v.begin(), _v.end()
#define sz(_v) (int)_v.size()
#define pii pair <int, int>
#define pll pair <ll, ll>
#define veci vector <int>
#define vecll vector <ll>
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
const int MAXN = 2e5 + 10;
int a[MAXN];
void solve() {
int n;
cin >> n;
bool ch5 = 0;
for(int i = 1; i <= 2 * n; ++i) {
cin >> a[i];
if(i > n && -a[i - n] != a[i])
ch5 = 1;
if(i <= n && a[i] > 0)
ch5 = 1;
if(i > n && a[i] < 0)
ch5 = 1;
}
if(n == 1) {
cout << (a[1] > 0);
return;
}
if(!ch5) {
cout << n * 1ll * (n - 1) / 2;
return;
}
int ans = 0;
if(n <= 1000) {
for(int i = 1; i <= 2 * n; i += 2) {
if(a[i] > 0) {
for(int j = i + 1; j <= 2 * n; ++j) {
if(a[j] == -a[i]) {
for(int k = j; k > i; --k)
++ans, swap(a[k], a[k - 1]);
break;
}
}
} else {
for(int j = i + 1; j <= 2 * n; ++j) {
if(-a[j] == a[i]) {
for(int k = j; k > i + 1; --k)
++ans, swap(a[k], a[k - 1]);
break;
}
}
}
}
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
///cin >> T;
while(T--) solve(), cout << '\n';
return 0;
}