Kingdom of Black and White

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5583

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

HINT

题意

给你一个只含有01的字符串,然后这个串的权值就是每一段连续的0或1的长度的平方和,然后你可以修改一个数,使得这个数变成0,或者使这个数变成1

然后问你最大权值能为多少

题解:

不能直接暴力枚举每一位,但是我们可以枚举每一个连通块就好了

每个连通块毫无疑问,只会修改最左边或者最右边的位置,然后直接扫一遍就行了

代码:

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<vector>
using namespace std; string s;
int main()
{
int t;scanf("%d",&t);
for(int cas=;cas<=t;cas++)
{
cin>>s;
int flag = -;
vector<long long> Q;
int len = ;
for(int i=;i<s.size();i++)
{
if(s[i]-''!=flag)
{
Q.push_back(len);
len = ;
flag = s[i]-'';
}
else
len++;
}
Q.push_back(len);
Q.push_back();
long long Ans = ;
for(int i=;i<Q.size()-;i++)
Ans += Q[i]*Q[i];
long long Ans2 = Ans;
for(int i=;i<Q.size()-;i++)
{
long long tmp = ;
if(Q[i]==)
Ans2 = max(Ans2,Ans-Q[i-]*Q[i-]-Q[i]*Q[i]-Q[i+]*Q[i+]+(Q[i-]+Q[i+]+)*(Q[i-]+Q[i+]+));
else
{
Ans2 = max(Ans2,Ans-Q[i-]*Q[i-]-Q[i]*Q[i]+(Q[i-]+)*(Q[i-]+)+(Q[i]-)*(Q[i]-));
Ans2 = max(Ans2,Ans-Q[i+]*Q[i+]-Q[i]*Q[i]+(Q[i+]+)*(Q[i+]+)+(Q[i]-)*(Q[i]-));
}
}
printf("Case #%d: %lld\n",cas,Ans2);
}
}

HDU 5583 Kingdom of Black and White 水题的更多相关文章

  1. 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 ...

  2. 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 ...

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

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

  4. HDU 5832 A water problem(某水题)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  5. hdu 2393:Higher Math(计算几何,水题)

    Higher Math Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. <hdu - 3999> The order of a Tree 水题 之 二叉搜索的数的先序输出

    这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999  Problem Description: As we know,the sha ...

  7. HDOJ/HDU 1256 画8(绞下思维~水题)

    Problem Description 谁画8画的好,画的快,今后就发的快,学业发达,事业发达,祝大家发,发,发. Input 输入的第一行为一个整数N,表示后面有N组数据. 每组数据中有一个字符和一 ...

  8. hdu 1164:Eddy's research I(水题,数学题,筛法)

    Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. HDU ACM 1073 Online Judge -&gt;字符串水题

    分析:水题. #include<iostream> using namespace std; #define N 5050 char a[N],b[N],tmp[N]; void Read ...

随机推荐

  1. $("#province").val();取不到select的值求解

    MVC下的razor视图开发中无法取到select的值问题求解 cshtml 如下 <select name="province" id="province&quo ...

  2. memcache、memcached、groupcache的区别

    对PHP语言来说,PHP使用memcache有两个模块,分别叫memcache和memcached,他们的区别看下表: 参考:http://hi.baidu.com/tony_wd/item/605e ...

  3. (转载)OC学习篇之---Foundation框架中的NSObject对象

    前一篇文章讲到了OC中的代理模式,而且前几篇文章就介绍了OC中的类相关知识,从这篇文章开始我们开始介绍Foundation框架. OC中的Foundation框架是系统提供了,他就相当于是系统的一套a ...

  4. STL源码分析读书笔记--第三章--迭代器(iterator)概念与traits编程技法

    1.准备知识 typename用法 用法1:等效于模板编程中的class 用法2:用于显式地告诉编译器接下来的名称是类型名,对于这个区分,下面的参考链接中说得好,如果编译器不知道 T::bar 是类型 ...

  5. c++声明与定义

    c++声明与定义 声明是将一个名称引入程序.定义提供了一个实体在程序中的唯一描述.声明和定义有时是同时存在的. 如 int  a; extern int b=1; 只有当extern中不存在初始化才是 ...

  6. 一起学习 微服务(MicroServices)-笔记

    笔记 微服务特性: 1. 小 专注与做一件事(适合团队就是最好的) 2. 松耦合 独立部署 3. 进程独立 4. 轻量级通信机制 实践: 1. 微服务周边的一系列基础建设 Load Balancing ...

  7. USB设备不能用。提示Windows 无法启动这个硬件设备。 (代码 19)

    USB,由于其配置信息(注册表中的)不完整或已损坏, Windows 无法启动这个硬件设备. (代码 19) 原因:提示Windows 无法启动这个硬件设备. (代码 19) 处理解决方法: 1) r ...

  8. 利用UIImagePickerController或者利用UIKit的 UIGraphicsBeginImageContext保存图片

    转载自:http://my.oschina.net/hmj/blog/99970    应用中有时我们会有保存图片的需求,如利用UIImagePickerController用IOS设备内置的相机拍照 ...

  9. joj 2453 candy 网络流建图的题

    Problem D: Candy As a teacher of a kindergarten, you have many things to do during a day, one of whi ...

  10. HDU 5458 Stability (树链剖分+并查集+set)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 给你n个点,m条边,q个操作,操作1是删边,操作2是问u到v之间的割边有多少条. 这题要倒着做才 ...