hdu 5583 Kingdom of Black and White(高效)
来源:互联网 发布:曲阜网络问政 编辑:程序博客网 时间:2024/05/17 07:53
题目链接:hdu 5583 Kingdom of Black and White
代码
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 1e5 + 5;typedef long long ll;char str[maxn];int L[maxn][2], R[maxn][2];ll solve () { int n = strlen(str+1); memset(L, 0, sizeof(L)); memset(R, 0, sizeof(R)); for (int i = 1; i <= n; i++) { int d = str[i] - '0'; L[i][d] = L[i-1][d] + 1; } for (int i = n; i; i--) { int d = str[i] - '0'; R[i][d] = R[i+1][d] + 1; } ll sum = 0; for (int i = 1; i <= n; i++) if (str[i] != str[i+1]) { int d = str[i] - '0'; sum += 1LL * L[i][d] * L[i][d]; } ll ans = sum; for (int i = 1; i <= n; i++) { int t = str[i] - '0'; ll al = i > 1 ? L[i-1][str[i-1]-'0'] : 0, am = 1, ar = i < n ? R[i+1][str[i+1]-'0'] : 0; ll dl = al, dm = am, dr = ar; if (dl && str[i-1] == str[i]) { dm += dl; dl = 0; } else if (al) { am += al; al = 0; } if (dr && str[i+1] - '0' == t) { dr += dm; dm = 0; } else if (ar) { ar += am; am = 0; } ll add = al * al + am * am + ar * ar; ll del = dl * dl + dm * dm + dr * dr; //printf("+%lld %lld %lld -%lld %lld %lld\n", al, am, ar, dl, dm, dr); ans = max(ans, sum + add - del); } return ans;}int main () { int cas; scanf("%d", &cas); for (int kcas = 1; kcas <= cas; kcas++) { scanf("%s", str+1); printf("Case #%d: %lld\n", kcas, solve()); } return 0;}
0 0
- 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
- HDU-5583 Kingdom of Black and White(思维)
- HDU 5583 Kingdom of Black and White (暴力)
- HDU 5583 Kingdom of Black and White [Ad Hoc]
- 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 5578 Friendship of Frog(水)
- Linux异步通知
- 测试百度云平台BAE的mysql服务
- Gradle简介与安装
- Android之发送短信后按钮(60秒)变灰色每隔一秒递减显示
- hdu 5583 Kingdom of Black and White(高效)
- 配置ubuntu开机自动启动samba服务命令
- 百度 地图 slidingmenu 黑边 使用截图的方式解决黑边问题,步骤: 1.slidingMenu打开的时候调用BaiduMap的snapshot方法截图获取Bitmap对象; 2.使用
- mysql学习 mysql密码
- nginx配置反向代理
- 在VIM中配置Python编辑自动补齐
- SQL Server系列(10) -- 报表
- Object-C,对象和方法
- 笔试题