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

2015ACM/ICPC亚洲区上海站-重现赛(感谢华东理工)

Recommend

wange2014   |   We have carefully selected several similar problems for you:  5906 5905 5904 5903 5902

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <iomanip>
#include <math.h>
#include <map>
using namespace std;
#define FIN     freopen("input.txt","r",stdin);
#define FOUT    freopen("output.txt","w",stdout);
#define INF     0x3f3f3f3f
#define INFLL   0x3f3f3f3f3f3f3f
#define lson    l,m,rt<<1
#define rson    m+1,r,rt<<1|1
typedef long long LL;
typedef pair<int,int> PII;

const int MAXN = 1e5 + 5;
int T;
char a[MAXN];
LL dp[MAXN];
LL b[MAXN];

int main()
{
    //FIN
    scanf("%d", &T);
    int cas = 1;
    while(T--) {
        scanf("%s", a);
        int lena = strlen(a);
        dp[0] = 1;
        LL sum = 0;
        int t = 0;
        LL mx = 0;
        memset(b, 0, sizeof(b));
        for(int i = 1; i < lena; i++) {
            if(a[i] == a[i - 1]) dp[i] = dp[i - 1] + 1;
            else {
                mx = max(dp[i - 1], mx);
                b[t++] = dp[i - 1];
                sum += LL(dp[i - 1] * dp[i - 1]);
                dp[i] = 1;
            }
        }

        b[t] = dp[lena - 1];
        mx = max(b[t], mx);
        sum += LL(b[t] * b[t]);
        LL ans = sum;

        if(b[0] == lena) {
            ans = sum;
            printf("Case #%d: %lld\n", cas++, ans);
            continue;
        }

        LL tt;

        //cout << "mx=" << mx << endl;

        for(int i = 0; i <= t; i++) {
            if(b[i] == mx) {
                //cout << "i=" << i << endl;
                if(i == 0) tt = b[1];
                else if(i == t) tt = b[t - 1];
                else tt = min(b[i - 1], b[i + 1]);
                LL now = sum + (mx + 1) * (mx + 1) - (mx * mx) + (tt - 1) * (tt -1) - tt * tt;
                ans = max(ans, now);
            }
        }

        for(int i = 0; i <= t; i++) {
            if(b[i] == 1) {
                LL now = sum - b[i - 1] * b[i - 1] - b[i + 1] * b[i + 1] - 1 + (b[i + 1] + 1 + b[i - 1]) * (b[i + 1] + 1 + b[i - 1]);
                ans = max(ans, now);
            }
        }

        printf("Case #%d: %lld\n", cas++, ans);
    }
    return 0;
}

  

时间: 2024-10-13 04:55:53

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(模拟)

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

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 frog

HDU 3699 A hard Aoshu Problem (暴力搜索)

题意:题意:给你3个字符串s1,s2,s3;要求对三个字符串中的字符赋值(相同的字符串进行相同的数字替换), 替换后的三个数进行四则运算要满足左边等于右边,求有几种解法. Sample Input 2 A A A BCD BCD B Sample Output 5 72 eg:ABBDE   ABCCC   BDBDE :令 A = 1, B = 2, C = 0, D = 4, E = 5 12245 + 12000 = 24245: 注意没有前导零!! #include<stdio.h>

hdu 2721(字符串处理,位运算 暴力)

Persistent Bits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 201    Accepted Submission(s): 116 Problem Description WhatNext Software creates sequence generators that they hope will produce

hdu 4585 Shaolin两种方法(暴力和STL)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 Problem Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every year, trying to be a monk there. The mast

hdu 4886 TIANKENG’s restaurant(2)(hash+暴力)

题目链接:hdu 4886 TIANKENG's restaurant(2) 题目大意:给定一个字符串S,要求在该字符串中找到一个最短并且字符串字典序最小. 解题思路:每次枚举字符串的长度,然后将S中所有该长度的子串映射成一个9进制数,最后再遍历一遍标记数组. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1000005; i

HDU 4930 Fighting the Landlords (超级暴力+读懂题意)

题目链接:HDU 4930 Fighting the Landlords 斗地主!!.不会玩这游戏,真是苦逼.题意其他都还好,就是要注意只要第一个回合1号玩家能压制2号玩家就算赢了(突破点). 其他就分类暴力了,思路还是比较清晰的. 注意点: 1.对方炸弹,必输 2.一回合就出完牌,必胜 AC代码: #include<stdio.h> #include<string.h> int vis1[30],vis2[30]; int find(char s) { if(s=='T') re