<center>

问题 A: Kingdom of Black and White

时间限制: 1 Sec  内存限制:64 MB
提交: 18  解决: 8

</center>

题目描述

In the Kingdom of Black and White (KBW), there are two kinds of mice: black mouse and white mouse.

Now N mice are standing in a line, some of them are black, the others are white. The total strength of those mice are calculated by dividing the line into minimum parts, each part should still be continuous, and can only contain one kind of mouse. Then the strength is the sum of the squared length for each part.

However, an old, evil witch comes, and tells the mice that she will change the color of at most one mouse and thus the strength of those mice might change.

The mice wonder the maximum possible strength after the witch finishes her job.

输入

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 mouse) and 1 (representing a white mouse).

⋅ 1≤T≤50.

⋅ for 60% data, 1≤N≤1000.

⋅ for 100% data, 1≤N≤10 5.

⋅ the string only contains 0 and 1.
 

输出

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.

样例输入

2
000011
0101

样例输出

Case #1: 26
Case #2: 10

思路:

分情况讨论,其实很简单,把数据离散化,转化成连续数个数的数组,然后对这些数字进行判断。如果有连续长度为1的,就把它变为旁边的使之连接起来;如果长度不为1,实际上就是要看他的左右边缘。要把这个1分给相邻的更大的那个,因为大的数加上1之后平分会变得更大。一开始就是TLE,主要是在于我把每次改变了之后都遍历累加一遍看和值,其实不然,只需要看周围这三个数的平方和变化就好了。所以,实际上只有2种情况,第一,x^2+y^2变成(x-1)^2+(y+1)^2或者(x+1)^2+(y-1)^2,所以增量是2|x-y|+2。第二,x^2+1+y^2变成(x+1+y)^2,增量为2(x+y)+2注意,第一种的是x和y相邻,第二种的是x和y之间夹了一个1。只要按照顺序统计每一段的长度s,同时把s*s求和到sum里面,最后sum就是可能的解。


代码:

//TLE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;  
const int maxnn = 101000;  
long long maxn = -1;  

char a[maxnn];
ll p[maxnn];

int main()
{
	freopen("in.txt","r",stdin);
	int t;
	scanf("%d",&t);
	for(int iii=1;iii<=t;iii++)
	{
		printf("Case #%d: ",iii);
		scanf("%s",a);
		int len=strlen(a); 
        int tot = 0;  
        int l = 0;  
        p[0]=0;
        p[len]=0;
        p[len+1]=0;
		while(l<len)
		{
			int r=l;  
            while(r<len && a[r]==a[l])  
                r++;  
            tot++; 
            p[tot]=r-l;   
            l=r;  
		}
		for(int i=1;i<=tot;i++)
		{
			if(p[i]==1)//长度为1 
			{
				ll t1=p[i];
				ll t2=p[i-1];
				ll t3=p[i+1];
				p[i]=p[i]+p[i-1]+p[i+1];
				p[i-1]=0;
				p[i+1]=0;
				ll re=0;
				for(int j=1;j<=tot;j++)
				{
					re+=p[j]*p[j];
				} 
				maxn=max(maxn,re);
				p[i]=t1;
				p[i-1]=t2;
				p[i+1]=t3;
			}
			
			else
			{
				if(p[i-1]>=p[i+1])
				{
					p[i-1]++;
					p[i]--;
					ll re=0;
					for(int j=1;j<=tot;j++)
					{
						re+=p[j]*p[j];
					} 
					maxn=max(maxn,re);
					p[i-1]--;
					p[i]++;
				}
				else 
				{
					p[i+1]++;
					p[i]--;
					ll re=0;
					for(int j=1;j<=tot;j++)
					{
						re+=p[j]*p[j];
					} 
					maxn=max(maxn,re);
					p[i+1]--;
					p[i]++;
				}
			} 	
		}
       printf("%lld\n",maxn);
       maxn=-1;
	}
	return 0;
}


//AC
#include <cstdio>  
#include <cstring>  
#include <cstdlib>  
#include <algorithm>  
using namespace std; 
typedef long long ll;
const int maxn=1e5+10;
int t,n;char s[maxn];ll a[maxn];
int main(){
    cin>>t;int kk=1;
    while(t--){
        ll ans=0;
        scanf("%s",s+1);memset(a,0,sizeof(a));
        int len=strlen(s+1);ll sum=1,count=1;
        ll maxx=0;
        for(int i=2;i<=len;i++){
            if(s[i]==s[i-1]){
                sum++;
            }
            else{
                maxx=max(maxx,sum);
                ans=ans+sum*sum;
                a[count++]=sum;sum=1;
            }
        }
        a[count++]=sum;ans=ans+sum*sum;
        ll tmp=0;ll anss=ans,tmpp=0;
        for(int i=1;i<count;i++){
            if(a[i]==maxx){
                if(i==1)tmp=a[i+1];
                else if(i==count-1) tmp=a[i-1];
                else tmp=min(a[i-1],a[i+1]);
                tmpp=ans+(ll)((a[i]+1)*(a[i]+1))-(ll)(a[i]*a[i]);
                tmpp=tmpp+(ll)((tmp-1)*(tmp-1))-(ll)(tmp*tmp);
                anss=max(anss,tmpp);
            }
        }
        for(int i=1;i<count;i++){
            if(a[i]==1){
                tmpp=ans+(ll)((a[i]+a[i-1]+a[i+1])*(a[i]+a[i-1]+a[i+1]));
                tmpp=tmpp-(ll)(a[i]*a[i])-(ll)(a[i-1]*a[i-1])-(ll)(a[i+1]*a[i+1]);
                anss=max(tmpp,anss);
            }
        }
        printf("Case #%d: %lld\n",kk++,anss);
    }
}