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 ...
随机推荐
- PostgreSQL auto_explain
The auto_explain module provides a means for logging execution plans of slow statements automaticall ...
- 几个PostgreSQL数据库操作总结
创建表 语法:如下 create table table_name (column_name column_type(parametes)options,…); 注意: ...
- for 穷举、迭代 while循环
1.穷举: 把所有可能的情况都走一遍,使用if条件筛选出来满足条件的情况. 2.百鸡百钱:公鸡2文钱一只,母鸡1文钱一只,小鸡半文钱一只,总共只有100文钱,如何在凑够100只鸡的情况下刚好花完100 ...
- eclipse 导入jdbc4.jar 包
详细讲解链接 http://wenku.baidu.com/link?url=QUhO2rIL2fYRgOUyd1TQPEgbl0jQr156ioxK5fiwSPm_Tset2okpBEJcO1fmz ...
- XML工程配置文件的读写
TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译,使用TinyXML进行C++ XML解析,使用简单,容易上手.这个解析库的模型通过解析XML文件, ...
- Ruby On Rails环境搭建
注:现在http://rubyforge.org 网站已经停止运行,取而代之的是https://rubygems.org这个网站,下文中所需要的gem包都可以去这个网站搜索下载.其他完全按照下文说的去 ...
- << 链式重载
#include <iostream> using namespace std; class Complex { private: int a, b; public: Complex(in ...
- EXTJS 5 开发环境搭建
WEBstrom eclipse下载: http://www.eclipse.org/downloads/ spket 下载: 安装方式: http://wangke0611.iteye.com/bl ...
- C# 把字符串类型日期转换为日期类型
方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss ================================== ...
- angular 自定义指令 link or controller
Before compilation? – Controller After compilation? – Link var app = angular.module('plunker', []); ...