Submission #538820

#TimeUsernameProblemLanguageResultExecution timeMemory
538820AntekbGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++14
0 / 100
3 ms212 KiB
#include<bits/stdc++.h> using namespace std; int ans; string s; void swa(int i){ if(s[i]!=s[i+1]){ swap(s[i], s[i+1]); ans++; } } int main(){ int n; cin>>n; cin>>s; if(n==1){ cout<<0; return 0; } //s='a'+s; for(int i=0; i<n; i++){ if(i>=2 && (s[i-1]==s[i-2] && s[i-1]!=s[i])){ for(int j=i-1; ;j--){ if(j==0 || s[j]!=s[j-1]){ ans+=i-j-1; swap(s[i], s[j+1]); break; } } } } //cout<<s<<"\n"; reverse(s.begin(), s.end()); for(int i=0; i<n; i++){ if(i>=2 && (s[i-1]==s[i-2] && s[i-1]!=s[i])){ for(int j=i-1; ; j--){ if(j==0 || s[j]!=s[j-1]){ ans+=i-j-1; swap(s[i], s[j+1]); break; } } } } //cout<<s<<"\n"; if(s[n-1]==s[n-2])cout<<"-1"; else cout<<ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...