CodeForces 484B Maximum Value

题意:

a序列有n(2*10^5)个数字  问在a[i]>=a[j]的情况下  a[i]%a[j]的最大值是多少

思路:

感觉是一道挺乱来的题……

我们可以将ans表示为a[i]-k*a[j]  这样我们枚举k只要知道比k*a[j]大但是不到(k+1)*a[j]的值就好了  考虑到a[i]只要10^6大  因此可以用一个last数组记录小于等于i的数组中的数字  因此只要拿出last[(k+1)*a[j]-1]就好

暴力可能会T有一些可以让程序加速的方法  输入开挂  a数字去重  从大到小枚举a[i]当ans>=a[i]时候可以break

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<cstdlib>
#include<ctime>
#include<cmath>
using namespace std;
typedef long long LL;
#define N 200010
#define M 1000010

int a[N], last[M];
int n, ans;

inline void scand(int &ret) {
	char c;
	ret = 0;
	while ((c = getchar()) < '0' || c > '9')
		;
	while (c >= '0' && c <= '9')
		ret = ret * 10 + (c - '0'), c = getchar();
}

int main() {
	scand(n);
	for (int i = 1; i <= n; i++) {
		scand(a[i]);
		last[a[i]] = a[i];
	}
	for (int i = 1; i < M; i++) {
		if (!last[i])
			last[i] = last[i - 1];
	}
	sort(a + 1, a + n + 1);
	n = unique(a + 1, a + n + 1) - a - 1;
	for (int i = n - 1; i >= 1; i--) {
		if (a[i] <= ans)
			break;
		for (int j = a[i] * 2; j < M && j - a[i] <= a[n]; j += a[i]) {
			ans = max(ans, last[j - 1] % a[i]);
		}
		ans = max(ans, last[M - 1] % a[i]);
	}
	printf("%d\n", ans);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值