如何进一步优化时间限制 [英] How do I optimize it further for time restrictions

查看:87
本文介绍了如何进一步优化时间限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何优化它不到1秒



我尝试过:



 import java.util。*; 

class TestClass {

public static void main( String args []){
扫描仪sc = 扫描仪(系统。 in );
int n = sc.nextInt();
int a [] = new int < /跨度> [N];
int b [] = new int < /跨度> [N];
for int i = 0 ; i< n; i ++)
{
a [i] = sc.nextInt();
b [i] = sc.nextInt();
}
long sum = 0 ;
for int i = 0 ; i< n-1; i ++)
{
for int j = i + 1; j< n; j ++)
{
sum + = Math.abs(a [i] -a [j])*(Math.max(b [i], b [j]));
}
}
系统。 out .println(sum);
}

}

解决方案

永远不会在1秒内执行。有很多东西,



1)用户输入是请求,本身将超过秒。

2)大小是未知,数组操作需要很长时间。

3)两个循环,即使我们改进一个,第二个怎么样?

4)数学库函数也需要一些CPU时间。



只需一个解决方案,确保数组和循环的大小小于10个元素。只有这样它才有用。我的工作,

  / *   
*要更改此许可证标题,请在项目属性中选择许可证标题。
*要更改此模板文件,请选择工具|模板
*并在编辑器中打开模板。
* /

package hellojava;

/ * *
*
* @author afzaa
* /

public class HelloJava {

/ * *
* @param args命令行参数
* /

public static void main( String [] args){
// 此处的TODO代码应用程序逻辑
int [] arr = { 1 2 3 4 ,< span class =code-digit> 5 , 6 7 8 9 10 };
int n = 10 ;
int a [] = new int < /跨度> [N];
int b [] = new int < /跨度> [N];
for int i = 0; i< n; i ++)
{
a [i] = arr [i];
b [i] = arr [i];
}
long sum = 0;
for int i = 0; i< n- 1 ; i ++)
{
for int j = i + 1; j< n; j ++)
{
sum + = Math.abs(a [i] -a [j])*(Math.max(b [i], b [j]));
}
}
System.out.println(sum);
}
}



这个程序只需1秒即可执行。

 run:
1320
BUILD SUCCESSFUL(总时间:1秒)



见?就是我说的。 :)


How can i optimize it for less than 1 sec

What I have tried:

import java.util.*;

class TestClass {

public static void main(String args[] ) {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int a[]=new int[n];
    int b[]=new int[n];
    for(int i=0;i<n;i++)
    {
        a[i]=sc.nextInt();
        b[i]=sc.nextInt();
    }
    long sum=0;
    for(int i=0;i<n-1;i++)
    {
        for(int j=i+1;j<n;j++)
        {
          sum += Math.abs(a[i]-a[j])*(Math.max(b[i],b[j]));   
        }
    }
    System.out.println(sum);
    }

}

解决方案

That will never execute under 1 second. There are many things,

1) User input is request, which will be more than seconds in itself.
2) The size is unknown, array manipulation will take a long time.
3) Two loops, even if we improve one, what about second?
4) Math library functions also require some CPU time.

Just one solution, make sure the size of arrays and loops is less than 10 elements. Only then it might work. What I worked,

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package hellojava;

/**
 *
 * @author afzaa
 */
public class HelloJava {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int n = 10; 
        int a[] = new int[n];
        int b[] = new int[n];
        for(int i=0;i<n;i++)
        {
            a[i] = arr[i];
            b[i] = arr[i];
        }
        long sum=0;
        for(int i=0;i<n-1;i++)
        {
            for(int j=i+1;j<n;j++)
            {
              sum += Math.abs(a[i]-a[j])*(Math.max(b[i],b[j]));   
            }
        }
        System.out.println(sum);
    }
}


This program, takes exactly 1 second to execute.

run:
1320
BUILD SUCCESSFUL (total time: 1 second)


See? Just what I said. :)


这篇关于如何进一步优化时间限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