cf_484b Maximum Value (two points)

Source: Internet
Author: User
B. Maximum Value time limit per test 1 second memory limit per test megabytes input standard input output Put

You are given a sequence a consisting of n integers. Find the maximum possible value of (integer remainder of AI divided by AJ), where 1≤i, J≤n and Ai≥aj. Input

The first line contains integer n-the length of the sequence (1≤n≤2 105).

The second line contains n space-separated integers ai (1≤ai≤106). Output

Print the answer to the problem. Sample Test (s) input

3
3 4 5
Output
2

Test Instructions:

The title is short, but the problem is still very interesting. To the number of N, ask for two numbers from the inside to make the largest number to the decimal value of the maximum.

The following:

The brute force enumeration is certainly undesirable, and O (N*n) will definitely time out. Can think of the idea is the large framework of the second Division to find the remainder, each time to judge whether it is consistent. The key is how to judge without timing out. Just started to tangle, want to use two for loop pruning, but actually to put the numerator or the denominator outside is not sure. Later, I thought of a good method based on the concrete examples.

Assuming that the current incoming expected remainder is X, the purpose is to determine whether such a large remainder can be found:

1. First two points to find the denominator, the denominator must be greater than X, two points to find x in the array corresponding subscript (no x words to find the first greater than X);

2. The remainder should be clarified first, since it is an increment of the array, which should be found in the X integer multiples of the left one. For example, 24, the maximum remainder must be at 48 left, if 47 exists, then the maximum remainder is 23, can not be large.

3. Two points to find an integer multiple of x in the corresponding subscript in the array, to obtain the remainder, update the remainder;

4. Two points useful to Lower_bound () &upper_bound () function, function lookup principle or two points, attach a link, you can refer to how to use: http://blog.csdn.net/jack_incredible/ article/details/7377035

Code implementation:

<span style= "FONT-SIZE:14PX;" > #include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstring> #include <

algorithm> #define INF 200005 #define MAX 1000006 using namespace std;
The int num[inf];//stores the number void solve ();//the main solve function bool Ifmod (int);//Determine if you can find such a large remainder int n,result,max_num;
    int show[max];//Determine if a number has occurred, prune int main () {scanf ("%d", &n);
    Result=0;max_num=max;
    memset (num,0,sizeof (num));
    for (int i=0;i<n;i++) scanf ("%d", &num[i]);
    Sort (num,num+n);
    MAX_NUM=NUM[N-1];
    Solve ();
return 0;
    } void Solve () {///two to find the remainder, the boundary is so initialized that it is the first time to Judge Double lb=1,ub=2* (Max_num)-1 from the maximum;
        for (int i=0;i<100;i++) {int mid= (int) ((Lb+ub)/2);
        if (Ifmod (mid)) Lb=mid;
        else Ub=mid;
    if (ub==lb)//Shear break;
    } printf ("%d\n", result);
return;
    } bool Ifmod (int x) {if (X<=result) return true;
    Memset (Show,0,sizeof (show)); Call function to find upper bound int I=uppeR_bound (num,num+n,x)-num;
    int lb,mod;
        for (; i<n;i++) {if (num[i]==1) continue;
        If there is a direct continue if (show[num[i]]==1) continue;
        Show[num[i]]=1; for (int j=2;;
            J + +) {//Find a number Lb=lower_bound (NUM,NUM+N,NUM[I]*J)-num-1 than an integer multiple to the left of one digit;
            Mod=num[lb]%num[i];
                if (mod>=x) {Result=max (result,mod);
            return true; }//exceeds the maximum number or the remainder is already equal to the maximum, pruning if (num[i]*j>max_num| |
        MOD==NUM[I]-1) break;
}} return false; }</span>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.