HDU 5583 Kingdom of Black and White
2016-08-06 16:44
411 查看
题目:
Description
In the Kingdom of Black and White (KBW), there are two kinds of frogs: black frog and white frog.
Now
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
,
which indicates the number of test cases.
Every test case only contains a string with length
,
including only
(representing a
black frog) and
(representing
a white frog).
.
for
60% data,
.
for
100% data,
.
the
string only contains 0 and 1.
Output
For every test case, you should output " Case #x: y",where
indicates
the case number and counts from
and
is
the answer.
Sample Input
Sample Output
首先, 不可能把一个长x+1+y的串拆开成x和1和y的串,因为(x+1+y)^2>x^2+1+y^2
所以,要不不修改任何数字,要不修改的那个数字就是处于原串的边缘。
所以,实际上只有2种情况,
第一,x^2+y^2变成(x-1)^2+(y+1)^2或者(x+1)^2+(y-1)^2,所以增量是2|x-y|+2
第二,x^2+1+y^2变成(x+1+y)^2,增量为2(x+y)+2
注意,第一种的是x和y相邻,第二种的是x和y之间夹了一个1。
只要按照顺序统计每一段的长度s,同时把s*s求和到sum里面,最后sum就是可能的解。
同时还要记录2种增量,保持刷新留下最大值,最后加到sum里面去就大功告成了。
代码:
Description
In the Kingdom of Black and White (KBW), there are two kinds of frogs: black frog and white frog.
Now
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
,
which indicates the number of test cases.
Every test case only contains a string with length
,
including only
(representing a
black frog) and
(representing
a white frog).
.
for
60% data,
.
for
100% data,
.
the
string only contains 0 and 1.
Output
For every test case, you should output " Case #x: y",where
indicates
the case number and counts from
and
is
the answer.
Sample Input
2 000011 0101
Sample Output
Case #1: 26 Case #2: 10
首先, 不可能把一个长x+1+y的串拆开成x和1和y的串,因为(x+1+y)^2>x^2+1+y^2
所以,要不不修改任何数字,要不修改的那个数字就是处于原串的边缘。
所以,实际上只有2种情况,
第一,x^2+y^2变成(x-1)^2+(y+1)^2或者(x+1)^2+(y-1)^2,所以增量是2|x-y|+2
第二,x^2+1+y^2变成(x+1+y)^2,增量为2(x+y)+2
注意,第一种的是x和y相邻,第二种的是x和y之间夹了一个1。
只要按照顺序统计每一段的长度s,同时把s*s求和到sum里面,最后sum就是可能的解。
同时还要记录2种增量,保持刷新留下最大值,最后加到sum里面去就大功告成了。
代码:
#include<iostream> #include<string.h> using namespace std; char c[100001]; int main() { int t; cin >> t; int l; long long s, sum; char ch; cin.getline(c, 100001); long long temps, dif, temptemps, dsum; for (int i = 1; i <= t; i++) { cin.getline(c, 100001); l = strlen(c); c[l] = '2'; ch = '2'; s = 0; sum = 0; temps = 0; dif = 0; dsum = 0; for (int i = 0; i <= l; i++) { if (c[i] != ch) { if (temps == 1 && dsum < temptemps + s + temptemps*s)
dsum = temptemps + s + temptemps*s; if (s && temps) { if (dif < s - temps)dif = s - temps; if (dif < temps - s)dif = temps - s; } temptemps = temps; temps = s; sum += s*s; s = 1; ch = c[i]; } else s++; } if(temptemps)dif++; sum += ((dif > dsum) ? dif : dsum) * 2; cout << "Case #" << i << ": " << sum<< endl; } return 0; }
相关文章推荐
- HDU 5583 Kingdom of Black and White 水题
- HDU-5583-Kingdom of Black and White(2015ACM/ICPC亚洲区上海站-重现赛)
- HDU 5583 Kingdom of Black and White
- hdu 5583 Kingdom of Black and White
- hdu 5583 Kingdom of Black and White 2015ACM/ICPC亚洲区上海站-重现赛
- HDU 5583 Kingdom of Black and White
- hdu 5583 Kingdom of Black and White
- hdu 5583 Kingdom of Black and White【枚举】
- HDU 5583 Kingdom of Black and White 暴力
- HDU-5583-Kingdom of Black and White【2015上海赛区】【暴力】
- hdu 5583 Kingdom of Black and White
- HDU - 5583 Kingdom of Black and White
- HDU 5583 Kingdom of Black and White
- hdu 5583 Kingdom of Black and White
- HDU 5583 Kingdom of Black and White
- HDU-5583 Kingdom of Black and White(思维)
- hdoj 5583 Kingdom of Black and White
- hdoj 5583 Kingdom of Black and White 【模拟】
- hdoj Kingdom of Black and White 5583 (模拟&&DP) 好题
- hdu 5583 Kingdom of Black and White(高效)