14.Binary Deque

题面翻译

Binary Deque - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

有多组数据。

每组数据给出 \(n\) 个数,每个数为 \(0\) 或 \(1\) 。你可以选择从两边删数,求至少删几个数才可以使剩下的数总和为 \(s\) 。

如果不能达到 \(s\) ,则输出 \(-1\) 。

题目描述

Slavic has an array of length $ n $ consisting only of zeroes and ones. In one operation, he removes either the first or the last element of the array.

What is the minimum number of operations Slavic has to perform such that the total sum of the array is equal to $ s $ after performing all the operations? In case the sum $ s $ can't be obtained after any amount of operations, you should output -1.

输入格式

The first line contains a single integer $ t $ ( $ 1 \leq t \leq 10^4 $ ) — the number of test cases.

The first line of each test case contains two integers $ n $ and $ s $ ( $ 1 \leq n, s \leq 2 \cdot 10^5 $ ) — the length of the array and the needed sum of elements.

The second line of each test case contains $ n $ integers $ a_i $ ( $ 0 \leq a_i \leq 1 $ ) — the elements of the array.

It is guaranteed that the sum of $ n $ over all test cases doesn't exceed $ 2 \cdot 10^5 $ .

输出格式

For each test case, output a single integer — the minimum amount of operations required to have the total sum of the array equal to $ s $ , or -1 if obtaining an array with sum $ s $ isn't possible.

样例 #1

样例输入 #1

7
3 1
1 0 0
3 1
1 1 0
9 3
0 1 0 1 1 1 0 0 1
6 4
1 1 1 1 1 1
5 1
0 0 1 1 0
16 2
1 1 0 0 1 0 0 1 1 0 0 0 0 0 1 1
6 3
1 0 1 0 0 0

样例输出 #1

0
1
3
2
2
7
-1

提示

In the first test case, the sum of the whole array is $ 1 $ from the beginning, so we don't have to make any operations.

In the second test case, the sum of the array is $ 2 $ and we want it to be equal to $ 1 $ , so we should remove the first element. The array turns into $ [1, 0] $ , which has a sum equal to $ 1 $ .

In the third test case, the sum of the array is $ 5 $ and we need it to be $ 3 $ . We can obtain such a sum by removing the first two elements and the last element, doing a total of three operations. The array turns into $ [0, 1, 1, 1, 0, 0] $ , which has a sum equal to $ 3 $ .

思路:

本题可以使用双指针来动态获取最优解,我们可以先计算出所有元素之和sum,若sum小于s,则不可能达到目标,输出-1并接着处理下一个样例,若s==sum,则正好操作次数为0,可以输出0并直接返回。否则,我们使用双指针的思路,此处为快慢指针,我们维护一个动态区间。

  1. 若这个动态区间内的元素之和sum=s,则我们此时更新答案,并让快指针向右走
  2. 若这个动态区间内的元素之和 sum<s,则我们需要扩大区间范围,此时让快指针r接着向右走
  3. 若这个动态区间内的元素之和sum>s,则我们需要缩小区间范围,找到最优的答案。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int n,s;
int a[N];
void solve(){
int n,s;
cin>>n>>s;
int sum=0;
//先计算所有的元素之和,若所有元素之和还小于s,则不可能达到目标,若刚好等于s,则操作次数为0次。
for(int i=1;i<=n;i++){
cin>>a[i];
sum+=a[i];
}
if(sum<s){
cout<<-1<<"\n";
return;
}
if(sum==s){
cout<<0<<'\n';
return;
}
sum=a[1];
int l=1,r=1,ans=1e10;
while(r<=n&&l<=r){
//若当前区间的和等于s,则此时更新答案,并且让快指针接着往后走
if(sum==s){
//当前所需要删除的数的个数为:总个数n-区间的长度。
ans=min(ans,n-(r-l+1));
r++;
sum+=a[r];
}
//若当前区间的和小于s,则让快指针向右走,扩大区间的范围。
if(sum<s){
r++;
sum+=a[r];
}
//若当前区间的和大于s,则让慢指针向右走,减少区间的长度,看看能不能达到最优的答案,
//注意这里是先减去a[l],再让l++,与上面不同。
if(sum>s){
sum-=a[l];
l++;
}
}
cout<<ans<<'\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int t=1;cin>>t;
while(t--) solve();
return 0;
}

