HDU-5583-Kingdom of Black and White【2015上海赛区】【暴力】
来源:互联网 发布:汉字听写大赛软件 编辑:程序博客网 时间:2024/04/27 16:24
HDU-5583-Kingdom of Black and White
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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
题目链接:HDU-5583
题目大意:给出一串只含有01的字符串,例如:
最多只能改变一个数字,0->1或者1 -> 0,使得最后平方和最大。
题目思路:直接暴力,枚举一个块边缘两个数字被改变之后的情况,找出最大值
以下是代码:
#include <vector>#include <map>#include <set>#include <algorithm>#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <string>#include <cstring>using namespace std;int main(){ int T; cin >> T; int cas = 1; while(T--) { string s; cin >> s; long long cnt = 1,ans = 0; char last = s[0]; vector <long long> block; block.push_back(0); for (int i = 1; i < s.size(); i++) { if (s[i] == last) cnt++; else { ans += cnt * cnt; block.push_back(cnt); cnt = 1; } last = s[i]; } block.push_back(cnt); ans += cnt * cnt; block.push_back(0); long long loy = ans; for (int i = 1; i < block.size() - 2; i++) { if (block[i] == 1) { long long temp = (block[i + 1] + block[i - 1] + 1) * (block[i + 1] + block[i - 1] + 1) - 1 - block[i + 1] * block[i + 1] - block[i - 1] * block[i - 1]; loy = max(loy,ans + temp); } else { long long temp2 = (block[i + 1] - 1) * (block[i + 1] - 1) + (block[i] + 1) * (block[i] + 1) - block[i + 1] * block[i + 1] - block[i] * block[i]; loy = max(loy,ans + temp2); } if (block[i + 1] == 1) { long long temp3 = (block[i] + block[i + 2] + 1) * (block[i] + block[i + 2] + 1) - 1 - block[i] * block[i] - block[i + 2] * block[i + 2]; loy = max(loy,ans + temp3); } else { long long temp4 = (block[i] + 1) * (block[i] + 1) + (block[i + 1] - 1) * (block[i + 1] - 1) - block[i] * block[i] - block[i + 1] * block[i + 1]; loy = max(loy,ans + temp4); } } printf("Case #%d: %lld\n",cas++,loy); } return 0;}
- HDU-5583-Kingdom of Black and White【2015上海赛区】【暴力】
- HDU 5583 Kingdom of Black and White(依旧是暴力)——2015ACM/ICPC亚洲区上海站
- HDU 5583 Kingdom of Black and White (暴力)
- HDU 5583 Kingdom of Black and White 暴力
- hdu 5583 Kingdom of Black and White 2015ACM/ICPC亚洲区上海站-重现赛
- HDU 5583 Kingdom of Black and White
- hdu 5583 Kingdom of Black and White
- hdu 5583 Kingdom of Black and White
- hdu 5583 Kingdom of Black and White
- HDU 5583 Kingdom of Black and White
- hdu 5583 Kingdom of Black and White
- HDU 5583 Kingdom of Black and White
- HDU 5583 Kingdom of Black and White
- hdu 5583 Kingdom of Black and White
- HDU 5583 Kingdom of Black and White
- 【HDU5583 2015上海赛区K】【暴力合并】Kingdom of Black and White 连续01串权值贡献为len^2最多改变一次最大权值
- HDU-5583-Kingdom of Black and White(2015ACM/ICPC亚洲区上海站-重现赛)
- 文章标题 HDU 5583:Kingdom of Black and White(暴力+代码实现能力)
- authors sdks 2.6.31 内核中关于gdb的配置
- 从七个角度诠释选购净水器的诀窍
- 基础练习 Huffuman树(贪心算法)
- BWT (Burrows–Wheeler_transform) 解码分析
- matlab下 rgb2hsi(图片路径) 函数代码
- HDU-5583-Kingdom of Black and White【2015上海赛区】【暴力】
- select管道通信
- hdu 2087 剪花布条(KMP)
- 老徐谈谈软件测试职业的现状,以及市场情况
- linux学习笔记(一)--linux系统的安装
- BZOJ 3720: Gty的妹子树|树分块
- hdu 5504 GT and sequence(思维,逻辑,脑洞)
- poj 2785
- 运行时数据区域