Untitled

                Never    
C++
       
#include <iostream>
#include <vector>
#include <string>

using namespace std;

// fruit node
typedef struct Fruit{
    string name; // fruit name
    vector<int> hf; // key list
    // I wanted to use linked list so I added this element
    struct Fruit* nxt; 
} Fruit;

typedef struct GradBinHyperWeight{
    // sub structures
    int idx; // key index for sorting
    double gs; // constant for gradiant slider
    int bt; // gate for distributing the elements into 2 sub array
    // sorting tree ptrs
    struct GradBinHyperWeight* bc = nullptr;
    struct GradBinHyperWeight* fc = nullptr;

    // sub funcs
    bool shouldBackward(Fruit* f) const{
        // return whether an element should go forward or backward
        return f->hf[idx] < bt;
    }
    
    void applyGradSlider(Fruit* f) const{
        // appyly key value change
        f->hf[idx] = (int)((double) f->hf[idx]*gs);
    }
} gbw;

gbw** arr;
int n, m;

// previous key index; key index for this round sort; subarray
void light(int prev, int cnt, Fruit* f){ // description called the sort lightning sort
    if(!f->nxt){ // the length of the sub array is 1
        // print fruit name and last used key
        cout << f->name << ' ' << f->hf[arr[prev]->idx] << endl;
        return;
    }
  
    // make 2 ptr to store 2 sub array
    Fruit *left = nullptr, *right = nullptr, *cur = f;
    Fruit *left_mov = left, *right_mov = right;
    // left: should go back; right: should go forward

    while(f){ // while the qrray isn't empty
        // get the first element
        cur = f; f = f->nxt; cur->nxt = nullptr;
        // apply key value change
        arr[cnt]->applyGradSlider(cur);
        // decide which sub array the element should go
        if(arr[cnt]->shouldBackward(cur)){
            if(!left_mov){ // op for first element
                left_mov = cur;
                left = left_mov;
            } else{ // op for others
                left_mov->nxt = cur;
                left_mov = left_mov->nxt;
            } 
        } else{
            if(!right_mov){ // op for first
                right_mov = cur;
                right = right_mov;
            } else{ // op for others
                right_mov->nxt = cur;
                right_mov = right_mov->nxt;
            } 
        } // can only have one op if use dummy node
    }
  
    // continue sorting the subarrays with the same approach
    light(cnt, cnt*2, left);
    light(cnt, cnt*2+1, right);
}

int main(){
    // how many fruits; how many keys per fruit
    cin >> n >> m;
    // get fruits
    Fruit *f = new Fruit, *cur = f;
    for(int i = 0; i<n; i++){
        // get one node
        cin >> cur->name;
        for(int j = 0; j<m; j++){
            int temp; cin >> temp;
            cur->hf.push_back(temp);
        }
        // push it into the array
        if(i < n-1){
            cur->nxt = new Fruit;
            cur->nxt->nxt = nullptr;
            cur = cur->nxt;
        }
    }

    int num; cin >> num; // number of sorting opertions
    arr = new gbw*[num];
    for(int i = 1; i<=num; i++){
        // get one sorting round
        arr[i] = new gbw;
        cin >> arr[i]->idx >> arr[i]->gs >> arr[i]->bt;
        if(i/2){ // build the sorting tree
            if(!(i%2)) arr[i/2]->bc = arr[i];
            else arr[i/2]->fc = arr[i];
        }
    }

    // sort the arrays
    light(0, 1, f);

    return 0;
}

Raw Text