题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5583
一个01串,求修改一个位置,使得所有数均为0或1的子串长度的平方和最大。先分块,然后统计好原来的结果,每次更新块,更新最大值。
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; typedef long long ll;
const int maxn = ;
int n;
char s[maxn];
ll grid[maxn]; int main() {
// freopen("in", "r", stdin);
int T;
scanf("%d", &T);
for(int _ = ; _ <= T; _++) {
scanf("%s", s);
memset(grid, , sizeof(grid));
ll cur = , ans = ;
n = ; grid[n] = ;
for(int i = ; s[i]; i++) s[i] == s[i-] ? grid[n]++ : grid[++n] = ;
for(int i = ; i <= n; i++) cur += grid[i] * grid[i];
if(n == ) ans = grid[] * grid[];
else {
for(int i = ; i <= n; i++) {
if(grid[i] == ) ans = max(cur+*(grid[i-]*grid[i+]+grid[i-]+grid[i+]), ans);
else {
if(grid[i-] >= grid[i])
ans = max(cur+*(grid[i-]-grid[i]+), ans);
else
ans = max(cur+*(grid[i]-grid[i-]+), ans);
}
}
}
printf("Case #%d: %I64d\n", _, ans);
}
return ;
}