题目链接: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. POJ3467(预处理)

    Cross Counting Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 1331   Accepted: 375 De ...

  2. NYOJ 1068 ST(段树 为段更新+间隔总和)

    ST 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描写叙述 "麻雀"lengdan用随机数生成了后台数据.可是笨笨的他被妹纸的问题给难住了. .. 已知 ...

  3. 使用SQLServer 2008的CDC功能实现数据变更捕获

    原文:使用SQLServer 2008的CDC功能实现数据变更捕获 最近由于工作需要,研究了一下2008 CDC功能,觉得还不错,下面整理了一下研究过程,虽然比较粗略,但是基本上能用了,如果有补充请大 ...

  4. DP Leetcode - Maximum Product Subarray

    近期一直忙着写paper,非常久没做题,一下子把题目搞复杂了..思路理清楚了非常easy,每次仅仅需更新2个值:当前子序列最大乘积和当前子序列的最小乘积.最大乘积被更新有三种可能:当前A[i]> ...

  5. 在CMD命令行和PowerShell中实现复制粘贴功能

    在CMD命令行和PowerShell中实现复制粘贴功能         常常使用命令行或者PowerShell的朋友肯定会遇到这样的情况:粘贴文本非常easy,右键--选择粘贴就可以,可是想要复制命令 ...

  6. atitit.无损传输二进制数据串传输网络

    atitit.无损传输二进制数据串传输网络 1. gbk的网络传输问题,为什么gbk不能使用来传输二进制数据 1 2. base64 2 3. iso-8859-1  (推荐) 2 4. utf-8  ...

  7. FZU2126:消除类游戏(DP)

    Problem Description S近期在玩一种游戏. 这样的游戏的规则是一个一个地往一个栈里放有颜色的球,当栈顶连续k个球颜色同样时.这k个球立马同一时候消失. 如今S已经往栈里放了n个球,他 ...

  8. Android开发模板------自己定义SimpleCursorAdapter的使用

    使用SimpleCursorAdapter所设计的table(数据表)一定要有_id字段名称,否则会出现"找不到_id"的错误 SimpleCursorAdapter直接使用的方法 ...

  9. &quot;CoolReaper&quot; --酷派手机后门

    文章转自:http://drops.wooyun.org/tips/4342 注:译文未获得平底锅授权,纯属学习性质翻译 原文:https://www.paloaltonetworks.com/con ...

  10. Mybatis基金会: 经常问的问题FAQ

    Mybatis基金会: #{...} 和 ${...} 差额 MyBatis将 #{-} 解释为JDBC prepared statement 参数标记.而将 ${-} 解释为一个字符串替换.非常实用 ...