hhu Kingdom of Black and White 暴力 TWT Tokyo Olympic 1COMBO -1

来源:互联网 发布:js中使用java变量 编辑:程序博客网 时间:2024/05/16 18:04

问题 A: Kingdom of Black and White

时间限制: 1 Sec  内存限制: 64 MB
提交: 41  解决: 14
[提交][状态][讨论版]

题目描述

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≤105.

⋅ 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.

样例输入

20000110101

样例输出

Case #1: 26Case #2: 10


看完题就是想每个能改变的点检查过去呗。。。如果当前区间只有1只老鼠就前后加和呗。。。如果有>1只老鼠就看看是让前一个区间+1还是当前区间+1呗。。最后特判一下第一个区间呗。。。。然后更新最大值最后输出呗。。。这不就O(n)吗。。。。

然后写写写,交一发。啥!?居然TLE了!!!O(n)也能超时啥玩意啊这是!!!

然后就检查自己检查点的时候是不是写麻烦了,之前检查点的时候是检查一个区间的平方和,想想难道是一直用pow很费时间?于是在纸上写了一下式子。。。

然后改改改,再交

啥玩意!!!!还是超时!????

于是把代码改短了一点。。。感觉并没有缩短时间总之交一发

奶奶的还是超时?!!!

。。。。。

。。。。。。。

然后就蒙蔽了啊。。。。上网搜题解。。。结果百度第一页就是想飞小菜鸡和zhangjiatao1的题解啊。。。。zhangjiatao1用线段树,啥玩意?!这东西还能用线段树?!

然后想都没想就关了他的博客。。。

也没有从想飞菜鸡的题解中找到灵感。。。

然后就看自己的代码。。。。

居然是用了n遍strlen。。。。。。

啊。。。

哈哈哈。。。。

请把我的骨灰埋在对面的山岗上。。。

/* ━━━━━┒ ┓┏┓┏┓┃μ'sic foever!! ┛┗┛┗┛┃\○/ ┓┏┓┏┓┃ / ┛┗┛┗┛┃ノ) ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┃┃┃┃┃┃ ┻┻┻┻┻┻ */#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>using namespace std;const int maxn =1e5;int mice[maxn+5];long long record[maxn+5];char in[maxn+5];long long j;void count_value(long long &first){    long long i;    for(i=0;i<=j;i++){        first+=record[i]*record[i];    }}int main(){    int t;    scanf("%d",&t);    int rnd=1;    while(t--){        scanf("%s",in);        long long i,ans=1;        j=0;        long long len=strlen(in);        for(i=0;i<len;i++){            if(i>0){                if(in[i]!=in[i-1]){                    record[j]=ans;                    j++;                    ans=1;                }else{                    ans++;                }            }            if(i==len-1){                record[j]=ans;            }        }        long long max_value=0,first=0;        count_value(first);        max_value=first;        record[j+1]=0;        for(i=0;i<=j;i++){            if(i>0){                if(record[i]==1){                    long long a=record[i-1],b=record[i+1];                    max_value=max(first+2*a*b+2*(a+b),max_value);                }else{                    long long a=record[i-1],b=record[i];                    if(b>a){                        swap(a, b);                    }                    max_value=max(max_value,first+2*(a-b)+2);                }            }        }        if(record[0]==1){            max_value=max(max_value,first-1-(long long)pow(record[1],2)+(long long)pow(record[1]+1,2));        }        printf("Case #%d: %lld\n",rnd,max_value);        rnd++;    }    return 0;}






0 0
原创粉丝点击