-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol.cpp
More file actions
executable file
·87 lines (69 loc) · 1.37 KB
/
Copy pathsol.cpp
File metadata and controls
executable file
·87 lines (69 loc) · 1.37 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
#include <bits/stdc++.h>
using namespace std;
string parseInput()
{
int n;
cin >> n;
string s;
cin >> s;
return s;
}
pair<int, int> countDigits(string s)
{
int zero = 0;
int one = 0;
for (int i = 0; i < s.length(); ++i)
{
if (s[i] == '0')
++zero;
else
++one;
}
return {zero, one};
}
bool canShuffleToPalindrome(string s)
{
auto [zero, one] = countDigits(s);
bool all_same = zero == s.length() || one == s.length();
bool even_split = zero == one;
bool both_even = zero % 2 == 0 && one % 2 == 0;
if (s.length() % 2 == 0)
{
return all_same || even_split || both_even;
}
else
{
bool off_by_one = abs(zero - one) == 1 || zero == 1 || one == 1;
bool evenish = (zero % 2) ^ (one % 2);
return all_same || off_by_one || evenish;
}
}
int removeFixpoints(const vector<int> &V)
{
int index = 1;
int inserts = 0;
for (int i = 0; i < V.size(); ++i)
{
if (V[i] == index)
{
++index;
++inserts;
}
++index;
}
return inserts;
}
int main()
{
int T;
cin >> T;
while (T--)
{
string s = parseInput();
if (canShuffleToPalindrome(s))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}