51nod1421 Max MoD Value

Source: Internet
Author: User

O (n2) tle. O (NLOGNLOGN)

#include <cstdio> #include <cstring> #include <cctype> #include <algorithm>using namespace std; #define REP (I,s,t) for (int. i=s;i<=t;i++) #define DWN (i,s,t) for (int i=s;i>=t;i--) #define CLR (x,c) memset (X,c, sizeof (x)) int read () {int X=0;char C=getchar (), while (!isdigit (c)) C=getchar (), while (IsDigit (c)) x=x*10+c-' 0 ', c= GetChar (); return x;} const int Nmax=2e5+5;int a[nmax];int Main () {int n=read (); Rep (I,1,n) A[i]=read (); sort (a+1,a+n+1); int Ans=0,tp;rep (i,1, n-1) {for (int j=a[i]+a[i];j<=a[n]*2;j+=a[i]) {tp=lower_bound (a+1,a+n+1,j)-a-1;ans=max (Ans,a[tp]%a[i]);}} printf ("%d\n", ans); return 0; }

  

1421 Max MoD value title Source: Codeforces Base time limit: 1 second space limit: 131072 KB score: 80 Difficulty: 5-Level algorithm topic collection concern

There is an an an array of n integers. Now you want to find two numbers (can be the same one)ai,aJ Makesai mod aJ Max and ai ≥ aJ.

Input
A single set of test data. The first line contains an integer n, which represents the size of the array A. (1≤n≤2*10^5) The second line has n spaces separated by an integer AI (1≤ai≤10^6).
Output
Output An integer representing the maximum mod value.
Input example
33 4 5
Output example
2

51nod1421 Max MoD Value

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.