G.German Conference for Public Counting (数位dp)

本文介绍了解决Codeforces竞赛中G题的方法,使用数位dp计算1到n范围内每个数字出现的最大次数。通过乘法扩展数字并判断范围,得出答案。
摘要由CSDN通过智能技术生成

 题目: https://codeforces.com/gym/104466/problem/G

思想:数位dp,求l-r每个数字出现的最多次数,就是每个数x 然后10*x+x,判断是否在l,r之间,便可以找到数字出现的最长次数

代码:

// Problem: G. German Conference for Public Counting
// Contest: Codeforces - 2023-2024 ICPC German Collegiate Programming Contest (GCPC 2023)
// URL: https://codeforces.com/gym/104466/problem/G
// Memory Limit: 512 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 2e5+5;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);

	ll n;
	cin>>n;
	if(n>=0&&n<=9){
		cout<<n+1<<"\n";
		return 0;
	}
	map<int,ll> mp;
	for(int i=1;i<=9;i++){
		ll num=0,x=i;
		while(x<=n){
			//cout<<x<<' ';
			mp[i]++;
			x=x*10+i;
		}
	}
	string a=to_string(n);
	int ans=a.size()-1;
	for(int i=0;i<=9;i++){
		//cout<<mp[i]<<' ';
		ans+=mp[i];
	}
	cout<<ans<<"\n";
	
	
	return 0;	

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值