题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5583

Kingdom of Black and White

Time Limit: 2000/1000 MS (Java/Others)  

Memory Limit: 65536/65536 K (Java/Others)

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
题意:改变一个狐狸的颜色,让strength达到最大值;
思路:由于是平方和,所以数越大最后得值越大,所以要把相邻的两个数目相对小的那个-1,大的+1,等于1的时候特判一下,详情见代码;
AC代码:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=1e5+;
long long dp[N];
char str[N];
int main()
{
int t;
scanf("%d",&t);
int cnt=;
while(t--)
{
int num=;
long long ans=,sum=;
scanf("%s",str);
int len=strlen(str);
memset(dp,,sizeof(dp));
dp[]=;
for(int i=;i<len;i++)
{
if(str[i]==str[i-])
{
dp[num]++;
}
else
{
sum+=dp[num]*dp[num];
num++;
dp[num]++;
}
}
sum+=dp[num]*dp[num];
if(num==)
{
ans=dp[]*dp[];
}
for(int i=;i<=num;i++)
{
if(dp[i]==)
{
ans=max(ans,sum+*(dp[i-]*dp[i+]+dp[i-]+dp[i+]));
}
else
{
if(dp[i-]>=dp[i])
{
ans=max(ans,sum+*(dp[i-]-dp[i]+));
}
else ans=max(ans,sum+*(dp[i]-dp[i-]+));
}
}
cout<<"Case #"<<cnt++<<": "<<ans<<"\n";
}
return ;
}

hdu-5583 Kingdom of Black and White(数学,贪心,暴力)的更多相关文章

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

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

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

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

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

  5. HDU 5073 Galaxy (2014 Anshan D简单数学)

    HDU 5073 Galaxy (2014 Anshan D简单数学) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5073 Description G ...

  6. hdu 4948 Kingdom(推论)

    hdu 4948 Kingdom(推论) 传送门 题意: 题目问从一个城市u到一个新的城市v的必要条件是存在 以下两种路径之一 u --> v u --> w -->v 询问任意一种 ...

  7. HDU5583 Kingdom of Black and White

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

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

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

  9. HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

随机推荐

  1. mysql 考勤表异常 【待修改】

    有考勤刷卡记录表,表名为attendance ,有如下字段: 姓名 卡号 刷卡时间 刷卡类型 name id time type    张三 59775623 2010-04-01 07:23:37  ...

  2. 嵌入式驱动开发之sensor---sensor 图形传感器调试

    图像传感器(image sensor)复位顺序 1. 硬件复位寄存器:2. 软件复位寄存器:3. 设置时钟寄存器:4. 设置PLL倍频:5. 设置分辨率:6. 设置窗口控制:7. 设置输出顺序:8. ...

  3. DJI SDK iOS 开发之中的一个:前言

    写这个开发教程之前,还是先说点什么. 首先要声明的是我并非DJI的员工.仅仅是DJI 飞行器的爱好者. 在DJI的phantom出来之后.我就一直期待着能够推出SDK.之前最早是Parrot的AR D ...

  4. 可执行jar包

    我已经解决了这个问题,在eclipse中有一个打包工具,可以将程序打包成.jar文件: 右键要打包的 project--->Export--->Java--->JAR file--- ...

  5. flex hack 记录

    IE从IE10开始. //共通 display: flex; flex-direction: column; align-items: flex-start;justify-content: cent ...

  6. Pexpect--example--hive.py解读

    python version 2.6.6 ; pexpect 2.3 login方法解读: def login (args, cli_username=None, cli_password=None) ...

  7. Android屏幕密度(Density)和分辨率概念详解

    移动设备有大有小,那么如何适应不同屏幕呢,这给我们编程人员造成了很多困惑.我也是突然想到这些问题,然后去网上搜搜相关东西,整理如下.   首先,对下面这些长度单位必须了解. Android中的长度单位 ...

  8. EasyDarwin开源流媒体服务器Golang版本:拉转推功能之拉流实现方法

    EasyDarwin开源流媒体服务器(www.easydarwin.org),拉转推是一个很有意义的功能,它可将一个独立的RTSP数据源"拉"到服务器,再通过转发协议转发给多个客户 ...

  9. 给MDI窗体加背景,解释MakeObjectInstance和CallWindowProc的用法

    工程含有1个MDI主窗口和2个子窗口.唯一需要注意的是,每个窗体都有ClientHandle,但只有当自己是MDI主窗体的情况下才有值,否则等于0. unit Unit1; interface use ...

  10. 【题解】POJ2279 Mr.Young′s Picture Permutations dp

    [题解]POJ2279 Mr.Young′s Picture Permutations dp 钦定从小往大放,然后直接dp. \(dp(t1,t2,t3,t4,t5)\)代表每一行多少人,判断边界就能 ...