双指针习题:Binary Deque的更多相关文章

  1. 《算法导论》习题2.3-5 二分搜索 Binary Search

    地球人都知道“二分查找”,方法也非常简单,但是你能不能在10分钟内写出一个没有bug的程序呢? 知易行难,自己动手写一下试一试吧. public class BinarySearch { public ...

  2. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  3. [LeetCode] Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  4. LeetCode——Serialize and Deserialize Binary Tree

    Description: Serialization is the process of converting a data structure or object into a sequence o ...

  5. Serialize and Deserialize Binary Tree

    Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...

  6. 【转】STL之二分查找 (Binary search in STL)

    Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第4 ...

  7. LeetCode Binary Tree Right Side View (DFS/BFS)

    题意: 给一棵二叉树,要求收集每层的最后一个节点的值.按从顶到底装进vector返回. 思路: BFS比较简单,先遍历右孩子就行了. /** * Definition for a binary tre ...

  8. leetcode 144. Binary Tree Preorder Traversal ----- java

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  9. leetcode 103 Binary Tree Zigzag Level Order Traversal ----- java

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  10. 【Binary Tree Zigzag Level Order Traversal】cpp

    题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from lef ...

随机推荐

  1. Qt Creator pro文件常见配置

    HEADERS:指定项目的头文件(.h) SOURCES:指定项目的源文件(.cpp) FORMS:指定协议UIC处理的由Qt Designer生成的.ui文件 RESOURCES:指定需要rcc处理 ...

  2. 3.7 Linux切换目录(cd命令)

    cd 命令,是 Change Directory 的缩写,用来切换工作目录. Linux 命令按照来源方式,可分为两种,分别是 Shell 内置命令和外部命令.所谓 Shell 内置命令,就是 She ...

  3. 两台笔记本电脑实现同一wifi下虚拟主机网络实现互通

    一台win笔记本 (安装vmware) 一台macbookpro 本人考虑到M1的macbook,无法安装vmware,这让我这个linux运维人员很是dan疼,没办法只能在自己的win笔记本上安装v ...

  4. Docker制作私有镜像仓库

    构建私有仓库 启动Docker Registry,使用Docker官方提供的Registry镜像就可以搭建本地私有镜像仓库,具体指令如下. docker run -d -p 5000:5000 --r ...

  5. python基础之__init__.py

    如何使用 在 Python 中,当一个目录被作为包来使用时,它会在包中寻找一个名为 __init__.py 的文件.如果该文件存在,Python 会将它加载到内存中,并在其中执行所有的代码. __in ...

  6. QT 6.8 安卓 Android 环境安装配置,你踩了几个坑,我教你跳出来,早看不入坑… …

    安装了QT6.8 最新版本,在线安装,用了数天后,想开始写一个Android程序,发现还在配置环境才可以继续,于是就开始配置: 菜单:编辑 -->preferences-->设备--> ...

  7. IPC-7711/7721D-中文版 CN 2024 电子组件的返工、修改和维修标准

    IPC-7711D-7721D-中文版 CN 2024 电子组件的返工.修改和维修标准 链接:https://pan.baidu.com/s/1r5dm9vsj4-Oj2Jw0HgeTHw?pwd=1 ...

  8. 三剑客-grep-awk-sed

    三剑客-grep-awk-sed grep 格式: grep 参数 过滤文件内容 文件名称 cat file|grep '过滤的内容' ​ 参数: -v 取反 ​ -E 支持扩展正则 | 或者 egr ...

  9. 《花100块做个摸鱼小网站! 》第十篇—响应式布局适配PC端和移动端

    ️基础链接导航️ 服务器 → ️ 阿里云活动地址 看样例 → 摸鱼小网站地址 学代码 → 源码库地址 一.前言 大家好呀,我是summo,小网站一直有个问题,就是PC端的样式和移动端的样式是两套,并且 ...

  10. The Bento Box Adventure

    题目来源:codeforces 2041A 题目名称:The Bento Box Adventure 题目链接:https://codeforces.com/contest/2041/problem/ ...