[uoj24]缩紧优化

前言

写写题

题目相关

link

题目大意

给你 n n n个正整数 a i a_i ai
f ( x ) = ∑ i = 1 n ( a i % x + a i / x ) f(x)=\sum_{i=1}^{n}(a_i\%x+a_i/x) f(x)=i=1n(ai%x+ai/x)
x x x为正整数,求最小的 f ( x ) f(x) f(x)

数据范围

n ≤ 1 0 6 , a i ≤ 1 0 6 n\le10^6,a_i\le10^6 n106,ai106

题解

开个桶,然后前缀和即可
对于所有的数枚举转移区间
复杂度 O ( n l o g n ) \mathcal O(nlogn) O(nlogn)
代码

#include<cstdio>
#include<cctype>
#include<algorithm>
#include<vector>
#include<cstring>
#define rg register
typedef long long ll;
template <typename T> inline T max(const T a,const T b){return a>b?a:b;}
template <typename T> inline T min(const T a,const T b){return a<b?a:b;}
template <typename T> inline void mind(T&a,const T b){a=a<b?a:b;}
template <typename T> inline void maxd(T&a,const T b){a=a>b?a:b;}
template <typename T> inline void read(T&x)
{
	char cu=getchar();x=0;bool fla=0;
	while(!isdigit(cu)){if(cu=='-')fla=1;cu=getchar();}
	while(isdigit(cu))x=x*10+cu-'0',cu=getchar();
	if(fla)x=-x;
}
template <typename T> inline void printe(const T x)
{
	if(x>=10)printe(x/10);
	putchar(x%10+'0');
}
template <typename T> inline void print(const T x)
{
	if(x<0)putchar('-'),printe(-x);
	else printe(x);
}
const int maxn=2000001;
int n,a[maxn];
ll ans,ANS;
int main()
{
	read(n);
	for(rg int i=1;i<=n;i++)
	{
		int x;read(x);
		a[x]++;
		ANS+=x;
	}
	ans=ANS;
	for(rg int i=1;i<=2000000;i++)a[i]+=a[i-1];
	for(rg int x=1;x<=1000000;x++)
	{
		ll res=ANS;
		for(rg int i=x;i<=1000000;i+=x)
			res-=(ll)i/x*(a[i+x-1]-a[i-1])*(x-1);
		mind(ans,res);
	}
	print(ans);
	return 0;
}

总结

关键是要发现没有性质,于是只能暴力

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值