B. Maximum Value

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Sample test(s)

Input

3

3 4 5

Output

2

Hash记录输入的数值,Dp[i]记录输入中距离i最近的数值(不包括本身)

这里写代码片#include <set>
#include <map>
#include <list>
#include <stack>
#include <cmath>
#include <queue>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define PI cos(-1.0)
#define RR freopen("input.txt","r",stdin) using namespace std; typedef long long LL; const int MAX = 2*1e6+10; const int R =1e6; int Dp[MAX]; int n; int a[MAX]; bool Hash[MAX]; int main()
{
int n;
memset(Hash,false,sizeof(Hash));
scanf("%d",&n);
int Min=MAX,Max=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
Hash[a[i]]=true;
Min=min(Min,a[i]);
Max=max(Max,a[i]);
}
for(int i=Min;i<MAX;i++)
{
if(Hash[i-1])
{
Dp[i]=i-1;
}
else
{
Dp[i]=Dp[i-1];
}
}
int ans=0;
for(int i=Min;i<=R;i++)
{
if(Hash[i])
{
for(int j=i*2;;j+=i)
{
if(Dp[j]<i)
{
continue;
}
ans=max(Dp[j]%i,ans);
if(Dp[j]==Max)
{
break;
}
}
}
}
printf("%d\n",ans);
return 0;
}

Maximum Value(哈希)的更多相关文章

  1. [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  2. [LeetCode] Maximum Product of Word Lengths 单词长度的最大积

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  3. poj1200Crazy Search (哈希)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Crazy Search Time Limit: 1000MS   Memory ...

  4. 一致性哈希(附带C++实现)

    在分布式集群中,对机器的添加删除,或者机器故障后自动脱离集群这些操作是分布式集群管理最基本的功能.如果采用常用的hash(object)%N算 法,那么在有机器添加或者删除后,就需要大范围的移动原有数 ...

  5. [LeetCode] Maximum Length of Repeated Subarray 最长的重复子数组

    Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...

  6. POJ 1200:Crazy Search(哈希)

    Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32483   Accepted: 8947 Des ...

  7. MySql 自适应哈希索引

    一.介绍 哈希(hash)是一种非常快的查找方法,一般情况下查找的时间复杂度为O(1).常用于连接(join)操作,如Oracle中的哈希连接(hash join). InnoDB存储引擎会监控对表上 ...

  8. Maximum Size Subarray Sum Equals k -- LeetCode

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  9. POJ 2774 后缀数组 || 二分+哈希

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 35607   Accepted: 14 ...

随机推荐

  1. MAX(A,B)

    MAX(A,B)  可以把x,或者y的变量扔进去比较,会自动放出比较结果,这样就避免的三目运算.

  2. Leetcode: Arithmetic Slices

    A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...

  3. 一群猴子排成一圈,按1,2,...,n依次编号。然后从第1只开始数,数到第m只,把它踢出圈,从它后面再开始数,再数到第m只,在把它踢出去...,如此不停的进行下去,直到最后只剩下一只猴子为止,那只猴子就叫做大王。要求编程模拟此过程,输入m、n, 输出最后那个大王的编号

    <?php/** * [猴子选大王] * @param  [type] $m [猴子数] * @param  [type] $n [出局次数] * @return [type]    [desc ...

  4. POJ 2947 Widget Factory(高斯消元)

    Description The widget factory produces several different kinds of widgets. Each widget is carefully ...

  5. bean在容器上的生命周期

            初始化两种方法:         1,使用init-method属性指定那个方法在bean依赖关系设置好后自动执行.         2,实现initializingBean接口 实现 ...

  6. 一天弹出一次广告cookie

    function setCookie(name, value, expire) { window.document.cookie = name + "=" + escape(val ...

  7. Java IO总结之缓冲读入文件

    package com.io; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException ...

  8. [MacOS] xcrun: error: active developer path ("/Volumes/Xcode/Xcode6-Beta.app/Contents/Developer") does not exist, use xcode-select to change

    When using MacOS with xcode6-beta, i always meet these error: xcrun: error: active developer path (& ...

  9. sqlhelper-sql数据库

    using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; usin ...

  10. 使用Mysql ID自增长时 在Mapper的<insert>里添加对应的代码

    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long&quo ...