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  (representing
a black frog) and (representing a white frog). ⋅ ≤T≤. ⋅ for % data, ≤N≤. ⋅ for % data, ≤N≤. ⋅ the string only contains and .
 
Output
For every test case, you should output "Case #x: y",where x indicates the case number and counts from  and y is the answer.
 
Sample Input

 
Sample Output
Case #:
Case #:
 
Source
 
 
 
题意:例如000011,有连续为0的子序列长度为4,连续为1的子序列长度为2,所以这段字符串的价值为4*4+2*2=20,女巫最多可以将一个0或1改成1或0,那么把第5个字符1改成0,最后可以得到5*5+1*1=26,
例如0101,连续为0的子序列有2段,长度皆为1,连续为1的子序列也是两段,长度皆为1。当前价值为1*1*4=4,把第二个字符改0或者把第三个字符改1可以得到3*3+1*1=10,先离散化,求出结果,如果全部的颜色都一样则不用改变就是最优。总的来说是,先离散化,求出结果,如果全部的颜色都一样则不用改变就是最优。离散化之后,然后枚举每个离散块,尝试改变每个离散块两边的青蛙的颜色,更新最大值。
 
 
问题很多啊,开始是超时,原来把改变的那部分数算了就可以了,后来是答案错误,没用long long。
其他不难,主要是模拟各种情况,一开始要先离散化成各个模块,接下来就简单了。具体看代码。
 
 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 100006
