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. 亚马逊的PuTTY连接AWS出现network error connection refused,终极解决方案。

    使用PuTTY连接AWS的时候,一直出现network error connection refused.百度了这个问题,大家都说是SSH要设置成22.但是我已经设置过了,为什么还是遇到这个问题呢? ...

  2. 《javascript设计模式与开发实践》阅读笔记(10)—— 组合模式

    组合模式:一些子对象组成一个父对象,子对象本身也可能是由一些孙对象组成. 有点类似树形结构的意思,这里举一个包含命令模式的例子 var list=function(){ //创建接口对象的函数 ret ...

  3. io文本加密

    文本加密习题 package io; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Inpu ...

  4. LeetCode & Q121-Best Time to Buy and Sell Stock-Easy

    Array DP Description: Say you have an array for which the ith element is the price of a given stock ...

  5. HDFS文件读写操作(基础基础超基础)

    环境 OS: Ubuntu 16.04 64-Bit JDK: 1.7.0_80 64-Bit Hadoop: 2.6.5 原理 <权威指南>有两张图,下次po上来好好聊一下 实测 读操作 ...

  6. Connect Appium Server Fail.A new session could not be created

    1.由于安卓测试机性能低下,并不能支持测试工作,想安装一个模拟器帮助测试,然后发现群里有朋友发了一个夜神模拟器..下载..安装..美滋滋的准备运行脚本.What..居然报错了..orz..然后百度查找 ...

  7. maven多环境部署

    1.首先在maven的pom.xml文件中添加profiles标签,然后分别添加3个不同环境的profile, 本例中添加了dev.test.product三个环境,这个可以根据自己的需要添加和减少. ...

  8. Mybatis的mapper代理开发dao方法

    看完了之前的mybatis原始的dao开发方法是不是觉得有点笨重,甚至说没有发挥mybatis 作为一个框架的优势.总结了一下,原始的dao方法有以下几点不足之处 dao接口实现方法中存在大量的模板方 ...

  9. Python-字典、集合、字符编码、文件操作整理-Day3

    1.字典 1.1.为什么有字典: 有个需求,存所有人的信息 这时候列表就不能轻易的表示完全names = ['stone','liang'] 1.2.元组: 定义符号()t = (1,2,3)tupl ...

  10. leetcode算法: Find the Difference

    Given two strings s and t which consist of only lowercase letters.String t is generated by random sh ...