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 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 }
时间: 2024-08-07 04:31:02

hdu-5583 Kingdom of Black and White(数学,贪心,暴力)的相关文章

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/showproblem.php?pid=5583 Description In the Kingdom of Black and White (KBW), there are two kinds of frogs: black frog and white frog. Now N frogs are stand

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 b

HDU 5583 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): 1890    Accepted Submission(s): 588 Problem Description In the Kingdom of Black and White (KBW), there are two kinds of

HDU 5583 Kingdom of Black and White(模拟)

题目戳这 题意:给你一个零一串,如果都是一个或者都是零的部分形成一个联通块,然后我们得到一个值,这个值是每个联通块的零的数目或者一的数目的平方和.然后最多可以改变一个数字,把零改成一或者把一改成零,然后得到的这个数值要最大. 思路:只要把联通块的数字都求出来,然后从前往后扫一遍,两个数字要么前面的加一后面的减一,要么前面的减一后面的加一,如果当前的数字为一,就是要求出这个数的左边加当前数加右边的数的平方,因为要合并.然后更新到最大的就行了. P.S.这道题,虽然想说莫名其妙WA了好几发,但是做到

HDU 5175 Misaki&#39;s Kiss again(数学,暴力枚举)

题目大意: After the Ferries Wheel, many friends hope to receive the Misaki's kiss again,so Misaki numbers them 1,2...N?1,N,if someone's number is M and satisfied the GCD(N,M) equals to N XOR M,he will be kissed again. Please help Misaki to find all M(1<=

hdu 5171 GTY&#39;s birthday gift(数学,矩阵快速幂)

题意: 开始时集合中有n个数. 现在要进行k次操作. 每次操作:从集合中挑最大的两个数a,b进行相加,得到的数添加进集合中. 以此反复k次. 问最后集合中所有数的和是多少. (2≤n≤100000,1≤k≤1000000000) 思路: 写出来发现是要求Fibonaci的前n个数的和. Fibonaci是用矩阵快速幂求的,这个也可以. [Sn,Fn,Fn-1]=[某个矩阵]*[Sn-1,Fn-1,Fn-2] [S2,F2,F1]=[2,1,1] 然后写,,, 这个代码有些繁琐,应该把矩阵操作单独

hdu 5024 Wang Xifeng&#39;s Little Plot (dfs+暴力)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 194    Accepted Submission(s): 131 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

HDU 5742 It&#39;s All In The Mind (贪心) 2016杭电多校联合第二场

题目:传送门. 题意:求题目中的公式的最大值,且满足题目中的三个条件. 题解:前两个数越大越好. #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; int gcd(int a,int b) { if(!b) return a; return gcd(b,a%b); } int main() { int t; ci

HDU 2491 Priest John&#39;s Busiest Day(贪心)

题目链接 题意: n场婚礼 给定每场婚礼的开始和结束时间,要求每场婚礼至少举行一半以上(不包括一半) 如果可行输出YES,否则输出NO 思路: 算出每场婚礼的至少要举行的时间t, 最早的结束时间mid 以最早结束时间mid为第一变量,开始时间s为第二变量从小到大排序 用cnt记录举办完每场婚礼的结束时间 分三种情况: ①加参前当婚礼的时间够不一半 ②果加参婚礼时婚礼已开始,结束时间就加上婚礼一半的时光 ③婚礼没开始,结束时间就是婚礼的最早结束时间mid 代码如下: #include<cstdio