B. Maximum Value(Codeforces Round #276(div1)_html/css_WEB-ITnose

php中文网
发布: 2016-06-24 11:54:42
原创
1457人浏览过

B. 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 ai divided byaj), where 1?≤?i,?j?≤?n and ai?≥?aj.

Input

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).

仿B站视频帧预览插件
仿B站视频帧预览插件

仿B站视频帧预览插件

仿B站视频帧预览插件 49
查看详情 仿B站视频帧预览插件

Output

Print the answer to the problem.

Sample test(s)

input

33 4 5
登录后复制

output

<strong>找a[i]<a[j]中的a[j]%a[i]的最大值</strong>
登录后复制
<strong>由于ai<1000000;可以hash搞,如果一个点不存在,记录比它小的最大值。</strong>
登录后复制
<strong>至于找最大模后的值,取a[i]==i也就是这个点存在,取模后的最大值肯定i+1+k*i,这样每次增加i查询,</strong>
登录后复制
<strong>查到离最大值最近的值。</strong>
登录后复制
<strong>代码:</strong>
登录后复制
<strong></strong><pre name="code" class="n">#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=2000000+100;int a[maxn];int main(){    int n;    scanf("%d",&n);    int x;    for(int i=0;i<n;i++)    {        scanf("%d",&x);        a[x]=x;    }    for(int i=0;i<maxn;i++)    {        if(a[i]!=i)        a[i]=a[i-1];    }    int ans=0;    for(int i=2;i<maxn;i++)    {        if(a[i]==i)        {            for(int j=i+i-1;j<maxn;j=j+i)            {                if(a[j]%i>ans&&a[j]>i)                ans=a[j]%i;            }        }    }    printf("%d\n",ans);    return 0;}
登录后复制


HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号