Codeforces 484B Maximum Value(高效+二分)

题目链接: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;
}
时间: 2024-08-02 12:40:51

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

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

题目链接: 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

CodeForces 484B Maximum Value

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #define sc(x) scanf("%d", &x) 7 #define sc2(x,y) scanf("%d%d", &x, &y) 8 #define pf(x

Codeforces C. Maximum Value(枚举二分)

题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a sequence a consisting of n integers. Find the maximum possible value of (integer remainder of *a**i* divi

HDU 1839 Delay Constrained Maximum Capacity Path(二分+最短路)

题目地址:HDU 1839 我去..原来这题这么简单...网络流中这种二分建图的方式做了一大堆了..这种题还能难倒我吗...白天一直没怎么看懂题,对题意懵懵懂懂的...晚上好好看了看题,这不就是网络流中练的最多的那种二分建图模型吗....只是把网络流算法改成最短路就行了..但是两个地方手残了没能在实验室当场A掉..sad... 这题就是二分最小容量,对满足容量的加边,对时间求最短路.如果最短时间比规定时间少的话就可以继续增加容量,直到不能增加为止. 代码如下: #include <iostrea

Codeforces 374D Inna and Sequence 二分+树状数组

题目链接:点击打开链接 给定n个操作,m长的序列a 下面n个数 if(co>=0)则向字符串添加一个co (开始是空字符串) else 删除字符串中有a的下标的字符 直接在序列上搞,简单模拟 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h&

Codeforces 8D Two Friends 三分+二分+计算几何

题目链接:点击打开链接 题意:点击打开链接 三分house到shop的距离,二分这条斜边到cinema的距离 #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #include<set> #include<queue> #include<vector> #include<

CodeForces 484B 数学 Maximum Value

很有趣的一道题,题解戳这. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 const int maxn = 200000 + 10; 8 const int maxm = 1000000 + 10; 9 10 int a[maxn], f[maxm]; 11 12 int main

Codeforces 483B - Friends and Presents - [二分]

题目链接:http://codeforces.com/contest/483 A - Counterexample - [简单构造题] Your friend has recently learned about coprime numbers. A pair of numbers $(a,?b)$ is called coprime if the maximum number that divides both $a$ and $b$ is equal to one. Your friend

Codeforces 479D Long Jumps(贪心+二分)

题目链接:Codeforces 479D Long Jumps 题目大意:valery是个体育老师,现在他要为学生考跳远,女生标准为x,男生为y,现在一个长为L的刻度尺,有N个刻 度,给定N个刻度,现在为说还需要加几个刻度才能测量x,y这两个长度. 解题思路:因为总共就x,y两个长度,所以最多加两个刻度.所以只要判断不加和加一个的情况即可. 先枚举每个刻度a[i],然后用二分查找一下a[i]+x或者a[i]-x刻度存不存在,同理y.如果x和y都通过判断,那么就是不需 要加刻度. 如果只通过x或只