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));
		}
	}
}


你可能感兴趣的:(模拟,贪心)