题目链接: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. j经常使用ava应用server

    什么是应用server它?它主要提供的执行环境的应用程序,为组件提供服务. 要了解更多关于,您可以查看我的博客:何为容器? Java 的应用server非常多,从功能上分为两类:WEB 应用serve ...

  2. Linus Torvalds来自开发商的消息:成就,不定

    于IEEE 计算机学会在接受记者采访时, Linux父亲解释了他的哲学了操作系统的成功背后.Linus Torvalds在接受IEEE采访计算机学会谈过:"Linux这项新技术是不是它的,但 ...

  3. FreeBSD包管理

    FreeBSD软件没有安装Ubuntu的apt-get.它也不是Gentoo的portage.有三种方式: package ports 自主进行源代码编译安装 这里简介前两种. FreeBSD 6.0 ...

  4. struts2-dojo-plugin-2.3.1.2.jar!/struts-plugin.xml:29:119

    Unable to load configuration. - bean - jar:file:/D:/code_workspace/SSHWorkSpace3/.metadata/.plugins/ ...

  5. 苹果(APPLE)开发人员账号说明及注冊流程(99美元公司版/个人版及299美元企业版)

    APPLE的政策是,要公布到APP STORE必须用99美元的个人版或公司版Apple Developer Programs,要在非商店外下载仅仅能使用299美元的企业版iOS Developer E ...

  6. [置顶] Hibernate从入门到精通(十一)多对多双向关联映射

    上次我们在中Hibernate从入门到精通(十)多对多单向关联映射讲解了一下多对多单向关联映射,这次我们讲解一下七种映射中的最后一种多对多双向关联映射. 多对多双向关联映射 按照我们之前的惯例,先看一 ...

  7. linux sdio card睡眠治疗 sdio card removed解决方案

    近期调试几款sdio card suspend时都会出现sdio card removed,之后 要么死机要么模块不能正常工作.根本原因也就是休眠没有处理好.昨天最终找到了 解决方法. 1:host端 ...

  8. PC结束 Spark 二次开发 收到自己主动,并允许好友请求

    本次Spark二次开发是为了客服模块的开发, 能让用户一旦点击该客服则直接自己主动加入好友.而客服放则需自己主动加入好友,不同弹出对话框进行允许,这方便的广大客服. 如今废话不多说,直接上代码. pa ...

  9. Java内部类详解(转)

    说起内部类这个词,想必很多人都不陌生,但是又会觉得不熟悉.原因是平时编写代码时可能用到的场景不多,用得最多的是在有事件监听的情况下,并且即使用到也很少去总结内部类的用法.今天我们就来一探究竟.下面是本 ...

  10. neu1458 方格取数 dp解法

    题意: 有N * N个格子,每一个格子里有正数或者0,从最左上角往最右下角走,仅仅能向下和向右,一共走两次(即从左上角走到右下角走两趟),把全部经过的格子的数加起来,求最大值SUM,且两次假设经过同一 ...