Play With Numbers Programming Challenge(在Java中查找子数组的平均值)

huangapple 未分类评论61阅读模式
英文:

Play With Numbers Programming Challenge (finding mean value of subarray in java)

问题

  1. import java.io.*;
  2. import java.util.*;
  3. class TestClass
  4. {
  5. public static void main(String args[]) throws IOException
  6. {
  7. Scanner sc=new Scanner(System.in);
  8. int n= sc.nextInt();
  9. int q= sc.nextInt();
  10. int arr[]=new int[n];
  11. int sums[]=new int[n+1];
  12. sums[0]=0;
  13. for(int i=0;i<n;i++)
  14. {
  15. arr[i]=sc.nextInt();
  16. sums[i+1]=sums[i]+arr[i];
  17. }
  18. for(int i=0;i<q;i++)
  19. {
  20. int q1=sc.nextInt();
  21. int q2=sc.nextInt();
  22. int end=(q2-q1)+1;
  23. int mean= (sums[q2]-sums[q1-1])/end;
  24. System.out.println(mean);
  25. }
  26. }
  27. }
英文:

I'm trying to solve this Play With Numbers. I have passed the test cases but, I kept getting time limit exceeded. Can someone help me improve its performance in order to pass the time limit, please?

Problem:

You are given an array of n numbers and q queries. For each query you have to print the floor of the expected value(mean) of the subarray from L to R.

Input:

First line contains two integers N and Q denoting number of array elements and number of queries.

Next line contains N space seperated integers denoting array elements.

Next Q lines contain two integers L and R(indices of the array).

Output:

print a single integer denoting the answer.

Constraints:

1<= N ,Q,L,R <= 10^6

1<= Array elements <= 10^9

My code:

  1. import java.io.*;
  2. import java.util.*;
  3. class TestClass
  4. {
  5. public static void main(String args[] ) throws IOException
  6. {
  7. Scanner sc=new Scanner(System.in);
  8. int n= sc.nextInt();
  9. int q= sc.nextInt();
  10. int arr[]=new int[n];
  11. int sums[]=new int[n+1];
  12. sums[0]=0;
  13. for(int i=0;i&lt;n;i++)
  14. {
  15. arr[i]=sc.nextInt();
  16. sums[i+1]=sums[i]+arr[i];
  17. }
  18. for(int i=0;i&lt;q;i++)
  19. {
  20. int q1=sc.nextInt();
  21. int q2=sc.nextInt();
  22. int end=(q2-q1)+1;
  23. int mean= (sums[q2]-sums[q1-1])/end;
  24. System.out.println(mean);
  25. }
  26. }
  27. }

答案1

得分: 0

根据我的非科学测试,这将加快速度:

  1. sc.nextLine(); // 完成第一行
  2. String line = sc.nextLine();
  3. String[] parts = line.split(" ");
  4. for (int i = 0; i < parts.length; i++) {
  5. arr[i] = Integer.parseInt(parts[i]);
  6. sums[i + 1] = sums[i] + arr[i];
  7. }

正如您所见,小技巧是尽量减少 Scanner 需要进行的读取次数(因为每次“读取”通常都会有一些“延迟”)。我从文件中读取,而不是从 System.in 中读取,所以在您的设置中可能看不到同样的改进。

英文:

According to my not-so-scientific tests, this doubles the speed:

  1. sc.nextLine(); // to finish row #1
  2. String line = sc.nextLine();
  3. String[] parts = line.split(&quot; &quot;);
  4. for (int i = 0; i &lt; parts.length; i++) {
  5. arr[i] = Integer.parseInt(parts[i]);
  6. sums[i + 1] = sums[i] + arr[i];
  7. }

As you see, the little trick is to minimize the number of reads that the Scanner has to to (as there normally is a bit of 'lag' surrounding each 'read'). I read from a file, not from System.in, so maybe you will not see the same improvement in your setup.

huangapple
  • 本文由 发表于 2020年3月15日 17:06:50
  • 转载请务必保留本文链接:https://java.coder-hub.com/60691273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定