博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu-5583 Kingdom of Black and White(数学,贪心,暴力)
阅读量:4699 次
发布时间:2019-06-09

本文共 2275 字,大约阅读时间需要 7 分钟。

题目链接:

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).
 1T50.
 for 60% data, 1N1000.
 for 100% data, 1N105.
 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
题意:改变一个狐狸的颜色,让strength达到最大值;
思路:由于是平方和,所以数越大最后得值越大,所以要把相邻的两个数目相对小的那个-1,大的+1,等于1的时候特判一下,详情见代码;
AC代码:
1 #include 
2 #include
3 #include
4 #include
5 using namespace std; 6 const int N=1e5+4; 7 long long dp[N]; 8 char str[N]; 9 int main()10 {11 int t;12 scanf("%d",&t);13 int cnt=1;14 while(t--)15 {16 int num=1;17 long long ans=0,sum=0;18 scanf("%s",str);19 int len=strlen(str);20 memset(dp,0,sizeof(dp));21 dp[1]=1;22 for(int i=1;i
=dp[i])49 {50 ans=max(ans,sum+2*(dp[i-1]-dp[i]+1));51 }52 else ans=max(ans,sum+2*(dp[i]-dp[i-1]+1));53 }54 }55 cout<<"Case #"<
<<": "<
<<"\n";56 }57 return 0;58 }

 

转载于:https://www.cnblogs.com/zhangchengc919/p/5260277.html

你可能感兴趣的文章
JMS
查看>>
gulpfile 压缩模板
查看>>
【34.14%】【BZOJ 3110】 [Zjoi2013]K大数查询
查看>>
【 henuacm2016级暑期训练-动态规划专题 A 】Cards
查看>>
第五篇:白话tornado源码之褪去模板的外衣
查看>>
设备常用框架framework
查看>>
bootstrap模态框和select2合用时input无法获取焦点(转)
查看>>
MockObject
查看>>
BZOJ4516: [Sdoi2016]生成魔咒(后缀自动机)
查看>>
查看手机已经记住的WIFI密码
查看>>
最新版IntelliJ IDEA2019 破解教程(2019.08.07-情人节更新)
查看>>
C# 两个datatable中的数据快速比较返回交集或差集
查看>>
关于oracle样例数据库emp、dept、salgrade的mysql脚本复杂查询分析
查看>>
adb shell am 的用法
查看>>
iOS10 UI教程视图和子视图的可见性
查看>>
FindChildControl与FindComponent
查看>>
中国城市json
查看>>
android下载手动下载Android SDK
查看>>
C++学习:任意合法状态下汉诺塔的移动(原创)
查看>>
leetcode133 - Clone Graph - medium
查看>>