Help Needed

Please could someone help me with this

This is Arranging Books a part of the Junior CCC Contest 2020

My Code:
It fails duplicates - “SSLL”

#include<bits/stdc++.h>
#define ll long long int
using namespace std;

int main(){

string s;
cin>>s;
ll n = s.size();
if(n==0){
    return 0;
}
ll arr[s.size()];
ll temp[s.size()];

for(ll i=0;i<s.size();i++){
    if(s[i]=='L'){
        arr[i] = 1;
        temp[i] = 1;
    }
    else if(s[i]=='M'){
        arr[i] = 2;
        temp[i] = 2;
    }
    else{
        arr[i] = 3;
        temp[i] = 3;
    }
}

ll ans = 0;
map<ll,ll>h;
sort(temp,temp+n);

for(ll i=0;i<n;i++){
    h[arr[i]] = i;
}

for (ll i = 0; i < n; i++){
   
    if (arr[i] != temp[i])
    {
      ans++;
      ll init = arr[i];

      swap(arr[i], arr[h[temp[i]]]);
 
      h[init] = h[temp[i]];
      h[temp[i]] = i;
    }
}
cout<<ans<<"\n";

}

I know that you are posting also the link, but can you format your code and post also the problem statement? Thanks

Just count the number of positions where the original string differs from the target string and will just be the half of that

CODE
#include "bits/stdc++.h"
using namespace std ;
int main(){    
    string s ;
    cin>>s ;
    string t =s ;
    sort(t.begin(),t.end()) ;
    int ans=0 ;
    for(int i=0;i<s.size();i++)   
        ans+=t[i]!=s[i] ;
    cout<<ans/2<<'\n' ;
}
1 Like