Kingdom of Black and White
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3735 Accepted Submission(s): 1122
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.
⋅ for 60% data, 1≤N.
⋅ for 100% data, 1≤N.
⋅ 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.
⋅ for 60% data, 1≤N.
⋅ for 100% data, 1≤N.
⋅ 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
Source
Recommend
Statistic | Submit | Discuss | Note
【题解】
水前缀和乱搞,注意细节吧
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstdlib> 5 #include <cstring> 6 #include <ctime> 7 #define min(a, b) ((a) < (b) ? (a) : (b)) 8 #define max(a, b) ((a) > (b) ? (a) : (b)) 9 10 inline void swap(long long &x, long long &y) 11 { 12 long long tmp = x;x = y;y = tmp; 13 } 14 15 inline void read(long long &x) 16 { 17 x = 0;char ch = getchar(), c = ch; 18 while(ch < '0' || ch > '9')c = ch, ch = getchar(); 19 while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar(); 20 if(c == '-')x = -x; 21 } 22 23 const long long INF = 0x3f3f3f3f; 24 const long long MAXN = 100000 + 10; 25 26 long long num[MAXN], tot, n, t, sum[MAXN], ans; 27 28 int main() 29 { 30 read(t); 31 for(register int tt = 1;tt <= t;++ tt) 32 { 33 char tmp;tmp = getchar(); 34 while(tmp != '0' && tmp != '1')tmp = getchar(); 35 tot = 0;memset(num, 0, sizeof(num)); 36 ans = -1; 37 char now = tmp;++ tot; 38 ++ num[tot]; 39 for(register long long i = 2;;++ i) 40 { 41 tmp = getchar(); 42 if(tmp != '0' && tmp != '1')break; 43 if(tmp == now)++ num[tot]; 44 else ++ tot, ++num[tot], now = tmp; 45 } 46 for(register long long i = 1;i <= tot;++ i) 47 sum[i] = sum[i - 1] + num[i] * num[i]; 48 ans = sum[tot]; 49 for(register long long i = 1;i < tot;++ i) 50 { 51 if(i != 1 && num[i] == 1) 52 ans = max(ans, 53 sum[tot] - (sum[i + 1] - sum[i - 2]) 54 + (num[i - 1] + num[i + 1] + num[i]) 55 * (num[i - 1] + num[i + 1] + num[i])); 56 else 57 ans = max(ans, 58 sum[tot] - (sum[i + 1] - sum[i - 1]) + 59 max((num[i] - 1) * (num[i] - 1) + (num[i + 1] + 1) * (num[i + 1] + 1), 60 (num[i] + 1) * (num[i] + 1) + (num[i + 1] - 1) * (num[i + 1] - 1))); 61 } 62 printf("Case #%d: %lld ", tt, ans); 63 } 64 return 0; 65 }