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

     1T50.

     for 60% data, 1N1000.

     for 100% data, 1N105.

     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<<"
    ";
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    SQLite的SQL语法
    C/C++中各种类型int、long、double、char表示范围(最大最小值)
    君子性非异也,善假于物也
    简单工厂模式
    Linux下通配符总结
    Readprocessmemory使用方法
    C++ 清空消息队列
    一周自学动态站点设计
    iOS 8.0正式公布啦
    What is the difference between JRE,JVM and JDK?
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5260277.html
Copyright © 2011-2022 走看看