Tìm hai số có tích lớn nhất


Submit solution

Points: 1 (partial)
Time limit: 1.0s
Memory limit: 98M

Author:
Problem type
Allowed languages
Ada, Assembly, Awk, C, C++, C11, CLANG, CLANGX, Classical, COBOL, Coffee, CSC, D lang, DART, F95, FORTH, Fortrn, GAS32, GO, Haskell, Itercal, Java, kotlin, LEAN, LISP, LUA, MONOVB, Nasm, OCAML, Pascal, Perl, php, PIKE, prolog, Pypy, Python, Ruby 2, RUST, Scala, SCM, SED, SWIFT, TCL, TUR, V8JS, VB, ZIG

Cho dãy số nguyên a1,a2,...,aN, bạn phải tìm hai số sao cho tích của chúng là lớn nhất

Input

Dòng đầu là số phần tử N (1<N106)

Dòng tiếp theo là N số nguyên mỗi số có giá trị tuyệt đối không vượt quá 109

Output

Một số nguyên duy nhất là tích lớn nhất của hai số

Example 1

Input

Copy
5
1 9 8 -5  0

Output

Copy
72

Example 2

Input

Copy
6
2 -3 -8 6 -9 0

Output

Copy
72
tichpx

Comments


  • 0
    TuanKiet_HUSC  commented on Nov. 16, 2023, 2:45 p.m.

    mảng không vượt quá 10^9 sài 1 for được chơ mn


  • 1
    Ledinhminh_CNTT3_K62  commented on Jan. 1, 2022, 7:37 a.m. edit 2

    Sao cứ bị TLE hoài ạ

    Copy
        #include <stdio.h>
    #include <math.h>
    
    void nhap(int n, long long a[]){
        for(int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
        }
    }
    
    int main(){
        int n,max1=0,max2=0,min1=0,min2=0;
        long long a[10000];
        scanf("%d",&n);
        nhap(n,a);
    
        for(int i=1;i<=n;i++){
            if(max1<a[i]){
                max2=max1;
                max1=a[i];
            }else if(a[i]>max2){
                max2=a[i];
            }
        }
    
        for(int i=1;i<=n;i++){
            if(min1>a[i]){
                min2=min1;
                min1=a[i];
            }else if(a[i]<min2){
                min2=a[i];
            }
        }
    
        if(abs(max1*max2)<abs(min1*min2)){
            printf("%d",min1*min2);
        }else printf("%d",max1*max2);
    }

    • 1
      TICHPX  commented on Jan. 1, 2022, 8:43 a.m.

      Có 10^6 phần tử mà em khai báo mảng 10^4 nên nó lỗi RTE


  • 1
    ga123  commented on Sept. 18, 2021, 2:58 a.m. edit 2

    bài này đơn giản mà sao nhiều lte thế nhỉ o.o

    Copy
    #include"stdio.h"
    #include"math.h"
    int main()
    {
    long n;
    long long x,y[2];
    y[1]=-1;y[2]=-1;
    scanf("%ld",&n);
    meo1:
        {
            if(n==1)
            {
            scanf("%lld",&x);
            printf("%lld",abs(x));
            return 0;
            }
            else goto meo2;
        }
    meo2:
        {
            if(n==2)
            {
                scanf("%lld %lld",&y[1],&y[2]);
                x =abs(y[1]*y[2]);
                printf("%lld",x);
                return 0;
            }
            else goto meo3;
        }
    meo3:
        {
        while(n>0)
        {
            scanf("%lld",&x);
            x=abs(x);
            if(x>y[1])
            goto meo4;
            n--;
        }
        goto meo5;
        }
    meo4:
        {
            if(x>y[2])
            {
                y[1]=y[2];
                y[2]=x;
    
            }
            else y[1]=x;
            n--;
            goto meo3;
        }
    meo5:
        {
            long long ans=y[1]*y[2];
            printf("%lld",ans);
        }
    }

    mn dùng sort sẽ đẩy độ phức tạp lên mức n^2 . dùng cách đi từng bước như trên là cố định độ phức tạp ở mức 3n luôn.


  • 1
    ga123  commented on Sept. 18, 2021, 2:33 a.m.

    tích hai số lớn nhất mà lại có trường hợp 1 phần tử @@


  • 1
    ga123  commented on Sept. 18, 2021, 2:27 a.m.

    cái này phải ghi thêm là trị tuyệt đối á @@ nhầm nhiều ghê