牛客练习赛 113

小红的基环树

在这里插入图片描述

题解:明显最优为一朵花的形状,那么任意俩俩之间的距离都为2,除了3是1其他都是2

实现代码

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    
    if(n == 3) cout << 1 << endl;
    else cout << 2 << endl;
    
    return 0;
}

小红的回文串

在这里插入图片描述

题解:首先回文串那么就对半遍历,若n为奇数,则多处理一次最中间,由题目可知,?能代替所有字母,对半遍历的时候,如果两边不相等且都不为?那么直接输出0,若有一个为?那么这个?只能相对应变前成这个,如果为俩问号,则乘以26(不开LL见祖宗);

实现代码

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int Mod = 1e9 + 7;

int main()
{
    string s;
    cin >> s;
    
    int n = s.size();
    int sum = 0;
    for(int i = 0 ; i < n / 2 ; i ++ )
        if(s[i] == '?' && s[n - i - 1] == '?') sum ++;
        else if(s[i] != s[n - 1 - i])
        {
            if(s[i] != '?' && s[n - i - 1] != '?')
            {
                cout << 0 << endl;
                return 0;
            }
        }
    
    if(n & 1 && s[n / 2] == '?') sum ++;
    
    long long ans = 1;
    for(int i = 0 ; i < sum ; i ++ ) ans = (ans * 26) % Mod;
    
    cout << ans << endl;
}

小红的数组操作(easy version)

设a为加x次数,b为减y次数 , sum为元素总和。
(sum + ax - by) % n = 0
(sum % n - by % n) % n = -ax % n;
n - (sum % n - by % n) % n = ax % n = a % n * x % n
所以只需枚举减y的数量,其x的数量就是左半边 /

实现代码

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long LL;

const int N = 100010;

int a[N];

int main()
{
    LL n , p , x , q , y;
    cin >> n >> p >> x >> q >> y;
    
    LL sum = 0;
    for(int i = 0 ; i < n ; i ++ )
    {
        cin >> a[i];
        sum += a[i];
    }
    
    sum %= n;
    LL ans = 2e18;
    for(int i = 0 ; i < n ; i ++ )
    {
        LL c = y * i % n;
        LL s = (n - ((sum - c) % n)) % n;
        ans = min(ans , i * q + s * p);
    }
    
    cout << ans << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值