题目大意:

希望在 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 ;

 #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define ll long long
const int N = ;
char s[N];
int k , pos[N] ;
ll sum[N]; bool ok(int st , int len)
{
int la = st + len - ;
int m = (st + la) >> ;
ll ans = ;
ans += (ll)pos[m]*(m-st+) - (sum[m] - sum[st-])- (ll)(m-st+)*(m-st)/ ;
ans += (sum[la] - sum[m-]) - (ll)pos[m]*(la-m+) - (ll)(la-m+)*(la-m)/ ;
if(ans > k) return false;
return true;
} int main()
{
// freopen("a.in" , "r" , stdin);
int T;
scanf("%d" , &T);
while(T--){
scanf("%s%d" , s+ , &k);
int len = strlen(s+) , t = ;
for(int i = ; i<=len ; i++){
if(s[i] == '') pos[++t] = i;
}
for(int i = ; i<=t ; i++)
sum[i] = sum[i-] + pos[i];
int maxnLen = , st = ;
while(st + maxnLen - <= t){
if(ok(st , maxnLen)){
// cout<<"here: "<<t<<" "<<st<<" "<<maxnLen<<endl;
maxnLen ++;
}
else st++;
}
printf("%d\n" , maxnLen-);
}
return ;
}

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. bzoj 1652: [Usaco2006 Feb]Treats for the Cows【区间dp】

    裸的区间dp,设f[i][j]为区间(i,j)的答案,转移是f[i][j]=max(f[i+1][j]+a[i](n-j+i),f[i][j-1]+a[j]*(n-j+i)); #include< ...

  2. 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 The Heaviest Non-decreasing Subsequence Problem

    Let SS be a sequence of integers s_{1}s​1​​, s_{2}s​2​​, ......, s_{n}s​n​​Each integer is is associ ...

  3. ACM_抢糖果

    抢糖果 Time Limit: 2000/1000ms (Java/Others) Problem Description: 今天计实班的生活委员心情大好,在永诚超市狂购了好多好多糖果,好开心~o(∩ ...

  4. Hadoop Hive概念学习系列之hive的正则表达式初步(六)

    说在前面的话 hive的正则表达式,是非常重要!作为大数据开发人员,用好hive,正则表达式,是必须品! Hive中的正则表达式还是很强大的.数据工作者平时也离不开正则表达式.对此,特意做了个hive ...

  5. Spark学习笔记1:Application,Driver,Job,Task,Stage理解

    看了spark的原始论文和相关资料,对spark中的一些经常用到的术语做了一些梳理,记录下. 1,Application application(应用)其实就是用spark-submit提交的程序.比 ...

  6. CF804B Minimum number of steps

    思路: 找规律.参考了http://blog.csdn.net/harlow_cheng/article/details/71190999. 实现: #include <iostream> ...

  7. JavaScript(十一)Dom

    Dom(Document object module) 1.获取dom对象的方法 正常用的方法 推荐 getElementById()//通过id选择唯一的dom getElementsByClass ...

  8. Java图片上查找图片算法

    之前用按键精灵写过一些游戏辅助,里面有个函数叫FindPic,就是在屏幕范围查找给定的一张图片,返回查找到的坐标位置. 现在,Java来实现这个函数类似的功能. 算法描述: 屏幕截图,得到图A,(查找 ...

  9. error C2143: syntax error : missing ';' before '}'

    今天弄Tab控件,干了一件非常愚蠢的事,没有去声明头文件.这也是今天要记录的问题,提示如下各种 前面一个符号是错误的.如果初学者遇到,算作一个提示,记得声明新类的.h 头文件 标签空间再进一步.cpp ...

  10. (转)淘淘商城系列——导入商品数据到索引库——Service层

    http://blog.csdn.net/yerenyuan_pku/article/details/72894187 通过上文的学习,我相信大家已经学会了如何使用Solrj来操作索引库.本文我们将把 ...