Skip to main content
Code Review

Return to Question

added 266 characters in body; edited tags; edited title
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

How to decrease the time of proceeding for my program? Counting athletes whose height is in a given range

How to decrease the time of proceeding for my program? MyChallenge: Sort by growth

Input

  • The first line of each query contains one number n (1 ≤ n ≤ 20000) - the number of members in corresponding delegation.
  • The second line contains n unsorted integers - the heights of athletes in centimeters. Every height is at least 150 cm, and no greater than 250 cm.
  • The third line contains a and b: the lower and upper height limits.

Output

For each query print the count on a separate line.

Time limit: 2 seconds
Memory limit: 256 MiB

My program works correctly, but I have a limit ofrunning time 3020ms. how to improve my code?

https://www.e-olymp.com/uk/problems/3607

n- is a number of people, then there are n numbers of input which describe their height, the program should answer the question how much people have a height between a and b (which are also given in input)3020 ms.

 import java.io.PrintWriter;
 import java.util.Scanner;
 public class OlimpGames {
 public static void main(String[] args) {
 Scanner in = new Scanner(System.in);
 PrintWriter out = new PrintWriter(System.out, true);
 {
 while (in.hasNextInt()) {
 int n = in.nextInt();
 int heigh[] = new int[100];
 for (int i = 0; i < n; i++) {
 heigh[in.nextInt() - 150]++;
 }
 int a = in.nextInt();
 int b = in.nextInt();
 int an = 0;
 for (int i = a - 150; i <= b - 150; i++) {
 an = an + heigh[i];
 }
 System.out.println(an);
 }
 }
 }
}

} }

How to decrease the time of proceeding for my program?

How to decrease the time of proceeding for my program? My program works correctly, but I have a limit of time 3020ms. how to improve my code?

https://www.e-olymp.com/uk/problems/3607

n- is a number of people, then there are n numbers of input which describe their height, the program should answer the question how much people have a height between a and b (which are also given in input)

 import java.io.PrintWriter;
 import java.util.Scanner;
 public class OlimpGames {
 public static void main(String[] args) {
 Scanner in = new Scanner(System.in);
 PrintWriter out = new PrintWriter(System.out, true);
 {
 while (in.hasNextInt()) {
 int n = in.nextInt();
 int heigh[] = new int[100];
 for (int i = 0; i < n; i++) {
 heigh[in.nextInt() - 150]++;
 }
 int a = in.nextInt();
 int b = in.nextInt();
 int an = 0;
 for (int i = a - 150; i <= b - 150; i++) {
 an = an + heigh[i];
 }
 System.out.println(an);
 }
}

} }

Counting athletes whose height is in a given range

Challenge: Sort by growth

Input

  • The first line of each query contains one number n (1 ≤ n ≤ 20000) - the number of members in corresponding delegation.
  • The second line contains n unsorted integers - the heights of athletes in centimeters. Every height is at least 150 cm, and no greater than 250 cm.
  • The third line contains a and b: the lower and upper height limits.

Output

For each query print the count on a separate line.

Time limit: 2 seconds
Memory limit: 256 MiB

My program works correctly, but I have a running time of 3020 ms.

 import java.io.PrintWriter;
 import java.util.Scanner;
 public class OlimpGames {
 public static void main(String[] args) {
 Scanner in = new Scanner(System.in);
 PrintWriter out = new PrintWriter(System.out, true);
 {
 while (in.hasNextInt()) {
 int n = in.nextInt();
 int heigh[] = new int[100];
 for (int i = 0; i < n; i++) {
 heigh[in.nextInt() - 150]++;
 }
 int a = in.nextInt();
 int b = in.nextInt();
 int an = 0;
 for (int i = a - 150; i <= b - 150; i++) {
 an = an + heigh[i];
 }
 System.out.println(an);
 }
 }
 }
}
Source Link

How to decrease the time of proceeding for my program?

How to decrease the time of proceeding for my program? My program works correctly, but I have a limit of time 3020ms. how to improve my code?

https://www.e-olymp.com/uk/problems/3607

n- is a number of people, then there are n numbers of input which describe their height, the program should answer the question how much people have a height between a and b (which are also given in input)

 import java.io.PrintWriter;
 import java.util.Scanner;
 public class OlimpGames {
 public static void main(String[] args) {
 Scanner in = new Scanner(System.in);
 PrintWriter out = new PrintWriter(System.out, true);
 {
 while (in.hasNextInt()) {
 int n = in.nextInt();
 int heigh[] = new int[100];
 for (int i = 0; i < n; i++) {
 heigh[in.nextInt() - 150]++;
 }
 int a = in.nextInt();
 int b = in.nextInt();
 int an = 0;
 for (int i = a - 150; i <= b - 150; i++) {
 an = an + heigh[i];
 }
 System.out.println(an);
 }
}

} }

lang-java

AltStyle によって変換されたページ (->オリジナル) /