Codeforces 484B Maximum Value (sort + dichotomy)

Source: Internet
Author: User

Topic Links:

Http://codeforces.com/problemset/problem/484/B

Test instructions

The maximum value of the remainder of A[i]%a[j] (A[i]>a[j])

Analysis:

The maximum value of the remainder is very obvious a[i] the closer to A[j] the larger the remainder, so we sort all the elements from large to small;

It then enumerates multiples of a[j], finds the maximum value less than a[i] multiples, and then updates the maximum value of the remainder.

The code is as follows:

#include <iostream> #include <cstdio> #include <algorithm>using namespace Std;int a[200010];inline int Mymax (int a,int b) {return a > B? a:b;}    inline int bin_search (int l,int r,int val)//binary lookup is smaller than the maximum value of val {int mid;        while (l<=r) {mid= (l+r) >>1;        if (a[mid]==val) return mid-1;        else if (a[mid]>val) r=mid-1;    else l=mid+1; } return R;}    int main () {int n;        while (~SCANF ("%d", &n)) {for (int i=0;i<n;i++) scanf ("%d", &a[i]);        Sort (a,a+n);        int Mmax = 0; for (int i=0;i<n-1;i++) {if (a[i]==0| | A[i]!=a[i-1]) {//pruning.            If you have seen it before, you don't have to check it; int tmp=a[i]+a[i];                while (tmp<=a[n-1]) {int pos=bin_search (0,N-1,TMP);                Mmax = Mymax (Mmax,a[pos]%a[i]);            Tmp+=a[i];            } Mmax =mymax (Mmax,a[n-1]%a[i]);    }} printf ("%d\n", Mmax); } return 0;}



Codeforces 484B Maximum Value (sort + dichotomy)

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.