#define inf 1e12
struct Node{
char c;
ll len;
}node[N];
char s[N];
int main()
{
int t;
ll ac=;
scanf("%I64d",&t);
while(t--){
for(ll i=;i<N;i++){
node[i].len=;
}
scanf("%s",s);
ll len=strlen(s);
ll k=;
node[k].c=s[];
node[k].len++;
ll now=;
for(ll i=;i<len;i++){
if(s[i]==s[now]){
node[k].len++;
now++;
}else{
k++;
node[k].c=s[i];
node[k].len++;
now++;
}
}
//for(ll i=0;i<=k;i++){
// prllf("%c %d\n",node[i].c,node[i].len);
//}
ll ans=;
for(ll i=;i<=k;i++){
ans+=node[i].len*node[i].len;
}
ll sum=ans; //prllf("%d\n",ans);
for(ll i=;i<=k;i++){
if(node[i].len==){ if(i==){
ll new_num=;
ll old_num=;
new_num=(node[].len+)*(node[].len+);
old_num=(node[].len)*(node[].len)+(node[].len)*(node[].len);
ans=max(ans,sum-old_num+new_num);
}else if(i==k){
ll new_num=;
ll old_num=;
new_num=(node[i-].len+)*(node[i-].len+);
old_num=(node[i].len)*(node[i].len)+(node[i-].len)*(node[i-].len);
ans=max(ans,sum-old_num+new_num);
}else{
ll new_num=;
ll old_num=; new_num=(node[i-].len+)*(node[i-].len+);
old_num=(node[i].len)*(node[i].len)+(node[i-].len)*(node[i-].len);
ans=max(ans,sum-old_num+new_num); new_num=;
old_num=; new_num=(node[i+].len+)*(node[i+].len+);
old_num=(node[i].len)*(node[i].len)+(node[i+].len)*(node[i+].len);
ans=max(ans,sum-old_num+new_num); new_num=;
old_num=;
if((node[i].c!=node[i-].c) && (node[i].c!=node[i+].c)){
new_num=(node[i-].len++node[i+].len)*(node[i-].len++node[i+].len);
old_num=(node[i].len)*(node[i].len)+(node[i+].len)*(node[i+].len)+(node[i-].len)*(node[i-].len);
ans=max(ans,sum-old_num+new_num);
}
} }else{
if(i==){ ll new_num=;
ll old_num=;
new_num+=(node[].len-)*(node[].len-);
new_num+=(node[].len+)*(node[].len+); old_num+=(node[].len)*(node[].len);
old_num+=(node[].len)*(node[].len); ans=max(ans,sum-old_num+new_num); }else if(i==k){ ll new_num=;
ll old_num=;
new_num+=(node[k].len-)*(node[k].len-);
new_num+=(node[k-].len+)*(node[k-].len+); old_num+=(node[k].len)*(node[k].len);
old_num+=(node[k-].len)*(node[k-].len); ans=max(ans,sum-old_num+new_num); }else{ ll new_num=;
ll old_num=; new_num+=(node[i-].len+)*(node[i-].len+);
new_num+=(node[i].len-)*(node[i].len-);
old_num+=(node[i].len)*(node[i].len);
old_num+=(node[i-].len)*(node[i-].len);
ans=max(ans,sum-old_num+new_num); new_num=;
old_num=;
new_num+=(node[i].len-)*(node[i].len-);
new_num+=(node[i+].len+)*(node[i+].len+);
old_num+=(node[i].len)*(node[i].len);
old_num+=(node[i+].len)*(node[i+].len);
ans=max(ans,sum-old_num+new_num);
}
}
}
printf("Case #%I64d: ",++ac);
printf("%I64d\n",ans);
}
return ;
}

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

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

  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 5908 Abelian Period (BestCoder Round #88 模拟+暴力)

    HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=59 ...

  6. hdu 4948 Kingdom(推论)

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

  7. HDU5583 Kingdom of Black and White

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

  8. hdu 2629 Identity Card (字符串解析模拟题)

    这题是一个字符串模拟水题,给12级学弟学妹们找找自信的,嘿嘿; 题目意思就是要你讲身份证的上的省份和生日解析出来输出就可以了: http://acm.hdu.edu.cn/showproblem.ph ...

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

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

随机推荐

  1. nginx模块开发获取post参数

    > 您好!>     我想请问下nginx模块里面怎么获取post参数,能有具体的代码更好!谢谢> 对于 "application/x-www-form-urlencode ...

  2. project小技巧:快捷键

    project小技巧:快捷键 任务升级         ALT  +  SHIFT + 向左键 任务降级         ALT  +  SHIFT + 向右键 滚动到表头(第一个任务)    Ctr ...

  3. read write spinlock

    发一个自己基于 C++11 写的 read write spinlock,在 MinGW 4.8.2 (gcc 4.8 全面支持c++ 11,但由于gcc windows平台 libstdc++ 目前 ...

  4. poj 3287 The Trip, 2007_贪心

    题意:把一个包放入另一个包内,使得总共要带的件数最少,就是说大包可以装小包,且一个大包只能装一个小包,但是这个小包可以继续装更小的包. 思路:因为相同大小的包不能互相装,所以最小数量就是有相同尺寸的包 ...

  5. Do we need other languages other than C and C++?

    There were hundreds of or thousands of programming languages created since the invention of computer ...

  6. HDU 4081 MST

    这道题在LRJ的书上看到,今天回过头来继续看这题,发现很多东西都已经明白了. 题意:有N个城市,每个城市有一个坐标和人口. 现在要建一些边使得他们都联通,花费就是这些边的长度,然后有一条边可以免费.问 ...

  7. vim乱码问题

    有的时候使用gvim查看文本文件时,出现乱码,现在来确定下原因. 经过我的查资料,发现gvim里有几个关于编码设置的变量:encoding.fileencoding.fileencodings.ter ...

  8. Hadoop 安装(3) JDK 的安装

    使用vsftp上传,JDK 和 Hadoop 到 Hadoop 用户目录. 安装JDK root 身份登录Slave4.Hadoop,在 /usr 下创建 "Java"文件夹,将j ...

  9. C# 运算符 if

    运算符: 一.算术运算符: + - * / % ——取余运算 取余运算的应用场景: 1.奇偶数的区分. 2.把数变化到某个范围之内.——彩票生成. 3.判断能否整除.——闰年.平年. int a = ...

  10. shell 学习

    将maven 项目里面的jar 包导出目录: 在项目里面执行: mvn dependency:copy-dependencies -DoutputDirectory=lib shell 参数之间必有空 ...