题目链接:

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.

Examples
input
3
3 4 5
output
2

题意:

给n个数,要求算出最大的a[i]%a[j]的值,其中a[i]>=a[j];

思路:

可以枚举a[j],然后找出它的所有倍数,然后再在序列中找到小于它且最接近它的数.然后就是这个阶段里面最大的值了;

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=2e5+20;
const int maxn=1e6+220;
const double eps=1e-12; int a[N],n,vis[maxn]; int main()
{
read(n);
For(i,1,n)read(a[i]);
sort(a+1,a+n+1);
int ans=0;
for(int i=1;i<=n;i++)
{
if(a[i]==a[i-1])continue;
int pos=i;
for(int k=2; ;k++)
{
int h=k*a[i];
int l=pos+1,r=n;
while(l<=r)
{
int mid=(l+r)>>1;
if(a[mid]>=h)r=mid-1;
else l=mid+1;
}
pos=r;
ans=max(ans,a[pos]%a[i]);
if(h>=maxn)break;
}
}
cout<<ans<<endl;
return 0;
}

  

codeforces 484B B. Maximum Value(二分)的更多相关文章

  1. CodeForces 484B 数学 Maximum Value

    很有趣的一道题,题解戳这. #include <iostream> #include <cstdio> #include <cstring> #include &l ...

  2. Codeforces 484B Maximum Value(高效+二分)

    题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...

  3. Codeforces 484B Maximum Value(排序+二分)

    题目链接: http://codeforces.com/problemset/problem/484/B 题意: 求a[i]%a[j] (a[i]>a[j])的余数的最大值 分析: 要求余数的最 ...

  4. CodeForces 484B Maximum Value (数学,其实我也不知道咋分类)

    B. Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  5. Codeforces 888E:Maximum Subsequence(枚举,二分)

    You are given an array a consisting of n integers, and additionally an integer m. You have to choose ...

  6. Educational Codeforces Round 32 Maximum Subsequence CodeForces - 888E (meet-in-the-middle,二分,枚举)

    You are given an array a consisting of n integers, and additionally an integer m. You have to choose ...

  7. Maximum Value(CodeForces - 484B)

    Maximum Value Time limit 1000 ms Memory limit 262144 kB You are given a sequence a consisting of n i ...

  8. CodeForces 484B Maximum Value

    意甲冠军: a序列n(2*10^5)数字  问道a[i]>=a[j]如果是  a[i]%a[j]最大值是多少 思路: 感觉是一道挺乱来的题-- 我们能够将ans表示为a[i]-k*a[j]  这 ...

  9. codeforces 484b//Maximum Value// Codeforces Round #276(Div. 1)

    题意:给一个数组,求其中任取2个元素,大的模小的结果最大值. 一个数x,它的倍数-1(即kx-1),模x的值是最大的,然后kx-2,kx-3模x递减.那么lower_bound(kx)的前一个就是最优 ...

随机推荐

  1. 后缀数组---Musical Theme

    POJ   1743 Description A musical melody is represented as a sequence of N (1<=N<=20000)notes t ...

  2. json在php中的使用之如何转换json为数组

    <?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}' ...

  3. 深入浅出java 8 lambda表达式--零基础一分钟入门

    lambda从使用上来说,第一感觉直白的理解就是,少了很多不必要的匿名回调类的写法,比如: public static void main(String[] args) { PlatformQuery ...

  4. mysql乱码以及Data too long for column全解(最完整实用版)

    今天系统升级,开发.测试说本地环境.测试环境都没有问题,都用ssh client升的,演示环境报错了Data too long for column. 仔细检查了下,表字符集都是utf-8,目测长度肯 ...

  5. 【GOF23设计模式】解释器模式 & 访问者模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_解释器模式.访问者模式.数学表达式动态解析库式 1.解释器模式Interpreter  2.访问者模式Visitor 

  6. 推荐几个的chorme的扩展程序

    下面来解释下这几个插件 (0)Context : 管理这些乱七八糟的扩展程序,插件用的时候,点开,不用的时候,关闭,这样可以降低游览器的负荷 (1)AngularJS Batarang : 测试ang ...

  7. CSS选择器特殊性与重要性

    特殊性 在编写CSS代码的时候,我们会出现多个样式规则作用于同一个元素的情况,例如 <!-- HTML --> <header> <nav class="nav ...

  8. SharePoint 2010 文档管理系列之准备篇

    前言:很早自己就想写一个系列的文章,但是不知道写什么,最近在QQ群里,好多人说在做文档管理,其实文档管理也是SharePoint的一个很不错的功能点,自己想了想,也想多学习点东西,所以写这个主题吧,今 ...

  9. iOS 使用SDwebImage缓存图片并在断网时候显示

     [_loadImageView setShowActivityIndicatorView:YES];    [_loadImageView setIndicatorStyle:UIActivityI ...

  10. 朝花夕拾-android 自定义application 管理activity的生命周期

    为了安全退出多个已创建的activity? 可以自定义application:myapplication. 增加一个list成员保存,一些关键的已创建的activity实例: private List ...