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/showproblem.php?pid=5583
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
HINT
题意
给你一个只含有01的字符串,然后这个串的权值就是每一段连续的0或1的长度的平方和,然后你可以修改一个数,使得这个数变成0,或者使这个数变成1
然后问你最大权值能为多少
题解:
不能直接暴力枚举每一位,但是我们可以枚举每一个连通块就好了
每个连通块毫无疑问,只会修改最左边或者最右边的位置,然后直接扫一遍就行了
代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<vector>
using namespace std; string s;
int main()
{
int t;scanf("%d",&t);
for(int cas=;cas<=t;cas++)
{
cin>>s;
int flag = -;
vector<long long> Q;
int len = ;
for(int i=;i<s.size();i++)
{
if(s[i]-''!=flag)
{
Q.push_back(len);
len = ;
flag = s[i]-'';
}
else
len++;
}
Q.push_back(len);
Q.push_back();
long long Ans = ;
for(int i=;i<Q.size()-;i++)
Ans += Q[i]*Q[i];
long long Ans2 = Ans;
for(int i=;i<Q.size()-;i++)
{
long long tmp = ;
if(Q[i]==)
Ans2 = max(Ans2,Ans-Q[i-]*Q[i-]-Q[i]*Q[i]-Q[i+]*Q[i+]+(Q[i-]+Q[i+]+)*(Q[i-]+Q[i+]+));
else
{
Ans2 = max(Ans2,Ans-Q[i-]*Q[i-]-Q[i]*Q[i]+(Q[i-]+)*(Q[i-]+)+(Q[i]-)*(Q[i]-));
Ans2 = max(Ans2,Ans-Q[i+]*Q[i+]-Q[i]*Q[i]+(Q[i+]+)*(Q[i+]+)+(Q[i]-)*(Q[i]-));
}
}
printf("Case #%d: %lld\n",cas,Ans2);
}
}
HDU 5583 Kingdom of Black and White 水题的更多相关文章
- 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 ...
- 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 ...
- HDU 5583 Kingdom of Black and White(暴力)
http://acm.hdu.edu.cn/showproblem.php?pid=5583 题意: 给出一个01串,现在对这串进行分组,连续相同的就分为一组,如果该组内有x个数,那么就对答案贡献x* ...
- HDU 5832 A water problem(某水题)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- hdu 2393:Higher Math(计算几何,水题)
Higher Math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- <hdu - 3999> The order of a Tree 水题 之 二叉搜索的数的先序输出
这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999 Problem Description: As we know,the sha ...
- HDOJ/HDU 1256 画8(绞下思维~水题)
Problem Description 谁画8画的好,画的快,今后就发的快,学业发达,事业发达,祝大家发,发,发. Input 输入的第一行为一个整数N,表示后面有N组数据. 每组数据中有一个字符和一 ...
- hdu 1164:Eddy's research I(水题,数学题,筛法)
Eddy's research I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU ACM 1073 Online Judge ->字符串水题
分析:水题. #include<iostream> using namespace std; #define N 5050 char a[N],b[N],tmp[N]; void Read ...
随机推荐
- ActiveMQ之二--JMS消息类型
1.前言 //发送文本消息 session.createTextMessage(msg); //接受文本消息 public void onMessage(Message msg) { TextMess ...
- Android实现button一边圆角一边直角
http://www.it165.net/pro/html/201503/36211.html
- mysql安装与配置
想在个人电脑上安装mysql学习用.在此做下记录 步骤一: MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.如果是msi格式的可以直接点击安装,按照它给出的安装提示进行安装(相信大 ...
- 关于python中的unicode字符串的使用
基于python2.7中的字符串: unicode-->编码encode('utf-8')-->写入文件 读出文件-->解码decode('utf-8')-->unicode ...
- 初识HTTP 1.1与HTTP 1.0
HTTP 1.1与HTTP 1.0的比较 一个WEB站点每天可能要接收到上百万的用户请求,为了提高系统的效率,HTTP 1.0规定浏览器与服务器只保持短暂的连接,浏览器的每次请求都需要与服务器建立一个 ...
- Java 面向对象概念
Interface 接口 An interface defines a protocol of communication between two objects. An interface decl ...
- BITED-Windows8应用开发学习札记之二:Win8应用常用视图设计
感觉自我表述能力有欠缺,技术也不够硬,所以之后的Windows8应用开发学习札记的文章就偏向于一些我认为较难的地方和重点了多有抱歉. 上节课是入门,这节课就已经开始进行视图设计了. Windows应用 ...
- 【Python学习笔记】字典操作
字典dict是Python中唯一内置的映射类型,由键值对组成,字典是无序的.字典的键必须是不变对象,如字符串.数字.元组等,而包含可变对象的列表.字典和元组则不能作为键.这里可变和不可变的意思是指这个 ...
- 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程08:虚拟键盘实现》--本系列完结
8.虚拟键盘实现 概述: 硬键盘就是物理键盘,平时敲的那种.软键盘是虚拟的键盘,不是在键盘上,而是在"屏幕"上.虚拟按键就是虚拟键盘的一部分,根据功能需求,提供部分按键效果的UI可 ...
- 浅谈w3c标准
#浅谈w3c标准 ##w3c标准是什么 w3c标准包括多个方面,官方是从应用角度分的,相关的文档可以戳[这里](http://www.w3.org/standards/).如果从WEB技术角度,可以分 ...