-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1556B.cpp
More file actions
95 lines (85 loc) · 2.16 KB
/
1556B.cpp
File metadata and controls
95 lines (85 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int INF = 0x66554433;
inline void Quick_IO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
int main() {
Quick_IO();
int t;
cin >> t;
while (t--) {
int n, odd=0, even=0;
cin>>n;
int arr[n];
// 1은 0101...일 때, 2는 1010...일 때
deque<int> oddQ1, oddQ2;
deque<int> evenQ1, evenQ2;
for (int i = 0, num; i < n; ++i) {
cin>>num;
arr[i] = num%2;
if(i%2!=arr[i]){
if(arr[i]%2==1){
evenQ1.push_back(i);
}else{
oddQ1.push_back(i);
}
}
if((i+1)%2!=arr[i]){
if(arr[i]%2==1){
evenQ2.push_back(i);
}else{
oddQ2.push_back(i);
}
}
if(num%2==1) {
odd++;
}
else{
even++;
}
}
if(abs(odd-even)>1){
cout<<-1<<'\n';
continue;
}
int answer1 = 0, answer2 = 0;
// cout<<"even: ";
// for(auto x: evenQ1) cout<<x<<' ';
// cout<<'\n';
// cout<<"odd: ";
// for(auto x: oddQ1) cout<<x<<' ';
// cout<<'\n';
//
// cout<<"even: ";
// for(auto x: evenQ2) cout<<x<<' ';
// cout<<'\n';
// cout<<"odd: ";
// for(auto x: oddQ2) cout<<x<<' ';
// cout<<'\n';
// 1로 시작
if(evenQ2.size()==oddQ2.size()){
int size = (int)evenQ2.size();
for (int i = 0; i < size; ++i) {
answer2+=abs(evenQ2[i] - oddQ2[i]);
}
}else{
answer2 = INF;
}
if(evenQ1.size()==oddQ1.size()){
int size = (int)evenQ1.size();
for (int i = 0; i < size; ++i) {
answer1+=abs(evenQ1[i] - oddQ1[i]);
}
}else{
answer1 = INF;
}
// 0으로 시작
cout<<min(answer1, answer2)<<'\n';
}
return 0;
}