Kingdom of Black and White
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4395 Accepted Submission(s): 1315
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.
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.
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
20000110101
Sample Output
Case #1: 26Case #2: 10
Source
一道模拟题
可以把所有的区间都转成数字的形式储存,如 10011 可以转成1,2, 2
然后我们让 (a[i] + 1 )^2 - (a[i -1])^2 - a[i]^2 和 a[i] + 1 )^2 - (a[i +1])^2 - a[i]^2 的值最大就好了
下面是ac代码
#include<bits/stdc++.h> using namespace std; #define MID(x,y) ((x+y)>>1) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); const double PI = acos(-1.0); const int INF = 0x3f3f3f3f; const int N=2e5+7; char a[N]; vector<long long> v; int main() { // freopen("f:/input.txt", "r", stdin); int n, i , j , k; scanf("%d", &n); for (int co = 1; co <= n; co ++) { v.clear(); CLR(a,0); scanf("%s", a); int le = strlen(a); int cnt = 1; int maxn = -INF, maxi = -INF, vi; for (i = 1 ; i < le ; i ++) { if (a[i] == a[i - 1]) { ++cnt; } else { v.push_back(cnt); if (cnt > maxn) { maxn = cnt; //结束 i maxi = i - 1; vi = v.size(); } cnt = 1; } } v.push_back(cnt); long long sum = 0; int vs = v.size(); long long ct = -INF; int cti = -1; for (i = 1; i < vs; i ++) { long long cct; if (v[i - 1] != 1) cct = (v[i] + 1)*(v[i] + 1) - (v[i - 1] ) * (v[i - 1] ); else { if (i - 2 >= 0) cct = (v[i] + 1 + v[i - 2])*(v[i] + 1 + v[i - 2]) - 1 - v[i - 2] * v[i - 2]; else cct = (v[i] + 1)*(v[i] + 1 ) - 1; } cct -= (v[i] * v[i]); if (cct > ct) { ct = cct; cti = i; } } int cti2 = -1; for (i = 1; i < vs; i ++) { long long cct ; if (v[i] != 1) cct = (v[i - 1] + 1) * (v[i - 1] + 1) - (v[i] ) * (v[i] ); else { if (i < vs - 1) cct = (v[i - 1] + 1 + v[i + 1]) * (v[i - 1] + 1 + v[i + 1]) - 1 - v[i + 1] * v[i + 1]; else cct = (v[i - 1] + 1) * (v[i - 1] + 1) - 1; } cct -= (v[i - 1] * v[i - 1]); if (cct > ct) { ct = cct; cti2 = i; } } if (cti2 != -1) { int sum1 = 0; for (i = 0; i < cti2; i++) { sum1 += v[i]; } // ++sum1; if (a[sum1] == '0') a[sum1] = '1'; else a[sum1] = '0'; } else { int sum1 = 0; for (i = 0; i < cti; i++) { sum1 += v[i]; } --sum1; // ++sum1; if (a[sum1] == '0') a[sum1] = '1'; else a[sum1] = '0'; } v.clear(); cnt = 1; for (i = 1 ; i < le ; i ++) { if (a[i] == a[i - 1]) { ++cnt; } else { v.push_back(cnt); if (cnt > maxn) { maxn = cnt; //结束 i maxi = i - 1; vi = v.size(); } cnt = 1; } } v.push_back(cnt); vs = v.size(); for (i = 0; i < vs; i ++) { sum += (long long)v[i] * v[i]; } printf("Case #%d: %lld\n", co, sum); } }