题目链接:

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. Word中使用代码高亮插件

    Word中使用代码高亮插件 1.下载并安装:SyntaxHighlighter4Word.zip 解压,然后双击bin\word2010\Kong.SyntaxHighlighter.Word2010 ...

  2. Linux 下github的使用

    *初始化git仓库,使用git init命令 *添加文件到git仓库分两步: 1.使用git add filename  :可分多次使用,添加多个文件到暂存区 2.使用git commit -m  “ ...

  3. form的验证用法

    models.py forms.py html页面 第二种方法: 不继承model直接自定义内容 获得内容: if form.is_valid():            price = reques ...

  4. Spring如何管理Session【转贴】

    在使用Spring进行系统开发的时候,数据库连接一般都是配置在Spring的配置文件中,并且由Spring来管理的.在利用Spring + Hibernate进行开发时也是如此.下面是一个简单的Spr ...

  5. Windows Phone 8初学者开发—第19部分:设置RecordAudio.xaml页面

    原文 Windows Phone 8初学者开发—第19部分:设置RecordAudio.xaml页面 原文地址:  http://channel9.msdn.com/Series/Windows-Ph ...

  6. java困惑(二)函数以及引用改变

    具体的Visio 文档在资源

  7. HDU 4883 TIANKENG’s restaurant (贪心)

    链接:pid=4883">带我学习.带我飞 第一次BC,稳挂,WA n多次.今天又一次做了一下 略挫 #include <iostream> #include <cs ...

  8. [Andriod官方API指南]连接之蓝牙

    Bluetooth —— 蓝牙 The Android platform includes support for the Bluetooth network stack, which allows ...

  9. HDU1878 欧拉回路 - from lanshui_Yang

    Problem Description 欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一个图,问是否存在欧拉回路?   Input 测试输入包含若干测试用例.每个 ...

  10. Inter IPP的一些基本类型对应的vs中类型

    来自为知笔记(Wiz)