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 divided by a**j), where 1 ≤ i, j ≤ n and a**i ≥ a**j.
Input
The first line contains integer n — the length of the sequence (1 ≤ n ≤ 2·105).
The second line contains n space-separated integers a**i (1 ≤ a**i ≤ 106).
Output
Print the answer to the problem.
Examples
Input
Copy
33 4 5
Output
Copy
2
思路:
题目是说,给一个数列,求这个数列的任两个数中大数模小数的最大值。先说说正确的思路,考虑每个数,枚举它的倍数,在数列中每次二分查找比这个倍数大于等于的数的位置,减一后就是这个数被数列中的数模得到最大值的位置。具体的,a[i]来说,我们每次枚举a[i]的倍数,发现\([a[i]*k,a[i]*(k+1))\),在这个区间内一个数模a[i]值是递增的。那么数列中最接近\(a[i]*(k+1)\)的数来%a[i]就可以得到最大值。这个数怎么求呢?具体的,就是lower_bound找到大于等于\([i]*(k+1)\)的值的位置减一。为什么减一,加入数列中刚好有\(a[i]*(k-1)\),我们减一就得到最接近它的数,如果没有,减一也得到了最接近它的数。注意题目要求要大数模小数。
再来说说一个看着正确的思路。转化研究对象我们来看对数列中的每一个数,怎么才能让它的余数最大。
比如:9,9%2=1,%3=0,%4=1,%5=4,%6=3,%7=2,%8=1我们发现,当它模自己一半左右的数时取得模的最大值。这个思路就是在数组中二分查找一个数一半的值,在一半的值的位置附近遍历找最大。这个只能过12个测试点,好像。原因呢,可不可以构造出一组数据,让一个数的一半左右的值都不在遍历范围,然后取到不是最大值的数刚刚好在遍历范围内呢?暂时不知道,留待解答。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#define max_n 200005
using namespace std;
int n;
int a[max_n];
int tot = 0;
int cnt[1000005];
int maxm = 0;
inline void read(int& x)
{
x=0;int f=0;char ch = getchar();
while(ch<'0'||ch>'9') {if(ch=='-')f=1;ch=getchar();}
while('0'<=ch&&ch<='9') {x=10*x+ch-'0';ch=getchar();}
x=f?-x:x;
}
#pragma optimize(2)
int main()
{
read(n);
for(int i = 0;i<n;i++)
{
int num;
read(num);
if(cnt[num]==0)
{
a[tot++] = num;
cnt[num] = 1;
}
}
sort(a,a+tot);
for(int i = 0;i<tot;i++)
{
for(int j = a[i];j<=2*a[tot-1];j+=a[i])
{
int pos = lower_bound(a,a+tot,j+a[i])-a-1;
if(pos==-1)pos++;
//cout << "pos " << pos << endl;
//cout << a[pos] << "%" << a[i] << endl;
maxm = max(maxm,a[pos]%a[i]);
}
}
cout << maxm << endl;
return 0;
}
参考文章:
张松超,codeforces 485D. Maximum Value(二分+思维),https://blog.csdn.net/ZscDst/article/details/78775965
Codeforces C. Maximum Value(枚举二分)的更多相关文章
- Codeforces 484B Maximum Value(高效+二分)
题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...
- codeforces 613B B. Skills(枚举+二分+贪心)
题目链接: B. Skills time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces 497B Tennis Game( 枚举+ 二分)
B. Tennis Game time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- HDU4430 Yukari's Birthday(枚举+二分)
Yukari's Birthday HDU4430 就是枚举+二分: 注意处理怎样判断溢出...(因为题目只要10^12) 先前还以为要用到快速幂和等比数列的快速求和(但肯定会超__int64) 而 ...
- CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。
1514: Packs Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 61 Solved: 4[Submit][Status][Web Board] ...
- 4 Values whose Sum is 0(枚举+二分)
The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute ...
- 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 ...
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分
C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...
- Codeforces 484B Maximum Value(排序+二分)
题目链接: http://codeforces.com/problemset/problem/484/B 题意: 求a[i]%a[j] (a[i]>a[j])的余数的最大值 分析: 要求余数的最 ...
随机推荐
- sqlite3 数据库表查看步骤
sqlite3 数据库表查看步骤 1 sqlite3 local.db2 .mode column3 .headers on4 .tables5 select * from tablename6 ;( ...
- PHP防止刷微信红包方法
PHP防止刷微信红包方法1 输入验证码2授权登陆后 领取红包记录下 openid ip 第二次用openid或者ip(ip)连接同一个路由器是一样的 所以用ip 判断最好是判断有没有6个以上 判断有没 ...
- Windows下同时安装了Python2与Python3时如何使用RobotFrameWork
由于windows下不能像linux那样指定python文件的运行路径,当电脑中即安装了python2,又安装了python3时,也不能在环境变量中都配置运行路径吧(当然是可以配置的,系统会按照靠前的 ...
- GitLabCICD
CI/CD是什么 CI全名Continuous Integration,啥意思?就是我们经常听到的持续集成概念.当开发每天会提交多次代码到主干上,会做一些重复性的动作时,就可以用持续集成环境来操作.有 ...
- JavaSE面试题:类初始化和实例初始化等
类初始化过程 1.一个类要创建实例需要先加载并初始化该类 main方法所在的类需要先加载和初始化 2.一个子类要初始化需要先初始化父类 3.一个类初始化就是执行<clinit>()方法 & ...
- 2019-6-28笔记总结-编程语言发展史和python安装
一.编程语言的发展史 1.机器语言(就是010101的二进制数,直接用二进制跟计算机直接沟通交流,直接操作硬件) 优点:计算机能够直接读懂,速度快 缺点:开发效率极低 2.汇编语言(用简单的英文标签来 ...
- python代码执行SQL文件(逐句执行)
一.简介 关于Python如何连接数据库并执行SQL语句,几乎所有的Python教程都会讲,教程里基本只介绍了执行单条SQL语句的方法,但是实际生产过程中可不只是执行一两条语句,动辄几十条甚至上百条的 ...
- Sqlserver 总结(2) 存储过程
目录 写在前面 什么是存储过程 存储过程的优点 存储过程的分类 1.只返回单一记录集的存储过程 2.没有输入输出的存储过程 3.有返回值的存储过程 4.有输入参数和输出参数的存储过程 5.同时具有返回 ...
- mysql 5.7 非正常安装,无法启动 服务没有报告任何错误
以前,完整安装mysql5.7程序时,由于程序太大,可以将安装缓存目录中的安装文件(较小)复制出来后,留以后使用. mysql--win32.msi 2 mysql-5.7.17-winx64.msi ...
- js 杂症,this with 变量提升
一.this.xx 和 xx 是两回事 受后端语言影响,总把this.xx 和xx 当中一回事,认为在function中,xx 就是this.xx,其实完全两回事: this.xx 是沿着this 原 ...