Problem Description
  1. In the Kingdom of Black and White (KBW), there are two kinds of frogs: black frog and white frog.
  2.  
  3. 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.
  4.  
  5. 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.
  6.  
  7. The frogs wonder the maximum possible strength after the witch finishes her job.
 
Input
  1. First line contains an integer T, which indicates the number of test cases.
  2.  
  3. Every test case only contains a string with length N, including only (representing
  4. a black frog) and (representing a white frog).
  5.  
  6. T≤.
  7.  
  8. for % data, N≤.
  9.  
  10. for % data, N≤.
  11.  
  12. the string only contains and .
 
Output
  1. For every test case, you should output "Case #x: y",where x indicates the case number and counts from and y is the answer.
 
Sample Input
  1.  
 
Sample Output
  1. Case #:
  2. Case #:
 
Source
 
 
 
题意:例如000011,有连续为0的子序列长度为4,连续为1的子序列长度为2,所以这段字符串的价值为4*4+2*2=20,女巫最多可以将一个0或1改成1或0,那么把第5个字符1改成0,最后可以得到5*5+1*1=26,
例如0101,连续为0的子序列有2段,长度皆为1,连续为1的子序列也是两段,长度皆为1。当前价值为1*1*4=4,把第二个字符改0或者把第三个字符改1可以得到3*3+1*1=10,先离散化,求出结果,如果全部的颜色都一样则不用改变就是最优。总的来说是,先离散化,求出结果,如果全部的颜色都一样则不用改变就是最优。离散化之后,然后枚举每个离散块,尝试改变每个离散块两边的青蛙的颜色,更新最大值。
 
 
问题很多啊,开始是超时,原来把改变的那部分数算了就可以了,后来是答案错误,没用long long。
其他不难,主要是模拟各种情况,一开始要先离散化成各个模块,接下来就简单了。具体看代码。
 
  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<math.h>
  7. #include<algorithm>
  8. #include<queue>
  9. #include<set>
  10. #include<bitset>
  11. #include<map>
  12. #include<vector>
  13. #include<stdlib.h>
  14. #include <stack>
  15. using namespace std;
  16. #define PI acos(-1.0)
  17. #define max(a,b) (a) > (b) ? (a) : (b)
  18. #define min(a,b) (a) < (b) ? (a) : (b)
  19. #define ll long long
  20. #define eps 1e-10
  21. #define MOD 1000000007
  22. #define N 100006
  23. #define inf 1e12
  24. struct Node{
  25. char c;
  26. ll len;
  27. }node[N];
  28. char s[N];
  29. int main()
  30. {
  31. int t;
  32. ll ac=;
  33. scanf("%I64d",&t);
  34. while(t--){
  35. for(ll i=;i<N;i++){
  36. node[i].len=;
  37. }
  38. scanf("%s",s);
  39. ll len=strlen(s);
  40. ll k=;
  41. node[k].c=s[];
  42. node[k].len++;
  43. ll now=;
  44. for(ll i=;i<len;i++){
  45. if(s[i]==s[now]){
  46. node[k].len++;
  47. now++;
  48. }else{
  49. k++;
  50. node[k].c=s[i];
  51. node[k].len++;
  52. now++;
  53. }
  54. }
  55. //for(ll i=0;i<=k;i++){
  56. // prllf("%c %d\n",node[i].c,node[i].len);
  57. //}
  58. ll ans=;
  59. for(ll i=;i<=k;i++){
  60. ans+=node[i].len*node[i].len;
  61. }
  62. ll sum=ans;
  63.  
  64. //prllf("%d\n",ans);
  65. for(ll i=;i<=k;i++){
  66. if(node[i].len==){
  67.  
  68. if(i==){
  69. ll new_num=;
  70. ll old_num=;
  71. new_num=(node[].len+)*(node[].len+);
  72. old_num=(node[].len)*(node[].len)+(node[].len)*(node[].len);
  73. ans=max(ans,sum-old_num+new_num);
  74. }else if(i==k){
  75. ll new_num=;
  76. ll old_num=;
  77. new_num=(node[i-].len+)*(node[i-].len+);
  78. old_num=(node[i].len)*(node[i].len)+(node[i-].len)*(node[i-].len);
  79. ans=max(ans,sum-old_num+new_num);
  80. }else{
  81. ll new_num=;
  82. ll old_num=;
  83.  
  84. new_num=(node[i-].len+)*(node[i-].len+);
  85. old_num=(node[i].len)*(node[i].len)+(node[i-].len)*(node[i-].len);
  86. ans=max(ans,sum-old_num+new_num);
  87.  
  88. new_num=;
  89. old_num=;
  90.  
  91. new_num=(node[i+].len+)*(node[i+].len+);
  92. old_num=(node[i].len)*(node[i].len)+(node[i+].len)*(node[i+].len);
  93. ans=max(ans,sum-old_num+new_num);
  94.  
  95. new_num=;
  96. old_num=;
  97. if((node[i].c!=node[i-].c) && (node[i].c!=node[i+].c)){
  98. new_num=(node[i-].len++node[i+].len)*(node[i-].len++node[i+].len);
  99. old_num=(node[i].len)*(node[i].len)+(node[i+].len)*(node[i+].len)+(node[i-].len)*(node[i-].len);
  100. ans=max(ans,sum-old_num+new_num);
  101. }
  102. }
  103.  
  104. }else{
  105. if(i==){
  106.  
  107. ll new_num=;
  108. ll old_num=;
  109. new_num+=(node[].len-)*(node[].len-);
  110. new_num+=(node[].len+)*(node[].len+);
  111.  
  112. old_num+=(node[].len)*(node[].len);
  113. old_num+=(node[].len)*(node[].len);
  114.  
  115. ans=max(ans,sum-old_num+new_num);
  116.  
  117. }else if(i==k){
  118.  
  119. ll new_num=;
  120. ll old_num=;
  121. new_num+=(node[k].len-)*(node[k].len-);
  122. new_num+=(node[k-].len+)*(node[k-].len+);
  123.  
  124. old_num+=(node[k].len)*(node[k].len);
  125. old_num+=(node[k-].len)*(node[k-].len);
  126.  
  127. ans=max(ans,sum-old_num+new_num);
  128.  
  129. }else{
  130.  
  131. ll new_num=;
  132. ll old_num=;
  133.  
  134. new_num+=(node[i-].len+)*(node[i-].len+);
  135. new_num+=(node[i].len-)*(node[i].len-);
  136. old_num+=(node[i].len)*(node[i].len);
  137. old_num+=(node[i-].len)*(node[i-].len);
  138. ans=max(ans,sum-old_num+new_num);
  139.  
  140. new_num=;
  141. old_num=;
  142. new_num+=(node[i].len-)*(node[i].len-);
  143. new_num+=(node[i+].len+)*(node[i+].len+);
  144. old_num+=(node[i].len)*(node[i].len);
  145. old_num+=(node[i+].len)*(node[i+].len);
  146. ans=max(ans,sum-old_num+new_num);
  147. }
  148. }
  149. }
  150. printf("Case #%I64d: ",++ac);
  151. printf("%I64d\n",ans);
  152. }
  153. return ;
  154. }

