B. Maximum Value

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/484/problem/B

Description

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 Input

3
3 4 5

Sample Output

2

HINT

题意

给你n个数(n<=1e5),每个数(大小<=2*1e6),要求找到最大的ai%aj(ai>aj)

题解:

类似于筛法,对于每个数,我们都筛出他的倍数

ai%aj最大,可以转化为ai*k-aj最小,记录下每个数的倍数的前面的最大的数就好了

代码

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std; int pre[]; int main()
{
int n;scanf("%d",&n);
for(int i=;i<=n;i++)
{
int x;scanf("%d",&x);
pre[x]=x;
}
for(int i=;i<=;i++)
pre[i]=max(pre[i],pre[i-]);
int ans = ;
int flag = ;
for(int i=;i<=;i++)
{
if(pre[i]==i)
for(int j=i;j<=;j+=i)
ans = max(pre[i+j-]%i,ans);
}
printf("%d\n",ans);
}

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

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

    D. Maximum Value     You are given a sequence a consisting of n integers. Find the maximum possible ...

  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. 【编程之美】计算1-N中含1的个数

    转自:点我  1位数的情况: 在解法二中已经分析过,大于等于1的时候,有1个,小于1就没有. 2位数的情况: N=13,个位数出现的1的次数为2,分别为1和11,十位数出现1的次数为4,分别为10,1 ...

  2. Struts2中通配符

    1.Struts2中通配符可通过请求的url路径来确定包.类.方法.返回值名. 如 <action name="*_*_*_*" class="cn.javass. ...

  3. LXD 2.0 系列(二):安装与配置

    导读 简单来说,LXD是一个守护进程,为LXC容器的管理提供一组REST API.主要目标是提供一种类虚拟机的用户体验,是一种第三方的容器管理工具.下面呢,我们来介绍LXD 2.0 的安装与配置 安装 ...

  4. <转>如何测试大型ERP软件?

    大型ERP软件是一个在企业范围内部应用的.高度集成的软件,且操作频繁,数据在各业务系统之间高度共享.那么针对大型ERP软件的特点,我们应该怎么测试呢?要使用怎样的测试方法?需要什么样的测试人员?... ...

  5. g++安装 Gnome/Gtk+开发库 的 环境安装(安装widgets的必要条件)

    sudo yum install gcc-c++ 我在更新软件源后,看了看最新版的GTK开发版是3.0的. 所以,基本可以照搬. sudo yum -t install gtk3 sudo yum - ...

  6. JAVA中的异常(异常处理流程、异常处理的缺陷)

    异常处理流程 1)首先由try{...}catch(Exception e){ System.out.println(e); e.printStackTrace(); }finally{...}结构 ...

  7. 搜狐云景paas平台实践之路

    前言: 搜狐云景作为搜狐的paas平台,在2014年5月22日的云计算大会上正式发布了公测.初测,注册用户必须先申请邀请码参与公测会赠送用户100元电子券,经过实名认证之后会再赠送100电子券,目测可 ...

  8. Creating Help Pages for ASP.NET Web API -摘自网络

    When you create a web API, it is often useful to create a help page, so that other developers will k ...

  9. 无奈卸载Clover 转投TotalCommand

    Clover 是个好的多Tab 资源管理器,但在Win8下总是崩溃啊,让人很崩溃. 无奈投奔TotalCommand吧,就是梯度有些高. 当然不习惯,也可以使用下 XYPlorer

  10. 【转】iOS开发工具系列(按功能分)

    http://www.cocoachina.com/newbie/basic/2014/0417/8187.html 这是我们多篇iOS开发工具系列篇中的一篇,此前的文章比如:那些不能错过的Xcode ...