#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
#define nn "\n"
#define x_x ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define intt int _; cin >> _; while (_--)
#define emp push_back
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define ld long double
#define A first
#define B second
typedef long long ll;
const ld eps = 1e-27;
// diff between decimals 0.000000001 mt19937 mt(time(nullptr));
int fx[]={1,-1,0,0}, fy[]={0,0,1,-1};
int main() {
/*freopen("teleport.in","r",stdin);
freopen("teleport.out","w", stdout);*/
x_x
int n; string s; cin>>n>>s; int ar[n];
int ans=0; int z=n; vector<char>v;
for (int i=0; i<n; i++) ar[i]=i;
for (int i=0; i<n-1; i++) {
if (s[i]==s[i+1]) {
int j=i+1;
while(j<n&&(s[j]==s[i]))++j;
if (j==n){z=i; break;}
ans+=(j-i-1);
for (int k=j; k>(i+1); k--) {
swap(s[k],s[k-1]);
}
//v.emp({s[i],ar[i]}),v.emp({s[j],ar[i]+1}); s[j]='0';
}
}
for (; z<(n-1); z++)
{
bool f=0;
int i=z; char j=s[z];
while (i>0) {
ans++; i--;
if (i) {
if (s[i]!=j&&s[i-1]!=j) {
swap(s[i],s[i+1]); f=1; break;
}
}
else if(s[i]!=j) {
swap(s[i],s[i+1]); f=1; break;
}
}
if (!f) return cout<<-1, 0;
}
cout<<ans;
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:27:33: warning: variable 'ar' set but not used [-Wunused-but-set-variable]
27 | int n; string s; cin>>n>>s; int ar[n];
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |