题目大意:

希望在 k 步之内,将尽可能多的1移到相邻的位置上

这里依靠前缀和解决问题

我们用pos[i]保存第i个1的位置,这里位置我以1开始

用sum[i]保存前 i 个1从 0 点移到当前位置所需的步数

每次进行判断能否将 st 号 到 la 号的1移到相邻位置,我们要先清楚,为了使移动步数最少,我们需要固定中间的数保持它的位置不动,将两边的数向它靠拢

那么移动的步数就分为左右两侧

中间的数编号为 m = (st + la)>> 1

首先将左侧移到中间,将 m 也作为其中的一部分,我们先将这 (m-st+1)个数均移到 pos[m]的位置上,而原本已经移好了 sum[m] - sum[st-1]个位置

因为是相邻,所以要把都在pos[m]上的位置一个个左移,分别左移 0 , 1 , 2 。。。。到(m-st)

所以左半部分为 (ll)pos[m]*(m-st+1) - (sum[m] - sum[st-1])- (ll)(m-st+1)*(m-st)/2 ;

右半部分同样道理,但是这回是因为其本身所在位置更大,所以是

(sum[la] - sum[m-1]) - (ll)pos[m]*(la-m+1) - (ll)(la-m+1)*(la-m)/2 ;

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. using namespace std;
  5. #define ll long long
  6. const int N = ;
  7. char s[N];
  8. int k , pos[N] ;
  9. ll sum[N];
  10.  
  11. bool ok(int st , int len)
  12. {
  13. int la = st + len - ;
  14. int m = (st + la) >> ;
  15. ll ans = ;
  16. ans += (ll)pos[m]*(m-st+) - (sum[m] - sum[st-])- (ll)(m-st+)*(m-st)/ ;
  17. ans += (sum[la] - sum[m-]) - (ll)pos[m]*(la-m+) - (ll)(la-m+)*(la-m)/ ;
  18. if(ans > k) return false;
  19. return true;
  20. }
  21.  
  22. int main()
  23. {
  24. // freopen("a.in" , "r" , stdin);
  25. int T;
  26. scanf("%d" , &T);
  27. while(T--){
  28. scanf("%s%d" , s+ , &k);
  29. int len = strlen(s+) , t = ;
  30. for(int i = ; i<=len ; i++){
  31. if(s[i] == '') pos[++t] = i;
  32. }
  33. for(int i = ; i<=t ; i++)
  34. sum[i] = sum[i-] + pos[i];
  35. int maxnLen = , st = ;
  36. while(st + maxnLen - <= t){
  37. if(ok(st , maxnLen)){
  38. // cout<<"here: "<<t<<" "<<st<<" "<<maxnLen<<endl;
  39. maxnLen ++;
  40. }
  41. else st++;
  42. }
  43. printf("%d\n" , maxnLen-);
  44. }
  45. return ;
  46. }

COJ 1411 Longest Consecutive Ones的更多相关文章

  1. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  2. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  3. Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...

  4. 【leetcode】Longest Consecutive Sequence(hard)☆

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  5. 128. Longest Consecutive Sequence(leetcode)

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  6. [LeetCode] Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  7. [LintCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. H ...

  8. LeetCode Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  9. 24. Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

随机推荐

  1. Tomcat的jvm配置

    Tomcat本身不能直接在计算机上运行,需要依赖于操作系统和一个JAVA虚拟机.Tomcat的内存溢出本质就是JVM内存溢出,JAVA程序启动时JVM会分配一个初始内存和最大内存给程序.当程序需要的内 ...

  2. 清北考前刷题day1早安

    立方数(cubic) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方数 ...

  3. 赋予option元素点击事件后,点击select时却触发了option事件。如何解决?

    将select的优先级提到option之前就可以了. 方法:为select元素添加position:relative: <select class="adt" name=&q ...

  4. 慕课网4-6 编程练习:jQuery后排兄弟选择器

    4-6 编程练习 结合所学的兄弟选择器" ~ ",实现如下图所示效果: 任务 (1)使用兄弟选择器" ~ "将技术语言的背景色变成红色 (2)使用jQuery的 ...

  5. maptalks 如何加载 ArcGIS 瓦片图层

    最近需要加载 ArcGIS 瓦片图层,运行官网加载 ArcGIS 瓦片图层的 demo 是没有问题的.如果把 ArcGIS 瓦片图层 URL 换成是自已发布的 ArcGIS 地图服务,发现加载不出来, ...

  6. HDU 4691 后缀数组+RMQ

    思路: 求一发后缀数组,求个LCP 就好了 注意数字有可能不只一位 (样例2) //By SiriusRen #include <bits/stdc++.h> using namespac ...

  7. 6.12---bug

  8. Python,报错NameError: name 'math' is not defined

    1 #-*- coding : utf-8 -*- 2 import math 3 4 def move(x, y, step, angle=0): 5 nx = x + step * math.co ...

  9. [ USACO 2017 FEB ] Why Did the Cow Cross the Road III (Gold)

    \(\\\) \(Description\) 给定长度为\(2N\)的序列,\(1\text ~N\)各出现过\(2\)次,\(i\)第一次出现位置记为\(a_i\),第二次记为\(b_i\),求满足 ...

  10. 对比hive和mysql 复杂逻辑流处理

      1.Mysql中可用存储过程和函数来实现复杂逻辑处理,两者的对比如下:存储过程作为可执行文件,编译一次放在数据库中,函数又返回值.可设定使用权限. 存储过程中可使用游标,声明变量.用call调用. ...