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. Asp.Net Core(.net内核)

    Asp.Net Core(.net内核) //----------------Day1----------------一章    Web基本原理 1节课程说明 web窗体--设计界面--加法使用Chr ...

  2. java jvm学习笔记十一(访问控制器)

     欢迎装载请说明出处: http://blog.csdn.net/yfqnihao/article/details/8271665 这一节,我们要学习的是访问控制器,在阅读本节之前,如果没有前面几节的 ...

  3. CXF之七 传输文件

    CXF的文件传输通过MTOM实现.MTOM(SOAP Message Transmission Optimization Mechanism)SOAP消息传输优化机制,可以在SOAP消息中发送二进制数 ...

  4. C++指针的引用

    [1]指针的引用,必须加上头文件<iomanip>因为调用类setw() 对一个数据可以使用“引用”(reference)这是C++           对C的一个重要扩充,引用是一种新的 ...

  5. 多级联动导航栏(top导航)

    http://www.dynamicdrive.com/style/csslibrary/item/jquery_multi_level_css_menu_2/ This is a multi-lev ...

  6. 02《老罗Android开发视频教程》第二集:android系统框架的介绍

  7. MapReducer Counter计数器的使用,Combiner ,Partitioner,Sort,Grop的使用,

    一:Counter计数器的使用 hadoop计数器:可以让开发人员以全局的视角来审查程序的运行情况以及各项指标,及时做出错误诊断并进行相应处理. 内置计数器(MapReduce相关.文件系统相关和作业 ...

  8. java console ( mac osx ) 命令行编码

    方法 vi ~/.bash_profile #添加新行, UTF-8表示你平台的编码方式 #例如你是GBK.GB18030的 #替换成你平台console可现实字符编码即可 export JAVA_T ...

  9. Linux虚拟机创建后如何进行登录(Windows Azure)

    Linux虚拟机创建后如何进行登录 若要管理虚拟机的设置以及在其上运行的应用程序,可以使用安全外壳 (SSH) 客户端.为此,您必须在计算机上安装要用于访问虚拟机的 SSH 客户端.您可以选择很多 S ...

  10. Makefile中用宏定义进行条件编译

    在源代码里面如果这样是定义的: #ifdef   MACRONAME //可选代码 #endif 那在makefile里面 gcc   -D   MACRONAME=MACRODEF 或者 gcc   ...