当前位置: 首页 > news >正文

珠海市网站建设的公司爱站网关键词长尾挖掘

珠海市网站建设的公司,爱站网关键词长尾挖掘,韩国做 mp4下载网站,合肥软件公司20强排行榜CF1692E Binary Deque 题解题目链接字面描述题面翻译题目描述输入格式输出格式样例 #1样例输入 #1样例输出 #1提示思路代码实现题目 链接 https://www.luogu.com.cn/problem/CF1692E 字面描述 题面翻译 有多组数据。 每组数据给出 nnn 个数,每个数为 000 或 1…

CF1692E Binary Deque 题解

  • 题目
    • 链接
    • 字面描述
      • 题面翻译
      • 题目描述
      • 输入格式
      • 输出格式
      • 样例 #1
        • 样例输入 #1
        • 样例输出 #1
      • 提示
  • 思路
  • 代码实现

题目

链接

https://www.luogu.com.cn/problem/CF1692E

字面描述

题面翻译

有多组数据。

每组数据给出 nnn 个数,每个数为 000111 。你可以选择从两边删数,求至少删几个数才可以使剩下的数总和为 sss

如果不能达到 sss ,则输出 −1-11

题目描述

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 $ .

思路

因为题目要求为从原数组2边删最少元素,使得总元素和为sss,

设初始数组元素总和为cntcntcnt

cnt<scnt<scnt<s,直接输出−1-11

否则程序继续

这道题十分友善
∵允许从数组2边删数\because允许从数组2边删数允许从数组2边删数
∴最终序列内部顺序相比于原数组不变\therefore 最终序列内部顺序相比于原数组不变最终序列内部顺序相比于原数组不变

一个很好思考的想法油然而生,

  1. 对原数组进行前缀和预处理
  2. 枚举i=[1,n]i=[1,n]i=[1,n]
    • 二分第一个f[op]−f[i]≥sf[op]-f[i]≥sf[op]f[i]sopopop
    • 算位数取最小值

时间复杂度: ο(n⋅log2(n))\omicron (n·log2(n))ο(nlog2(n))

代码实现

#include<bits/stdc++.h>
using namespace std;const int maxn=2e5+10;
const int inf=2e9;
int t,n,op,x,ans=inf;
int f[maxn];
int main(){scanf("%d",&t);while(t--){ans=inf;scanf("%d%d",&n,&op);for(int i=1;i<=n;i++){scanf("%d",&x);f[i]=f[i-1]+x;}if(f[n]<op){printf("-1\n");continue; }for(int i=1;i<=n;i++){if(f[i]<op)continue;int p=lower_bound(f,f+n+1,f[i]-op)-f;ans=min(ans,p+n-i);}printf("%d\n",ans);}return 0;
}
http://www.hkea.cn/news/562602/

相关文章:

  • 中国网网址是多少网站推广优化教程
  • 关于加强机关网站建设运营培训
  • dw做的网站怎么让别人看到如何建立一个网站
  • 保险网站建设优缺点seo代码优化步骤
  • 如何快速建网站百度电脑版入口
  • 山东省建设工程信息网站最近最新的新闻
  • 免费网站建设方案锦绣大地seo官网
  • 电子商务的网站建设牛排seo系统
  • 资源收费网站怎么做网站快速优化排名官网
  • 招标网哪个网站信息可靠百度站长工具网站
  • 郑州七七网站建设互联网推广公司
  • 佛山做外贸网站代理商百度收录技术
  • 公司网站建设需要什么今日热搜第一名
  • 烟台建设企业网站网站快速收录入口
  • 怎么做繁体字网站网络营销公司注册找哪家
  • 做ppt介绍网站吗网站搜索引擎优化工具
  • 深圳网站建设有没有市场百度搜索推广的五大优势
  • 网站建设好的图片百度互联网营销
  • 柳州网站制作公司seo优化什么意思
  • 网站建设做的好的公司淘宝关键词优化怎么弄
  • 手机网站用模版方象科技的企业愿景
  • 沈阳网站建设技术公司排名公司市场营销策划方案
  • 赣州网站建设怎样石家庄最新消息
  • 公司注册地址和经营地址不一致可以吗长春seo招聘
  • 好的做问卷调查的网站好推广有奖励的app平台
  • 有专业设计网站吗百度指数与百度搜索量
  • 网站的整体结构百度云网盘资源搜索引擎入口
  • 咸阳网站建设哪家专业杭州优化公司在线留言
  • 地板网站建设门户网站
  • 新增备案网站负责人人工智能培训心得体会