# |
Author |
Problem |
Lang |
Verdict |
Time |
Memory |
Sent |
Judged |
|
130615105 |
Practice:
KunalHS |
1556B
- 33
|
C++17 (GCC 7-32)
|
Wrong answer on test 2
|
31 ms
|
8 KB
|
2021-10-03 07:57:54 |
2021-10-03 07:57:54 |
|
#include <bits/stdc++.h>
using namespace std;
#define fo(i, n) for (int i = 0; i < n; i++)
#define count(n) for (int i = 1; i <= n; i++)
#define ll long long int
#define l long int
#define ld long double
#define ia(arr, n) fo(i, n) cin >> arr[i]
#define mo % 1000000007
#define precise(x) fixed << setprecision(x)
int weightOfBinary(ll n)
{
int ans = 0;
while (n != 0)
{
n = n & (n - 1);
ans++;
}
return ans;
}
template <typename T>
bool isSet(T n, int bit)
{
if ((n >> (bit)) & 1)
{
return 1;
}
return 0;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
ll a[n];
bool nev = 0;
if (n % 2 == 0)
{
nev = 1;
}
int ne = 0, no = 0;
fo(i, n)
{
cin >> a[i];
if (a[i] % 2 == 0)
{
a[i] = 0;
ne++;
}
else
{
a[i] = 1;
no++;
}
}
if (abs(ne - no) <= 1)
{
int ans = 0;
if (nev)
{
int a1 = 0, a2 = 0, d1 = -1, d2 = -1; // d1 is for EO, d2 is for OE
fo(i, n)
{
if (isSet(a[i] ^ i, 0) == 0) // Correct place for EO
{
if (d2 == -1)
{
d2 = i;
}
else
{
a2 += (i - d2);
d2 = -1;
}
}
else // Correct place OE
{
if (d1 == -1)
{
d1 = i;
}
else
{
a1 += (i - d1);
d1 = -1;
}
}
}
ans = min(a1, a2);
}
else
{
int d = -1;
bool f = 1;
if (no > ne)
{
f = 0;
}
fo(i, n)
{
if (isSet(((a[i] ^ i) ^ f), 0) == 0)
{
if (d == -1)
{
d = i;
}
else
{
ans += (i - d);
d = -1;
}
}
}
}
cout << ans << "\n";
}
else
{
cout << "-1\n";
}
}
return 0;
}
Click to see test details