hdu1025Constructing Roads In JGShining's Kingdom

一开始是一些不知道是什么的错误,访问到危险的空间啊,栈溢出啊,超时啊.....

后来到网上看了人家的答案,才知道人家的运行时间真的很短,同样是LIS ,但是人家用了哈希查找,所以快了很多

记住哈希查找最后是返回low

#include "iostream"
#include "string.h"
using namespace std;
int max(int a,int b){return a>b?a:b;}
int f[1000000],num[1000000];

int find(int low,int high,int i){
  while(low<=high){
    int mid=(low+high)/2;
    if(i>f[mid])low=mid+1;
    else high=mid-1;
  }
  return low;
}

int main(){
  int n,i,a,b,len,low,top=1;
  while(cin>>n){
    for(i=1;i<=n;i++){cin>>a>>b;num[a]=b;}
    f[1]=num[1];
    len=1;
    for(i=2;i<=n;i++){
      low=find(1,len,num[i]);
      f[low]=num[i];
      if(low>len)len++;
    }
    cout<<"Case "<<top++<<":"<<endl;
    if(len==1)cout<<"My king, at most "<<len<<" road can be built."<<endl<<endl;
    else cout<<"My king, at most "<<len<<" roads can be built."<<endl<<endl;
  }
}

 

 

 

posted @ 2013-08-23 11:54  龙城星  阅读(143)  评论(0编辑  收藏  举报