Maximum Value(哈希)
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(哈希)的更多相关文章
- [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 ...
- [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 ...
- poj1200Crazy Search (哈希)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Crazy Search Time Limit: 1000MS Memory ...
- 一致性哈希(附带C++实现)
在分布式集群中,对机器的添加删除,或者机器故障后自动脱离集群这些操作是分布式集群管理最基本的功能.如果采用常用的hash(object)%N算 法,那么在有机器添加或者删除后,就需要大范围的移动原有数 ...
- [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 ...
- POJ 1200:Crazy Search(哈希)
Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32483 Accepted: 8947 Des ...
- MySql 自适应哈希索引
一.介绍 哈希(hash)是一种非常快的查找方法,一般情况下查找的时间复杂度为O(1).常用于连接(join)操作,如Oracle中的哈希连接(hash join). InnoDB存储引擎会监控对表上 ...
- 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 ...
- POJ 2774 后缀数组 || 二分+哈希
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 35607 Accepted: 14 ...
随机推荐
- 《30天自制操作系统》09_day_学习笔记
harib06a: 在昨天的最后一部分,我们已经变成了32位的模式,目的就是希望能够使用电脑的全部内存. 虽然鼠标的显示处理有一些叠加问题,不过笔者为了不让我们感到腻烦,先带我们折腾一下内存 这里笔者 ...
- sdutoj 2154 Shopping
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2154 Shopping Time Limit: ...
- Java基础(5):试用Array类对数据进行操作(Sort和toString方法)
Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现数组的排序.搜索等 1. 排序 语法: Arrays.sort(数 ...
- 关于vptr指针初始化的分步
vptr:一个具有虚函数类的对象所具有的隐藏的成员,指向该类的虚函数表. 父类对象的vptr指向是一直指向父类的.但子类的vptr指针最终是指向子类的, 当子类创建的时候,先调用父类构造函数,这个时候 ...
- node.js http.get 和http.post 数据
http.get('http://codestudy.sinaapp.com', function (response) {}); node.js http post 样例: var reqData= ...
- paper 52 :windows7环境下theano安装
要做卷积神经网络的一些东西,所以要装theano,网上很多Theano安装教程版本较老,而各安装包更新很快,参考价值有限.走了很多弯路才装好,把这个过程记录下来,希望对大家有帮助~ ~ 我的配置:wi ...
- paper 13:计算机视觉研究群体及专家主页汇总
做机器视觉和图像处理方面的研究工作,最重要的两个问题:其一是要把握住国际上最前沿的内容:其二是所作工作要具备很高的实用背景.解决第一个问题 的办法就是找出这个方向公认最高成就的几个超级专家(看看他们都 ...
- sql server 查看数据库编码格式
user masterselect SERVERPROPERTY(N'edition') as Edition --数据版本,如企业版.开发版等,SERVERPROPERTY(N'collation' ...
- qsort函数用法(转)
qsort函数用法 qsort 功 能: 使用快速排序例程进行排序 用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(c ...
- 【crunch bang】调整窗口大小
在终端下, <super> + 上箭头 == 向上调整大小 <super> + 下箭头(左.右)