问题 J: 求个最大值
时间限制: 1 Sec 内存限制: 128 MB
提交: 283 解决: 64
[提交][状态][讨论版]
题目描述
给出 n(1 <= n <= 200000)个数字 ai(1 <= ai <= 1000000),i 为数字的下标,按输入顺序从 1 开始编号
一直到 n,求满足 ai >= aj 的最大的 ai % aj。
输入
第一行一个数字 n,第二行 n 个整数。
输出
题目要求的最大值。
样例输入
2
2 3
样例输出
1
提示
先把ai计数排序去重,然后枚举aj,再枚举aj的倍数 k∗a[j] ,然后统计它的前驱为 ai ,复杂度 O(n∗(n+n/2+n/3+n/4+..+n/n)+max(ai))
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
struct FastIO {
static const int S = 5 << 20; //MB
int wpos; char wbuf[S];
FastIO() : wpos(0) {}
inline int xchar() {
static char buf[S];
static int len = 0, pos = 0;
if(pos == len)
pos = 0, len = fread(buf, 1, S, stdin);
if(pos == len) return -1;
return buf[pos ++];
}
inline int xuint() {
int c = xchar(), x = 0;
while(~c && c <= 32) c = xchar();
for(; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
return x;
}
inline int xint() {
int s = 1, c = xchar(), x = 0;
while(c <= 32) c = xchar();
if(c == '-') s = -1, c = xchar();
for(; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
return x * s;
}
inline void xstring(char* s) {
int c = xchar();
while(c <= 32) c = xchar();
for(; c > 32; c = xchar()) * s++ = c;
*s = 0;
}
inline void wchar(int x) {
if(wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0;
wbuf[wpos ++] = x;
}
inline void wint(int x) {
if(x < 0) wchar('-'), x = -x;
char s[24];
int n = 0;
while(x || !n) s[n ++] = '0' + x % 10, x /= 10;
while(n--) wchar(s[n]);
}
inline void wstring(const char* s) {
while(*s) wchar(*s++);
}
~FastIO() {
if(wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0;
}
} io;
#define MAXN (3000000+100)
int a[MAXN],n;
int l[MAXN];
int main()
{
// freopen("J.in","r",stdin);
// freopen(".out","w",stdout);
int n=read();
MEM(l)
For(i,n) {
a[i]=read();
l[a[i]]=a[i];
}
sort(a+1,a+1+n);
n=unique(a+1,a+1+n)-a-1;
int ans=0;
For(i,3000000) {
l[i]=max(l[i],l[i-1]);
}
ForD(i,n) {
if (ans>=a[i]) break;
for(int j=2*a[i];j<=1000000;j+=a[i]) {
int p=l[j-1];
if (p<a[i]) continue;
ans=max(ans,p%a[i]);
// if ( p%a[i]==a[i]-1) break;
}
}
printf("%d\n",ans);
return 0;
}