zoukankan      html  css  js  c++  java
  • 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 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

    HINT

    题意

     给你一个只含有01的字符串,然后这个串的权值就是每一段连续的0或1的长度的平方和,然后你可以修改一个数,使得这个数变成0,或者使这个数变成1

    然后问你最大权值能为多少

    题解:

    不能直接暴力枚举每一位,但是我们可以枚举每一个连通块就好了

    每个连通块毫无疑问,只会修改最左边或者最右边的位置,然后直接扫一遍就行了

    代码:

    #include<iostream>
    #include<stdio.h>
    #include<cstring>
    #include<math.h>
    #include<vector>
    using namespace std;
    
    string s;
    int main()
    {
        int t;scanf("%d",&t);
        for(int cas=1;cas<=t;cas++)
        {
            cin>>s;
            int flag = -1;
            vector<long long> Q;
            int len = 0;
            for(int i=0;i<s.size();i++)
            {
                if(s[i]-'0'!=flag)
                {
                    Q.push_back(len);
                    len = 1;
                    flag = s[i]-'0';
                }
                else
                    len++;
            }
            Q.push_back(len);
            Q.push_back(0);
            long long Ans = 0;
            for(int i=1;i<Q.size()-1;i++)
                Ans += Q[i]*Q[i];
            long long Ans2 = Ans;
            for(int i=1;i<Q.size()-1;i++)
            {
                long long tmp = 0;
                if(Q[i]==1)
                    Ans2 = max(Ans2,Ans-Q[i-1]*Q[i-1]-Q[i]*Q[i]-Q[i+1]*Q[i+1]+(Q[i-1]+Q[i+1]+1)*(Q[i-1]+Q[i+1]+1));
                else
                {
                    Ans2 = max(Ans2,Ans-Q[i-1]*Q[i-1]-Q[i]*Q[i]+(Q[i-1]+1)*(Q[i-1]+1)+(Q[i]-1)*(Q[i]-1));
                    Ans2 = max(Ans2,Ans-Q[i+1]*Q[i+1]-Q[i]*Q[i]+(Q[i+1]+1)*(Q[i+1]+1)+(Q[i]-1)*(Q[i]-1));
                }
            }
            printf("Case #%d: %lld
    ",cas,Ans2);
        }
    }
  • 相关阅读:
    搭建cdh单机版版本的hive所遇到的问题总汇
    CentOS下Java的安装与环境配置
    重新认识Maven
    spring boot 搭建web项目常见五种返回形式
    一段递归代码引发的对于传参以及关于基本类型的一点了解
    爬虫
    .NET简谈接口
    C# Dictionary用法总结
    select @@identity的用法 转
    DataSet用法详细 转
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5029380.html
Copyright © 2011-2022 走看看