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 ...
随机推荐
- php:mysqli扩展
mysqli功能概述:http://php.net/manual/zh/mysqli.summary.php 代码(基本的用法):貌似可以防止sql注入 $root = "root" ...
- 来自“Java中国”优秀的程序员不会觉得累成狗是一种荣耀
分享下“https://java-china.org/topic/28“,也算是对自己的一种告诫吧. 原文:Sleep deprivation is not a badge of honor 先介绍一 ...
- VCL 如何加载Gif图片和Png图片
加上头文件#include <Vcl.Imaging.GIFImg.hpp> #include <Vcl.Imaging.pngimage.hpp> Image1->Pi ...
- Eclipse开发C++遇到的Binary not found等问题
但是一些人可能在编写实际程序的时候,主要是执行程序的时候,会遇到如下两条错误信息,而无法进行开发. Launch failed.Bianry not found A program file was ...
- 动画--过渡延迟时间 transition-delay
transition-delay属性和transition-duration属性极其类似,不同的是transition-duration是用来设置过渡动画的持续时间,而transition-delay ...
- 在Visual Studio 2013/2015上使用C#开发Android/IOS安装包和操作步骤
Xamarin 配置手册和离线包下载 http://pan.baidu.com/s/1eQ3qw8a 具体操作: 安装前提条件 1. 安装Visual Studio 2013,安装过程省略,我这里安装 ...
- logstash5.x改变
5.x版本 logstash中 elasticsearch插件的workers,无法配置大于1,会提示 This plugin uses the shared and doesn't need thi ...
- 有关dwr推送的笔记
想做一个web推送相关的东东,昨天搞了一天,终于把这些杂乱的配制弄清了,今天写出来方便以后记住,也方便大家看一下吧 1:引入dwr包,我用的是maven <dependency> < ...
- ajax“显示弹窗详情”和“删除”功能练习
1.查看详细信息,以弹窗的形式显示,使用ajax 2.批量删除 “查询”功能可以参考前面的文章,这里只讲解ajax“显示弹窗详情”和“删除”功能 第一:在body中的代码 <title>a ...
- small details
1.操作数据时,一般能入库就入库,或者用xml文件,用文件操作比较复杂