hdoj 5583 Kingdom of Black and White 【模拟】

来源:互联网 发布:收银软件论坛 编辑:程序博客网 时间:2024/06/06 07:16



Kingdom of Black and White

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 18    Accepted Submission(s): 10


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
20000110101
 

Sample Output
Case #1: 26Case #2: 10
 



定义01串:将串分成最少的连续的块(相同字符),该串价值即为 所有块的字符个数平方 之和。

题意:给定一个01串,最多只能改变一个字符(1->0 或 0->1),问最大价值。



AC代码:

#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <vector>#define INF 0x3f3f3f#define eps 1e-8#define MAXN (100000+10)#define MAXM (100000)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1using namespace std;struct Node{    LL sum;};Node num[MAXN];char str[MAXN];int main(){    int t, kcase = 1; Ri(t);    W(t)    {        Rs(str);        int len = strlen(str);        int top = 0, pre = -1;        for(int i = 0; i < len; i++)        {            int op = str[i] - '0';            if(op != pre)            {                ++top;                num[top].sum = 1;                pre = op;            }            else                num[top].sum++;        }        if(top == 1)        {            printf("Case #%d: %lld\n", kcase++, num[top].sum*num[top].sum);            continue;        }        LL ans = 0;        for(int i = 1; i <= top; i++)            ans += num[i].sum * num[i].sum;        LL ans1 = ans;        for(int i = 1; i <= top; i++)        {            LL temp = ans;            LL l = 0, r = 0, m = num[i].sum;            if(i == 1)                r = num[i+1].sum;            else if(i == top)                l = num[i-1].sum;            else            {                l = num[i-1].sum;                r = num[i+1].sum;            }            temp -= m*m;            temp += (m-1)*(m-1);            if(m == 1)            {                temp -= l*l + r*r;                temp += (l+r+1)*(l+r+1);            }            else            {                if(l == 0)                {                    temp -= r*r;                    temp += (r+1)*(r+1);                }                else if(r == 0)                {                    temp -= l*l;                    temp += (l+1)*(l+1);                }                else                {                    temp -= max(l, r)*max(l, r);                    temp += (max(l, r)+1)*(max(l, r)+1);                }            }            ans1 = max(ans1, temp);        }        printf("Case #%d: %lld\n", kcase++, max(ans, ans1));    }    return 0;}


1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 红绿色盲驾考怎么办 驾驶证忘带被交警查了怎么办 车子被扣45分怎么办 驾照被扣在外省交警支队怎么办 从渭南把驾照转到西安怎么办 a1a2驾驶证扣3分怎么办 车辆累计扣12分怎么办 驾照扣了40分怎么办 驾驶证扣了30分怎么办 b2驾照逾期未审怎么办 c1实习期扣6分怎么办 车子累计扣30分怎么办 实习期间扣满12分怎么办 新手驾照扣6分怎么办 a2驾驶证逾期未审验怎么办 c1驾照扣了6分怎么办 b1驾照被扣12分怎么办 b2驾驶本扣分了怎么办 驾驶本扣9分后怎么办 b1照扣12分怎么办 b2扣了15分怎么办 b2有扣分未年审怎么办 b2驾驶证扣4分怎么办 b2驾驶证扣10分怎么办 刚发驾驶证照片太丑想换怎么办! 考驾照时户口变更怎么办 驾照年审色盲未过怎么办 考驾驶证互联网注册号码怎么办 驾驶证体检视力不过关怎么办 六年驾照满了怎么办 驾照扣了40多分怎么办 一个驾照扣24分怎么办 南昌电动车牌照丢了怎么办 上海餐饮工作人员怎么办健康证 房产过户没有遗嘱公证怎么办 在外地被扣12分怎么办 公务员体检视力不过关怎么办弱视 身份证被盗用注册公司怎么办 驾照分卖了12分怎么办 一年12分扣完了怎么办 滴滴车管所信息不同步怎么办