题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5583

一个01串,求修改一个位置,使得所有数均为0或1的子串长度的平方和最大。先分块,然后统计好原来的结果,每次更新块,更新最大值。

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cstring>
  5. #include <climits>
  6. #include <complex>
  7. #include <fstream>
  8. #include <cassert>
  9. #include <cstdio>
  10. #include <bitset>
  11. #include <vector>
  12. #include <deque>
  13. #include <queue>
  14. #include <stack>
  15. #include <ctime>
  16. #include <set>
  17. #include <map>
  18. #include <cmath>
  19.  
  20. using namespace std;
  21.  
  22. typedef long long ll;
  23. const int maxn = ;
  24. int n;
  25. char s[maxn];
  26. ll grid[maxn];
  27.  
  28. int main() {
  29. // freopen("in", "r", stdin);
  30. int T;
  31. scanf("%d", &T);
  32. for(int _ = ; _ <= T; _++) {
  33. scanf("%s", s);
  34. memset(grid, , sizeof(grid));
  35. ll cur = , ans = ;
  36. n = ; grid[n] = ;
  37. for(int i = ; s[i]; i++) s[i] == s[i-] ? grid[n]++ : grid[++n] = ;
  38. for(int i = ; i <= n; i++) cur += grid[i] * grid[i];
  39. if(n == ) ans = grid[] * grid[];
  40. else {
  41. for(int i = ; i <= n; i++) {
  42. if(grid[i] == ) ans = max(cur+*(grid[i-]*grid[i+]+grid[i-]+grid[i+]), ans);
  43. else {
  44. if(grid[i-] >= grid[i])
  45. ans = max(cur+*(grid[i-]-grid[i]+), ans);
  46. else
  47. ans = max(cur+*(grid[i]-grid[i-]+), ans);
  48. }
  49. }
  50. }
  51. printf("Case #%d: %I64d\n", _, ans);
  52. }
  53. return ;
  54. }

[HDOJ5583]Kingdom of Black and White(暴力)的更多相关文章

  1. hdu-5583 Kingdom of Black and White(数学,贪心,暴力)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5583 Kingdom of Black and White Time Limit: 2000/1000 ...

  2. HDU 5583 Kingdom of Black and White 水题

    Kingdom of Black and White Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showpr ...

  3. hdu 5583 Kingdom of Black and White(模拟,技巧)

    Problem Description In the Kingdom of Black and White (KBW), there are two kinds of frogs: black fro ...

  4. hdu 5583 Kingdom of Black and White

    Kingdom of Black and White Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  5. HDU5583 Kingdom of Black and White

    Kingdom of Black and White Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  6. HDU 5583 Kingdom of Black and White(暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=5583 题意: 给出一个01串,现在对这串进行分组,连续相同的就分为一组,如果该组内有x个数,那么就对答案贡献x* ...

  7. HDU-5583-Kingdom of Black and White(2015ACM/ICPC亚洲区上海站-重现赛)

    Kingdom of Black and White                                                                           ...

  8. 2015ACM/ICPC亚洲区上海站

    5573 Binary Tree(构造) 题意:给你一个二叉树,根节点为1,子节点为父节点的2倍和2倍+1,从根节点开始依次向下走k层,问如何走使得将路径上的数进行加减最终结果得到n. 联想到二进制. ...

  9. 「WC2018」即时战略

    「WC2018」即时战略 考虑对于一条链:直接随便找点,然后不断问即可. 对于一个二叉树,树高logn,直接随便找点,然后不断问即可. 正解: 先随便找到一个点,问出到1的路径 然后找别的点,考虑问出 ...

随机推荐

  1. Shiro workshop

    吃掉<Shiro教程>的精要部分,Go ahead!

  2. Hibernate exercise 54

    针对马士兵的Hibernate讲解第54讲的练习: 1) 学生.课程.分数的设计,并用Hibernate操作 在实际中,一般是先手动写SQL(可以优化)去创建表和关系,再设置Hibernate配置为u ...

  3. Nodejs Express 4.X 中文API 4--- Router篇

    相关阅读: Express 4.X API 翻译[一] --  Application篇 Express4.XApi 翻译[二] --  Request篇 Express4.XApi 翻译[三] -- ...

  4. DSP5509的时钟发生器(翻译总结自TI官方文档)

    一.C5509时钟发生器的两个功能 1.将从CLKIN引脚输入的时钟信号变换为适当频率的CPU时钟,提供给CPU.外设和其他模块使用: 2.将CPU时钟通过可编程分频器输出到CLKOUT引脚. 时钟发 ...

  5. 让IE支持max-width

    1:expression在FF下不支持 2:*html内的width不要带单位(px). 3:width:expression(eval(this.offsetWidth>1600?1600:t ...

  6. 【译】 沙箱中的间谍 - 可行的 JavaScript 高速缓存区攻击

    王龑 - MAY 27, 2015 原文连接 The Spy in the Sandbox – Practical Cache Attacks in Javascript 相关论文可在 https:/ ...

  7. Nutch配置:nutch-default.xml详解

    /×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...

  8. 连接池和 "Timeout expired"异常【转】

    异常信息: MySql.Data.MySqlClient.MySqlException (0x80004005): error connecting: Timeout expired. The tim ...

  9. 【zoj2562】反素数

    题意:给定一个数N,求小于等于N的所有数当中,约数最多的一个数,如果存在多个这样的数,输出其中最小的一个.(1 <= n <= 10^16) 题目:http://acm.hust.edu. ...

  10. 545B. Equidistant String

    题目链接 输入两个只含有01的字符串,s,t 求一个字符串p使到s,t的距离一样 这里的距离是指对应位置:0-0的距离是0 ,o-1的距离是1 ,1-1的距离是0,1-0的距离是1 这里只要求找出满足 ...