现在的位置: 首页 > 综合 > 正文

带返还值的非递归 bs算法为例子

2013年06月23日 ⁄ 综合 ⁄ 共 900字 ⁄ 字号 评论关闭
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#define N 20000
using namespace std;
int Max(int x,int y){if(x<y) return y;return x;}
int p[N];
struct seg
{
    int s,e;
    seg(int _s=0,int _e=0){s=_s,e=_e;}
}a[N];
int k[N],top,top2,m[N];
int M(int s,int e)
{
    if(s==e) return p[e];
    int mid=(s+e)>>1;
     return Max(M(s,mid),M(mid+1,e));
}
int M2(int s,int e)
{
    int  ans;
    top=0;top2=0;
    a[++top]=seg(s,e);k[top]=0;
    L:;
    if(a[top].s==a[top].e) {ans=p[a[top].s];goto OUT;}
    for(;k[top]<2;k[top]++)
    {
        if(k[top]==0) a[top+1]=seg(a[top].s,(a[top].s+a[top].e)/2);
        else  a[top+1]=seg((a[top].s+a[top].e)/2+1,a[top].e);
        k[top+1]=0;
        top++;  goto L;
        Z:;
    }
      ans=Max(m[top2],m[top2-1]);
       top2-=2;
    OUT:;
    m[++top2]=ans;--top;if(top)goto  Z;else  return ans;
}
int main()
{
    while(1){
    srand(time(NULL));
   for(int i=1;i<=1000;++i)
   p[i]=rand();
  int ans=p[1];
      for(int i=1;i<=1000;++i)
  {ans=Max(ans,p[i]);}
       int ans2=M2(1,1000);
       if(ans==ans2)
       cout<<"Yes"<<endl;
       else break;
    }
    return 0;
}

抱歉!评论已关闭.