Kingdom of Black and WhiteTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 18 Accepted Submission(s): 10
Problem Description
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). ⋅ 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
Sample Output
|
定义01串:将串分成最少的连续的块(相同字符),该串价值即为 所有块的字符个数平方 之和。
题意:给定一个01串,最多只能改变一个字符(1->0 或 0->1),问最大价值。
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f
#define eps 1e-8
#define MAXN (100000+10)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
struct Node{
LL sum;
};
Node num[MAXN];
char str[MAXN];
int main()
{
int t, kcase = 1; Ri(t);
W(t)
{
Rs(str);
int len = strlen(str);
int top = 0, pre = -1;
for(int i = 0; i < len; i++)
{
int op = str[i] - '0';
if(op != pre)
{
++top;
num[top].sum = 1;
pre = op;
}
else
num[top].sum++;
}
if(top == 1)
{
printf("Case #%d: %lld\n", kcase++, num[top].sum*num[top].sum);
continue;
}
LL ans = 0;
for(int i = 1; i <= top; i++)
ans += num[i].sum * num[i].sum;
LL ans1 = ans;
for(int i = 1; i <= top; i++)
{
LL temp = ans;
LL l = 0, r = 0, m = num[i].sum;
if(i == 1)
r = num[i+1].sum;
else if(i == top)
l = num[i-1].sum;
else
{
l = num[i-1].sum;
r = num[i+1].sum;
}
temp -= m*m;
temp += (m-1)*(m-1);
if(m == 1)
{
temp -= l*l + r*r;
temp += (l+r+1)*(l+r+1);
}
else
{
if(l == 0)
{
temp -= r*r;
temp += (r+1)*(r+1);
}
else if(r == 0)
{
temp -= l*l;
temp += (l+1)*(l+1);
}
else
{
temp -= max(l, r)*max(l, r);
temp += (max(l, r)+1)*(max(l, r)+1);
}
}
ans1 = max(ans1, temp);
}
printf("Case #%d: %lld\n", kcase++, max(ans, ans1));
}
return 0;
}