Maximum Value (Unique function, Lower_bound () function, use of upper_bound () function)

Source: Internet
Author: User

Transmission Door

When looking at the code of the big guy, I met the unique function and the lower_bound and upper_bound functions of the binary lookup, so I wrote this article to record it for review.

Unique function

In STL, the unique function is a deduplication function, the unique function is to remove the adjacent repeating elements (only one), in fact, it does not really delete the duplicate elements, is to move the duplicate elements to the back, and then still saved to the original array, and then return to the last element after the address of the back, Because unique removes the adjacent repeating elements, it is generally used before the order is ordered.

The functions of binary lookup in STL are three Lower_bound, Upper_bound. Both of these functions are used in an ordered interval (which is also the premise of using binary search), which is documented below.

Forwarditer Lower_bound (forwarditer First, Forwarditer last,const _tp& val) algorithm returns a non-descending sequence [first, last] The position of the first greater than or equal to Val in the value.

Forwarditer Upper_bound (forwarditer First, Forwarditer last, const _tp& val) The algorithm returns a non-descending sequence of the position in the "number" of the value Val.

Idea: Enumerate the multiples of each number and then find the first number less than that multiple, and maintain the maximum answer.

Code:

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <Set>6 #defineINF 0x3f3f3f3f3f7 8 using namespacestd;9typedefLong Longll;Ten Const intMAXN = 2e5+5; One intBUF[MAXN]; A  -  - intMain () the { -     intN; -scanf"%d",&n); -memset (BUF,-1,sizeof(BUF)); +      for(inti =0; I < n; i++) -     { +scanf"%d",&buf[i]); A     } atSort (buf, buf +n); -n = Unique (buf, BUF + N)-buf;//Go heavy -     intAns =0, T; -      for(inti =0; I < n; i++) -     { -          for(intj = Buf[i]; J <= buf[n-1]; J + =Buf[i]) in         { -             intpos = Lower_bound (Buf+i, Buf+n, Buf[i]+j)-BUF-1;//from buf[i] twice times . to             //printf ("pos:%d\n", Buf[pos]); +t = Buf[pos]%Buf[i]; -Ans =max (ans, t); the             if(t = = buf[i]-1) *                  Break; $         }Panax Notoginseng     } -printf"%d\n", ans); the     return 0; +}
View Code

Maximum Value (Unique function, Lower_bound () function, use of upper_bound () function)

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.