-
Notifications
You must be signed in to change notification settings - Fork 0
/
DEVCLASS.cpp
124 lines (113 loc) · 1.54 KB
/
DEVCLASS.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int abs(int x)
{
return (x>0)?x:-x;
}
int type;
string str;
ll solve(stack<int>& qg , stack<int>& qb)
{
int a,b,c;
ll cost = 0;
if(type == 0)
return qg.size();
while(!qb.empty())
{
a = qb.top(); qb.pop();
b = qg.top(); qg.pop();
c = abs(b-a);
cost += c;
/*
if(type == 1)
cost += c;
else
cost += 4*(c/2) + c%2;
*/
}
return cost;
}
ll start_b()
{
stack<int> qb;
stack<int> qg;
int l = str.length();
for (int i = 0; i < l; ++i)
{
if(i%2==0)
{
if(str[i] != 'G')
{
qg.push(i);
}
}
else
{
if(str[i] != 'B')
{
qb.push(i);
}
}
}
return solve(qb,qg);
}
ll start_g()
{
stack<int> qb;
stack<int> qg;
int l = str.length();
for (int i = 0; i < l; ++i)
{
if(i%2==0)
{
if(str[i] != 'B')
qb.push(i);
}
else
{
if(str[i] != 'G')
qg.push(i);
}
}
return solve(qb,qg);
}
int main()
{
int t,g,b;
ll cost1,cost2;
cin.sync_with_stdio(0);
cout.sync_with_stdio(0);
cin>>t;
while(t--)
{
g = b = 0;
cin>>type;
cin>>str;
int l = str.length();
for (int i = 0; i < l; ++i)
{
if(str[i] == 'G') g++;
else b++;
}
if(l%2==1 and abs(g-b) != 1)
{ cout<<"-1"<<endl; continue; }
if(l%2==0 and g!=b)
{ cout<<"-1"<<endl; continue; }
if(l%2 == 0)
{
cost1 = start_b();
cost2 = start_g();
// cout<<cost1<<" "<<cost2<<endl;
cout<<min(cost1,cost2)<<endl;
}
else
{
if(g>b)
cost1 = start_b();
else
cost1 = start_g();
cout<<cost1<<endl;
}
}
}