Kingdom of Black and White (分类讨论)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a1046765624/article/details/82919077

In the Kingdom of Black and White (KBW), there are two kinds of frogs: black frog and white frog.
Now N frogs are standing in a line, some of them are black, the others are white. The total
strength of those frogs are calculated by dividing the line into minimum parts, each part should still be
continuous, and can only contain one kind of frog. Then the strength is the sum of the squared length
for each part.
However, an old, evil witch comes, and tells the frogs that she will change the color of at most one
frog and thus the strength of those frogs might change.
The frogs wonder the maximum possible strength after the witch finishes her job.
Input
First line contains an integer T, which indicates the number of test cases.
Every test case only contains a string with length N, including only 0 (representing a black frog)
and 1 (representing a white frog).
Restrictions:
• 1 ≤ T ≤ 50.
• for 60% data, 1 ≤ N ≤ 1000.
• for 100% data, 1 ≤ N ≤ 105
.
• the string only contains 0 and 1.
Output
For every test case, you should output ‘Case #x: y’, where x indicates the case number and counts
from 1 and y is the answer.
Sample Input
2
000011
0101
Sample Output
Case #1: 26
Case #2: 10

题目大概:

给你一个01串,然后连续的0或1的个数的平方是这一段串的价值,可以修改一个字符的类型,求串的总价值。

比如例题  000011 价值为20 ---》修改为000001  价值为26

思路:

分两种情况讨论求出最大值。

首先把连续的一段01串都合并为一个数据结构,一起算。

然后连续的两段串,可以把少的串的一个字符修改成多的。计算最大值。

连续三段串,中间串长度为1.可以把三段串连起来,求最大值。

这样两种情况求最大,注意爆long  long

代码:

#include<bits/stdc++.h>

using namespace std;
#define ll long long

const int maxn=1e5+10;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
string s;

struct node
{
    ll id,length;
} G[maxn];
int tem;
int main()
{
    int t;
    int ans=1;
    scanf("%d",&t);
    while(t--)
    {
        cin>>s;
        memset(G,0,sizeof(G));
        int l=s.size();
        tem=0;
        int flag=-1;
        ll sum=0;
        for(int i=0; i<l; i++)
        {
            int w=s[i]-'0';
            if(flag==w)sum++;
            else if(sum!=0)
            {
                G[tem].id=flag;
                G[tem++].length=sum;
                flag=w;
                sum=1;
            }
            else
            {
                flag=w;
                sum=1;
            }
        }
        if(sum>0)
        {
            G[tem].id=flag;
            G[tem++].length=sum;
        }
        ll max_1=0;
        ll sum_1=0;
        for(int i=0; i<tem; i++)
        {
            //cout<<G[i].length<<endl;
            sum_1+=G[i].length*G[i].length;
        }
        max_1=sum_1;
        for(int i=0; i<tem-1; i++)
        {
            ll l1=G[i].length;
            ll l2=G[i+1].length;
            if(l1<l2)swap(l1,l2);
            max_1=max(max_1,sum_1-l1*l1-l2*l2+(l1+1)*(l1+1)+(l2-1)*(l2-1));
        }
        for(int i=0; i<tem-2; i++)
        {
            ll l1=G[i].length;
            ll l2=G[i+1].length;
            ll l3=G[i+2].length;
            if(l2==1)
            {
                max_1=max(max_1,sum_1-1-l1*l1-l3*l3+(l1+l3+1)*(l1+l3+1));
            }
        }
        printf("Case #%d: %lld\n",ans++,max_1);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/a1046765624/article/details/82919077