D. Maximum Value
 
 

You are given a sequence a consisting of n integers. Find the maximum possible value of  (integer remainder of ai divided byaj), 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.

Sample test(s)
input
3
3 4 5
output
2

题意:给你一个a数组,问你最大的ai%aj是多少(ai>aj);
题解:我们要求ai%aj最大,ai%(aj*k)最大,类似筛法去最大就是了
///

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//****************************************
const int N=+;
#define mod 10000007
#define inf 1000000001
#define maxn 10000 int a[N];
int main() {
int n=read();
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
// H[a[i]]=1;
}
sort(a+,a+n+);
a[]=-;int ans=-;
for(int i=;i<=n;i++) {
if(a[i]==a[i-])continue;
for(int j=a[i]+a[i];j<=a[n];j+=a[i])
{
int tmp=lower_bound(a+,a+n+,j)-a;
ans=max(ans,a[tmp-]%a[i]);
}
ans=max(ans,a[n]%a[i]);
}
cout<<ans<<endl;
return ;
}

代码

 

Codeforces Round #276 (Div. 1)B. Maximum Value 筛法的更多相关文章

  1. Codeforces Round #276 (Div. 1) B. Maximum Value 筛倍数

    B. Maximum Value Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/prob ...

  2. Codeforces Round #276 (Div. 2)D - Maximum Value(筛法)

    就是一种筛法思想的应用. #include<iostream> #include<cstdio> #include<cstdlib> #include<cst ...

  3. codeforces 484b//Maximum Value// Codeforces Round #276(Div. 1)

    题意:给一个数组,求其中任取2个元素,大的模小的结果最大值. 一个数x,它的倍数-1(即kx-1),模x的值是最大的,然后kx-2,kx-3模x递减.那么lower_bound(kx)的前一个就是最优 ...

  4. Codeforces Round #276 (Div. 1) D. Kindergarten dp

    D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...

  5. Codeforces Round #221 (Div. 1) B. Maximum Submatrix 2 dp排序

    B. Maximum Submatrix 2 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  6. Codeforces Round #276 (Div. 1) A. Bits 二进制 贪心

    A. Bits Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/problem/A Des ...

  7. Codeforces Round #276 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/485 A题.Factory 模拟.判断是否出现循环,如果出现,肯定不可能. 代码: #include<cstdio> ...

  8. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits

    http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...

  9. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence

    http://codeforces.com/contest/484/problem/E 题意: 给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值 第i棵线段树表示>=i的数 维护 ...

随机推荐

  1. Leetcode0133--Clone Graph 克隆无向图

    [转载请注明]:https://www.cnblogs.com/igoslly/p/9699791.html 一.题目 二.题目分析 给出一个无向图,其中保证每点之间均有连接,给出原图中的一个点 no ...

  2. HTML CSS, JavaScript 计算器

    效果图: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  3. jquery jstree 插件的使用

    最近一个项目 需要用到jstree 这个jQuery插件,就研究了下,做目录树 菜单还是很强大的,下面对经常会用到几个用法做下说明. 1. 首先页面 引用 jquery.jstree 2. html ...

  4. ipc (进程间通信

    进程间通信(IPC,Inter-Process Communication),指至少两个进程或线程间传送数据或信号的一些技术或方法.进程是计算机系统分配资源的最小单位(严格说来是线程).每个进程都有自 ...

  5. Cesium学习笔记(四)Camera ----http://blog.csdn.net/hobhunter/article/details/74909641

    Cesium 相机控制场景中的视野.操作相机的方法有很多,如旋转,缩放,平移和飞到目的地.Cesium具有默认的鼠标和触摸事件处理程序与相机进行交互,还有一个API以编程方式操纵相机. 我们可以使用该 ...

  6. spring boot 2.0 报错:“jdbcUrl is required with driverClassName.” 解决办法!

    springboot 升级到2.0之后发现配置多数据源的时候报错: "jdbcUrl is required with driverClassName."或者Cause: java ...

  7. 9 Java 堆排序

    堆是具有以下性质的完全二叉树,每个结点的值都大于或等于其左右孩子结点的值,称为大顶堆:或者每个结点的值都小于或等于其左右孩子结点的值,称为小顶堆.如下图: 同时,我们对堆中的结点按层进行编号,将这种逻 ...

  8. demo__webpack

    webpack 中使用的包更新非常频繁,使用方式可能很快就会改变,解决方式 看webapck文档 和 包的使用文档 看包的源码 其他... 环境 win10 + webstorm 2019.1.3 + ...

  9. openstack——cinder服务篇

    一.cinder 介绍:     理解 Block Storage 操作系统获得存储空间的方式一般有两种: 通过某种协议(SAS,SCSI,SAN,iSCSI 等)挂接裸硬盘,然后分区.格式化.创建文 ...

  10. Scala 技术笔记之 Option Some None

    避免null使用 大多数语言都有一个特殊的关键字或者对象来表示一个对象引用的是“无”,在Java,它是null.在Java 里,null 是一个关键字,不是一个对象,所以对它调用任何方法都是非法的.但 ...