题目链接:Codeforces 484B Maximum Value

题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj

解题思路:类似于素数筛选法的方式,每次枚举aj,然后枚举k,每次用二分找到小于k∗aj而且最大的ai,维护答案,过程中加了一些剪枝。

#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int maxn = 1e6+5; int N, a[maxn]; int solve (int x) {
int ret = 0, p = x;
while (p < maxn) {
p += x;
int k = lower_bound(a, a + N, p) - a; if (k == 0) continue;
else k--; if (a[k] <= x) continue; ret = max(ret, a[k] % x);
}
return ret;
} int main () {
scanf("%d", &N);
for (int i = 0; i < N; i++)
scanf("%d", &a[i]);
sort(a, a + N); int ans = 0;
for (int i = N-1; i >= 0; i--) {
if (ans >= a[i] - 1)
break;
if (i < N - 1 && a[i] == a[i+1])
continue;
ans = max(ans, solve(a[i]));
}
printf("%d\n", ans);
return 0;
}

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

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

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

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

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

  3. CodeForces 484B Maximum Value

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

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

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

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

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

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

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

  7. 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 ...

  8. Maximum Value(CodeForces - 484B)

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

  9. CodeForces 484B 数学 Maximum Value

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

随机推荐

  1. Java彻底 - WEB容器的侦听具体解释 ServletContextListener

    WEB容器的侦听器ServletContextListener主要用于监测容器启动和 当破坏需要做一些操作,听众将能够使用此做. ServletContextListener在Spring开始,然后再 ...

  2. Objective-C路成魔【11-多态性、动态类型和动态绑定】

    郝萌主倾心贡献.尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠.支持郝萌主.捐赠数额任意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 多态这个其 ...

  3. SenchaTouch2.3.1 正在使用listpaging以及pullrefresh插入 分页演示样品做

    实际上本实施例是相对简单的.自定义PullRefreshFn插头继承Ext.plugin.PullRefresh. 主要是其附加refreshFn下拉监控事件. listpaging么改动.再将这两个 ...

  4. SQLServer 2008 技术内幕——T-SQL 查询 笔记

    原文:SQLServer 2008 技术内幕--T-SQL 查询 笔记 1.SQL编程有许多独特之处,如:面向集合的思维方式.查询元素的逻辑处理顺序.三值逻辑.如果不掌握这些知识就开始用SQL编程,得 ...

  5. SQL Server错误代码及解释(留着备用)

    原文:SQL Server错误代码及解释(留着备用) 转自:http://www.ajia.me/Article/193.html Code Error Message 0 操作成功完成.  1 功能 ...

  6. sqlserver缓存程序-只能使用一次清除缓存计划

    plan cache非常大.将仅仅使用一次的缓存计划清除,而不用清除整个cache. declare @sid varbinary(64) declare cur01 cursor for selec ...

  7. 移动端 延迟加载echo.js的使用

    浏览器支持ie8+   <img src="img/blank.gif" alt="" data-echo="img/album-1.jpg&q ...

  8. 学习笔记之TCP/IP协议的重要性

    1. 随处可见的协议     在计算机网络与信息通信领域里,人们常常提及"协议"一词.互联网中常 用的具有代表性的协议有IP.TCP.HITP等. 而LAN(局域网)中经常使用的协 ...

  9. VB.Net出口Excel原则

        在VB机房的版本中,我们已经暴露导出Excel特征,此功能已重新接触到不同的理解 一.原理 要实现导出Excel的功能,首先要引用命名空间,目的是能够使用该命名空间下的方法和类 Imports ...

  10. ASN.1 Encode an Object Identifier (OID) with OpenSSL

    OID(Object Identifier) denotes an object. Examples: ------------------------------------------------ ...