zoj 1108解题报告

来源:互联网 发布:蜂窝数据打开连不上网 编辑:程序博客网 时间:2024/06/11 19:45
问题描述:给出一列数据 格式为 老鼠体重 移动速度;
求出 体重越大 速度越慢的最长序列
比如给出数据
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
可以求得结果
4 //注释:表示最长序列长度 以下4位即为按体重越轻 速度越快原则排序的序列
4
5
9
7
问题描述的不是很清楚 请参照原题http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=108

解题思路:问题的关键在于 从哪只老鼠开始排序会得到最长序列,很明显可以用DP解题,设想从任意一只老鼠开始,
以它为起点开始的序列的第二个元素肯定满足以下条件
1:它的体重比上一次老鼠重 2:它的速度比上一次老鼠慢,3:在所有满足1,2条件的老鼠中 以它开始的序列最长
所以问题就转化为求以第二只老鼠开始的最长序列。。。。以此递推下去,问题得解

  1. //============================================================================
  2. // Name        : zoj1108.cpp
  3. // Author      : zl
  4. // Version     : 0.1
  5. // Copyright   : Your copyright notice
  6. //
  7. //============================================================================
  8. #include <iostream>
  9. #include <vector>
  10. #include <algorithm>
  11. using namespace std;
  12. struct mice
  13. {
  14.    int w,s,index;
  15.    bool operator<(const mice& m) const
  16.    {
  17.        return w<m.w;
  18.    }
  19. };
  20. struct statu
  21. {
  22.     int max,next;
  23.     statu():max(-1),next(-1){}
  24. };
  25. statu result[1000];
  26. vector<mice> data;
  27. int seachResult(int index)
  28. {
  29.      if(result[index].max!=-1)
  30.          return result[index].max;
  31.      else
  32.      {
  33.          int end=data.size();
  34.          int max=0;
  35.          int s=data[index].s;
  36.          int w=data[index].w;
  37.          int temp;
  38.          for(int i=index+1;i<end;++i)
  39.          {
  40.              if(data[i].w==w)
  41.                  continue;
  42.              if(data[i].s<s)
  43.              {
  44.                  temp=seachResult(i);
  45.                  if(temp>max)
  46.                  {
  47.                      max=temp;
  48.                      result[index].next=i;
  49.                  }
  50.              }
  51.          }
  52.          result[index].max=++max;
  53.          return max;
  54.      }
  55. }
  56. int main() {
  57.     mice temp;
  58.     int tt=0;
  59.     while((scanf("%d%d",&temp.w,&temp.s))!=EOF)
  60.     {
  61.         temp.index=++tt;
  62.         data.push_back(temp);
  63.     }
  64.     sort(data.begin(),data.end());
  65.     int end=data.size();
  66.     int max=0,start,tep;
  67.     for(int i=0;i<end;++i)
  68.     {
  69.         tep=seachResult(i);
  70.         if(tep>max)
  71.         {
  72.             max=tep;
  73.             start=i;
  74.         }
  75.     }
  76.     printf("%d/n",max);
  77.     while(start!=-1)
  78.     {
  79.         printf("%d/n",data[start].index);
  80.         start=result[start].next;
  81.     }
  82.     return 0;
  83. }



原创粉丝点击