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.
NowN 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.
Now
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 lengthN , including only 0 (representing
a black frog) and1 (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.
Every test case only contains a string with length
a black frog) and
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
- hdoj 5583 Kingdom of Black and White 【模拟】
- hdoj 5583 Kingdom of Black and White
- hdoj Kingdom of Black and White 5583 (模拟&&DP) 好题
- HDU 5583 Kingdom of Black and White(模拟)
- HDU 5583 Kingdom of Black and White
- hdu 5583 Kingdom of Black and White
- hdu 5583 Kingdom of Black and White
- hdu 5583 Kingdom of Black and White
- HDU 5583 Kingdom of Black and White
- hdu 5583 Kingdom of Black and White
- HDU 5583 Kingdom of Black and White
- HDU 5583 Kingdom of Black and White
- hdu 5583 Kingdom of Black and White
- HDU 5583 Kingdom of Black and White
- Kingdom of Black and White
- Kingdom of Black and White
- hdu 5583 Kingdom of Black and White(高效)
- UVALive 7410 && POJ 5583 Kingdom of Black and White (枚举)
- effective c++为异常安全而努力是值得的(1)
- 我的第一篇博客
- MySQL 忘记密码:skip-grant-tables
- jQuery.getJSON() 函数详解
- SPFA算法 Bellman_ford优化
- hdoj 5583 Kingdom of Black and White 【模拟】
- 有向图强连通分量的Tarjan算法
- 配置较为稳定的kali源
- Android验证码简单设计
- 我们可以推测矩阵乘法最优解的时间复杂度么?
- Sublime Text3 + Golang搭建开发环境
- DHCP协议分析
- 保护成员 http://c.biancheng.net/cpp/biancheng/view/229.html
- 使用RMAN命令时提示“ RECOVERY_CATALOG_OWNER role must be granted to user RCAT”