I'm tring to sort a subset of a vector with sort function . the problem is that the program compile perfectly but when debugging I noticed that sort function isn't actually sorting !!
so my question here is there anyway to detect if the sort function failed to sort the vector array string ..etc ? and how to solve such a problem .
#include <bits/stdc++.h>
#define FOR(i,j) for( int i = 0 ; i < j;i++)
using namespace std;
int findMinDiff(vector< int> arr, int b, int e)
{
// Sort vector in non-decreasing order
sort(arr.begin()+b, arr.begin()+e);
// Initialize difference as infinite
int diff = INT_MAX;
// Find the min diff by comparing adjacent
// pairs in sorted vector
for (vector< int>::iterator i=arr.begin()+b; i!=arr.begin()+e-1; i++){
if (*(i+1)-*i < diff)
diff = *(i+1)-*i ;
}
// Return min diff
return diff;
}
int main() {
int n, q;
cin >> n >> q;
vector< int> a(n);
int last=0;
FOR(i,n){
cin>> a[i];
}
int x,y;
int l=0,r=0;
int temp;
FOR(i,q){
cin>>x;
cin>>y;
l=1+(x+last)%n;
r=1+(y+last)%n;
if(l>r){
temp=r;
r=l;
l=temp;
}
last=findMinDiff(a,l-1,r-1);
cout<<"last: "<<last<<endl;
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire