P6101 出言不逊解题报告(贪心)

题目

链接:https://www.luogu.com.cn/problem/P6101

思路分析

还是挺好想的:就是找出目前字符数最多的一种字符,然后再不停的*2,直到超过要求的数目。主要是这道题会爆ll,只能用__int128,主要是要记住模板:

完整代码

#include <iostream>
#include <map>
#include <cstdio>
#include<cstring>
using namespace std;
typedef unsigned long long ull;
inline void read(__int128 &X)
{
	X = 0;
	int w=0; char ch=0;
	while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
	while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
	if (w) X = -X;
}

void print(__int128 x)
{
	if (!x) return ;
	if (x < 0) putchar('-'),x = -x;
	print(x / 10);
	putchar(x % 10 + '0');
}

int main()
{
	ios::sync_with_stdio(false);
	char str[1000005];
	__int128 l;
	map<char,ull> counter;
	scanf("%s",str);
	read(l);
	ull len=strlen(str);
	if(len>=l)
	{
		cout<<"0"<<endl;
		return 0;
	}
	for(ull i=0;i<len;i++)
	{
		counter[str[i]]++;
	}
	ull maxn=0;
	for(map<char,ull>::iterator it=counter.begin();it!=counter.end();it++)
	{
		maxn=max(it->second,maxn);
	}
	__int128 xu=l-len;
	__int128 pl=maxn;
	__int128 ans=0;
	do{
		pl=pl*2;
		ans++;
	}while(pl-maxn<xu);
	print(ans);
	return 0; 
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值