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. Java-MyBatis-MyBatis3-XML映射文件:参数

    ylbtech-Java-MyBatis-MyBatis3-XML映射文件:参数 1.返回顶部 1. 参数 你之前见到的所有语句中,使用的都是简单参数.实际上参数是 MyBatis 非常强大的元素.对 ...

  2. 杂项-公司:Apple

    ylbtech-杂项-公司:Apple 苹果公司(Apple Inc. )是美国的一家高科技公司.由史蒂夫·乔布斯.斯蒂夫·沃兹尼亚克和罗·韦恩(Ron Wayne)等人于1976年4月1日创立,并命 ...

  3. 嵌入式开发—C语言面试题

    嵌入式开发—C语言面试题 源地址:http://blog.csdn.net/xdx2ct1314/article/details/7358929   1. 用预处理指令#define 声明一个常数,用 ...

  4. 如何应用AxureRP做原型设计

    什么是原型呢?这个在之前介绍为什么需要进行原型设计当中有提到,原型是产品的最初形态,确认用户对产品界面和操作功能可用性的需求,高保真的原型接近于产品的最终形态,但仍只是原型.产品原型简单的说就是产品设 ...

  5. python多线程建立代理ip池

    之前有写过用单线程建立代理ip池,但是大家很快就会发现,用单线程来一个个测试代理ip实在是太慢了,跑一次要很久才能结束,完全无法忍受.所以这篇文章就是换用多线程来建立ip池,会比用单线程快很多.之所以 ...

  6. sqlmap:入门(手工注入)

    一. 联合查询注入union(less-1) 1. union操作符用于合并两个或多个select语句结果集: 2. union后的select语句必须拥有和最前的select语句拥有相同数量的字段, ...

  7. OpenGL学习笔记2017/8/29

    OpenGL学习日志: 感谢doing5552 的OpenGL入门学习:http://www.cppblog.com/doing5552/archive/2009/01/08/71532.html 相 ...

  8. 跟我一起学koa之在koa中使用mongoose(四)

    第一步安装mongoose,创建数据库文件夹 第二步引入mongoose,连接数据库 第三步运行项目 这个报错 只需要将es6写法变成es5写法即可 我们连接数据库,并且以post请求的方式插入数据 ...

  9. ROS多线程编程

    int main(int argc, char **argv) { ros::init(argc, argv, "multi_sub"); multiThreadListener ...

  10. $cordovaNetwork 使用

    1 .安装插件 直接安装: cordova plugin add cordova-plugin-network-information 下载到本地安装: https://github.com/apac ...