Maximum Value(CodeForces - 484B)
Maximum Value
Time limit 1000 ms
Memory limit 262144 kB
You are given a sequence a consisting of n integers. Find the maximum possible value of (integer remainder of ai divided by aj), where 1 ≤ i, j ≤ n and ai ≥ aj.
Input
The first line contains integer n — the length of the sequence (1 ≤ n ≤ 2·105).
The second line contains n space-separated integers ai (1 ≤ ai ≤ 106).
Output
Print the answer to the problem.
Example
3
3 4 5
2 题意:求aj%ai的最大值,其中j>i;
分析:每次寻找小于k*aj的最大值,类似于素数筛,例如n=5时,3 4 5 6 7 ,我们如果查找对3取模的最大值,毫无疑问就是找到3到2*3中间的最大值; AC代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 2*100000+10
const int maxn=+;
typedef long long ll;
using namespace std;
int a[N];
int n;
int C(int x){
int w=x,ans=;
while (w<a[n-]){
w+=x;
int k=lower_bound(a,a+n,w)-a;
if (k==) continue;
else k--;
if (a[k]<=x) continue;
ans=max(ans,a[k]%x);
}
return ans;
}
int main(){
ios::sync_with_stdio(false);
int ans=;
scanf("%d",&n);
for (int i=;i<n;i++) scanf("%d",&a[i]);
sort(a,a+n);
for (int i=n-;i>=;i--){
if (i<n-&&a[i]==a[i+])
continue;
if (ans>=a[i]-)
break;
ans=max(ans,C(a[i]));
}
printf("%d\n",ans);
return ;
}
Maximum Value(CodeForces - 484B)的更多相关文章
- Codeforces 484B Maximum Value(高效+二分)
题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...
- codeforces 484B B. Maximum Value(二分)
题目链接: B. Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces 484B Maximum Value(排序+二分)
题目链接: http://codeforces.com/problemset/problem/484/B 题意: 求a[i]%a[j] (a[i]>a[j])的余数的最大值 分析: 要求余数的最 ...
- CodeForces 484B Maximum Value (数学,其实我也不知道咋分类)
B. Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- CodeForces 484B Maximum Value
意甲冠军: a序列n(2*10^5)数字 问道a[i]>=a[j]如果是 a[i]%a[j]最大值是多少 思路: 感觉是一道挺乱来的题-- 我们能够将ans表示为a[i]-k*a[j] 这 ...
- codeforces 484b//Maximum Value// Codeforces Round #276(Div. 1)
题意:给一个数组,求其中任取2个元素,大的模小的结果最大值. 一个数x,它的倍数-1(即kx-1),模x的值是最大的,然后kx-2,kx-3模x递减.那么lower_bound(kx)的前一个就是最优 ...
- CodeForces 484B 数学 Maximum Value
很有趣的一道题,题解戳这. #include <iostream> #include <cstdio> #include <cstring> #include &l ...
- Educational Codeforces Round 32 Maximum Subsequence CodeForces - 888E (meet-in-the-middle,二分,枚举)
You are given an array a consisting of n integers, and additionally an integer m. You have to choose ...
- Maximum Questions CodeForces - 900E (字符串,dp)
大意:给定长$n$的字符串$s$, 只含'a','b','?', '?'可以替换为任意字符, 在给定长$t$的字符串, "ababab...", 求替换尽量少的'?', 使得$s$ ...
随机推荐
- 02-python-运算符与表达式
目录 1. 比较运算符 2. 算数运算符 3. 赋值运算符 4. 位于运算符 5. 逻辑运算符 6. 成员运算符 7. 身份运算符 8. 运算符优先级 9. 输出输入 10. 数字类型转换及常用数学方 ...
- eclipse Java EE 与 Java 区别
1. 综述 eclipse IDE 一般来说有三种可切换的模式 Java EE Java 调试 可直接下拉至底部看两者的比较. 2. Java Java 是带有用户界面的 基本IDE ,缺少数据库和w ...
- Maven--Eclipse maven相关配置
选择自己安装的 Maven 版本: 更改配置文件路径,这里选择自己安装的 Maven 下的配置文件,方便配置及统一控制:
- 用FFmpeg+nginx+rtmp搭建环境实现推流
Windows: 1.下载文件: 链接:https://pan.baidu.com/s/1c2LmIHHw-dwLOlRN6iTIMg 提取码:g7sj 2.解压文件: 解压到nginx-1.7.11 ...
- win10下安装cygwin全过程
简单讲:cygwin就是在windows系统上跑linux和unix的环境,跨平台移植的应用程序移植. 安装步骤: 下载cygwin: 打开官网https://cygwin.com/install.h ...
- SVN常见错误解决办法和批量add等命令
批量添加所有更改文件 svn add . --no-ignore --force 提交文件 svn commit -m "up" File already exists: file ...
- XSL使用写法与效果
data.xml <?xml-stylesheet type="text/xsl" href="getdata.xsl"?> <ROOT> ...
- 2019深圳Android千人开发者大会【NEW·无界】
报名地址:https://www.hdb.com/dis/mjcsegnslu 安卓巴士技术社区是中国领先的安卓开发者社区,现已聚集超过85万开发者,数年来一直致力于IT从业者的知识分享服务. 安卓巴 ...
- tc/traffic control 网络控制工具
第一个例子 增加延时 tc qdsic add dev enp0s3 root netem delay 200ms qdisc : queuing discipline, 当内核需要发送包到某个接口时 ...
- Iterator模式
Iterator英文意思是重复做某件事,中文翻译为迭代器,这个设计模式中主要有Iterator(迭代器),ConcreteIterator(具体的迭代器),Aggergate(集合),Concrete ...