HDU

来源:互联网 发布:怎么做数据分析报告 编辑:程序博客网 时间:2024/06/04 00:02
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 ofat 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).

1T50.

for 60% data, 1N1000.

for 100% data, 1N105.

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


题意就是给出一个01串每次只能改变以为,相同数字组成一个区域,求每个区域大小平方的和最大

思路:输入的时候先把每个区间的大小统计一遍,然后暴力跑一边修改就能过


#include <iostream>#include <cstdio>#include <cmath>#include <cstdio>#include <algorithm>#include <cstring>#include <queue>#include <vector>#define inf 0x3f3f3f3fusing namespace std;char str[110000];int s[110000];int main(){ios::sync_with_stdio(false);long long int t, n, i;long long int m, x = 1;long long int mm, ss;long long int sum;cin>>t;while(t--){cin>>str;n = strlen(str);m = 0;mm = 0;sum = 0;for(i = 1;i <= n;i++){mm++;if(str[i] != str[i-1]){s[m++] = mm;sum += (mm * mm);mm = 0;}}mm = sum;for(i = 1;i < m;i++){ss = sum - s[i] * s[i] - s[i-1] * s[i-1];ss = ss + (s[i]+1) * (s[i]+1) + (s[i-1] -1) * (s[i-1]-1);mm = max(ss,mm);ss = sum - s[i] * s[i] - s[i-1] * s[i-1];ss = ss + (s[i]-1)*(s[i]-1) + (s[i-1]+1)*(s[i-1]+1);mm = max(ss,mm);if(s[i] == 1 && i + 1 < m){ss = sum - s[i-1] * s[i-1] -s[i] * s[i] - s[i+1] * s[i+1];ss = ss + (s[i-1] + s[i] + s[i+1]) * (s[i-1] + s[i] + s[i+1]);mm = max(mm,ss);}}cout<<"Case #"<<x<<": "<<mm<<endl;x++;}return 0;}