题目链接: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. saltstack内置state模块file之append

    添加文件文本在文件尾部 salt.states.file.append(name, text=None, makedirs=False, source=None, source_hash=None, ...

  2. 如何更好的利用Node.js的性能极限

    通过使用非阻塞.事件驱动的I/O操作,Node.js为构建和运行大规模网络应用及服务提供了很好的平台,也受到了广泛的欢迎.其主要特性表现为能够处理庞大的并且高吞吐量的并发连接,从而构建高性能.高扩展性 ...

  3. iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)

    本文转载至 http://www.tuicool.com/articles/3ymMzub CSDN博客原文  http://blog.csdn.net/hmt20130412/article/det ...

  4. IT人和普洱茶

    IT人与普洱茶 作为一个平凡的IT人,在小孩眼中我就像黑客帝国的主角一样了不起:在亲戚眼中我是在写字楼做办公室吹空调的人:在朋友眼中我就是一个会写代码.掌握高科技术的人:在女友眼中我是一个在名企工作的 ...

  5. 装服务器,测试数据库,简单的maven命令

    [说明]今天总体回顾一下:大概是早上装服务器,下午测试数据库,晚上了解简单的maven命令 一:今日完成 1)在远程服务器的tomcat 设置好管理员的登录账号 2)登录tomcat 的项目管理 查看 ...

  6. poj2485

    Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27912   Accepted: 12734 Descri ...

  7. 【BZOJ3510】首都 LCT维护子树信息+启发式合并

    [BZOJ3510]首都 Description 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打 ...

  8. 九度OJ 1349:数字在排序数组中出现的次数 (排序、查找)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2489 解决:742 题目描述: 统计一个数字在排序数组中出现的次数. 输入: 每个测试案例包括两行: 第一行有1个整数n,表示数组的大小. ...

  9. python基础5 ---python文件处理

    python文件处理 一.文件处理的流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 二.文件的操作方法 1.文件打开模式格式: 文件句柄 = open('文件路径', ...

  10. 谷歌浏览器chrome上的firepath ----chropath

    图标如下 功能:获取相对xpath,绝对xpath和CSS选择器.在devtools面板中编辑,检查并验证XPath和CSS选择器. 使用方法