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 ...
随机推荐
- Android 获取assets的绝对路径
第一种方法: String path = "file:///android_asset/文件名"; 第二种方法: InputStream abpath = get ...
- Ajax轮询以及Comet模式—写在Servlet 3.0发布之前(转)
2008 年的夏天,偶然在网上闲逛的时候发现了 Comet 技术,人云亦云间,姑且认为它是由 Dojo 的 Alex Russell 在 2006 年提出.在阅读了大量的资料后,萌发出写篇 blog ...
- Drupal处理缓存的方式
Drupal的后台数据库中有很多以cache开头的表,这些都是Drupal的缓存数据表. Drupal的缓存机制使用了接口方式,所有的缓存对象都必须实现DrupalCacheInterface接口: ...
- .NET下用C#实现邮箱激活功能
最近要用到安全邮箱激活的功能,故写篇博客记录下. 思路:在表中增加一个字段State来记录邮箱是否激活(0激活,1未激活.) 1.发送邮件. 1-1,给邮箱发送邮件.内容:激活地址+GUID. ...
- 【LeetCode】223 - Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- 32+激发灵感的HTML5/CSS3网页设计教程
HTML5是寄托在HTML4基础上取得了的广泛成就.这不仅意味着你不必完全放弃现有的一些标记,而是可以借鉴,以加强 它. CSS3也以同样的方式在互联网内容的安排下,提供了它的柔韧性.CSS3是开 ...
- 【Python学习笔记】字典操作
字典dict是Python中唯一内置的映射类型,由键值对组成,字典是无序的.字典的键必须是不变对象,如字符串.数字.元组等,而包含可变对象的列表.字典和元组则不能作为键.这里可变和不可变的意思是指这个 ...
- java webservice的多种实现方法汇总
一.基于EJB容器管理webservice : 1.首先建立一个Web services EndPoint: package cn.test.service.impl; import java ...
- 【转】Linux下的多线程编程
1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的 Unix也支持线程的概念,但是在一个进程(proces ...
- 终于把你必须知道的.NET看完了
终于把你必须知道的.NET看完了,第二步就是把精通ASP.NET MVC3框架这本书搞定,练习MVC3的使用,并把EF,LINQ也练习一下,期间要做一个项目“多用户微信公众平台”项目,最近微信公众平台 ...