COJ 1411 Longest Consecutive Ones
题目大意:
希望在 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的更多相关文章
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...
- 【leetcode】Longest Consecutive Sequence(hard)☆
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LintCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. H ...
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- 24. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
随机推荐
- J20180116
用度 总务 用度係 总务科(管理办公用品等) 型紙 设计图 プラント 工厂
- bzoj 3743: [Coci2015]Kamp【树形dp】
两遍dfs一遍向下,一边向上,分别记录子树内人数a,当前点到所有点的距离b,最大值c和次大值d,最大值子树p 然后答案是2b-c #include<iostream> #include&l ...
- bzoj 1594: [Usaco2008 Jan]猜数游戏【二分+线段树】
写错一个符号多调一小时系列-- 二分答案,然后判断这个二分区间是否合法: 先按值从大到小排序,然后对于值相同的一些区间,如果没有交集则不合法:否则把并集在线段树上打上标记,然后值小于这个值的区间们,如 ...
- (数论)51NOD 1079 中国剩余定理
一个正整数K,给出K Mod 一些质数的结果,求符合条件的最小的K.例如,K % 2 = 1, K % 3 = 2, K % 5 = 3.符合条件的最小的K = 23. Input 第1行:1个数 ...
- Eclipse/STS 在线安装阿里java代码规约插件
1.打开Idea的在线安装插件界面,通过“Help”-->“Install New Software...” 进入 2. 在 “Work with” 栏输入插件包的下载地址:https://p3 ...
- P2973 [USACO10HOL]赶小猪
跟那个某省省选题(具体忘了)游走差不多... 把边搞到点上然后按套路Gauss即可 貌似有人说卡精度,$eps≤1e-13$,然而我$1e-12$也可以过... 代码: #include<cst ...
- Python之列表生成式、生成器
列表生成式 ——可以快速生成list,可以通过一个list推导出另一个list,而代码却十分简洁: >>> [x * x for x in range(1, 11)] [1, 4, ...
- Ajax 知识点总结
1.AJAX的优缺点都有什么? 最大的一点是页面无刷新,用户的体验非常好.使用异步方式与服务器通信,具有更加迅速的响应能力.可以把以前一些服务器负担的工作转嫁到客户端,利用客户端闲置的能力来处理,减轻 ...
- 【IIS7.5】Asp文件上传限制,加载页面大小限制
运行环境 window server 2008 R2 X64.IIS7.5.应用程序池.Net4.0 经典模式 分析 IIS7.5默认有两处上传限制: 第一处在,选择左侧的站点,然后找到后侧的管理—— ...
- maven idea
写在前面的话:此篇文章教程是在IntelliJ IDEA中搭建的maven项目.(建议eclipse党快点转IDEA吧,IDEA大法好.逃… 1.maven的安装 前往Apache Maven官网点击 ...