BZOJ2793 [Poi2012]Vouchers 调和级数

首先最暴力的算法肯定是O(n*sqrt(n))对吧……每次从开始枚举没用过的,直接标号即可。

注意到n/1+n/2+...+n/n=O(nlogn)。

这样我们记录一下对于x的倍数,现在已经搜到哪里了。

那么对于每一个x,都不会运算超过n/x次,时间复杂度就是O(nlogn)。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int MAXN=1000010;
int n,m,x,a[MAXN],now[MAXN],tot,tmp;
long long sum,sub,ans[MAXN],f[MAXN];
int main()
{
	//freopen("voucher.in","r",stdin);
	//freopen("voucher.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	scanf("%d",&m);
	for(int cas=1;cas<=m;cas++)
	{
		scanf("%d",&x);
		sum+=(long long)x,tmp=x;
		while(tmp--&&now[x]+x<=1000000)
		{
			if(f[now[x]+x]==0) f[now[x]+=x]=++sub;
			else tmp++,now[x]+=x;
		}
		sub=sum;
	}
	for(int i=1;i<=n;i++) if(f[a[i]]) ans[++tot]=f[a[i]];
	sort(ans+1,ans+tot+1);
	printf("%d\n",tot);
	for(int i=1;i<=tot;i++) printf("%lld\n",ans[i]);
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值