HDU 5583 Kingdom of Black and White 暴力
Posted Hyouka
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 5583 Kingdom of Black and White 暴力相关的知识,希望对你有一定的参考价值。
Kingdom of Black and White
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1890 Accepted Submission(s): 588
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.
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.
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
Source
Recommend
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #include <iomanip> #include <math.h> #include <map> using namespace std; #define FIN freopen("input.txt","r",stdin); #define FOUT freopen("output.txt","w",stdout); #define INF 0x3f3f3f3f #define INFLL 0x3f3f3f3f3f3f3f #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef long long LL; typedef pair<int,int> PII; const int MAXN = 1e5 + 5; int T; char a[MAXN]; LL dp[MAXN]; LL b[MAXN]; int main() { //FIN scanf("%d", &T); int cas = 1; while(T--) { scanf("%s", a); int lena = strlen(a); dp[0] = 1; LL sum = 0; int t = 0; LL mx = 0; memset(b, 0, sizeof(b)); for(int i = 1; i < lena; i++) { if(a[i] == a[i - 1]) dp[i] = dp[i - 1] + 1; else { mx = max(dp[i - 1], mx); b[t++] = dp[i - 1]; sum += LL(dp[i - 1] * dp[i - 1]); dp[i] = 1; } } b[t] = dp[lena - 1]; mx = max(b[t], mx); sum += LL(b[t] * b[t]); LL ans = sum; if(b[0] == lena) { ans = sum; printf("Case #%d: %lld\n", cas++, ans); continue; } LL tt; //cout << "mx=" << mx << endl; for(int i = 0; i <= t; i++) { if(b[i] == mx) { //cout << "i=" << i << endl; if(i == 0) tt = b[1]; else if(i == t) tt = b[t - 1]; else tt = min(b[i - 1], b[i + 1]); LL now = sum + (mx + 1) * (mx + 1) - (mx * mx) + (tt - 1) * (tt -1) - tt * tt; ans = max(ans, now); } } for(int i = 0; i <= t; i++) { if(b[i] == 1) { LL now = sum - b[i - 1] * b[i - 1] - b[i + 1] * b[i + 1] - 1 + (b[i + 1] + 1 + b[i - 1]) * (b[i + 1] + 1 + b[i - 1]); ans = max(ans, now); } } printf("Case #%d: %lld\n", cas++, ans); } return 0; }
以上是关于HDU 5583 Kingdom of Black and White 暴力的主要内容,如果未能解决你的问题,请参考以下文章
HDU 5583 - Kingdom of Black and White题解
HDU 5583 Kingdom of Black and White(模拟)
hdu 5943 Kingdom of Obsession 二分图匹配+素数定理
hdu5943 Kingdom of Obsession 二分图+打表找规律