HDU 5583 Kingdom of Black and White 水题
2015-12-08 15:57
387 查看
Kingdom of Black and White
Time Limit: 20 SecMemory Limit: 256 MB
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=5583Description
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=1;cas<=t;cas++) { cin>>s; int flag = -1; vector<long long> Q; int len = 0; for(int i=0;i<s.size();i++) { if(s[i]-'0'!=flag) { Q.push_back(len); len = 1; flag = s[i]-'0'; } else len++; } Q.push_back(len); Q.push_back(0); long long Ans = 0; for(int i=1;i<Q.size()-1;i++) Ans += Q[i]*Q[i]; long long Ans2 = Ans; for(int i=1;i<Q.size()-1;i++) { long long tmp = 0; if(Q[i]==1) Ans2 = max(Ans2,Ans-Q[i-1]*Q[i-1]-Q[i]*Q[i]-Q[i+1]*Q[i+1]+(Q[i-1]+Q[i+1]+1)*(Q[i-1]+Q[i+1]+1)); else { Ans2 = max(Ans2,Ans-Q[i-1]*Q[i-1]-Q[i]*Q[i]+(Q[i-1]+1)*(Q[i-1]+1)+(Q[i]-1)*(Q[i]-1)); Ans2 = max(Ans2,Ans-Q[i+1]*Q[i+1]-Q[i]*Q[i]+(Q[i+1]+1)*(Q[i+1]+1)+(Q[i]-1)*(Q[i]-1)); } } printf("Case #%d: %lld\n",cas,Ans2); } }
相关文章推荐
- 使用ios系统自带分享
- netduino之电源参考电路MC33269DT-5.0G
- python 杂记
- selenium grid 搭建
- 求最小正整数x,A^x=1(mod M)求阶模板
- 发布WCF项目到IIS
- 【Codeforces Round 274 (Div 2)B】【贪心】Towers 若干次移数后使得最大差值尽可能小
- 需求 - 11 - 等待动画
- UIWebView和WKWebView的使用及js交互
- 网络国际标准组介绍
- #学习笔记#(7)photoshop折纸风格
- 【图像识别】简单验证码识别
- android中遇到The connection to adb is down, and a severe error has occured.
- 【Codeforces Round 274 (Div 2)A】【暴力 水题】Expression 三个数值运算使得结果最大
- 10_11alarm函数的作用修复系统自己重启。
- 【C基础】#define宏定义中的#,##,@#,\ 这些符号的神奇用法
- iOS开发之block(二)
- MFC中使用Opencv打开摄像头并显示
- IOS:两种回调的方式实现(delegate和block)
- 异步上传图片