How can I find, inside compare function, the position of m1 and m2 relative to m array? Example:
0 1 2 3 4 5
m array: - - - - - -
^ ^
m1 m2
output: 1 and 4
It's important to avoid using extra memory, like for example have an extra argument in Mir struct (int pos). Also slow solutions are not welcome, like find() etc.. Keep also in mind that m array will be constantly shuffled (not sure if this is important). basically I need a fast and low memory usage solution. This must be simple.. The code:
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
struct Mir { int a,b; };
Mir *m;
bool cmp(Mir &m1, Mir &m2){
if(m1.b > m2.b){
// Find position of m1 and m2 relative to m array. How ???;
return true;
}
return false;
}
int main(){
int n, k;
scanf("%d %d", &n, &k);
m = (Mir *) malloc(n * sizeof(Mir));
for(int i = 0; i < n; i++)
scanf("%d %d", &m[i].a, &m[i].b);
std::sort(m, m+k, cmp);
}
Thanks.