HDU 5583 Kingdom of Black and White
来源:互联网 发布:制作windows to go盘 编辑:程序博客网 时间:2024/06/10 15:04
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5583
题意:一个01序列的值为连续相同元素作为一个部分,各部分元素个数的平方之和。现在最多修改一个元素(0变1,1变0),求这个序列的最大值。
思路:因为最多只变一个元素,所以我们先统计出每个部分的元素个数,然后直接枚举取最大值即可,注意当一个部分只有一个元素时,会把三部分接起来。
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <cstdlib>#include <iostream>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <sstream>#include <queue>#include <utility>using namespace std;#define rep(i,j,k) for (int i=j;i<=k;i++)#define Rrep(i,j,k) for (int i=j;i>=k;i--)#define Clean(x,y) memset(x,y,sizeof(x))#define LL long long#define ULL unsigned long long#define inf 0x7fffffff#define mod 100000007const int maxn = 100009;int T;char str[maxn];LL f[maxn];LL num[maxn];LL solve(){ LL sum = 0; LL ans = 0; LL n; Clean(f,0); int len = strlen(str); int cnt = 0; char last = str[0]; n = 1; rep(i,1,len-1) { if ( str[i] != last ) { cnt++; f[cnt] = n * n; num[cnt] = n; sum+=f[cnt]; n = 1; last = str[i]; } else n++; } cnt++; num[cnt] = n; f[cnt] = n * n; sum+=f[cnt]; ans = sum; rep(i,1,cnt-1) { if ( num[i] == 1 && i != 1 ) ans = max( ans , sum - f[i] - f[i-1] - f[i+1] + ( num[i+1] + num[i-1] + 1 )*( num[i+1] + num[i-1] + 1 ) ); else ans = max( ans , sum-f[i]-f[i+1] + max( (num[i]+1)*(num[i]+1) + (num[i+1]-1)*(num[i+1]-1) , (num[i]-1)*(num[i]-1) + (num[i+1]+1)*(num[i+1]+1) ) ); } return ans;}int main(){ cin>>T; getchar(); rep(kase,1,T) { gets(str); printf("Case #%d: %I64d\n",kase,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 [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 5583 Kingdom of Black and White(模拟)
- HDU 5583 Kingdom of Black and White 暴力
- 公开论文集
- Mysql数据库常用语句
- velocity判断字段为空
- IO流-字符编码表转换示例
- 架构:Android的MVP
- HDU 5583 Kingdom of Black and White
- oracle 开启supplemental logging
- iOS模拟器中图片在mac电脑中的实际位置
- 这些年,安放在我工位上的那些书
- ognl表达式
- centos 6.5 可选的类型说明如下
- ceil函数和floor函数的用法
- shell脚本实现轮询查看进程是否结束
- Android Scroller简单用法