您的位置:首页 > 其它

HDU5583 Kingdom of Black and White

2017-10-16 20:47 288 查看

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


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

2
000011
0101

[align=left]Sample Output[/align]

Case #1: 26
Case #2: 10

[align=left]Source[/align]
2015ACM/ICPC亚洲区上海站-重现赛(感谢华东理工)

[align=left]Recommend[/align]
wange2014 | We have carefully selected several similar problems for you: 6216 6215 6214 6213 6212

Statistic | Submit | Discuss | Note

【题解】

水前缀和乱搞,注意细节吧

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))

inline void swap(long long &x, long long &y)
{
long long tmp = x;x = y;y = tmp;
}

inline void read(long long &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9')c = ch, ch = getchar();
while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
if(c == '-')x = -x;
}

const long long INF = 0x3f3f3f3f;
const long long MAXN = 100000 + 10;

long long num[MAXN], tot, n, t, sum[MAXN], ans;

int main()
{
read(t);
for(register int tt = 1;tt <= t;++ tt)
{
char tmp;tmp = getchar();
while(tmp != '0' && tmp != '1')tmp = getchar();
tot = 0;memset(num, 0, sizeof(num));
ans = -1;
char now = tmp;++ tot;
++ num[tot];
for(register long long i = 2;;++ i)
{
tmp = getchar();
if(tmp != '0' && tmp != '1')break;
if(tmp == now)++ num[tot];
else ++ tot, ++num[tot], now = tmp;
}
for(register long long i = 1;i <= tot;++ i)
sum[i] = sum[i - 1] + num[i] * num[i];
ans = sum[tot];
for(register long long i = 1;i < tot;++ i)
{
if(i != 1 && num[i] == 1)
ans = max(ans,
sum[tot] - (sum[i + 1] - sum[i - 2])
+ (num[i - 1] + num[i + 1] + num[i])
* (num[i - 1] + num[i + 1] + num[i]));
else
ans = max(ans,
sum[tot] - (sum[i + 1] - sum[i - 1]) +
max((num[i] - 1) * (num[i] - 1) + (num[i + 1] + 1) * (num[i + 1] + 1),
(num[i] + 1) * (num[i] + 1) + (num[i + 1] - 1) * (num[i + 1] - 1)));
}
printf("Case #%d: %lld\n", tt, ans);
}
return 0;
}


HDU5583
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: