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 ...
随机推荐
- python 利用pymssql连接MSSQL数据库,简单示例
#-*- coding:GBK -*- import pymssql print 'Connect to the Datebase....' conn = pymssql.connect(host=' ...
- xcrun: error: unable to find utility "instruments", not a developer tool or in PATH
xcrun: error: unable to find utility "instruments", not a developer tool or in PATH 用web ...
- elasticsearch从入门到出门-06-剖析Elasticsearch的基础分布式架构
这个图来自中华石杉:
- iOS使用正则匹配限制输入密码格式
1.代码实现"密码至少为9位,并需包含大写字母.小写字母.数字或特殊字符等三种" 返回0.1.2为格式不正确,返回4为密码格式正确 -(int)checkIsHaveNumAndL ...
- 使用asn1tools进行asn1编解码
最近在做3GPP的编解码,发现有两个第三方库比较好用.一个是ASN1C(c语言编译环境),一个是python第三方库asn1tools.这里介绍下asn1tools的使用方法: 1 第一步:生成asn ...
- Swift编程语言学习6—— 闭包
闭包是自包括的函数代码块,能够在代码中被传递和使用. Swift 中的闭包与 C 和 Objective-C 中的代码块(blocks)以及其它一些编程语言中的 lambdas 函数比較类似. 闭 ...
- 路由器桥接(WIFI无线中继)设置及摆放位置图解
路由器桥接(WIFI无线中继)设置及摆放位置图解 WIFI实在好用,但它的波覆盖面小.穿透力很差.我们安装时要考虑波的衍射特点,装在衍射效果最佳的位置(居中,室外可绕,避开密封墙).确实无法兼顾的地方 ...
- Django下MEDIA_ROOT, MEDIA_URL, STATIC_ROOT, STATIC_URL解惑
Django中settings中的四个设置参数的一些故事: MEDIA_ROOT, MEDIA_URL, STATIC_ROOT, STATIC_URL 1.MEDIA_ROOT与MEDIA_URL ...
- 中国移动OnetNet云平台 使用WIFI模块ESP8266 TCP透传模式传输数据流步骤
测试使用工具: WIFI模块型号:ESP8266 https://item.taobao.com/item.htm?spm=a1z10.1-c.w137712-175513579.2.btbD9X&a ...
- 机器学习Coursera学习总结
Coursera上Andrew NG的机器学习实在是太火了,最近有时间花费了20来天的时间(每天3小时左右)终于学习完了全部的课程,总结如下: (1)适合入门,讲的比较基础,Andrew讲的很棒: ( ...