zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    大数加法、乘法实现的简单版本
    hdu 4027 Can you answer these queries?
    zoj 1610 Count the Colors
    2018 徐州赛区网赛 G. Trace
    1495 中国好区间 尺取法
    LA 3938 动态最大连续区间 线段树
    51nod 1275 连续子段的差异
    caioj 1172 poj 2823 单调队列过渡题
    数据结构和算法题
    一个通用分页类
  • 原文地址:https://www.cnblogs.com/juechen/p/5255949.html
Copyright © 2011-2022 走看看