C. Sum of Substrings CodeCraft-22 and Codeforces Round #795 (Div. 2)(思维)

传送门

题意:给你一个二进制的01字符串,让我们定义这个二进制字符串的结果为sis_i+_1的所有十进制数之和。

例如:S=1011;

他的所有结果为"10"+"01"+"11"=22.

现在给你m个操作数,每次操作可以交换任意两个相邻的字符串,问你经过小于等于的m任意次交换,S的最小的结果会是多少?

思路:通过样例2和样例3我们可以发现,如果交换不换到最后一位或者第一位的话,其结果是不会改变的,因为对于每一个'1',只要他不在第一位和最后一位,那么他一定会前面加1后面加10.

把1换到最后一位会少10,把一换到第一位会少1,那么我们就先尽可能的把1换到最后一位,如果可以就再考虑换到第一个,换完后直接暴力搜一遍结果就好了。

/**
 *  ┏┓   ┏┓+ +
 * ┏┛┻━━━┛┻┓ + +
 * ┃       ┃
 * ┃   ━   ┃ ++ + + +
 *  ████━████+
 *  ◥██◤ ◥██◤ +
 * ┃   ┻   ┃
 * ┃       ┃ + +
 * ┗━┓   ┏━┛
 *   ┃   ┃ + + + +Code is far away from  
 *   ┃   ┃ + bug with the animal protecting
 *   ┃    ┗━━━┓ 神兽保佑,代码无bug 
 *   ┃  	    ┣┓
 *    ┃        ┏┛
 *     ┗┓┓┏━┳┓┏┛ + + + +
 *    ┃┫┫ ┃┫┫
 *    ┗┻┛ ┗┻┛+ + + +
 */

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long
using namespace std;

const int N = 1000000 + 100;
int n, m, h;
char s[N];

int cal(int i, int j)
{
    int res = 0;
    if (s[i] == '1')
        res += 10;
    if (s[j] == '1')
        res++;
    return res;
}

int main()
{
    int t;
    sc_int(t);
    while (t--)
    {
        cin >> n >> m >> s + 1;
        int l = 0, r = 0;
        for (int i = 1; i <= n; i++)
        {
            if (s[i] == '1')
            {
                if (!l)
                    l = i;
                else
                    r = i; //找最小的和最长的1
            }
        }
        if (!r) //如果只有一个1
        {
            if (n - l <= m)
                swap(s[l], s[n]);
            else if (l - 1 <= m)
                swap(s[1], s[l]);
        }
        else
        { //有两个及以上
            if (n - r <= m)
            {
                swap(s[r], s[n]);
                m -= (n - r);
            }
            if (l - 1 <= m)
                swap(s[1], s[l]);
        }
        int res = 0;
        for (int i = 1; i < n; i++)
            res += cal(i, i + 1);

        cout << res << endl;
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值