题目链接:

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. 巡逻机器人(BFS)

    巡逻机器人问题(F - BFS,推荐) Description   A robot has to patrol around a rectangular area which is in a form ...

  2. Oracle的function

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

  3. Codeforces Round #254 (Div. 2) DZY Loves Chemistry【并查集基础】

    一开始不知道题意是啥意思,迟放进去反应和后放进去反应有什么区别 对于第三组数据不是很懂,为啥312,132的组合是不行的 后来发现这是一道考察并查集的题目 QAQ 怒贴代码: #include < ...

  4. 【集训笔记】贪心算法【HDOJ1052 【HDOJ2037

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. linux ubuntu安装jdk

    Oracle对Jdk7与Jre7的关系的经典图解 Oracle has two products that implement Java Platform Standard Edition(Java ...

  6. Json也可以这么使

    public string GetJSON() { //Json数据 string json112 = "{\"control_info\":{\"imei\& ...

  7. DHTML【11】--DOM

    大家好,从今天开始,我们将进入DOM的学习. DOM?DOM是何东东呢?大家还记得我在前面提过的DOM树吗?就是我在前面讲HTML的时候画的那个图,那个其实就是一个简单的DOM树,浏览器在解析HTML ...

  8. Linux内核源代码解析之——我与神童聊Linux内核

    本文原创为freas_1990,转载请标明出处:http://blog.csdn.net/freas_1990/article/details/11619609 我的朋友里,至少有2.5个神童. 有的 ...

  9. 关于C++的子类指针指向父类

    基类指针引用派生类对象 用基类指针引用一个派生类对象,由于派生类对象也是基类的对象,所以这种引用是安全的; 但是只能引用基类成员. 若试图通过基类指针引用那些只在派生类中才有的成员,编译器会报告语法错 ...

  10. c,assert 宏的实现

    预备知识:#define _VAL(x) #x //#x的作用就是把x表达式变成一个字符串.(注意 : 不带换行符'\n' , 换行符ascii==10).如:_STR(i<100)printf ...