Kingdom of Black and White

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 585    Accepted Submission(s): 193

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
 
Source
 

题意:例如000011,有连续为0的子序列长度为4,连续为1的子序列长度为2,所以这段字符串的价值为4*4+2*2=20,女巫最多可以将一个0或1改成1或0,那么把第5个字符1改成0,最后可以得到5*5+1*1=26

/*
本来以为很快就做出来,结果坑了囧。
开始想的是直接求怎样能得出最长的字段,然后求答案
但是发现110001这种会有问题,于是把他们的每段长度离散化处理
对于长为1的,变化后就莫有了- -
对于长度超过了1的,只需要变化它的左右端点位置  110001 => 100001 || 110000(取其他位置只会变得更小)
所以我们根据字段长度以及字段位置进行分类讨论
*/

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 101000;
const int INF = 0x3f3f3f3f; char a[maxn];
ll p[maxn]; int main()
{
int T;
int cas = 1;
scanf("%d",&T);
while(T--)
{
scanf("%s",a);
int len = strlen(a);
ll ans = 0;
int tot = 1;
int l = 0;
while(l < len) //先离散化处理
{
int r=l;
while(r<len && a[r]==a[l])
r++;
p[tot]=r-l;
ans+=p[tot]*p[tot];
tot++;
l=r;
}
ll pans = ans;
for(int i = 1; i < tot; i++)
{
ll tans = ans;
ll sum = 0;
if(p[i] == 1) //连续长度为1的情况
{
tans -=1;
sum = 1;
if(i > 1)
{
tans -= p[i-1]*p[i-1];
sum += p[i-1];
}
if(i < tot-1)
{
tans-=p[i+1]*p[i+1];
sum += p[i+1];
}
pans = max(pans,tans+=sum*sum);
}
else //如果连续长度超过1,则讨论其左右的情况
{
if(i > 1)
{
ll tt = tans;
tt -= p[i]*p[i];
tt-= p[i-1]*p[i-1];
tt += (p[i-1]+1)*(p[i-1]+1);
tt += (p[i]-1)*(p[i]-1);
pans = max(pans,tt);
} if(i < tot-1)
{
ll tt = tans;
tt -= p[i]*p[i];
tt-= p[i+1]*p[i+1];
tt += (p[i+1]+1)*(p[i+1]+1);
tt += (p[i]-1)*(p[i]-1);
pans = max(pans,tt);
}
}
}
printf("Case #%d: %I64d\n",cas++,pans);
}
return 0;
}

  

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(暴力)

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

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

  5. hdu 4948 Kingdom(推论)

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

  6. HDU5583 Kingdom of Black and White

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

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

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

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

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

  9. 拓扑排序 --- hdu 4948 : Kingdom

    Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

随机推荐

  1. 直方图均衡化及matlab实现

    在处理图像时,偶尔会碰到图像的灰度级别集中在某个小范围内的问题,这时候图像很难看清楚.比如下图: 它的灰度级别,我们利用一个直方图可以看出来(横坐标从0到255,表示灰度级别,纵坐标表示每个灰度级别的 ...

  2. CISCO路由器练习

    前言: 总结了昨天的学习和今天的单臂路由 写了今天的文章. 目录: 路由器的基本配置 单臂路由的练习 正文: 路由器基本配置 环境要求 cisco模拟器 2台交换机 2台PC 1台路由器 路由器介绍: ...

  3. Python扩展模块——selenium的使用(定位、下载文件等)

    想全面的使用selenium可以下载<selenium 2自动化测试实战-基于Python语言>PDF的电子书看看 我使用到了简单的浏览器操作,下载文件等功能... 推荐使用firefox ...

  4. vue-cli项目中,全局引入jquery

    命令行执行 npm install --save jquery 找到webpack.base.conf.js文件,写入代码: const webpack = require('webpack') 在m ...

  5. es6学习笔记--Interator和Generator(以及for-of的用法)

    这几天学习了遍历器和生成器,看着资料学,有点雾里缭绕的感觉,让人忍不住放弃,还好多看了好几遍,怼着资料里的例子让自己学会了Interator和Generator.   Interator,中文简称:遍 ...

  6. apollo1.7.1初探(一)安装apollo、创建并启动broker

    Apache Apollo是一个代理服务器,是在ActiveMQ基础上发展而来的,支持STOMP, AMQP, MQTT, Openwire, SSL, and WebSockets 等多种协议. A ...

  7. QT5.5与MYSQL5.6数据库连接的具体方法与实现

    由于毕设需要用到QT读取数据库中的数据,并将数据保存至数据库中.花了一天的时间,总算实现了从QT中读取数据库中的数据.网上相关资料很多,但是写得不是很全,中间出现了一些问题,解决起来比较麻烦.所以本文 ...

  8. Struts(二十四):短路验证&重写实现转换验证失败时短路&非字段验证

    短路验证: 若对一个字段使用多个验证器,默认情况下会执行所有的验证.若希望前面的验证器没有通过,后面的验证器就不再执行,可以使用短路验证. 1.如下拦截器,如果输入字符串,提交表单后,默认是会出现三个 ...

  9. scrapy安装教程

    Step 1 •安装Python2.7(32位版本) –https://www.python.org/downloads/release/python-279/ Setp 2 •打开"运行& ...

  10. Typescript学习

    一 什么是Typescript 简单的说,TypeScript 是 JavaScript 的一个超集,主要提供了类型系统和对 ES6 的支持,它由 Microsoft 开发,代码开源于 GitHub  ...