BZOJ 4029: [HEOI2015]定价|贪心|模拟

贪心 先保证去掉后导0位数最少然后让最后一位尽量是5

从高位到低位贪心

注意: 5000 比 100更优

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<iostream>
#define ll  long long
using namespace std;
int sc()
{
	int i=0; char c=getchar();
	while(c>'9'||c<'0') c=getchar();
	while(c>='0'&&c<='9') i=i*10+c-'0',c=getchar();
	return i;
}
ll b[10];
int count(int x)
{
	int ans=0;
	for(;x;x/=10)ans++;
	return ans;
}
int dfs(int L,int R,int l)
{
	if(!L) return 0;
	if(L<=5*b[l]&&5*b[l]<=R) return 5*b[l];
	if(L/b[l]!=R/b[l]) return ((L-1)/b[l]+1)*b[l];
	return  L/b[l]*b[l]+dfs(L%b[l],R%b[l],l-1);
}
int main()
{
	b[1]=1; for(int i=2; i<=10; i++) b[i]=b[i-1]*10;
	int T=sc();
	while(T--)
	{
		int L=sc(),R=sc();
		int l=count(L),r=count(R);
		if(l!=r)
		{
			if(L<=5*b[l]) printf("%d\n",5*b[l]);
			else if((ll)5*b[l+1]<=R) printf("%d\n",5*b[l+1]);
			else  printf("%d\n",((L-1)/b[l]+1)*b[l]);
		}
		else
		{
			printf("%d\n",dfs(L,R,l));
		}
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值