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.

 
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.

 
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
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 = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = -x;
} const long long INF = 0x3f3f3f3f;
const long long MAXN = + ; long long num[MAXN], tot, n, t, sum[MAXN], ans; int main()
{
read(t);
for(register int tt = ;tt <= t;++ tt)
{
char tmp;tmp = getchar();
while(tmp != '' && tmp != '')tmp = getchar();
tot = ;memset(num, , sizeof(num));
ans = -;
char now = tmp;++ tot;
++ num[tot];
for(register long long i = ;;++ i)
{
tmp = getchar();
if(tmp != '' && tmp != '')break;
if(tmp == now)++ num[tot];
else ++ tot, ++num[tot], now = tmp;
}
for(register long long i = ;i <= tot;++ i)
sum[i] = sum[i - ] + num[i] * num[i];
ans = sum[tot];
for(register long long i = ;i < tot;++ i)
{
if(i != && num[i] == )
ans = max(ans,
sum[tot] - (sum[i + ] - sum[i - ])
+ (num[i - ] + num[i + ] + num[i])
* (num[i - ] + num[i + ] + num[i]));
else
ans = max(ans,
sum[tot] - (sum[i + ] - sum[i - ]) +
max((num[i] - ) * (num[i] - ) + (num[i + ] + ) * (num[i + ] + ),
(num[i] + ) * (num[i] + ) + (num[i + ] - ) * (num[i + ] - )));
}
printf("Case #%d: %lld\n", tt, ans);
}
return ;
}

HDU5583

HDU5583 Kingdom of Black and White的更多相关文章

  1. hdu-5583 Kingdom of Black and White(数学,贪心,暴力)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5583 Kingdom of Black and White Time Limit: 2000/1000 ...

  2. HDU 5583 Kingdom of Black and White 水题

    Kingdom of Black and White Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showpr ...

  3. hdu 5583 Kingdom of Black and White(模拟,技巧)

    Problem Description In the Kingdom of Black and White (KBW), there are two kinds of frogs: black fro ...

  4. hdu 5583 Kingdom of Black and White

    Kingdom of Black and White Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  5. [HDOJ5583]Kingdom of Black and White(暴力)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5583 一个01串,求修改一个位置,使得所有数均为0或1的子串长度的平方和最大.先分块,然后统计好原来的 ...

  6. HDU 5583 Kingdom of Black and White(暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=5583 题意: 给出一个01串,现在对这串进行分组,连续相同的就分为一组,如果该组内有x个数,那么就对答案贡献x* ...

  7. HDU-5583-Kingdom of Black and White(2015ACM/ICPC亚洲区上海站-重现赛)

    Kingdom of Black and White                                                                           ...

  8. 2015ACM/ICPC亚洲区上海站

    5573 Binary Tree(构造) 题意:给你一个二叉树,根节点为1,子节点为父节点的2倍和2倍+1,从根节点开始依次向下走k层,问如何走使得将路径上的数进行加减最终结果得到n. 联想到二进制. ...

  9. imshow() displays a white image for a grey image

    Matlab expects images of type double to be in the 0..1 range and images that are uint8 in the 0..255 ...

随机推荐

  1. XJOI夏令营501训练1——分配工作

    传送门:QAQQAQ 题意:某公司有工作人员x1,x2,…,xn ,他们去做工作y1,y2,…,ym(n<=m) ,每个人都能做其中的几项工作,并且对每一项工作都有一个固定的效率.问能否找到一种 ...

  2. SpringIOC注入模块中xml文件导入其他xml文件配置

    如果我们在spring框架中配置了多个xml文件,我们可以在读取配置文件的时候把这些xml文件一下全都读取 也可以只读一个总的xml文件,在这个总的xml文件中把其他的xml全都都导入进来. 例如: ...

  3. 分布式锁的Redis实现

    当我们开始开发项目部署运行时,项目规模不大,只是在一个JVM实例中运行,对同一资源的并发访问用JDK自带的锁机制就可以解决资源同时访问的问题.而随着项目的不断发展,单体应用已经无法满足日益增长的访问需 ...

  4. HTML5定位功能,实现在百度地图上定位

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Leetcode961. N-Repeated Element in Size 2N Array重复N次的元素

    在大小为 2N 的数组 A 中有 N+1 个不同的元素,其中有一个元素重复了 N 次. 返回重复了 N 次的那个元素. 示例 1: 输入:[1,2,3,3] 输出:3 示例 2: 输入:[2,1,2, ...

  6. ajaxStart 和 ajaxSend 不执行

    我们一般会在loading 效果的时候会用上这两个全局事件 ajaxStart 和 ajaxSend 但是要注意的是 在同时有多个ajax 执行的时候ajaxStart 只会执行一次 所以一般情况下 ...

  7. 新浪新闻API

    新浪新闻API ustcmio 关注 2017.01.15 20:44* 字数 536 阅读 2479评论 2喜欢 7 新浪新闻的API:1.访问手机新浪网https://sina.cn/?from= ...

  8. 学习Python:StringIO与cStringIO

    StringIO的行为与file对象非常像,但它不是磁盘上文件,而是一个内存里的“文件”,我们可以将操作磁盘文件那样来操作StringIO.一个简单的例子,让你对StringIO有一个感性的认识: f ...

  9. 【hihocoder 1477】闰秒

    [题目链接]:http://hihocoder.com/problemset/problem/1477 [题意] 中文题 [题解] 首先,一年一年地加,把开始的年份和结束的年份之间的年根据是否为闰年; ...

  10. SQL有意思的面试题

    1.中软国际 SQL行转列  变成   --数据准备create table t_test( year int, month int, sale int, primary key (year, mon ...