zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    14、java中的equals()和toString()方法
    13、java中的多态
    1、editplus中将选取向前移动
    《Java4Android视频教程》学习笔记(一)
    android 构建数据库SQLite
    SWOT自我分析
    函数传递是如何让HTTP服务器工作的
    Node.js模块
    Viewcontrol的生命周期
    iOS开发-用宏定义求2个数中的最大值
  • 原文地址:https://www.cnblogs.com/FrankOu/p/14377616.html
Copyright © 2011-2022 走看看