Maximum Value(unique函数,lower_bound()函数,upper_bound()函数的使用)
在看大佬的代码时候遇到了unique函数以及二分查找的lower_bound和upper_bound函数,所以写这篇文章来记录以备复习。
unique函数
在STL中unique函数是一个去重函数, unique的功能是去除相邻的重复元素(只保留一个),其实它并不真正把重复的元素删除,是把重复的元素移到后面去了,然后依然保存到了原数组中,然后 返回去重后最后一个元素的地址,因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序。
STL中关于二分查找的函数有三个lower_bound 、upper_bound 。这两个函数都运用于有序区间(当然这也是运用二分查找的前提),下面记录一下这两个函数。
ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于等于值val的位置。
ForwardIter upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于值val的位置。
思路:枚举每个数的倍数,然后二分查找第一个小于该倍数的数字,维护一下最大的答案就可以了。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#define INF 0x3f3f3f3f3f using namespace std;
typedef long long ll;
const int maxn = 2e5+;
int buf[maxn]; int main()
{
int n;
scanf("%d",&n);
memset(buf, -, sizeof(buf));
for(int i = ; i < n; i++)
{
scanf("%d",&buf[i]);
}
sort(buf, buf + n);
n = unique(buf, buf + n) - buf;//去重
int ans = ,t;
for(int i = ; i < n; i++)
{
for(int j = buf[i]; j <= buf[n-]; j += buf[i])
{
int pos = lower_bound(buf+i, buf+n, buf[i]+j) - buf - ;//是从buf[i]的2倍开始搜的
//printf("pos:%d\n",buf[pos]);
t = buf[pos] % buf[i];
ans = max(ans, t);
if(t == buf[i] - )
break;
}
}
printf("%d\n",ans);
return ;
}
Maximum Value(unique函数,lower_bound()函数,upper_bound()函数的使用)的更多相关文章
- STL之std::set、std::map的lower_bound和upper_bound函数使用说明
由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...
- 二分检索函数lower_bound()和upper_bound()
二分检索函数lower_bound()和upper_bound() 一.说明 头文件:<algorithm> 二分检索函数lower_bound()和upper_bound() lower ...
- C++ lower_bound 与 upper_bound 函数
头文件: #include <algorithm> 二分查找的函数有 3 个: 参考:C++ lower_bound 和upper_bound lower_bound(起始地址,结束地址 ...
- lower_bound和upper_bound函数
lower_bound(ForwardIter first,ForwardIter last,const_TP & val) upper_bound(ForwardIter first,For ...
- STL函数 lower_bound 和 upper_bound 在算法竞赛中的用法
以前比较排斥这两个函数,遇到需要二分的情景都是手写 \(while(left<=right)\). 这次决定洗心革面记录一下这两个函数的在算法竞赛中的用法,毕竟一般不会导致TLE. 其实百度百科 ...
- C++二分查找:lower_bound( )和upper_bound( )
#include<algorithm>//头文件 //标准形式 lower_bound(int* first,int* last,val); upper_bound(int* first, ...
- C++中lower_bound函数和upper_bound函数
STL中关于二分查找的函数有三个lower_bound .upper_bound .binary_search .这三个函数都运用于有序区间(当然这也是运用二分查找的前提),下面记录一下这两个函数. ...
- lower_bound()函数,upper_bound()函数
1.查找:STL中关于二分查找的函数有三个lower_bound .upper_bound .binary_search .这三个函数都运用于有序区间(当然这也是运用二分查找的前提),下面记录一下这两 ...
- 奇思妙想:利用野指针和lower_bound()/upper_bound()函数实现整数二分
众所周知,c++的STL中提供了三个二分查找函数,binary_search(),lower_bound(),upper_bound(),功能分别是找某值是否在数组中出现,找到数组中第一个大于等于某值 ...
随机推荐
- Codeforces Round #316 (Div. 2) C. Replacement(线段树)
C. Replacement time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- js 里面的 function 与 Function
function 是 js 的标识符 Function 是 js 里面的一个 构造函数 1.new function 与 new Function 的区别 new 运算符在 js 里面是 创建一个自定 ...
- nodejs下cannot post错误
我写了一段CoffeeScript代码,主要是流程为: 1.当客户端请求方式为GET时,输出页面,页面上有个form,form 里有个submit按钮.form的 action="" ...
- jenkins安装和使用
1 jenkins安装 直接下载deb包,离线安装. 2 plugin的安装 直接下载hpi包,离线安装. 我在一次安装的时候,在配置gitlab时,在test connection的时候,出现了nu ...
- kafka02
- TextWatcher基本用法
editText.addTextChangedListener(new TextWatcher() { /** * 内容改变前调用 * 原有的文本s中,从start开始的count个字符将会被一个新的 ...
- jeesite自定义主题
jeesite cms首页太丑不够逼格,然而国内有很多高大上的皮肤供你选择,那么本文就一步一步教你如何定制自己的CMS站点视图. 1.下载 jeesite 源码,并安装配置成功 2.进入jeesite ...
- selenium3 + python - 异常处理截图 screenshot
一.截图方法 1.get_screenshot_as_file(self, filename) --这个方法是获取当前window的截图,出现IOError时候返回False,截图成功返回True. ...
- 一种高兼容性的JavaBean序列化方案
在对JavaBean做序列化时,我们可能在某些场景希望前后兼容性好一些.比如所有的javaBean都序列化后保存在数据库,用的时候需要反序列化创建.随着业务的发展,数据模型可能会进行变更,那么原来的数 ...
- sql 查询出当天记录数据
select updatetime,NewComment,HistoryID,sum(1) over(partition by UpdateTime) from LPProjectHistoryord ...