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 ;

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 #define ll long long
 6 const int  N = 100005;
 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 - 1;
14     int m = (st + la) >> 1;
15     ll ans = 0;
16     ans += (ll)pos[m]*(m-st+1) - (sum[m] - sum[st-1])- (ll)(m-st+1)*(m-st)/2 ;
17     ans += (sum[la] - sum[m-1]) - (ll)pos[m]*(la-m+1) - (ll)(la-m+1)*(la-m)/2 ;
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+1 , &k);
29         int len = strlen(s+1) , t = 0;
30         for(int i = 1 ; i<=len ; i++){
31             if(s[i] == ‘1‘) pos[++t] = i;
32         }
33         for(int i = 1 ; i<=t ; i++)
34             sum[i] = sum[i-1] + pos[i];
35         int maxnLen = 1 , st = 1;
36         while(st + maxnLen - 1 <= 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-1);
44     }
45     return 0;
46 }
时间: 2024-10-06 21:13:33

COJ 1411 Longest Consecutive Ones的相关文章

CSU 1411: Longest Consecutive Ones

关键是通过预处理,使每个区间内的1移动到一起的最少步骤可以在O(1)时间算出来.... 1411: Longest Consecutive Ones Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 185  Solved: 40 [Submit][Status][Web Board] Description Given a string consists of 0 and 1, you can swap any two adjacent digit

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

128. Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run i

LeetCode128 Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. (Hard) For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should

Binary Tree Longest Consecutive Sequence

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p

LeetCode: Longest Consecutive Sequence [128]

[题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should

[LeetCode]Longest Consecutive Sequence

题目描述:(链接) Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm shou

Longest Consecutive Sequence 数组连续数字的情况

Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4

【Longest Consecutive Sequence】cpp

题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run