hdu 5583 Kingdom of Black and White(模拟,技巧)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. HDU 5583 Kingdom of Black and White(暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=5583 题意: 给出一个01串,现在对这串进行分组,连续相同的就分为一组,如果该组内有x个数,那么就对答案贡献x* ...

  4. 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 ...

  5. HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力)

    HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=59 ...

  6. hdu 4948 Kingdom(推论)

    hdu 4948 Kingdom(推论) 传送门 题意: 题目问从一个城市u到一个新的城市v的必要条件是存在 以下两种路径之一 u --> v u --> w -->v 询问任意一种 ...

  7. HDU5583 Kingdom of Black and White

    Kingdom of Black and White Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  8. hdu 2629 Identity Card (字符串解析模拟题)

    这题是一个字符串模拟水题,给12级学弟学妹们找找自信的,嘿嘿; 题目意思就是要你讲身份证的上的省份和生日解析出来输出就可以了: http://acm.hdu.edu.cn/showproblem.ph ...

  9. [HDOJ5583]Kingdom of Black and White(暴力)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5583 一个01串,求修改一个位置,使得所有数均为0或1的子串长度的平方和最大.先分块,然后统计好原来的 ...

随机推荐

  1. Qt4.7文档翻译:Qt样式单参考,Qt Style Sheets Reference(超长,超全)

    内容目录 Qt样式单参考 可进行样式设置的部件列表 属性列表 图标列表 属性类型列表 伪状态列表 子控件列表 Qt样式单参考 Qt样式单支持各种属性.伪状态和子控件,这样使得妳能够自行设计部件的外观. ...

  2. SQL Server中各个系统表的作用

    sysaltfiles            主数据库               保存数据库的文件 syscharsets         主数据库               字符集与排序顺序 s ...

  3. Checkbutton 和 Radiobutton

    The Checkbutton widget is used to display a number of options to a user as toggle buttons. The user ...

  4. (一)boost库之日期、时间

    (一)boost库之日期.时间 一.计时器  计时器,通常在一个项目中统计一个函数的执行时间是非常实用的.   #include <boost/timer.hpp> void PrintU ...

  5. 关于SubclassWindow()和SubclassDlgItem

    msdn上的解析 CWnd::SubclassWindowBOOL SubclassWindow( HWND hWnd ); Return Value Nonzero if the function ...

  6. cf466B Wonder Room

    B. Wonder Room time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. TCP的那些事(转载)

    (转载本站文章请注明作者和出处 酷 壳 – CoolShell.cn ,请勿用于任何商业用途) TCP是一个巨复杂的协议,因为他要解决很多问题,而这些问题又带出了很多子问题和阴暗面.所以学习TCP本身 ...

  8. Android 绘图工具库AChartEngine

    From: http://www.oschina.net/p/achartengine AChartEngine是为android应用而设计的绘图工具库.目前该库的最新稳定版本是0.7,支持绘制以下类 ...

  9. 将常见对象转换成json字符串

    public class JsonUtil { public static String objectTojson(Object obj) { StringBuilder json = new Str ...

  10. ORA-03113 通信通道的文件结尾(ORA-19804 ORA-16038-归档空间满的处理方法)

    1.数据库启动报错SQL> startupORACLE 例程已经启动. Total System Global Area 1887350784 bytesFixed Size 2176848 b ...