题目链接:

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

题意:

求a[i]%a[j] (a[i]>a[j])的余数的最大值

分析:

要求余数的最大值非常明显a[i]越接近a[j]的倍数则余数越大 ,因此我们将全部的元素从大到小排序 ;

然后枚举a[j]的倍数 ,二分查找小于a[i]倍数的最大值,然后更新余数的最大值。

代码例如以下:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; int a[200010]; inline int mymax(int a,int b){
return a > b? a: b;
} inline int bin_search(int l,int r,int val)//二分查找小于val的最大值
{
int mid;
while(l<=r){
mid=(l+r)>>1;
if(a[mid]==val) return mid-1;
else if(a[mid]>val) r=mid-1;
else l=mid+1;
}
return r;
} int main()
{
int n;
while(~scanf("%d",&n)){
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
int mmax = 0;
for(int i=0;i<n-1;i++){
if(a[i]==0||a[i]!=a[i-1]){//剪枝。假设之前出现过了就不用查了;
int tmp=a[i]+a[i];
while(tmp<=a[n-1]){
int pos=bin_search(0,n-1,tmp);
mmax = mymax(mmax,a[pos]%a[i]);
tmp+=a[i];
}
mmax =mymax(mmax,a[n-1]%a[i]);
}
}
printf("%d\n",mmax);
}
return 0;
}

Codeforces 484B Maximum Value(排序+二分)的更多相关文章

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

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

  2. E - E CodeForces - 1100E(拓扑排序 + 二分)

    E - E CodeForces - 1100E 一个n个节点的有向图,节点标号从1到n,存在m条单向边.每条单向边有一个权值,代表翻转其方向所需的代价.求使图变成无环图,其中翻转的最大边权值最小的方 ...

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

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

  4. CodeForces 484B Maximum Value

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

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

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

  6. Codeforces C. Maximum Value(枚举二分)

    题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. UVA.10474 Where is the Marble ( 排序 二分查找 )

    UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了 ...

  8. codeforces 484B B. Maximum Value(二分)

    题目链接: B. Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. CodeForces - 762E:Radio stations (CDQ分治||排序二分)

    In the lattice points of the coordinate line there are n radio stations, the i-th of which is descri ...

随机推荐

  1. 什么是CALayer

    一.什么是CALayer * 在iOS系统中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. * 其实UIView之所以 ...

  2. Oracle的function

    写在这里,以便于以后忘记格式后可以查询. CREATE OR REPLACE FUNCTION TEMP_FUNC_WYL(PI_AAA100 VARCHAR2, PI_AAA102 VARCHAR2 ...

  3. 循环调用修正sic86

    create or replace procedure rebuild_sic86_wyl(pi_aac001 in number, po_fhz out varchar2, po_msg out v ...

  4. 【转】LINUX下一款不错的网站压力测试工具webbench

    原文链接:http://blog.csdn.net/xinqingch/article/details/8618704 安装: wget http://blog.s135.com/soft/linux ...

  5. QGraphicsTextItem中的文字对齐

    QGraphicsTextItem类可以放到QGraphicsScene或者QGraphicsItem上,用来显示格式化的文本内容,如HTML,当然纯文本也可以显示.如果只是显示纯文本,可以使用QGr ...

  6. JAVAC 命令使用方法

    结构 javac [ options ] [ sourcefiles ] [ @files ] 參数可按随意次序排列. options 命令行选项. sourcefiles 一个或多个要编译的源文件( ...

  7. 平衡工作与生活的艺术——GTD简单介绍

    1 开场白 大家好,今天是工作四年来第一次站在部门的分享会议上,所以有讲得不好的地方请大家见谅!而对于今天我想给大家介绍的"GTD工作方法",从2012年接触,认为对工作非常有帮助 ...

  8. House Robber 分类: leetcode 算法 2015-07-09 20:53 2人阅读 评论(0) 收藏

    DP 对于第i个状态(房子),有两种选择:偷(rob).不偷(not rob) 递推公式为: f(i)=max⎧⎩⎨⎪⎪{f(i−1)+vali,f(i−2)+vali,robi−1==0robi−1 ...

  9. Session 转台服务器的使用方法

    Session的缺陷:为了保持自身的稳定,IIS在访问量大的时候,可能会不自觉的重启,这时候Session就会丢失用户就会被迫下线 解决方案1:将Session放到一个专门的转台服务器上 方案2:将S ...

  10. uboot代码2:stage2代码,启动内核

    一.uboot最终目的: 1.读出内核 do_nand read kernel { flash上存的内核:uImage = 头部 + 真正的内核; } 2.启动内核. do_bootm_linux { ...