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. android context获取目录详解

    获取 sqlite系统数据库路径 方式1: ApkInfo apkInfo = new ResourceUtil(context).getApkInfo(); APP_PATH = new Strin ...

  2. Hibernate之HQL

    SQL语句的DML操作不外乎:增,删,改,查 增加 :  save(),persist() 删除 :   delete() 改动 :   update() 查询  :  get() ,load() 其 ...

  3. bzoj千题计划217:bzoj2333: [SCOI2011]棘手的操作

    http://www.lydsy.com/JudgeOnline/problem.php?id=2333 读入所有数据,先模拟一遍所有的合并操作 我们不关心联通块长什么样,只关心联通块内有谁 所以可以 ...

  4. 前端基础之html-Day12

    1.web服务本质 import socket def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bi ...

  5. j2ee基础(1)servlet的生命周期

    Servlet的生命周期 Servlet 生命周期规定了 Servlet 如何被加载.实例化.初始化. 处理客户端请求,以及何时结束服务. 该生命周期可以通过 javax.servlet.Servle ...

  6. .NET Core 2.1 Preview 2发布 - April 10, 2018

    我们今天宣布发布 .NET Core 2.1 Preview 2.这也是我们在接下来的两到三个月内接近最终发布的版本,该版本现已准备好进行广泛的测试.我们希望您有任何反馈意见. ASP.NET Cor ...

  7. POJ-3617 Best Cow Line---字符串贪心

    题目链接: https://vjudge.net/problem/POJ-3617 题目大意: 每次都可以从字符串的首部或者尾部提取字母,使得最后的字符串的字典序最小. 思路: 贪心做即可~每次从上和 ...

  8. Jenkins的安装

    安装环境: 512M内存 10G硬盘空间 Java 8环境 先来创建jenkins的运行目录: mkdir /data/jenkins && cd /data/jenkins 下载je ...

  9. angulajs_删除功能

  10. 消息中间件选型分析——从Kafka与RabbitMQ的对比来看全局

    一.前言 消息队列中间件(简称消息中间件)是指利用高效可靠的消息传递机制进行与平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排队模型,它可以在分布式环境下提供应用解耦 ...