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. labview与单片机串口通信

    labview与单片机串口通信   VISA是虚拟仪器软件体系结构的缩写(即Virtual Instruments Software Architecture),实质上是一个I/O口软件库及其规范的总 ...

  2. 国内maven仓库地址 || 某个pom或者jar找不到的解决方法

    解决方法 建议在maven仓库中新建settings.xml,然后把如下内容粘贴进去即可.也可以找到maven的安装目录中的conf/settings.xml,把如下的mirrors节复制到对应部分. ...

  3. Collaborative Filtering(协同过滤)算法详解

    基本思想 基于用户的协同过滤算法是通过用户的历史行为数据发现用户对商品或内容的喜欢(如商品购买,收藏,内容评论或分享),并对这些喜好进行度量和打分.根据不同用户对相同商品或内容的态度和偏好程度计算用户 ...

  4. Mego开发文档 - 快速概述

    Mego 快速概述 Mego 是一款轻量级,可扩展和跨平台的数据访问技术. Mego 是一个对象关系映射器(O / RM),它使.NET开发人员能够使用.NET对象处理数据库.它消除了开发人员通常需要 ...

  5. Mego开发文档 - 数据注释建模

    数据注释建模 Mego框架使用一组约定来基于CLR类来构建模型.您可以指定其他配置来补充或覆盖通过约定发现的内容. 在 Mego 中所有的数据对象必须要有主键.这里需要声明与EF不同的是框架只支持数据 ...

  6. restful架构风格设计准则(三)资源识别和资源设计

    读书笔记,原文链接:http://www.cnblogs.com/loveis715/p/4669091.html,感谢作者! restful风格的设计中,首先要识别系统中的资源,然后用HTTP规范表 ...

  7. 新概念英语(1-37)Making a bookcase

    What is Susan's favourite color ? A:You're working hard, Georage. What are you doing? B:I am making ...

  8. 十个你需要在 PHP 7 中避免的坑

    1. 不要使用 mysql_ 类函数 终于,你不用再看到建议不要使用 mysql_ 函数的提示了.因为 PHP 7 从核心上完全移除了它们,这意味着请你移步至更好的 mysqli_ 类函数,或者更灵活 ...

  9. webpack打包性能优化

    1. 使用 gzip 压缩打包后的 js 文件 这个方法优化浏览器下载时的文件大小(打包后的文件大小没有改变) webpack.config.prod.js 中 var CompressionWebp ...

  10. Python/模块与包之模块

    Python/模块与包之模块 1.什么是模块? 模块就是py文件 2.为什么要用模块? 如果在解释器上进行编码,把解释器关闭之前写的文件就不存在了,如果使用模块的话就能永久保存在磁盘中. 3.如何使用 ...