codeforces 484B B. Maximum Value(二分)
题目链接:
1 second
256 megabytes
standard input
standard output
You are given a sequence a consisting of n integers. Find the maximum possible value of
(integer remainder of ai divided by aj), where 1 ≤ i, j ≤ n and ai ≥ aj.
The first line contains integer n — the length of the sequence (1 ≤ n ≤ 2·105).
The second line contains n space-separated integers ai (1 ≤ ai ≤ 106).
Print the answer to the problem.
3
3 4 5
2 题意: 给n个数,要求算出最大的a[i]%a[j]的值,其中a[i]>=a[j]; 思路: 可以枚举a[j],然后找出它的所有倍数,然后再在序列中找到小于它且最接近它的数.然后就是这个阶段里面最大的值了; AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=2e5+20;
const int maxn=1e6+220;
const double eps=1e-12; int a[N],n,vis[maxn]; int main()
{
read(n);
For(i,1,n)read(a[i]);
sort(a+1,a+n+1);
int ans=0;
for(int i=1;i<=n;i++)
{
if(a[i]==a[i-1])continue;
int pos=i;
for(int k=2; ;k++)
{
int h=k*a[i];
int l=pos+1,r=n;
while(l<=r)
{
int mid=(l+r)>>1;
if(a[mid]>=h)r=mid-1;
else l=mid+1;
}
pos=r;
ans=max(ans,a[pos]%a[i]);
if(h>=maxn)break;
}
}
cout<<ans<<endl;
return 0;
}
codeforces 484B B. Maximum Value(二分)的更多相关文章
- CodeForces 484B 数学 Maximum Value
很有趣的一道题,题解戳这. #include <iostream> #include <cstdio> #include <cstring> #include &l ...
- Codeforces 484B Maximum Value(高效+二分)
题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...
- Codeforces 484B Maximum Value(排序+二分)
题目链接: http://codeforces.com/problemset/problem/484/B 题意: 求a[i]%a[j] (a[i]>a[j])的余数的最大值 分析: 要求余数的最 ...
- CodeForces 484B Maximum Value (数学,其实我也不知道咋分类)
B. Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces 888E:Maximum Subsequence(枚举,二分)
You are given an array a consisting of n integers, and additionally an integer m. You have to choose ...
- 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 ...
- Maximum Value(CodeForces - 484B)
Maximum Value Time limit 1000 ms Memory limit 262144 kB You are given a sequence a consisting of n i ...
- CodeForces 484B Maximum Value
意甲冠军: a序列n(2*10^5)数字 问道a[i]>=a[j]如果是 a[i]%a[j]最大值是多少 思路: 感觉是一道挺乱来的题-- 我们能够将ans表示为a[i]-k*a[j] 这 ...
- codeforces 484b//Maximum Value// Codeforces Round #276(Div. 1)
题意:给一个数组,求其中任取2个元素,大的模小的结果最大值. 一个数x,它的倍数-1(即kx-1),模x的值是最大的,然后kx-2,kx-3模x递减.那么lower_bound(kx)的前一个就是最优 ...
随机推荐
- 转收藏:Git常用命令速查表
一. Git 常用命令速查 git branch 查看本地所有分支git status 查看当前状态 git commit 提交 git branch -a 查看所有的分支git branch -r ...
- mysql乱码以及Data too long for column全解(最完整实用版)
今天系统升级,开发.测试说本地环境.测试环境都没有问题,都用ssh client升的,演示环境报错了Data too long for column. 仔细检查了下,表字符集都是utf-8,目测长度肯 ...
- Web Service(一) 基础学习
1 基础的Web Service平台是XML+HTTP. 2 Web Service平台的元素包括:SOAP(Simple Object Access Protocol)简单对象访问协议: UDDI( ...
- (八)play之yabe项目【身份验证】
(八)play之yabe项目[身份验证] 博客分类: 框架@play framework 添加身份验证 play提供了一个模块-Secure(安全模块),用来做身份验证 允许Secure模块 修改 ...
- GDB调试器使用总结
概述:GDB是linux下调试程序的神器,做为linux程序员,如果不能熟练的使用GDB进行程序调试,那将是很失败的事情.强大的功能使GDB的使用也变得比较复杂,如果是初学者肯定会比繁杂的命令吓到.下 ...
- BaseServlet
1. 目的: 将提升Servlet的处理请求的能力,而不只限于doGet()/doPost()等请求. 让其Servlet能够自己根据请求,从而触发相应的方法进行处理. 2. 具体代码实现: impo ...
- VisualStudio中解决方案
在VS中创建一个项目通常会生成一个解决方案文件(.sln)和一个隐藏的解决方案用户选项文件(.suo). 解决方案文件是一个文本文件,包含以下信息: 将被加载的所有项目以构成完整解决方案的项目清单 解 ...
- android XMl 解析神奇xstream 六: 把集合list 转化为 XML文档
前言:对xstream不理解的请看: android XMl 解析神奇xstream 一: 解析android项目中 asset 文件夹 下的 aa.xml 文件 android XMl 解析神奇xs ...
- Universal-Image-Loader完全解析(上)
Universal-Image-Loader完全解析(上) 基本介绍及使用 大家平时做项目的时候,或多或少都会接触到异步加载图片,或者大量加载图片的问题,而加载图片时候经常会遇到各种问题,如oom,图 ...
- openstack kilo compute更新后报错IncompatibleObjectVersion: Version 1.2 of PciDeviceList is not supported
前几天在compute节点更新openstack的包后,发现报错IncompatibleObjectVersion: Version 1.2 of PciDeviceList is not suppo ...