#8970: JAVA-利用二分搜尋獲得TLE


alextsui2014 (unknown)

學校 : 不指定學校
編號 : 41821
來源 : [116.49.192.70]
最後登入時間 :
2014-07-06 22:24:14
d478. 共同的數 - 簡易版 | From: [116.49.187.114] | 發表日期 : 2014-07-08 23:28

import java.util.Scanner;
class d478{
 public static void main(String args[]){
  Scanner scanner = new Scanner(System.in);
  int total = scanner.nextInt(),totalOfNumber = scanner.nextInt();
  while(total-- > 0){
   int existCount = 0;
   int numberList[] = new int[totalOfNumber];
   for(int index = 0;index < totalOfNumber;index++){
    numberList[index] = scanner.nextInt();
   }
   for(int time = 0;time < totalOfNumber;time++){
    int number = scanner.nextInt(),low = 0,high = totalOfNumber - 1;
    while(low <= high){
     int middle = (low + high) / 2;
     if(numberList[middle] == number){
      ++existCount;
      break;
     }else if(numberList[middle] > number){
      high = middle - 1;
     }else{
      low = middle + 1;
     }    
    }
   }
   System.out.println(existCount);
  }
 }
}

請問如何優化 

 
#8977: Re:JAVA-利用二分搜尋獲得TLE


p3a_owhj (阿普二信)

學校 : 不指定學校
編號 : 39897
來源 : [36.227.79.178]
最後登入時間 :
2024-06-04 22:09:36
d478. 共同的數 - 簡易版 | From: [49.159.129.104] | 發表日期 : 2014-07-09 21:13

import java.util.Scanner;
class d478{
 public static void main(String args[]){
  Scanner scanner = new Scanner(System.in);
  int total = scanner.nextInt(),totalOfNumber = scanner.nextInt();
  while(total-- > 0){
   int existCount = 0;
   int numberList[] = new int[totalOfNumber];
   for(int index = 0;index < totalOfNumber;index++){
    numberList[index] = scanner.nextInt();
   }
   for(int time = 0;time < totalOfNumber;time++){
    int number = scanner.nextInt(),low = 0,high = totalOfNumber - 1;
    while(low <= high){
     int middle = (low + high) / 2;
     if(numberList[middle] == number){
      ++existCount;
      break;
     }else if(numberList[middle] > number){
      high = middle - 1;
     }else{
      low = middle + 1;
     }    
    }
   }
   System.out.println(existCount);
  }
 }
}

請問如何優化 


題目說:兩人的數字皆是遞增的  
#8979: Re:JAVA-利用二分搜尋獲得TLE


alextsui2014 (unknown)

學校 : 不指定學校
編號 : 41821
來源 : [116.49.192.70]
最後登入時間 :
2014-07-06 22:24:14
d478. 共同的數 - 簡易版 | From: [116.49.187.114] | 發表日期 : 2014-07-10 00:49

己改變了思考方法,還是TLE。

是不是利用JAVA 問題。。。。。。 

import java.util.Scanner;
class d478{
 public static void main(String args[]){
  Scanner scanner = new Scanner(System.in);
  int total = scanner.nextInt(),totalOfNumber = scanner.nextInt();
  while(total-- > 0){
   int existCount = 0;
   int numberList[] = new int[totalOfNumber];
   for(int index = 0;index < totalOfNumber;index++){
    numberList[index] = scanner.nextInt();
   }
   for(int time = 0;time < totalOfNumber;time++){
    int number = scanner.nextInt(),low = 0,high = totalOfNumber - 1;
    while(low <= high){
     int middle = (low + high) / 2;
     if(numberList[middle] == number){
      ++existCount;
      break;
     }else if(numberList[middle] > number){
      high = middle - 1;
     }else{
      low = middle + 1;
     }    
    }
   }
   System.out.println(existCount);
  }
 }
}



 
#8980: Re:JAVA-利用二分搜尋獲得TLE


alextsui2014 (unknown)

學校 : 不指定學校
編號 : 41821
來源 : [116.49.192.70]
最後登入時間 :
2014-07-06 22:24:14
d478. 共同的數 - 簡易版 | From: [116.49.187.114] | 發表日期 : 2014-07-10 00:50

己改變了思考方法,還是TLE。

是不是利用JAVA 問題。。。。。。 

import java.util.Scanner;
class d478{
 public static void main(String args[]){
  Scanner scanner = new Scanner(System.in);
  int total = scanner.nextInt(),totalOfNumber = scanner.nextInt();
  while(total-- > 0){
   int existCount = 0;
   int numberList[] = new int[totalOfNumber];
   for(int index = 0;index < totalOfNumber;index++){
    numberList[index] = scanner.nextInt();
   }
   for(int time = 0,pointer = 0;time < totalOfNumber;time++){
    int number = scanner.nextInt();
    for(int index = pointer;index < totalOfNumber;index++){
     if(number == numberList[index]){
      existCount++;
      pointer++;
      break;
     }else if(number > numberList[index]){
      pointer++;
      continue;
     }
    }
   }
   System.out.println(existCount);
  }
 }
}


 
#8981: Re:JAVA-利用二分搜尋獲得TLE


alextsui2014 (unknown)

學校 : 不指定學校
編號 : 41821
來源 : [116.49.192.70]
最後登入時間 :
2014-07-06 22:24:14
d478. 共同的數 - 簡易版 | From: [116.49.187.114] | 發表日期 : 2014-07-10 00:56

己改變了思考方法,還是TLE。

是不是利用JAVA 問題。。。。。。 

對不起,這才是最新的CODE 

import java.util.Scanner;
class d478{
 public static void main(String args[]){
  Scanner scanner = new Scanner(System.in);
  int total = scanner.nextInt(),totalOfNumber = scanner.nextInt();
  while(total-- > 0){
   int existCount = 0;
   int numberList[] = new int[totalOfNumber];
   for(int index = 0;index < totalOfNumber;index++){
    numberList[index] = scanner.nextInt();
   }
   for(int time = 0,pointer = 0;time < totalOfNumber;time++){
    int number = scanner.nextInt();
    for(int index = pointer;index < totalOfNumber;index++){
     if(number == numberList[index]){
      existCount++;
      pointer++;
      break;
     }else if(number > numberList[index]){
      pointer++;
      continue;
     }else{
      break;
     }
    }
   }
   System.out.println(existCount);
  }
 }
}


 
#8983: Re:JAVA-利用二分搜尋獲得TLE


tomoyaken14 (歐練)

學校 : 不指定學校
編號 : 6922
來源 : [122.117.127.202]
最後登入時間 :
2024-04-15 14:31:55
d478. 共同的數 - 簡易版 | From: [140.117.70.221] | 發表日期 : 2014-07-10 12:04

輸入輸出都還可以再更省時間

 

 
ZeroJudge Forum