HDU5583 Kingdom of Black and White
Kingdom of Black and White
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3735 Accepted Submission(s): 1122
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.
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.
000011
0101
Case #2: 10
Statistic | Submit | Discuss | Note
【题解】
水前缀和乱搞,注意细节吧
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b)) inline void swap(long long &x, long long &y)
{
long long tmp = x;x = y;y = tmp;
} inline void read(long long &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = -x;
} const long long INF = 0x3f3f3f3f;
const long long MAXN = + ; long long num[MAXN], tot, n, t, sum[MAXN], ans; int main()
{
read(t);
for(register int tt = ;tt <= t;++ tt)
{
char tmp;tmp = getchar();
while(tmp != '' && tmp != '')tmp = getchar();
tot = ;memset(num, , sizeof(num));
ans = -;
char now = tmp;++ tot;
++ num[tot];
for(register long long i = ;;++ i)
{
tmp = getchar();
if(tmp != '' && tmp != '')break;
if(tmp == now)++ num[tot];
else ++ tot, ++num[tot], now = tmp;
}
for(register long long i = ;i <= tot;++ i)
sum[i] = sum[i - ] + num[i] * num[i];
ans = sum[tot];
for(register long long i = ;i < tot;++ i)
{
if(i != && num[i] == )
ans = max(ans,
sum[tot] - (sum[i + ] - sum[i - ])
+ (num[i - ] + num[i + ] + num[i])
* (num[i - ] + num[i + ] + num[i]));
else
ans = max(ans,
sum[tot] - (sum[i + ] - sum[i - ]) +
max((num[i] - ) * (num[i] - ) + (num[i + ] + ) * (num[i + ] + ),
(num[i] + ) * (num[i] + ) + (num[i + ] - ) * (num[i + ] - )));
}
printf("Case #%d: %lld\n", tt, ans);
}
return ;
}
HDU5583
HDU5583 Kingdom of Black and White的更多相关文章
- hdu-5583 Kingdom of Black and White(数学,贪心,暴力)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5583 Kingdom of Black and White Time Limit: 2000/1000 ...
- 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/showpr ...
- 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 ...
- [HDOJ5583]Kingdom of Black and White(暴力)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5583 一个01串,求修改一个位置,使得所有数均为0或1的子串长度的平方和最大.先分块,然后统计好原来的 ...
- HDU 5583 Kingdom of Black and White(暴力)
http://acm.hdu.edu.cn/showproblem.php?pid=5583 题意: 给出一个01串,现在对这串进行分组,连续相同的就分为一组,如果该组内有x个数,那么就对答案贡献x* ...
- HDU-5583-Kingdom of Black and White(2015ACM/ICPC亚洲区上海站-重现赛)
Kingdom of Black and White ...
- 2015ACM/ICPC亚洲区上海站
5573 Binary Tree(构造) 题意:给你一个二叉树,根节点为1,子节点为父节点的2倍和2倍+1,从根节点开始依次向下走k层,问如何走使得将路径上的数进行加减最终结果得到n. 联想到二进制. ...
- imshow() displays a white image for a grey image
Matlab expects images of type double to be in the 0..1 range and images that are uint8 in the 0..255 ...
随机推荐
- webpack 清理旧打包资源插件
当我们修改带hash的文件并进行打包时,每打包一次就会生成一个新的文件,而旧的文件并 没有删除.为了解决这种情况,我们可以使用clean-webpack-plugin 在打包之前将文件先清除,之后再打 ...
- http response 过长 导致Connection reset
http response 过长(2W byte) 导致Connection reset
- PAT甲级——A1088 Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- C#窗体阴影
/// <summary> /// 边框阴影 /// </summary> protected override CreateParams CreateParams { get ...
- Java超简明入门学习笔记(三)
Java编程思想第4版学习笔记(三) 第五章 初始化与清理(构造器和垃圾回收机制) Java有和C++类似的构造函数来为新创建的对象执行初始化及完成一些特殊的操作,有的类数据成员可能会 ...
- js 属性的遍历
引自:http://es6.ruanyifeng.com/#docs/object 属性的遍历 ES6 一共有5种方法可以遍历对象的属性. (1)for...in for...in循环遍历对象自身的和 ...
- css3之2D 转换
浏览器支持 表格中的数字表示支持该属性的第一个浏览器版本号. 紧跟在 -webkit-, -ms- 或 -moz- 前的数字为支持该前缀属性的第一个浏览器版本号. Chrome 和 Safari 要求 ...
- Sqoop学习笔记_Sqoop的基本使用二(sqoop的import与export)
Sqoop抽取从mysql抽取到hive sqoop抽取到mysql一样有两种方式一种是用command line的方式,一种是用sqoop opt文件调用的方式.(由于两种sqoop一已经记录了,现 ...
- idea报错:Error:java不支持发行版本5的解决方法
将以下对应配置一致即可. File-->Project Structure File-->Settings
- Spring Boot 容器选择 Undertow 而不是 Tomcat Spring Boot 内嵌容器Unde
Spring Boot 内嵌容器Undertow参数设置 配置项: # 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接, 默认设置每个CPU核心一个线程 # 不要设置过大,如果过大,启动 ...