• 2015-ICPC-Shanghai K



    点我传送

    题面:

    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


    题目分析:

    题意

    改变一个(0)(1),让连续的0串和1串的长度平方和最大

    思路分析

    本题直接采用暴力方法,枚举每个相邻的01串,让较长的串加上1,较短的串减去1,取最大值即可。当串的长度为1时需要特判,因此改变后的平方和的计算分为三种情况:

    1. (overbrace{000cdots0000}^{X个}underbrace{1111cdots111}_{Y个})((Xge Y))

      那么改变后的平方和为(Sum_{new}) (= Sum_{old}-X^2-Y^2+(X+1)^2+(Y-1)^2\=Sum_{old}+2*(X-Y+1))

    2. (overbrace{000cdots0000}^{X个}underbrace{1111cdots111}_{Y个})((X < Y))

      那么改变后的平方和为(Sum_{new}) (= Sum_{old}-X^2-Y^2+(X-1)^2+(Y+1)^2\=Sum_{old}+2*(Y-X+1))

    3. (overbrace{000cdots0000}^{X个}underbrace{1}_{1个}overbrace{0000cdots000}^{Y个}):这种情况下,只需要将中间的数字变为跟左右两边相同的数字即可,那么改变后的值为

      (Sum_{new}) (=Sum_{old}-(X^2+Y^2+1)+(X+Y+1)^2\ =Sum_{old}+2*(X*Y+X+Y))

    最后只需要判断上面三种情况,枚举后取最大值即为答案。

    AC代码

    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    const int N = 1e5 + 100;
    long long q[N], ans, sum;
    char str[N];
    int main() {
        ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
        int t;
        cin >> t;
        
        for(int w = 1; w <= t; w++) {
            int pos = 1;
            ans = 0, sum = 0;
            cin >> str;
            
            q[1] = 1;
            for (int i = 1; str[i]; i++) {
                if (str[i] == str[i - 1]) {
                    q[pos]++;
                }
                else {
                    sum += q[pos] * q[pos];
                    q[++pos] = 1;
                }
            }
            sum += q[pos] * q[pos];
            
        	ans = sum;
            
            for (int i = 2; i <= pos; i++) {
                if (q[i] == 1) {
                    ans = max(ans, sum + 2 * (q[i - 1] * q[i + 1] + q[i - 1] + q[i + 1]));
                } 
                else if (q[i - 1] >= q[i]) {
                    ans = max(ans, sum + 2 * (q[i - 1] - q[i] + 1));
                } else
                    ans = max(ans, sum + 2 * (q[i] - q[i - 1] + 1));
                
            }
            cout << "Case #" << w << ": " << ans << endl;
            for(int i = 0; i <= pos; i++) q[i] = 0;
        }
        return 0;
    }
    
  • 相关阅读:
    MYBATIS 的parameter
    深入了解MyBatis参数
    js之onload事件的一点使用心得
    js中document.write的那点事
    MyBatis直接执行SQL查询及批量插入数据
    ng 服务
    ng json格式的序列化和反序列化
    ng 自定义过滤器的创建和使用
    ng 过滤器
    ng 双向数据绑定 实现 注册协议效果
  • 原文地址:https://www.cnblogs.com/FrankOu/p/14377616.html
Copyright © 2020-2023  润新知