Sort by increasing weight.
A O(n * n) LDS by IQ is fast enough for this problem.
Initialize each element of count to 1, and each element of previous to -1.
Code: Select all
int mx = 1, best_index;
for(int j = 1; j < n; j++)
for(int i = 0; i < j; i++)
if(e[i].weight < e[j].weight && e[i].iq > e[j].iq)
if(1 + count[i] > count[j]) {
count[j] = 1 + count[i];
previous[j] = i;
if ( count[j] > mx ) {
mx = count[j];
best_index = j;
}
}