题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=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
题意:改变一个狐狸的颜色,让strength达到最大值;
思路:由于是平方和,所以数越大最后得值越大,所以要把相邻的两个数目相对小的那个-1,大的+1,等于1的时候特判一下,详情见代码;
AC代码:
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 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<len;i++)
23         {
24             if(str[i]==str[i-1])
25             {
26                 dp[num]++;
27             }
28             else
29             {
30                 sum+=dp[num]*dp[num];
31                 num++;
32                 dp[num]++;
33             }
34         }
35         sum+=dp[num]*dp[num];
36         if(num==1)
37         {
38             ans=dp[1]*dp[1];
39         }
40         for(int i=2;i<=num;i++)
41         {
42             if(dp[i]==1)
43             {
44                 ans=max(ans,sum+2*(dp[i-1]*dp[i+1]+dp[i-1]+dp[i+1]));
45             }
46             else
47             {
48                 if(dp[i-1]>=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 #"<<cnt++<<": "<<ans<<"\n";
56     }
57     return 0;
58 }

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

hdu-5583 Kingdom of Black and White(数学,贪心,暴力)相关推荐

  1. HDU 4741 Save Labman No.004 计算几何 数学

    题目链接 这道题会暴露很多孩纸的高数学的好不好.如何计算三维直线的最短距离和垂足的坐标,学习链接 之后的就是代入公式计算了. 1 #include <stdio.h> 2 #include ...

  2. hdu 4278 2012天津赛区网络赛 数学 *

    8进制转为10进制 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include ...

  3. HDU - 4635 Strongly connected(强连通缩点+数学+思维)

    题目链接:点击查看 题目大意:给出一个由n个点和m条边构成的无向图,现在问最多能添加几条边,能使得原图仍然不是强连通图,若原图初始时就是强连通图,直接输出-1 题目分析:首先对于原图来说,肯定会有一些 ...

  4. 【HDU - 6237】A Simple Stone Game(贪心,思维,素因子分解,数学)

    题干: After he has learned how to play Nim game, Bob begins to try another stone game which seems much ...

  5. #10010 「一本通 1.1 练习 6」糖果传递 (数学+贪心)

    题目描述 原题来自:HAOI 2008 有 n个小朋友坐成一圈,每人有 ai 颗糖果.每人只能给左右两人传递糖果.每人每次传递一颗糖果的代价为 1 .求使所有人获得均等糖果的最小代价. 输入格式 第一 ...

  6. 【牛客 - 318E】爱摸鱼的Dillonh(数学,暴力,细节)

    题干: "我不做人啦,jojo!" "Dillonh起来回答问题!" "啊?"沉迷于jojo的Dillonh又一次上课摸鱼被老师抓到了,他慌 ...

  7. HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解

    思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...

  8. Codeforces Educational 38 C. Constructing Tests ( 数学公式推导+暴力)

    C. Constructing Tests time limit per test 1 second memory limit per test 256 megabytes input standar ...

  9. HDU 4912 Paths on the tree(LCA+贪心)

    题目链接 Paths on the tree 来源  2014 多校联合训练第5场 Problem B 题意就是给出m条树上的路径,让你求出可以同时选择的互不相交的路径最大数目. 我们先求出每一条路径 ...

最新文章

  1. 【mysql知识点】查看和修改系统参数
  2. shell脚本编程测试类型下
  3. python学习笔记1-基础语法
  4. 算法中的Strassen矩阵乘法
  5. 构造函数失败_抛出异常
  6. HTML 为啥称“超文本标记语言”?
  7. Android让文本输入框默认不获取焦点
  8. AcWing 166. 数独
  9. 前端项目架构模板-(三)交互式打包及自动化部署前端项目
  10. 台型计算机电源电路图,电脑ATX电源控制电路及原理
  11. 使用python对微信好友进行数据分析
  12. 电路基础知识之有源晶振设计
  13. PPT背景填充的几种方式,简单高效
  14. 数据治理成功要素篇1:数据战略管理
  15. seek()方法的使用
  16. 5个值得收藏的图片素材APP 和网站,高品质外加免费,拿走不谢
  17. 华为路由器:ospf协议入门介绍
  18. stc15f2k60s2.h
  19. office2007无法使用宏的解决方法
  20. 考研英语——阅读的逻辑关系分析(唐迟阅读的逻辑笔记+实战)更新中

热门文章

  1. Python 操作 Windows 粘贴板
  2. jQueryPager(JQuery分页插件pagination实现Ajax分页)
  3. 2009年IT就业“危”中寻“机”
  4. 面试题62. 圆圈中最后剩下的数字
  5. 萌新的Python练习菜鸟100例(十八)一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
  6. SAP License:ERP具有的九种优势
  7. 模型审查的4个核心要点
  8. 软件工程相关书目之《大道至简》读后感
  9. asp.net常见数据类型
  10. Newsgroups数据集介绍