zoukankan      html  css  js  c++  java
  • 10131

    /*
      最长递减子序列,
      题目说的是  每头大象都有体重和IQ 要证明 随着 体重的增加智商 在不断的减少的最长序列
      解 :
       先对 体重进行一次排序 ,排完后  LIS(最长递增(减)子序列) 
    */
    #include <cstdio>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <stack>
    using namespace std;
    struct Elephant
    {
    	 int num,W,S;
    	 bool operator <(const Elephant &T)const {
    	   return W<T.W||(W==T.W&&num<T.num);
    	 }
    }T[10005];
    int dp[10005],per[10005];
    int main()
    { 
    	  int j,i,W,S,num=0,maxv=1;
    	  while(scanf("%d%d",&W,&S)==2){
    	    T[num].W=W; 
    		T[num].S=S; 
    		T[num].num=num+1;
    		num++;
    	  }
    	  sort(T,T+num);
          for(i=0;i<num;i++) { dp[i]=1; per[i]=-1; }
    	  for(i=0;i<num;i++)
    		  for(j=0;j<i;j++)
    			  if(T[i].S<T[j].S&&T[i].W>T[j].W&&(dp[i]<dp[j]+1)){ 
    			     dp[i]=(dp[j]+1);
    				  per[i]=j;
                     if(dp[i]>maxv) maxv=dp[i];
    			  } 
    		 for(i=num-1;i>=0;i--) if(dp[i]==maxv) break;
    		 stack<int>Q;
    		 printf("%d
    ",maxv);
    		 W=i;
    		 S=T[W].num;
    		 Q.push(S);
    		 while(per[W]!=-1){
    		     W=per[W];
                 S=T[W].num;
    			 Q.push(S);
    		 }
             while(!Q.empty()){ S=Q.top(); printf("%d
    ",S);Q.pop();}
    	return 0;
    }


  • 相关阅读:
    浅谈HTTP中Get与Post的区别
    js 执行完setTimeout再接着执行函数
    2017年书单
    js判断img是否存在
    md5
    GIF播放器
    java 集合(二)
    java 泛型
    抓包工具
    js计算地球两个经纬度之间的距离
  • 原文地址:https://www.cnblogs.com/Opaser/p/3662025.html
Copyright © 2011-2022 走看看