I have recently encountered a problem while solving the issue. Can anyone provide me hints or solution for this.
Problem : https://www.spoj.com/problems/IITD1/
Thanks in Advance. Please find attached my code below. let me know on how to improve.
#include<iostream>
#define ll long int
using namespace std;
ll *seq;
int len;
void swap(int a,int b){
//cout << a << " " << b << endl;
ll temp;
temp = seq[b];
seq[b] = seq[a];
seq[a] = temp;
}
ll reverse(int i,int j){
ll count=0;
for(int k=i;k<=(i+j-1)/2;k++){
swap(k,i+j-k);
count++;
}
return count;
}
ll sort(){
ll swapCount=0;
for(int i=0; i<len;i++){
for(int j=i+1; j<len;j++){
if(seq[i]>seq[j]){
swapCount+=reverse(i,j);
}
}
for(int j=len-2;j>=i+2;j--)
swapCount+=reverse(i+1,j);
}
return swapCount;
}
int main(){
cin >> len;
seq = new long int[len];
for(int i=0;i<len;i++){
cin >> seq[i];
}
cout << sort() << endl;
/*
for(int i=0;i<len;i++){
cout << seq[i] << " ";
}
cout << endl;
*/
return 0;
}