zoukankan      html  css  js  c++  java
  • NYOJ 214(二分插入)

     
    #include<stdio.h>//**O(nlogn)**//
    #include<string.h>
    #define min -32769//**int型最小数为-32768**//
    int stack[100001];//**模拟栈,其实不是栈,为了更好形象比较,嘿嘿**//
    int main()
    {
    	int n,i,t,top,low,high,mid;
    	memset(stack,0,sizeof(stack));
    	while(~scanf("%d",&n))
    	{
    		top=0;stack[0]=min;
    		for(i=0;i<=n-1;i++)
    		{
    			scanf("%d",&t);
    			if(t>stack[top])//**如果输入进来的数比栈顶的数大,直接插入到栈的**//
    			{
    				top++;
    				stack[top]=t;
    			}
    			else
    			{
    				low=1;high=top;
    				while(low<=high)//**二分查找,寻找插入位置**//
    				{
    					mid=(low+high)/2;
    					if(t>stack[mid])
    					{
    						low=mid+1;
    					}
    					else
    					{
    						high=mid-1;
    					}
    				}
    				stack[low]=t;//**找到插入位置,并替换点原值**//
    			}
    		}
    		printf("%d\n",top);
    	}
    	return 0;
    }         
    

      

  • 相关阅读:
    二次识别
    IPC
    HW
    数据库异常:SQL Error: 0, SQLState: S0022
    mysql 分页查询
    [Err] 1248
    名句摘抄
    理智向左 疯狂向右
    kindle怎么导入电子书
    设置 myeclipse 编码格式
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2518875.html
Copyright © 2011-2022 走看看