zoukankan      html  css  js  c++  java
  • Hiho : 二分·二分查找之k小数

    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    在上一回里我们知道Nettle在玩《艦これ》,Nettle的镇守府有很多船位,但船位再多也是有限的。Nettle通过捞船又出了一艘稀有的船,但是已有的N(1≤N≤1,000,000)个船位都已经有船了。所以Nettle不得不把其中一艘船拆掉来让位给新的船。Nettle思考了很久,决定随机选择一个k,然后拆掉稀有度第k小的船。 已知每一艘船都有自己的稀有度,Nettle现在把所有船的稀有度值告诉你,希望你能帮他找出目标船。

     

    提示:非有序数组的二分查找之二

    输入

    第1行:2个整数N,k。N表示数组长度,
    第2行:N个整数,表示a[1..N],保证不会出现重复的数,1≤a[i]≤2,000,000,000。

    输出

    第1行:一个整数t,表示t在数组中是第k小的数,若K不在数组中,输出-1。

    样例输入
    10 4
    1732 4176 2602 6176 1303 6207 3125 1 1011 6600
    样例输出
    1732

    代码:
     1 import java.util.Arrays;
     2 import java.util.Scanner;
     3 
     4 
     5 public class Main {
     6     public static void main(String [] argv){
     7 
     8         
     9         Scanner in = new Scanner(System.in);
    10         int N = in.nextInt();
    11         int M = in.nextInt()-1;
    12         int [] s = new int[N];
    13         for (int i=0; i<N; i++){
    14             s[i]=in.nextInt();
    15         }
    16         in.close();
    17         if(M+1>N||M<0)
    18             System.out.println(-1);
    19         else
    20             KP(M,s);
    21         
    22 }
    23     
    24 static    public void KP(int m, int [] h){
    25         int s[] =h;
    26         int k = s[0];
    27         int first = 0;
    28         int last = s.length-1;
    29         while(true){                        
    30         while(first<last){                    
    31                 while(first<last&&s[last]>=k)                
    32                 --last;
    33                 s[first]=s[last];
    34                 while(first<last&&s[first]<=k)                    
    35                 ++first;
    36                 s[last]=s[first];
    37                 if(first==last)
    38                     s[first]=k;
    39             }            
    40             for(int t = 0 ; t< s.length; t++){
    41                 //System.out.print(s[t]);
    42             }
    43             //System.out.println("first:"+first+"m:"+m+"last:"+last);
    44             if(first<m){
    45                 int[] temp = new int[s.length-1-first];
    46                 for(int j = 0; j<s.length-1-first; j++){
    47                     temp[j]=s[j+first+1];
    48                 }
    49                 s = temp;
    50                 m = m-first-1;
    51                 first = 0;
    52                 last = s.length-1;
    53             }
    54             else{
    55                 if(first==m){
    56                     System.out.println(k);
    57                     return ;
    58                 }
    59                     
    60                 else{
    61                     int[] temp = Arrays.copyOf(s, first);
    62                     s=temp;
    63                     first = 0;
    64                     last = s.length-1;
    65                 }
    66                     
    67             }            
    68             if(s.length>=1)
    69                 k=s[0];
    70         }
    71         
    72     }
    73 
    74 
    75 }

    通过效果:

  • 相关阅读:
    Delphi IDE 设置
    我最喜欢的歌曲
    Window 常用文件
    Delphi TTable 组件
    Delphi TDatabase 组件
    c语言->和 .
    Shell 工具之 gawk
    Shell 工具之 sed
    Shell 语法之函数
    Shell 语法之信号与作业
  • 原文地址:https://www.cnblogs.com/udld/p/4363290.html
Copyright © 2011-2022 走看看