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 ...
随机推荐
- C++ STL疑惑知识点
1.remove的问题 用法参考:http://www.cnblogs.com/heyonggang/p/3263568.html
- 又一枚神器:nginx
一直听说过nginx的大名,也自己装过,但是未直接使用其各种牛X的功能. 今天试用了一下,只能用两字感叹:牛逼!比如它提供的配置检查功能,真是贴心极了,又比如我想要的静态内容浏览器端缓存功能,动态内容 ...
- 关于Cygwin的x-Server的自动运行以及相关脚本修改
常常需要用到远端服务器的图形工具,如果在windows端没用xserver的话,很多程序无法运行.一个特殊的例子,emacs在没用xserver的时候,是直接在终端中打开的,如果不修改cygwin.b ...
- Lucene 入门需要了解的东西
全文搜索引擎的原理网上大段的内容,要想深入的学习,最好的办法就是先用一下,lucene 发展比较快,下面是写第一个demo 要注意的一些事情: 1.Lucene的核心jar包,下面几个包分别位于不同 ...
- 【转】Linux Page Cache的工作原理
1 .前言 自从诞生以来,Linux 就被不断完善和普及,目前它已经成为主流通用操作系统之一,使用得非常广泛,它与Windows.UNIX 一起占据了操作系统领域几乎所有的市场份额.特别是在高性能计算 ...
- 隐藏apache版本号 PHP版本号
httpd-default.conf ServerTokens Prod ServerSignature Off php.ini expose_php Off 重启服务器
- 用C#.NET调用Java开发的WebService传递int,double问题
用C#.NET调用Java开发的WebService时,先在客户端封装的带有int属性的对象,当将该对象传到服务器端时,服务器端可以得到string类型的属性值,却不能得到int类型.double和D ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- 【FFT】专题总结
学了若干天终于学(bei)会了传说中的法法塔 感觉也没那么难用嘛 fft快速傅里叶变换 在大表课件上写就是解决高精乘的工具 其实很有理有据 fft就是用复数的折半引理优化两个多项式相乘的高端东西 他能 ...
- 轻松学习Linux之理解Shell的硬链接与软连接
大家在学习linux的过程中常常遇到一些模糊且容易混淆的概念比如什么是硬链接和软链接,他们有什么区别? 软连接有点象windows中的快捷方式,连接和目标文件具有相同的节点,而硬连接就好象重新复制 ...