Codeforces Round #276 (Div. 1)B. Maximum Value 筛法
You are given a sequence a consisting of n integers. Find the maximum possible value of
(integer remainder of ai divided byaj), 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 题意:给你一个a数组,问你最大的ai%aj是多少(ai>aj);
题解:我们要求ai%aj最大,ai%(aj*k)最大,类似筛法去最大就是了
/// #include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//****************************************
const int N=+;
#define mod 10000007
#define inf 1000000001
#define maxn 10000 int a[N];
int main() {
int n=read();
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
// H[a[i]]=1;
}
sort(a+,a+n+);
a[]=-;int ans=-;
for(int i=;i<=n;i++) {
if(a[i]==a[i-])continue;
for(int j=a[i]+a[i];j<=a[n];j+=a[i])
{
int tmp=lower_bound(a+,a+n+,j)-a;
ans=max(ans,a[tmp-]%a[i]);
}
ans=max(ans,a[n]%a[i]);
}
cout<<ans<<endl;
return ;
}
代码
Codeforces Round #276 (Div. 1)B. Maximum Value 筛法的更多相关文章
- Codeforces Round #276 (Div. 1) B. Maximum Value 筛倍数
B. Maximum Value Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/prob ...
- Codeforces Round #276 (Div. 2)D - Maximum Value(筛法)
就是一种筛法思想的应用. #include<iostream> #include<cstdio> #include<cstdlib> #include<cst ...
- codeforces 484b//Maximum Value// Codeforces Round #276(Div. 1)
题意:给一个数组,求其中任取2个元素,大的模小的结果最大值. 一个数x,它的倍数-1(即kx-1),模x的值是最大的,然后kx-2,kx-3模x递减.那么lower_bound(kx)的前一个就是最优 ...
- Codeforces Round #276 (Div. 1) D. Kindergarten dp
D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...
- Codeforces Round #221 (Div. 1) B. Maximum Submatrix 2 dp排序
B. Maximum Submatrix 2 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
- Codeforces Round #276 (Div. 1) A. Bits 二进制 贪心
A. Bits Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/problem/A Des ...
- Codeforces Round #276 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/485 A题.Factory 模拟.判断是否出现循环,如果出现,肯定不可能. 代码: #include<cstdio> ...
- CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits
http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...
- CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence
http://codeforces.com/contest/484/problem/E 题意: 给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值 第i棵线段树表示>=i的数 维护 ...
随机推荐
- Android项目实战_手机安全卫士系统加速
## 1.本地数据库自动更新的工作机制1. 开启一个服务,定时访问服务器2. 进行版本对比,如果最新版本比较高,获取需要更新的内容3. 将新内容插入到本地数据库中 ## 2.如何处理横竖屏切换1. 指 ...
- JS高级——eval
eval函数可以用来将字符串转换成JavaScript代码并且运行 <script> eval('var a=10'); console.log(a);//10 </script&g ...
- Java_Web三大框架之Hibernate+jsp+selvect+HQL注册用户
Hibernate比SQL语句简单多了,代码冗余少,切方便简洁明了.下面用Hibernate+jsp+selvect+HQL来实现注册用户. 第一步:编写用户实体类和Users2.hbm.xml映射. ...
- Linux Shell ssh登录脚本
Linux 登陆服务器敲命令太多,某时候确实不便,所以就用shell写了一个 我的blog地址: http://www.cnblogs.com/caoguo 一.说明 支持秘密和密钥两种格式 用户名 ...
- mysql_数据查询_连接查询
连接查询 1.连接(join) 也称θ连接,从两个关系的笛卡尔积中选择属性间满足一定条件的元组. 等值连接:θ为“=”的连接运算称为等值连接.从关系R和S的广义笛卡尔积中选取A.B属性值相等的元组. ...
- hibernate+struts2
一.环境 添加struts2和hibernate的jar包.创建web.xml(配置struts的过滤器).struts2.xml和hibernate.cfg.xml(设置一些数据库参数). 二.创建 ...
- TCP:三次握手,URG、ACK、PSH、RST、SYN、FIN 含义
http://blog.csdn.net/wudiyi815/article/details/8505726 TCP:SYN ACK FIN RST PSH URG简析 三次握手Three-way ...
- Objective-C在ARC下结合GCD的单例模式和宏模版
单例模式在iOS开发过程中经常用到,苹果提供过objective c单例的比较官方的写法: static MyGizmoClass *sharedGizmoManager = nil; + (MyGi ...
- 文章或者观点说说等点赞功能实现(thinkphp)
前端的代码: <!-- 点赞 --> <div class='btm'><a class='zan' id="{$article.id}" href= ...
- How To:使用dmidecode获取机器序列号(Serial number)
使用dmidecode可以获取关于机器的诸多信息,比如机器的序列号 [root@dc1db01 ~]# dmidecode -s system-serial-number processor的 ...