Codeforces 485D Maximum Value【思维+技巧枚举】
2017-04-22 21:15
337 查看
D. 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.
Examples
Input
Output
题目大意:
给你N个数,让你找到两个数Ai,Aj,使得Ai%Aj的值最大,这里需要保证Ai>Aj;
思路:
1、首先我们对数组排序&&去重。
2、然后考虑对于Ai来讲,其能够找到的Aj如果是Ai的因子数,那么一定Ai%Aj==0.如果此时Aj是Ai的最大因子数(不包括Ai本身),那么Ai%A【j+1-------->i-1】是会逐渐递减的,所以那么我们每一次找到一个因子数,那么期望的最大模值肯定要在Ai%Aj+1中选取,所以我们可以考虑O(n)枚举Ai然后sqrt(Ai)去枚举因字数,然后过程维护一个最大值即可。
总时间复杂度O(nsqrt(a【i】));
方法是对的,可惜被卡了TLE...................
那么我们考虑如何优化。
3、我们刚刚是在枚举因子数,反过来想,我们不妨枚举倍数。那么我们需要枚举1e6+1e6/2+1e6/3+1e6/4+.............1e6/1e6次。
估算一下,大概需要枚举1e7次。
那么我们枚举一个数之后,枚举其倍数,对应在数组中找到比这个倍数小的最大的数,那么ans=max(ans,这个倍数小的最大的数%枚举出来的这个数);
如果二分处理,大概也要1e8+次,实际尝试了一下,TLE了。
其实我们可以预处理出来一个数组vis【i】,表示原序列中,小于等于i的最大的数。
这里预处理O(n)就能实现,所以查询可以做到O(1);
一共1e7次左右的操作,单组数据就不会超时了。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
int pos[4000000];
int vis[5000000];
int a[500000];
int main()
{
int n;
while(~scanf("%d",&n))
{
int tot=0;
int maxn=0;
memset(vis,-1,sizeof(vis));
memset(pos,-1,sizeof(pos));
vis[1]=1;
for(int i=1;i<=n;i++)
{
int x;scanf("%d",&x);
vis[x]=x;
}
for(int i=1;i<=2000050;i++)if(vis[i]==-1)vis[i]=vis[i-1];
for(int i=1;i<=1000050;i++)
{
if(vis[i]==i)
{
for(int j=i*2;j<=2000050;j+=i)
{
if(vis[j-1]>i)output=max(output,vis[j-1]%i);
}
}
}
printf("%d\n",output);
}
}
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.
Examples
Input
3 3 4 5
Output
2
题目大意:
给你N个数,让你找到两个数Ai,Aj,使得Ai%Aj的值最大,这里需要保证Ai>Aj;
思路:
1、首先我们对数组排序&&去重。
2、然后考虑对于Ai来讲,其能够找到的Aj如果是Ai的因子数,那么一定Ai%Aj==0.如果此时Aj是Ai的最大因子数(不包括Ai本身),那么Ai%A【j+1-------->i-1】是会逐渐递减的,所以那么我们每一次找到一个因子数,那么期望的最大模值肯定要在Ai%Aj+1中选取,所以我们可以考虑O(n)枚举Ai然后sqrt(Ai)去枚举因字数,然后过程维护一个最大值即可。
总时间复杂度O(nsqrt(a【i】));
方法是对的,可惜被卡了TLE...................
那么我们考虑如何优化。
3、我们刚刚是在枚举因子数,反过来想,我们不妨枚举倍数。那么我们需要枚举1e6+1e6/2+1e6/3+1e6/4+.............1e6/1e6次。
估算一下,大概需要枚举1e7次。
那么我们枚举一个数之后,枚举其倍数,对应在数组中找到比这个倍数小的最大的数,那么ans=max(ans,这个倍数小的最大的数%枚举出来的这个数);
如果二分处理,大概也要1e8+次,实际尝试了一下,TLE了。
其实我们可以预处理出来一个数组vis【i】,表示原序列中,小于等于i的最大的数。
这里预处理O(n)就能实现,所以查询可以做到O(1);
一共1e7次左右的操作,单组数据就不会超时了。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
int pos[4000000];
int vis[5000000];
int a[500000];
int main()
{
int n;
while(~scanf("%d",&n))
{
int tot=0;
int maxn=0;
memset(vis,-1,sizeof(vis));
memset(pos,-1,sizeof(pos));
vis[1]=1;
for(int i=1;i<=n;i++)
{
int x;scanf("%d",&x);
vis[x]=x;
}
for(int i=1;i<=2000050;i++)if(vis[i]==-1)vis[i]=vis[i-1];
for(int i=1;i<=1000050;i++)
{
if(vis[i]==i)
{
for(int j=i*2;j<=2000050;j+=i)
{
if(vis[j-1]>i)output=max(output,vis[j-1]%i);
}
}
}
printf("%d\n",output);
}
}
相关文章推荐
- Codeforces 426D Sereja and Table【思维+暴力枚举】好题!
- Codeforces 551B ZgukistringZ【思维+枚举】
- E - Toda 2 CodeForces - 730A(简单思维和处理技巧)
- Codeforces 593B Anton and Lines 【思维】【枚举】【排序】
- CodeForces 253D Table with Letters - 2 减小复杂度的技巧+交替枚举
- Codeforces 672C Recycling Bottles【极限思维+贪心枚举】
- HDU 5701 中位数计数 (思维+枚举技巧)
- Codeforces 846C Four Segments【思维+预处理+前缀和枚举】
- Codeforces edu 6. F Xors on Segments 枚举 优化技巧
- Codeforces 822C Hacker, pack your bags!(思维+技巧)
- Codeforces 599D Spongebob and Squares【思维枚举+数学方程】
- Codeforces 466B Wonder Room【思维+暴力枚举】直觉题
- uva 12716 GCD XOR (数论)枚举技巧(筛法) + 思维
- Codeforces 544D Destroying Roads【思维枚举+最短路】好题~好题!
- CodeForces 253D Table with Letters - 2(枚举技巧)
- CodeForces 589B -- B. Layer Cake(多重集合+技巧枚举)
- Codeforces 769D k-Interesting Pairs Of Integers【思维+预处理+暴力枚举】
- codeforces 231B Magic, Wizardry and Wonders(超强技巧+思维+观察力)
- CodeForces - 822C Hacker, pack your bags!(区间端点处理技巧+思维)
- Codeforces 552D Vanya and Triangles【暴力枚举+思维】