找最值 *二分优化* 队列实现

int getMax(int *num,int n)
{
    int max;
    int a,b;
    queue<int> army;

    for(int i = 1; i < n; i+=2)//从数组中第一次提取
    {
        if(num[i] > num[i-1])
            army.push(num[i]);
        else
            army.push(num[i-1]);
    }
    army.push(num[n-1]);

    while(!army.empty())//每两个最大值进行比较
    {
        a = army.front();
        army.pop();
        b = army.front();
        army.pop();

        if(a > b)
            army.push(a);
        else
            army.push(b);

        if(army.size() == 1 || army.size() == 0)
        {
            max = army.front();
            break;
        }
    }
    return max;
}

你可能感兴趣的:(最值)