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 ...
随机推荐
- Tomcat的jvm配置
Tomcat本身不能直接在计算机上运行,需要依赖于操作系统和一个JAVA虚拟机.Tomcat的内存溢出本质就是JVM内存溢出,JAVA程序启动时JVM会分配一个初始内存和最大内存给程序.当程序需要的内 ...
- 清北考前刷题day1早安
立方数(cubic) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方数 ...
- 赋予option元素点击事件后,点击select时却触发了option事件。如何解决?
将select的优先级提到option之前就可以了. 方法:为select元素添加position:relative: <select class="adt" name=&q ...
- 慕课网4-6 编程练习:jQuery后排兄弟选择器
4-6 编程练习 结合所学的兄弟选择器" ~ ",实现如下图所示效果: 任务 (1)使用兄弟选择器" ~ "将技术语言的背景色变成红色 (2)使用jQuery的 ...
- maptalks 如何加载 ArcGIS 瓦片图层
最近需要加载 ArcGIS 瓦片图层,运行官网加载 ArcGIS 瓦片图层的 demo 是没有问题的.如果把 ArcGIS 瓦片图层 URL 换成是自已发布的 ArcGIS 地图服务,发现加载不出来, ...
- HDU 4691 后缀数组+RMQ
思路: 求一发后缀数组,求个LCP 就好了 注意数字有可能不只一位 (样例2) //By SiriusRen #include <bits/stdc++.h> using namespac ...
- 6.12---bug
- 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 ...
- [ USACO 2017 FEB ] Why Did the Cow Cross the Road III (Gold)
\(\\\) \(Description\) 给定长度为\(2N\)的序列,\(1\text ~N\)各出现过\(2\)次,\(i\)第一次出现位置记为\(a_i\),第二次记为\(b_i\),求满足 ...
- 对比hive和mysql 复杂逻辑流处理
1.Mysql中可用存储过程和函数来实现复杂逻辑处理,两者的对比如下:存储过程作为可执行文件,编译一次放在数据库中,函数又返回值.可设定使用权限. 存储过程中可使用游标,声明变量.用call调用. ...