vector<int> largestDivisibleSubset(vector<int>& nums) {
     /*
     first we can sort the nums.
     dp[i] is current max length of subset to satisfy requirements. 
     i indicates index of subset, 
     dp[i] = max(dp[j] + 1) if (nums[i]%dp[j]==0), 0<=j<i
     */
     vector<int> ret;
     int len = nums.size();
     if(len==0)
         return ret;
     
     std::sort(nums.begin(),nums.end());
     
     vector<int> dp(len,0);
     vector<int> former(len,0);
     
     int max_len =0;  //max len of subset
     int index;  //max value's index in subset
     
     for(int i=0;i<len;i++){
         for(int j=i;j>=0;j--){
             //since set is already divisible, if current number can divide max num in current set, it can divide all in that set
             if(nums[i]%nums[j]==0 && dp[i]<dp[j]+1){
                 dp[i] = dp[j]+1;
                 former[i] = j;  //point to former element index in subset
             }
         }
         if(dp[i]>max_len){
             max_len = dp[i];
             index = i;
         }
         
     }
     
     
     for(int i=0;i<max_len;i++){
         
         ret.insert(ret.begin(),nums[index]);
         index = former[index];
     }
     
     return ret;
 }
Beispiel #2
0
void LapTimerThread::sectionChangeSender(QList<double> list)
{
    //display
    QTime latter(list.at(2),list.at(3),list.at(4),list.at(5));
    QTime former(list.at(7),list.at(8),list.at(9),list.at(10));
    double dis = former.msecsTo(latter);
    QString dev = "";
    double scale = list.at(6)/(list.at(6)+list.at(1));
    QTime atWaypt = former.addMSecs((int)(scale*dis));
    if (list.first()==0)//cross finish line
    {
        if (list.last()==1)//normally finish a lap
        {
            current.replace(current.length()-1,qTimeGap(&currentStart,&atWaypt));
            dev = QString::number(qTimeGap(&(best.at(list.last())),&(current.at(list.last()))).msecsSinceStartOfDay()/1000)
                    .append(".").append(qTimeGap(&(best.at(list.last())),&(current.at(list.last()))).toString("zzz"));
            dev.prepend((best.last().msecsTo(current.last())>=0)?"+":"-");
            QStringList tmpList;
            tmpList.append(QString::number((int)list.first()));
            tmpList.append(current.last().toString("mm:ss.zzz"));
            tmpList.append(best.last().toString("mm:ss.zzz"));
            tmpList.append(dev);
            tmpList.append("");
            emit sectionInfoSender(tmpList);
            if ((best.last().msecsTo(current.last())<0)|best.last().isNull())
                best.replace(current.length()-1,current.last());

        }
        else if (list.last()==0)//restart from reset
        {
            //do something
        }
            currentStart = former.addMSecs((int)(scale*dis));
            qWarning()<<"currentStart: "<<currentStart.toString("hh:mm:ss.zzz");


        if (flag)
        {
            bufferWaypt.writeTextElement("time",current.last().toString("mm:ss.zzz"));
            bufferWaypt.writeTextElement("best",best.last().toString("hh:mm:ss.zzz"));
            bufferWaypt.writeTextElement("dev",dev);

            bufferCoord.writeEndElement();
            bufferWaypt.writeEndElement();
            bufferSpeed.writeEndElement();
        }



            bufferCoord.writeStartElement("lap");
            bufferCoord.writeAttribute("start",currentStart.toString("hh:mm:ss.zzz"));
            bufferWaypt.writeStartElement("lap");
            bufferSpeed.writeStartElement("lap");
            flag = true;

    }
    else //cross other section
    {
        current.replace(list.first()-1,qTimeGap(&currentStart,&atWaypt));
        dev = QString::number(qTimeGap(&(best.at(list.first()-1)),&(current.at(list.first()-1))).msecsSinceStartOfDay()/1000)
                .append(".").append(qTimeGap(&(best.at(list.first()-1)),&(current.at(list.first()-1))).toString("zzz"));
        dev.prepend((best.last().msecsTo(current.last())>=0)?"+":"-");
        QStringList tmpList;
        tmpList.append(QString::number((int)list.first()));//0 - waypt no
        tmpList.append(current.at(list.first()-1).toString("mm:ss.zzz"));// 1 - waypt time
        tmpList.append(best.at(list.first()-1).toString("mm:ss.zzz"));//2 - best @ this waypt
        tmpList.append(dev);//3 - waypt dev
        tmpList.append("");//4 - info
        emit sectionInfoSender(tmpList);
        if ((best.at(list.first()-1).msecsTo(current.at(list.first()-1))<0) | best.at(list.first()-1).isNull() )
            best.replace(list.first()-1,current.at(list.first()-1));

        bufferWaypt.writeTextElement("time",current.at(list.first()-1).toString("mm:ss.zzz"));
        bufferWaypt.writeTextElement("best",best.at(list.first()-1).toString("hh:mm:ss.zzz"));
        bufferWaypt.writeTextElement("dev",dev);
    }
}