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 ...
随机推荐
- linux下导入oracle数据表
提前说明:这个是默认oracle已经安装好切数据库默认表空间已经创建好.之后将数据表dmp文件直接导入到默认表空间里(默认表空间不用再指定,因为创建数据库时已经指定默认表空间) linux命令如下: ...
- MySQL-ProxySQL中间件(二)| Admin Schemas介绍
目录 MySQL-ProxySQL中间件(一)| ProxySQL基本概念: https://www.cnblogs.com/SQLServer2012/p/10972593.html ...
- C#自动内存分配
一.在程序运行时由CLR管理内存分配(Memory Allocation),程序启动时,操作系统会为每一个线程申请一个独立的栈内存,用于存储方法的局部变量.参数和返回值等:CLR会为进程申请一个连续的 ...
- Linux文件详解
一.Linux文件类型分:普通文件.目录文件.链接文件.设备文件.管道文件. 1.普通文件:由ls -al显示属性时,第一个属性为 [-],例如 [-rwxrwxrwx].包括: 纯文本文件(ASCI ...
- [NOI2003]Editor
Description 很久很久以前,DOS3.x的程序员们开始对 EDLINEDLIN 感到厌倦. 于是,人们开始纷纷改用自己写的文本编辑器?? 多年之后,出于偶然的机会,小明找到了当时的一个编辑软 ...
- 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe
题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...
- php循环结构
1.while循环 先判断条件,如果条件成立则执行循环的代码 嵌套循环,需要先把嵌套在内的循环执行完毕再执行外面的循环 While(条件语句){......} //如果()条件成立,执行{}里面的语句 ...
- FCC 基础JavaScript 练习5
在赌场21点游戏中,玩家可以通过计算牌桌上已经发放的卡牌的高低值来让自己在游戏中保持优势,这就叫21点算法. 根据下面的表格,每张卡牌都分配了一个值.如果卡牌的值大于0,那么玩家应该追加赌注.反之,追 ...
- 鼠标拖拽移动Java界面组件
默认的,Frame或者JFrame自身已经实现了鼠标拖拽标题栏移动窗口的功能. 只是,当你不满意java的JFrame样式,隐藏了标题栏和边框,又或者干脆直接使用JWindow,那你又该怎么实现鼠标拖 ...
- GEO/SRA数据库
GEO数据库 GEO数据库隶属于NCBI,是最大最全面的基因表达数据库,主要是芯片和转录组测序数据.除储存数据外,也提供一些数据挖掘工具,因此利用好这个数据库,没有实验,没有自己的数据也能发好文章! ...