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)