hdu 5583 Kingdom of Black and White(模拟,技巧)

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
 
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=0;
 33    scanf("%I64d",&t);
 34    while(t--){
 35          for(ll i=0;i<N;i++){
 36             node[i].len=0;
 37          }
 38       scanf("%s",s);
 39       ll len=strlen(s);
 40       ll k=0;
 41       node[k].c=s[0];
 42       node[k].len++;
 43       ll now=0;
 44       for(ll i=1;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
",node[i].c,node[i].len);
 57       //}
 58       ll ans=0;
 59       for(ll i=0;i<=k;i++){
 60          ans+=node[i].len*node[i].len;
 61       }
 62       ll sum=ans;
 63 
 64 
 65 
 66       //prllf("%d
",ans);
 67      for(ll i=0;i<=k;i++){
 68          if(node[i].len==1){
 69 
 70             if(i==0){
 71                ll new_num=0;
 72                ll old_num=0;
 73                new_num=(node[1].len+1)*(node[1].len+1);
 74                old_num=(node[0].len)*(node[0].len)+(node[1].len)*(node[1].len);
 75                ans=max(ans,sum-old_num+new_num);
 76             }else if(i==k){
 77                ll new_num=0;
 78                ll old_num=0;
 79                new_num=(node[i-1].len+1)*(node[i-1].len+1);
 80                old_num=(node[i].len)*(node[i].len)+(node[i-1].len)*(node[i-1].len);
 81                ans=max(ans,sum-old_num+new_num);
 82             }else{
 83                ll new_num=0;
 84                ll old_num=0;
 85 
 86                new_num=(node[i-1].len+1)*(node[i-1].len+1);
 87                old_num=(node[i].len)*(node[i].len)+(node[i-1].len)*(node[i-1].len);
 88                ans=max(ans,sum-old_num+new_num);
 89 
 90                new_num=0;
 91                old_num=0;
 92 
 93                new_num=(node[i+1].len+1)*(node[i+1].len+1);
 94                old_num=(node[i].len)*(node[i].len)+(node[i+1].len)*(node[i+1].len);
 95                ans=max(ans,sum-old_num+new_num);
 96 
 97                new_num=0;
 98                old_num=0;
 99                if((node[i].c!=node[i-1].c) && (node[i].c!=node[i+1].c)){
100                   new_num=(node[i-1].len+1+node[i+1].len)*(node[i-1].len+1+node[i+1].len);
101                   old_num=(node[i].len)*(node[i].len)+(node[i+1].len)*(node[i+1].len)+(node[i-1].len)*(node[i-1].len);
102                   ans=max(ans,sum-old_num+new_num);
103                }
104             }
105 
106          }else{
107             if(i==0){
108 
109                      ll new_num=0;
110                      ll old_num=0;
111                      new_num+=(node[0].len-1)*(node[0].len-1);
112                      new_num+=(node[1].len+1)*(node[1].len+1);
113 
114                      old_num+=(node[0].len)*(node[0].len);
115                      old_num+=(node[1].len)*(node[1].len);
116 
117 
118                      ans=max(ans,sum-old_num+new_num);
119 
120 
121             }else if(i==k){
122 
123                   ll new_num=0;
124                   ll old_num=0;
125                   new_num+=(node[k].len-1)*(node[k].len-1);
126                   new_num+=(node[k-1].len+1)*(node[k-1].len+1);
127 
128                   old_num+=(node[k].len)*(node[k].len);
129                   old_num+=(node[k-1].len)*(node[k-1].len);
130 
131 
132                   ans=max(ans,sum-old_num+new_num);
133 
134 
135             }else{
136 
137                   ll new_num=0;
138                   ll old_num=0;
139 
140                   new_num+=(node[i-1].len+1)*(node[i-1].len+1);
141                   new_num+=(node[i].len-1)*(node[i].len-1);
142                   old_num+=(node[i].len)*(node[i].len);
143                   old_num+=(node[i-1].len)*(node[i-1].len);
144                   ans=max(ans,sum-old_num+new_num);
145 
146                   new_num=0;
147                   old_num=0;
148                   new_num+=(node[i].len-1)*(node[i].len-1);
149                   new_num+=(node[i+1].len+1)*(node[i+1].len+1);
150                   old_num+=(node[i].len)*(node[i].len);
151                   old_num+=(node[i+1].len)*(node[i+1].len);
152                   ans=max(ans,sum-old_num+new_num);
153             }
154          }
155      }
156      printf("Case #%I64d: ",++ac);
157      printf("%I64d
",ans);
158    }
159     return 0;
160 }
View Code
原文地址:https://www.cnblogs.com/UniqueColor/p/5011549.html