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 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
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.
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.
000011
0101
#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(数学,贪心,暴力)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- HDU 5583 Kingdom of Black and White(暴力)
http://acm.hdu.edu.cn/showproblem.php?pid=5583 题意: 给出一个01串,现在对这串进行分组,连续相同的就分为一组,如果该组内有x个数,那么就对答案贡献x* ...
- HDU 5073 Galaxy (2014 Anshan D简单数学)
HDU 5073 Galaxy (2014 Anshan D简单数学) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5073 Description G ...
- hdu 4948 Kingdom(推论)
hdu 4948 Kingdom(推论) 传送门 题意: 题目问从一个城市u到一个新的城市v的必要条件是存在 以下两种路径之一 u --> v u --> w -->v 询问任意一种 ...
- HDU5583 Kingdom of Black and White
Kingdom of Black and White Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- [HDOJ5583]Kingdom of Black and White(暴力)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5583 一个01串,求修改一个位置,使得所有数均为0或1的子串长度的平方和最大.先分块,然后统计好原来的 ...
- HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))
Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
随机推荐
- COM对象模型
COM对象内存布局,多继承是虚继承吗? 接口之间怎么切换? 1) 是普通的多继承,不是虚继承.因为父类接口只是含有纯虚函数,不含任何数据成员,所以问题不大. 2) QueryInterface可以用来 ...
- maven安装jar包到本地仓库
mvn install:install-file -Dfile=D:/asm-1.5.3.jar -DgroupId=asm -DartifactId=asm -Dversion=1.5.3 -Dp ...
- 通过eclipse.ini修改Eclipse加载jdk的路径
这里直接把在网上的找到的答案记录下来,原文地址:http://www.oschina.net/question/109676_15561: 如果出现 启动 Eclipse 弹出“Failed to l ...
- 【BZOJ4928】第二题 树hash+倍增
[BZOJ4928]第二题 Description 对于一棵有根树,定义一个点u的k-子树为u的子树中距离u不超过k的部分. 注意,假如u的子树中不存在距离u为k的点,则u的k-子树是不存在的. 定义 ...
- thinkphp5, 省略index.php
Apache:1. httpd.conf配置文件中加载了mod_rewrite.so模块2. AllowOverride None 将None改为 All3. 把下面的内容保存为.htaccess文件 ...
- php7下 xhprof安装与使用
需要测试下 代码的性能,使用了 xhprof + xhgui 1. 下载xhprof, 这里下载吧 :https://github.com/longxinH/xhprof.git 2, 安装 cd x ...
- test_bdc
[转]REPORT zbdc_test_by_shir. * 定义个BDC格式的内表**************************************************DATA : B ...
- iOS 开发规范
公司来了大牛 是绝好的学习机会 今天分享了我们一个代码规范 比如UITableViewCell 1.首先根据这个cell 需要的数据源 建一个数据model ,只针对于 该cell 好处:数据独立 ...
- [原创]Scala学习:Tuple,Array,Map ,文件操作
1.Tuple:元祖.是一个有用的容器对象. 1)特点: ① 元祖是不可变的,即,当声明完了一个元祖,那么它的长度就不可以在改变: ② 元祖可以包含不同类型的数据,对象: ③ 索引是从 '_1 ...
- hd acm1425
给你n个整数,请按从大到小的顺序输出其中前m大的数. 先看代码: #include<stdio.h>#include<string.h>#define MAX 1100000i ...