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. String类型_static成员_动态内存分配_拷贝构造函数_const关键字_友元函数与友元类

    1:String类型 #include <iostream> using namespace std; int main() { //初始化方法 string s1 = "hel ...

  2. IDEA修改git账号密码

    wiin10:控制面板-凭据管理器-Windows凭据-普通凭据-git

  3. 为WCF增加UDP绑定(储备篇)

    日前我开发的服装DRP需要用到即时通信方面的技术,比如当下级店铺开出零售单时上级机构能实时收到XX店铺XX时XX分卖出XX款衣服X件之类的信息,当然在上级发货时,店铺里也能收到已经发货的提醒.即时通信 ...

  4. LintCode刷题笔记-- A+B problem

    标签: 位运算 描述 Write a function that add two numbers A and B. You should not use + or any arithmetic ope ...

  5. Android之RelativeLayout相对布局

    1.相关术语解释 1.基本属性 gravity :设置容器内组件的对齐方式 ignoreGravity : 设置该属性为true的组件,将不受gravity属性的影响 2.根据父容器定位 layout ...

  6. samba初级使用记录

    首先安利一下什么是samba: Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在 ...

  7. linux最基础最常用的命令快速手记 — 让手指跟上思考的速度(三)

    这一篇作为姐妹篇的第三篇,废话不多说,我觉得这个比mysql的还要重要,为什么,一旦你摊上linux 敲键盘输入命令简直是要飞的速度,不断的卡壳查命令,效率太低了,而且非常严重的影响思绪,思绪! 某些 ...

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

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

  9. PKU--2184 Cow Exhibition (01背包)

    题目http://poj.org/problem?id=2184 分析:给定N头牛,每头牛都有各自的Si和Fi 从这N头牛选出一定的数目,使得这些牛的 Si和Fi之和TS和TF都有TS>=0 F ...

  10. reac-native + typescript 的环境搭建

    一. RN-TS环境搭建 . 安装RN脚手架 yarn add create-react-native-app -g yarn global add typescript . 创建项目文件夹 crea ...