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. Scrum 冲刺 第三日

    Scrum 冲刺 第三日 目录 要求 项目链接 燃尽图 问题 今日任务 明日计划 成员贡献量 要求 各个成员今日完成的任务(如果完成的任务为开发或测试任务,需给出对应的Github代码签入记录截图:如 ...

  2. Beta冲刺Day3

    项目进展 李明皇 今天解决的进度 完善了程序的运行逻辑(消息提示框等) 明天安排 前后端联动调试 林翔 今天解决的进度 向微信官方申请登录验证session以维护登录态 明天安排 继续完成维护登录态 ...

  3. Django-rest-framework源码分析----认证

    一.前言 1.1.安装 两种方式: github pip直接安装 pip install django-rest-framework 1.2.需要先了解的一些知识 理解下面两个知识点非常重要,djan ...

  4. zf框架的思想及学习总结

    在Php的配置文件中可以设置日志文件 dos命令进入文件夹,然后利用命令:>zf.bat create project d:/hspzf这样就可以在d盘进行创建项目文件了:然后需要把框架的Zen ...

  5. c# 几种深拷贝方式的比较

    public static class Tools { //利用 BinaryFormatter 实现深拷贝 public static T DeepCopyByBinary<T>(thi ...

  6. 使用pie.htc时Border-radius的兼容

    如果一个图层中(navin)使用了pie.htc来对ie6,7,8进行兼容,如若上一层(navwrap)的样式中有背景的属性,则此层 (navin) 在ie6,7,8中背景颜色不显示.如下图:此部分的 ...

  7. SpringCloud的DataRest(四)restful特性展示

    一.get - list - http://10.110.20.16:8391/BusiSys/company?page=0&size=5&sort=comp_id,asc 二.pos ...

  8. Linux知识积累(3)$()和${}和$(())和(())

    $()和${}和$(())和(()) $()和${}的用法:在 bash shell 中,$( ) 与 ` ` (反引号) 都是用来做命令替换用(command substitution)的.而 $( ...

  9. Swing使用JavaFXweb组件

    概述 swing中内嵌入web组件的 需要使用一些其他的jar包 ,但是如果使用javafx的组件,那么也比较的方便,性能也比较高. 代码 webview 在javafx 中是作为 scene出现的所 ...

  10. 定点化_mif文件生成

    clc; %全屏清零 clear all; %变量清零 N=^; %设置ROM深度(字变量)的变量参数, s_p=:; %正弦波一个周期的采样点数 sin_data=sin(*pi*s_p/N); % ...