XiaoHuang's Space
XiaoHuang's Space
XiaoHuang
May the force be with you.
「Luogu P6101」[EER2]出言不逊
Posted: Mar 03, 2020
This article was last modified days ago. The content of this post may be outdated!

Portal

Portal1: Luogu

Solution

模拟,先找到在读入字符串内出现次数最多的字符,记录个数,然后以 $2$ 为指数在现有长度上递增,就可以算出答案。

但是long long会溢出,所以要判断一下,如mx + mx < mx说明已经溢出了,然后就退出答案做个标记,输出的时候$+1$,否则会死循环,至于__int128,我没试过。

代码纯属是为过而过,没什么可看的。

Code

#include<bits/stdc++.h>
#pragma GCC optimize(2)
//不知道为什么,不开会T
using namespace std;

char s[1000005];
unsigned long long L;
int main() {
    cin >> s;
    cin >> L;
    sort(s, s + strlen(s));//字符串排序方便计算出现最多的数
    unsigned long long lst = 0, mx = 0;
    s[strlen(s)] = '!';//最后要填一个随意的(不会出现的)字符,不然最后一段就不会算进去
    for (int i = 1; i <= strlen(s); i++)
        if (s[i] != s[i - 1]) {
            if (i - lst > mx) mx = i - lst;
            lst = i;
        }
    unsigned long long now = strlen(s) - 1, ans = 0;
    bool flag = 0;
    while (now < L) {
        now += mx;
        if (mx + mx < mx) {flag = 1; break;}
        mx = mx * 2;
        ans++;
    }
    if (flag) printf("%lld\n", ans + 1); else printf("%lld\n", ans);//溢出要标记
    return 0;
}
Article License: CC BY-NC-ND 4.0
Article Author: XiaoHuang
  1. 1. Portal
  2. 2. Solution
  3. 3. Code
Newer Post
「CF80A」Panoramix's Prediction
Older Post
「Luogu P6069」[MdOI2020] Group
Buy me a beer?
-->
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×