• Maximum Value(哈希)


    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 by aj), 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).
    Output

    Print the answer to the problem.
    Sample test(s)
    Input

    3
    3 4 5

    Output

    2

    Hash记录输入的数值,Dp[i]记录输入中距离i最近的数值(不包括本身)

    这里写代码片#include <set>
    #include <map>
    #include <list>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <string>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define PI cos(-1.0)
    #define RR freopen("input.txt","r",stdin)
    
    using namespace std;
    
    typedef long long LL;
    
    const int MAX = 2*1e6+10;
    
    const int R =1e6;
    
    int Dp[MAX];
    
    int n;
    
    int a[MAX];
    
    bool Hash[MAX];
    
    int main()
    {
        int n;
        memset(Hash,false,sizeof(Hash));
        scanf("%d",&n);
        int Min=MAX,Max=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            Hash[a[i]]=true;
            Min=min(Min,a[i]);
            Max=max(Max,a[i]);
        }
        for(int i=Min;i<MAX;i++)
        {
            if(Hash[i-1])
            {
                Dp[i]=i-1;
            }
            else
            {
                Dp[i]=Dp[i-1];
            }
        }
        int ans=0;
        for(int i=Min;i<=R;i++)
        {
            if(Hash[i])
            {
                for(int j=i*2;;j+=i)
                {
                    if(Dp[j]<i)
                    {
                        continue;
                    }
                    ans=max(Dp[j]%i,ans);
                    if(Dp[j]==Max)
                    {
                        break;
                    }
                }
            }
        }
        printf("%d
    ",ans);
        return 0;
    }
    
  • 相关阅读:
    python 将字符串作为脚本运行
    Django Model获取字段名称并转为List存储
    django 过滤查询
    django 视图中异步执行python脚本
    python 异步执行python脚本
    django 筛选不等于的内容
    phpstorm中快速添加函数注释
    搜索模板elasticsearch
    laravel模型关联
    laravel关联用户
  • 原文地址:https://www.cnblogs.com/juechen/p/5255949.html
Copyright © 2020-2023  润新知