zoukankan      html  css  js  c++  java
  • poj 2503 快排+二分

    var x,y:array[0..110000] of string;
        i,n,l,r,mid:longint;
        s:string;
    procedure qsort(h,l:longint);
     var i,j:longint;
         temp,m:string;
     begin
     i:=h;j:=l;m:=x[(i+h)>>1];
     repeat
      while x[i]<m do inc(i);
      while x[j]>m do dec(j);
      if i<=j then
       begin
        temp:=x[i];x[i]:=x[j];x[j]:=temp;
        temp:=y[i];y[i]:=y[j];y[j]:=temp;
        inc(i);dec(j);
       end;
     until i>j;
     if i<l then qsort(i,l);
     if j>h then qsort(h,j);
     end;
    procedure init;
     begin
     n:=0;
     while true do
      begin
      readln(s);if s='' then break;
      inc(n);
      y[n]:=copy(s,1,pos(' ',s)-1);
      delete(s,1,pos(' ',s));
      x[n]:=s;
      end;
     qsort(1,n);
     end;
    procedure main;
     begin
     while true do
      begin
      readln(s);if s='' then break;
      l:=1;r:=n;
      while l<r do
       begin
       mid:=(l+r)>>1;
       if s<x[mid] then r:=mid
       else if s>x[mid] then l:=mid+1
       else begin l:=mid;r:=mid;end;
       end;
      if x[l]=s then writeln(y[l]) else writeln('eh');
      end;
     end;
    begin
     init;
     main;
    end.    
    View Code

    水一道……体验了一把1A的感觉……

    还要说的是,这道题的字符串达到了100000,但还是能够直接排序处理,那是因为它的字符串太短了……

    var x,y:array[0..110000] of string;
        i,n,l,r,mid:longint;
        s:string;
    procedure qsort(h,l:longint);
     var i,j:longint;
         temp,m:string;
     begin
     i:=h;j:=l;m:=x[(i+h)>>1];
     repeat
      while x[i]<m do inc(i);
      while x[j]>m do dec(j);
      if i<=j then
       begin
        temp:=x[i];x[i]:=x[j];x[j]:=temp;
        temp:=y[i];y[i]:=y[j];y[j]:=temp;
        inc(i);dec(j);
       end;
     until i>j;
     if i<l then qsort(i,l);
     if j>h then qsort(h,j);
     end;
    procedure init;
     begin
     n:=0;
     while true do
      begin
      readln(s);if s='' then break;
      inc(n);
      y[n]:=copy(s,1,pos(' ',s)-1);
      delete(s,1,pos(' ',s));
      x[n]:=s;
      end;
     qsort(1,n);
     end;
    procedure main;
     begin
     while true do
      begin
      readln(s);if s='' then break;
      l:=1;r:=n;
      while l<r do
       begin
       mid:=(l+r)>>1;
       if s<x[mid] then r:=mid
       else if s>x[mid] then l:=mid+1
       else begin l:=mid;r:=mid;end;
       end;
      if x[l]=s then writeln(y[l]) else writeln('eh');
      end;
     end;
    begin
     init;
     main;
    end.    
  • 相关阅读:
    cocos2d与cocos2d-X中的draw和update
    poj1673
    hdu2128之BFS
    常用的js效验
    OMCS的语音视频带宽占用
    UML类图详细介绍
    [置顶] 获取激活码,激活myeclipse
    CBO学习----03--选择率(Selectivity)
    notepad++ 文件对比插件
    永远不要在Linux 执行的 10 个最危险的命令
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/3771818.html
Copyright © 2011-2022 走看看