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

浅谈用二分和三分法解决问题(c++)

目录

  • 问题引入
    • [NOIP2001 提高组] 一元三次方程求解
      • 题目描述
      • 输入格式
      • 输出格式
      • 样例 #1
        • 样例输入 #1
        • 样例输出 #1
      • 提示
      • 思路分析
      • AC代码
  • 思考
  • 关于二分和三分
    • 例题讲解
      • 进击的奶牛
        • 题目描述
        • 输入格式
        • 输出格式
        • 样例 #1
          • 样例输入 #1
          • 样例输出 #1
        • 思路
        • AC代码
      • 平均数
        • 题目描述
        • 输入格式
        • 输出格式
        • 样例 #1
          • 样例输入 #1
          • 样例输出 #1
        • 提示
            • 数据规模与约定
      • AC代码
      • Dropping Test
        • 题目描述
        • 输入格式
        • 输出格式
        • 样例 #1
          • 样例输入 #1
          • 样例输出 #1
        • 提示
      • 【模板】三分 | 函数
        • 题目描述
        • 输入格式
        • 输出格式
        • 样例 #1
          • 样例输入 #1
          • 样例输出 #1
        • 提示
      • AC代码
      • Doremy's IQ
        • 题面翻译
        • 题目描述
        • 输入格式
        • 输出格式
        • 样例说明
        • 题目描述
        • 输入格式
        • 输出格式
        • 样例 #1
          • 样例输入 #1
          • 样例输出 #1
        • 提示
      • AC代码
      • Empty Graph
        • 题面翻译
        • 题目描述
        • 输入格式
        • 输出格式
        • 样例 #1
          • 样例输入 #1
          • 样例输出 #1
        • 提示
      • AC代码

问题引入

首先我们来看一下这样一个问题

[NOIP2001 提高组] 一元三次方程求解

题目描述

有形如: a x 3 + b x 2 + c x + d = 0 a x^3 + b x^2 + c x + d = 0 ax3+bx2+cx+d=0 这样的一个一元三次方程。给出该方程中各项的系数( a , b , c , d a,b,c,d a,b,c,d 均为实数),并约定该方程存在三个不同实根(根的范围在 − 100 -100 100 100 100 100 之间),且根与根之差的绝对值 ≥ 1 \ge 1 1。要求由小到大依次在同一行输出这三个实根(根与根之间留有空格),并精确到小数点后 2 2 2 位。

提示:记方程 f ( x ) = 0 f(x) = 0 f(x)=0,若存在 2 2 2 个数 x 1 x_1 x1 x 2 x_2 x2,且 x 1 < x 2 x_1 < x_2 x1<x2 f ( x 1 ) × f ( x 2 ) < 0 f(x_1) \times f(x_2) < 0 f(x1)×f(x2)<0,则在 ( x 1 , x 2 ) (x_1, x_2) (x1,x2) 之间一定有一个根。

输入格式

一行, 4 4 4 个实数 a , b , c , d a, b, c, d a,b,c,d

输出格式

一行, 3 3 3 个实根,从小到大输出,并精确到小数点后 2 2 2 位。

样例 #1

样例输入 #1
1 -5 -4 20
样例输出 #1
-2.00 2.00 5.00

提示

【题目来源】

NOIP 2001 提高组第一题

思路分析

这道题的数据范围相当之小,用暴力就能过

AC代码

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
double a,b,c,d;
inline void check(double i){double j=i+0.001;double y1=a*i*i*i+b*i*i+c*i+d;double y2=a*j*j*j+b*j*j+c*j+d;if(y1>=0&&y2<=0||y1<=0&&y2>=0)printf("%.2lf ",(i+j)/2);
}
int  main() {scanf("%lf%lf%lf%lf",&a,&b,&c,&d);for(double i=-100;i<=100;i+=0.001)check(i);return 0;
}

思考

如果这道题的解空间再开打一下,开到1000,10000,那么暴力就一定过不去了

这时候就需要我们的二分法闪亮登场了

关于二分和三分

二分在下之前写过一篇博客讲解

戳这里

二分解决的问题都有一个共同的性质:单调性

而如果某个问题的解空间是单峰的,不管是向外凸还是向内凹,都可以用另一种算法解决,三分

顾名思义,三分就是一种把解空间分成三段的算法,答案一定在某个段内,时间是 l o g 3 ( n ) log_3(n) log3(n)但也是 O ( l o g ( n ) ) O(log(n)) O(log(n))的算法

简单来说,二分解决零点问题,三分解决极值问题

例题讲解

进击的奶牛

题目描述

Farmer John 建造了一个有 N N N 2 ≤ N ≤ 1 0 5 2 \leq N \leq 10 ^ 5 2N105) 个隔间的牛棚,这些隔间分布在一条直线上,坐标是 x 1 , x 2 , ⋯ , x N x _ 1, x _ 2, \cdots, x _ N x1,x2,,xN 0 ≤ x i ≤ 1 0 9 0 \leq x _ i \leq 10 ^ 9 0xi109)。

他的 C C C 2 ≤ C ≤ N 2 \leq C \leq N 2CN)头牛不满于隔间的位置分布,它们为牛棚里其他的牛的存在而愤怒。为了防止牛之间的互相打斗,Farmer John 想把这些牛安置在指定的隔间,所有牛中相邻两头的最近距离越大越好。那么,这个最大的最近距离是多少呢?

输入格式

1 1 1 行:两个用空格隔开的数字 N N N C C C

2 ∼ N + 1 2 \sim N+1 2N+1 行:每行一个整数,表示每个隔间的坐标。

输出格式

输出只有一行,即相邻两头牛最大的最近距离。

样例 #1
样例输入 #1
5 3
1
2
8
4
9
样例输出 #1
3
思路

二分每一种可能的间隔长度,检查是否符合条件

AC代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=100005;
int n,m,x[N];
template<typename T>
inline void read(T &x)
{x=0;char c = getchar();int s = 1;while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}x*=s;
}
template<typename T>
inline void write(T x)
{if(x<0)putchar('-'),x=-x;if(x>9)write(x/10);putchar(x%10+'0');return;
}
inline bool check(int d){int cow=1;int rgt=x[1]+d;for(int i=2;i<=n;i++){if(x[i]<rgt)continue;++cow;rgt=x[i]+d;}return cow>=m;
}
int main(){read(n),read(m);for(int i=1;i<=n;i++)read(x[i]);sort(x+1,x+n+1);int l=0,r=x[n]-x[1];while(l<=r){int mid=(l+r)>>1;if(check(mid))l=mid+1;else r=mid-1;} write(r);printf("\n");return 0;
}

平均数

题目描述

给一个长度为 n n n 的数列,我们需要找出该数列的一个子串,使得子串平均数最大化,并且子串长度 ≥ m \ge m m

输入格式

第一行两个整数 n n n m m m

接下来 n n n 行,每行一个整数 a i a_i ai,表示序列第 i i i 个数字。

输出格式

一个整数,表示最大平均数的 1000 1000 1000 倍,如果末尾有小数,直接舍去,不要用四舍五入求整。

样例 #1
样例输入 #1
10 6
6
4
2
10
3
8
5
9
4
1
样例输出 #1
6500
提示
数据规模与约定
  • 对于 60 % 60\% 60% 的数据,保证 m ≤ n ≤ 1 0 4 m\le n\le 10^4 mn104
  • 对于 100 % 100\% 100% 的数据,保证 1 ≤ m ≤ n ≤ 1 0 5 1 \leq m\le n\le 10^5 1mn105 0 ≤ a i ≤ 2000 0\le a_i\le2000 0ai2000

AC代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
const double eps=1e-10;
template<typename T>
inline void read(T &x)
{x=0;char c = getchar();int s = 1;while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}x*=s;
}
template<typename T>
inline void write(T x)
{if(x<0)putchar('-'),x=-x;if(x>9)write(x/10);putchar(x%10+'0');return;
}
int n,len,a[100010];
double b[100010],sum[100010];
int main(){read(n),read(len);for(int i=1;i<=n;i++)read(a[i]);double l=-1e6,r=1e6,mid;while(r-l>eps){mid=(l+r)/2;for(int i=1;i<=n;i++){b[i]=a[i]-mid;sum[i]=sum[i-1]+b[i];}double minn=1e9,tmp=-1e9;for(int i=len;i<=n;i++){minn=min(minn,sum[i-len]);tmp=max(tmp,sum[i]-minn);}if(tmp>-eps)l=mid;else r=mid;}cout<<(int)((r+eps)*1000)<<endl;return 0;
}

Dropping Test

题目描述

在某个课程中,你需要进行 n n n 次测试。

如果你在共计 b i b_i bi 道题的测试 i i i 上的答对题目数量为 a i a_i ai,你的累积平均成绩就被定义为

100 × ∑ i = 1 n a i ∑ i = 1 n b i 100\times \dfrac{\displaystyle \sum_{i=1}^n a_i}{\displaystyle \sum_{i=1}^n b_i} 100×i=1nbii=1nai

给定您的考试成绩和一个正整数 k k k,如果您被允许放弃任何 k k k 门考试成绩,您的累积平均成绩的可能最大值是多少。

假设您进行了 3 3 3 次测试,成绩分别为 5 / 5 , 0 / 1 5/5,0/1 5/5,0/1 2 / 6 2/6 2/6

在不放弃任何测试成绩的情况下,您的累积平均成绩是

100 × 5 + 0 + 2 5 + 1 + 6 = 50 100\times \frac{5+0+2}{5+1+6}=50 100×5+1+65+0+2=50

然而,如果你放弃第三门成绩,则您的累积平均成绩就变成了

100 × 5 + 0 5 + 1 ≈ 83.33 ≈ 83 100\times \frac{5+0}{5+1}\approx 83.33\approx 83 100×5+15+083.3383

输入格式

输入包含多组测试用例,每个测试用例包含三行。

对于每组测试用例,第一行包含两个整数 n n n k k k

第二行包含 n n n 个整数,表示所有的 a i a_i ai

第三行包含 n n n 个整数,表示所有的 b i b_i bi

当输入用例 n = k = 0 n=k=0 n=k=0 时,表示输入终止,且该用例无需处理。

输出格式

对于每个测试用例,输出一行结果,表示在放弃 k k k 门成绩的情况下,可能的累积平均成绩最大值。

结果应四舍五入到最接近的整数。

样例 #1
样例输入 #1
3 1
5 0 2
5 1 6
4 2
1 2 7 9
5 6 7 9
0 0
样例输出 #1
83
100
提示

数据范围 1 ≤ n ≤ 1000 1 \le n \le 1000 1n1000, 0 ≤ k < n 0 \le k < n 0k<n, 0 ≤ a i ≤ b i ≤ 1 0 9 0 \le a_i \le b_i \le 10^9 0aibi109

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
const double eps=1e-8;
int n,k;
double a[1010],b[1010],tmp[1010];
inline bool check(double m){double cnt=0;for(int i=1;i<=n;i++){tmp[i]=a[i]-b[i]*m;}sort(tmp+1,tmp+1+n);for(int i=n;i>k;i--){cnt+=tmp[i];}return cnt>=0;
}
int main(){while(scanf("%d%d",&n,&k)){if(n==0&&k==0)break;for(int i=1;i<=n;i++)scanf("%lf",&a[i]);for(int i=1;i<=n;i++)scanf("%lf",&b[i]);double st=0,ed=100;while(fabs(ed-st)>=eps){double mid=st+(ed-st)/2;if(check(mid))st=mid;else ed=mid;}st*=100.0;printf("%.0lf\n",st);}return 0;
}

【模板】三分 | 函数

题目描述

给定 n n n 个二次函数 f 1 ( x ) , f 2 ( x ) , … , f n ( x ) f_1(x),f_2(x),\dots,f_n(x) f1(x),f2(x),,fn(x)(均形如 a x 2 + b x + c ax^2+bx+c ax2+bx+c),设 F ( x ) = max ⁡ { f 1 ( x ) , f 2 ( x ) , . . . , f n ( x ) } F(x)=\max\{f_1(x),f_2(x),...,f_n(x)\} F(x)=max{f1(x),f2(x),...,fn(x)},求 F ( x ) F(x) F(x) 在区间 [ 0 , 1000 ] [0,1000] [0,1000] 上的最小值。

输入格式

输入第一行为正整数 T T T,表示有 T T T 组数据。

每组数据第一行一个正整数 n n n,接着 n n n 行,每行 3 3 3 个整数 a , b , c a,b,c a,b,c,用来表示每个二次函数的 3 3 3 个系数,注意二次函数有可能退化成一次。

输出格式

每组数据输出一行,表示 F ( x ) F(x) F(x) 的在区间 [ 0 , 1000 ] [0,1000] [0,1000] 上的最小值。答案精确到小数点后四位,四舍五入。

样例 #1
样例输入 #1
2
1
2 0 0
2
2 0 0
2 -4 2
样例输出 #1
0.0000
0.5000
提示

对于 50 % 50\% 50% 的数据, n ≤ 100 n\le 100 n100

对于 100 % 100\% 100% 的数据, T < 10 T<10 T<10 n ≤ 1 0 4 \ n\le 10^4  n104 0 ≤ a ≤ 100 0\le a\le 100 0a100 ∣ b ∣ ≤ 5 × 1 0 3 |b| \le 5\times 10^3 b5×103 ∣ c ∣ ≤ 5 × 1 0 3 |c| \le 5\times 10^3 c5×103

AC代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#define eps 1e-10
using namespace std;
int n,t;
struct f{int a,b,c;
}s[10005];
template<typename T>
inline void read(T &x)
{x=0;char c = getchar();int s = 1;while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}x*=s;
}
template<typename T>
inline void write(T x)
{if(x<0)putchar('-'),x=-x;if(x>9)write(x/10);putchar(x%10+'0');return;
}
inline double calc(double num){double maxn=-1e9;for(int i=1;i<=n;i++){maxn=max(maxn,s[i].a*num*num+s[i].b*num+s[i].c);}return maxn;
}
int main(){read(t);while(t--){read(n);for(int i=1;i<=n;i++){read(s[i].a);read(s[i].b);read(s[i].c);}double l=0,r=1000,midl,midr;while(r-l>eps){midl=l+(r-l)/3,midr=r-(r-l)/3;if(calc(midl)>calc(midr))l=midl;else r=midr;}printf("%.4lf\n",calc(l));}return 0;
}

Doremy’s IQ

题面翻译
题目描述

哆来咪·苏伊特参加了 n n n 场比赛。 比赛 i i i 只能在第 i i i 天进行。比赛 i i i 的难度为 a i a_i ai。最初,哆来咪的 IQ 为 q q q 。 在第 i i i 天,哆来咪将选择是否参加比赛 i。只有当她当前的 IQ 大于 0 0 0 时,她才能参加比赛。

如果哆来咪选择在第 i i i 天参加比赛 i i i,则会发生以下情况:

  • 如果 a i > q a_i>q ai>q,哆来咪会觉得自己不够聪明,所以 q q q 将会减 1 1 1
  • 否则,什么都不会改变。

如果她选择不参加比赛,一切都不会改变。哆来咪想参加尽可能多的比赛。请给哆来咪一个解决方案。

输入格式

第一行包含一个正整数 t t t ( 1 ≤ t ≤ 1 0 4 1\le t\le10^4 1t104) ,表示测试数据的组数。

第二行包含两个整数 n n n q q q ( 1 ≤ n ≤ 1 0 5 1\le n\le10^5 1n105, 1 ≤ q ≤ 1 0 9 1\le q\le10^9 1q109),表示比赛次数和哆来咪最开始时的 IQ。

第三行包含 n n n 个整数 a 1 , a 2 ⋯ a n a_1,a_2⋯a_n a1,a2an( 1 ≤ a i ≤ 1 0 9 1\le a_i≤10^9 1ai109),表示每场比赛的难度。

数据保证 n n n 之和不超过 1 0 5 10^5 105

输出格式

对于每组数据,输出一个二进制字符串 s s s,如果哆来咪应该选择参加比赛 i i i,则 s i = 1 s_i=1 si=1,否则 s i = 0 s_i=0 si=0。 字符串中 1 1 1 的数量应该尽可能的多,并且当她的 IQ 为 0 0 0 或更低时,她不应该参加比赛。

如果有多个解决方案,你可以输出任意一个。

样例说明

在第一个测试用例中,哆来咪参加了唯一的比赛。她的 IQ 没有下降。

在第二个测试用例中,哆来咪参加了两个比赛。在参加比赛 2 2 2 后,她的 IQ 下降了 1 1 1

在第三个测试用例中,哆来咪参加了比赛 1 1 1 和比赛 2 2 2。她的 IQ 在参加比赛 2 2 2 后降至 0 0 0,因此她无法参加比赛 3 3 3

题目描述

Doremy is asked to test $ n $ contests. Contest $ i $ can only be tested on day $ i $ . The difficulty of contest $ i $ is $ a_i $ . Initially, Doremy’s IQ is $ q $ . On day $ i $ Doremy will choose whether to test contest $ i $ or not. She can only test a contest if her current IQ is strictly greater than $ 0 $ .

If Doremy chooses to test contest $ i $ on day $ i $ , the following happens:

  • if $ a_i>q $ , Doremy will feel she is not wise enough, so $ q $ decreases by $ 1 $ ;
  • otherwise, nothing changes.

If she chooses not to test a contest, nothing changes.Doremy wants to test as many contests as possible. Please give Doremy a solution.

输入格式

The input consists of multiple test cases. The first line contains a single integer $ t $ ( $ 1\le t\le 10^4 $ ) — the number of test cases. The description of the test cases follows.

The first line contains two integers $ n $ and $ q $ ( $ 1 \le n \le 10^5 $ , $ 1 \le q \le 10^9 $ ) — the number of contests and Doremy’s IQ in the beginning.

The second line contains $ n $ integers $ a_1,a_2,\cdots,a_n $ ( $ 1 \le a_i \le 10^9 $ ) — the difficulty of each contest.

It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 10^5 $ .

输出格式

For each test case, you need to output a binary string $ s $ , where $ s_i=1 $ if Doremy should choose to test contest $ i $ , and $ s_i=0 $ otherwise. The number of ones in the string should be maximum possible, and she should never test a contest when her IQ is zero or less.

If there are multiple solutions, you may output any.

样例 #1
样例输入 #1
5
1 1
1
2 1
1 2
3 1
1 2 1
4 2
1 4 3 1
5 2
5 1 2 4 3
样例输出 #1
1
11
110
1110
01111
提示

In the first test case, Doremy tests the only contest. Her IQ doesn’t decrease.

In the second test case, Doremy tests both contests. Her IQ decreases by $ 1 $ after testing contest $ 2 $ .

In the third test case, Doremy tests contest $ 1 $ and $ 2 $ . Her IQ decreases to $ 0 $ after testing contest $ 2 $ , so she can’t test contest $ 3 $ .

AC代码

#include<cstdio>
#include<cstring>
using namespace std;
#define N 100000
int t,n,q,a[N+2],pos;
bool ans[N+2];
inline bool check(int x){int w=q;for(int i=x+1;i<=n;++i){if(a[i]>w)--w;}return w>=0;
}
int main(){scanf("%d",&t);while(t--){scanf("%d%d",&n,&q);for(int i=1;i<=n;++i)scanf("%d",a+i);for(int l=0,r=n,mid;l<=r;){mid=(l+r)>>1;if(check(mid)){pos=mid;r=mid-1;}else l=mid+1;}for(int i=1;i<=pos;++i){if(a[i]<=q)ans[i]=true;else ans[i]=false;}for(int i=pos+1;i<=n;++i)ans[i]=true;for(int i=1;i<=n;++i)printf("%d",ans[i]);printf("\n");}return 0;
}

Empty Graph

题面翻译

给定一个长为 n n n 的序列 a a a

定义一个 n n n 个点的无向完全图,点 l l l 和点 r r r 之间的距离为 min ⁡ i ∈ [ l , r ] { a i } \min\limits_{i\in[l,r]}\{a_i\} i[l,r]min{ai}

你可以进行 k k k 次操作,每次操作可以选定 ∀ i ∈ [ 1 , n ] \forall i \in [1,n] i[1,n] 并将 a i a_i ai 赋值为一个 [ 1 , 1 0 9 ] [1,10^9] [1,109] 的整数。请最大化这个图的直径。

d ( u , v ) d(u,v) d(u,v) 表示 u u u v v v 的最短路径长度,图的直径定义为 max ⁡ 1 ≤ u < v ≤ n d ( u , v ) \max\limits_{1\leq u < v \leq n} d(u,v) 1u<vnmaxd(u,v)

输出最大化的直径长度。

题目描述

— Do you have a wish?

— I want people to stop gifting each other arrays.

O_o and Another Young Boy

An array of $ n $ positive integers $ a_1,a_2,\ldots,a_n $ fell down on you from the skies, along with a positive integer $ k \le n $ .

You can apply the following operation at most $ k $ times:

  • Choose an index $ 1 \le i \le n $ and an integer $ 1 \le x \le 10^9 $ . Then do $ a_i := x $ (assign $ x $ to $ a_i $ ).

Then build a complete undirected weighted graph with $ n $ vertices numbered with integers from $ 1 $ to $ n $ , where edge $ (l, r) $ ( $ 1 \le l < r \le n $ ) has weight $ \min(a_{l},a_{l+1},\ldots,a_{r}) $ .

You have to find the maximum possible diameter of the resulting graph after performing at most $ k $ operations.

The diameter of a graph is equal to $ \max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)} $ , where $ \operatorname{d}(u, v) $ is the length of the shortest path between vertex $ u $ and vertex $ v $ .

输入格式

Each test contains multiple test cases. The first line contains the number of test cases $ t $ ( $ 1 \le t \le 10^4 $ ). Description of the test cases follows.

The first line of each test case contains two integers $ n $ and $ k $ ( $ 2 \le n \le 10^5 $ , $ 1 \le k \le n $ ).

The second line of each test case contains $ n $ positive integers $ a_1,a_2,\ldots,a_n $ ( $ 1 \le a_i \le 10^9 $ ).

It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 10^5 $ .

输出格式

For each test case print one integer — the maximum possible diameter of the graph after performing at most $ k $ operations.

样例 #1
样例输入 #1
6
3 1
2 4 1
3 2
1 9 84
3 1
10 2 6
3 2
179 17 1000000000
2 1
5 9
2 2
4 2
样例输出 #1
4
168
10
1000000000
9
1000000000
提示

In the first test case, one of the optimal arrays is $ [2,4,5] $ .

The graph built on this array:

$ \operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2 $ and $ \operatorname{d}(2, 3) = 4 $ , so the diameter is equal to $ \max(2,2,4) = 4 $ .

AC代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
const int N=100010;
typedef long long ll ;
ll t,n,k,a[N],pre[N],sub[N];
template<typename T>
inline void read(T &x)
{x=0;char c = getchar();ll s = 1;while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}x*=s;
}
template<typename T>
inline void write(T x)
{if(x<0)putchar('-'),x=-x;if(x>9)write(x/10);putchar(x%10+'0');return;
}
inline bool check(ll pos){for(ll i=1;i<=n;i++)pre[i]=pre[i-1]+(ll)((a[i]<<1)<pos);for(ll i=n;i;i--)sub[i]=sub[i+1]+(ll)((a[i]<<1)<pos);ll minx=0x3f3f3f3f;for(ll i=1;i<n;i++)minx=min(minx,pre[i-1]+sub[i+2]+(ll)(a[i] < pos) + (ll)(a[i + 1] < pos));return minx<=k;
}
int main(){read(t);while (t--) {read(n),read(k);memset(pre, 0, sizeof(pre));memset(sub, 0, sizeof(sub));for (ll i = 1; i <= n; i++) read(a[i]);ll l = 0, r = 1e9, ans = 0;while (l <= r) {ll mid = l + r >> 1;if (check(mid)) {l=mid+1;ans=mid;} else r = mid-1;}write(ans);printf("\n");}return 0;
}

这是我的第十二篇文章,如有纰漏也请各位大佬指正

辛苦创作不易,还望看官点赞收藏打赏,后续还会更新新的内容。

相关文章:

浅谈用二分和三分法解决问题(c++)

目录 问题引入[NOIP2001 提高组] 一元三次方程求解题目描述输入格式输出格式样例 #1样例输入 #1样例输出 #1 提示思路分析AC代码 思考关于二分和三分例题讲解进击的奶牛题目描述输入格式输出格式样例 #1样例输入 #1样例输出 #1 思路AC代码 平均数题目描述输入格式输出格式样例 …...

Cocos Creator2D游戏开发(9)-飞机大战(7)-爆炸效果

这个爆炸效果我卡在这里好长时间,视频反复的看, 然后把代码反复的测试,修改,终于给弄出来 视频中这段,作者也是修改了好几次, 跟着做也走了不少弯路; 最后反正弄出来了; 有几个坑; ① 动画体创建位置是enemy_prefab ② enemy_prefab预制体下不用放动画就行; ③ 代码中引用Anima…...

终于有人把华为认证全部说清楚了

在信息技术领域&#xff0c;华为认证好比一座金字招牌&#xff0c;吸引着无数技术专业人士的青睐。 市场上关于华为认证的声音纷繁复杂&#xff0c;存在不少争议&#xff0c;让人难以辨别真伪。 今天就来好好讲讲华为认证&#xff0c;从头到尾都帮你盘盘清楚。 01 华为认证是…...

【知识】pytorch中的pinned memory和pageable memory

转载请注明出处&#xff1a;小锋学长生活大爆炸[xfxuezhagn.cn] 如果本文帮助到了你&#xff0c;欢迎[点赞、收藏、关注]哦~ 目录 概念简介 pytorch用法 速度测试 反直觉情况 概念简介 默认情况下&#xff0c;主机 &#xff08;CPU&#xff09; 数据分配是可分页的。GPU 无…...

【系统架构设计】数据库系统(五)

数据库系统&#xff08;五&#xff09; 数据库模式与范式数据库设计备份与恢复分布式数据库系统数据仓库数据挖掘NoSQL大数据 数据库模式与范式 数据库设计 备份与恢复 分布式数据库系统 数据仓库 数据挖掘 对数据挖掘技术进行支持的三种基础技术已经发展成熟&#xff0c…...

如何对人工智能系统进行测试|要点,方法及流程

当今社会&#xff0c;人工智能发展非常快。现在人工智能的发展已经渗透到了我们生活的方方面面&#xff0c;自动驾驶、或者我们手机里经常用到的一些应用都或多或少涉及到了一些人工智能的功能&#xff0c;比如说美图秀秀、新闻推荐、机器翻译以及个性化的购物推荐等等都涉及到…...

CVE-2023-37569~文件上传【春秋云境靶场渗透】

# 今天我们拿下CVE-2023-37569这个文件上传漏洞# 经过简单账号密码猜测 账号&#xff1a;admin 密码&#xff1a;password# 找到了文件上传的地方# 我们直接给它上传一句话木马并发现上传成功# 上传好木马后&#xff0c;右键上传的木马打开发现上传木马页面# 直接使用蚁剑进行连…...

MySQL简介 数据库管理与表管理

文章目录 1 MySQL的优势2 MySQL数据类型1 数字类型2 日期和时间类型3 字符串类型 3 数据库管理4 数据表管理参考 1 MySQL的优势 性能优化&#xff1a;通过优化存储引擎&#xff08;InnoDB&#xff0c;MyISAM&#xff09;和查询优化。解决大规模数据处理和查询优化开源&#xf…...

PHP 函数性能优化的技巧是什么?

本文由 ChatMoney团队出品 本文将详细介绍 PHP 函数性能优化的技巧。通过分析 PHP 函数的执行过程和性能瓶颈&#xff0c;提供一系列实用的优化方法&#xff0c;并结合代码示例&#xff0c;帮助读者提升 PHP 代码的执行效率。文章内容将涵盖变量作用域、递归算法、循环优化、内…...

小程序支付(前端)

前端只需要调用 wx.requestPayment(Object object) 文档 参考代码 const openId wx.getStorageSync(openId)payOrder({payId: this.data.resData.payId,openId}).then((res) > {console.log(2222, res);try {const data JSON.parse(res.res)console.log(22, data)const {…...

开发一个自己的VSCode插件

1、前言 对于一个前端开发者来说&#xff0c;开发工具&#xff0c;最常用的应该就是VSCode了&#xff0c;因为它免费&#xff0c;速度快&#xff0c;提供了丰富了插件等优点&#xff0c;使得越来越多的前端开发者都来使用它了&#xff0c;在开发的时候如果有丰富的插件提供支持…...

Milvus 向量数据库进阶系列丨构建 RAG 多租户/多用户系统 (上)

本系列文章介绍 在和社区小伙伴们交流的过程中&#xff0c;我们发现大家最关心的问题从来不是某个具体的功能如何使用&#xff0c;而是面对一个具体的实战场景时&#xff0c;如何选择合适的向量数据库解决方案或最优的功能组合。在 “Milvus 向量数据库进阶” 这个系列文章中&…...

前缀和(更新中)

目录 1.寻找数组的中心下标 2.除自身以外数组的乘积 3.和为k的子数组 4.可被k整除的子数组 5.连续数组 1.寻找数组的中心下标 . - 力扣&#xff08;LeetCode&#xff09; class Solution { public:int pivotIndex(vector<int>& nums) {int size nums.size();v…...

记录一次单例模式乱用带来的危害。

项目场景&#xff1a; 我们在接受到短信网关下发的回执之后&#xff0c;需要将回执内容也下发给我们的下游服务。为了防止下游响应超时&#xff0c;我们需要将超时的信息存放到Redis中然后进行补发操作。 问题描述 在使用Redis进行数据存储的时候&#xff0c;报NPE问题。 原因…...

外卖项目day14(day11)---数据统计

Apache ECharts 大家可以看我这篇文章&#xff1a; Apache ECharts-CSDN博客 营业额统计 产品原型 接口设计 新建admin/ReportController /*** 数据统计相关接口*/ RestController RequestMapping("/admin/report") Api(tags "数据统计相关接口") Slf…...

养猫科普!牙口不好的猫咪怎么选粮?好吃易消化主食罐推荐

我家的猫猫已经九岁了&#xff0c;已经是一位老奶奶了&#xff0c;她的牙口不太好。对于她来说&#xff0c;膨化猫粮过于硬&#xff0c;很难咀嚼&#xff0c;所以我为她准备了质地柔软的主食罐头。哪种主食罐头更适合牙口不好的猫咪呢&#xff1f;下面&#xff0c;我就来分享一…...

力扣刷题之3143.正方形中的最多点数

题干描述 给你一个二维数组 points 和一个字符串 s &#xff0c;其中 points[i] 表示第 i 个点的坐标&#xff0c;s[i] 表示第 i 个点的 标签 。 如果一个正方形的中心在 (0, 0) &#xff0c;所有边都平行于坐标轴&#xff0c;且正方形内 不 存在标签相同的两个点&#xff0c…...

【更新2022】省级经济高质量发展指标体系测度 含代码 2000-2022

重磅更新&#xff01;【章汕】制作“省级经济高质量发展指标体系测度 含代码”&#xff0c;市面上有这个版本的数据&#xff0c;但其内容非常不全面&#xff0c;个别指标有误&#xff0c;没有stata和代码&#xff0c;即使有代码小白也很容易报错&#xff1b;没有权重、宽面板等…...

缓冲流练习

练习1&#xff1a;拷贝文件 四种方式拷贝文件&#xff0c;并统计各自用时。 字节流的基本流&#xff1a;一次读写一个字节 字节流的基本流&#xff1a;一次读写一个字节数组 字节缓冲流&#xff1a;一次读写一个字节 字节缓冲流&#xff1a;一次读写一个字节数组 这里我只使用了…...

自己履行很多的话语,依旧按照这个方式进行生活

《明朝那些事儿》最后一段讲述了徐霞客的故事&#xff0c;作者当年明月通过徐霞客的生平表达了一种人生哲学。在书的结尾&#xff0c;当年明月写道&#xff1a;"成功只有一个——按照自己的方式&#xff0c;去度过人生"&#xff0c;这句话被用作《明朝那些事儿》的结…...

交通预测数据文件梳理:METR-LA

文章目录 前言一、adj_METR-LA.pkl文件读取子文件1读取子文件2读取子文件3 二、METR-LA.h5文件 前言 最近做的实验比较多&#xff0c;对于交通预测数据的各种文件和文件中的数据格式理解愈加混乱&#xff0c;因此打算重新做一遍梳理来加深实验数据集的理解&#xff0c;本文章作…...

按钮类控件

目录 1.Push Button 代码示例: 带有图标的按钮 代码示例: 带有快捷键的按钮 代码示例: 按钮的重复触发 2.Radio Buttion 代码示例: 选择性别 代码示例: click, press, release, toggled 的区别 代码示例: 单选框分组 3.3 Check Box 代码示例: 获取复选按钮的取值 1.Pu…...

opencascade AIS_ViewController源码学习 视图控制、包含鼠标事件等

opencascade AIS_ViewController 前言 用于在GUI和渲染线程之间处理视图器事件的辅助结构。 该类实现了以下功能&#xff1a; 缓存存储用户输入状态&#xff08;鼠标、触摸和键盘&#xff09;。 将鼠标/多点触控输入映射到视图相机操作&#xff08;平移、旋转、缩放&#xff0…...

拉削基础知识——拉床的类型及特点

拉床是所有机械加工工具中最简单的一种&#xff0c;由拉削工具、夹具、驱动装置和支撑架组成。拉削加工可获得较高的尺寸精度和较小的表面粗糙度&#xff0c;生产率较高&#xff0c;适用于大批量生产。拉床按其结构主要分为卧式和立式。应用领域和功能可分为液压拉床、自动拉床…...

docker-compose笔记

docker 目前docker官网已经无法登录&#xff0c;但是还可以从清华镜像站&#xff08;https://mirrors.tuna.tsinghua.edu.cn/docker-ce/&#xff09;下载。 使用方法可以参考早期文章《docker笔记》 docker-compose 可以从Github下载不同版本的二进制文件&#xff0c;例如do…...

C# 自定义控件无法加载

问题 在做winform开发时自己定义了一个控件&#xff0c;控件在工具箱中显示了&#xff0c;但是拖动到窗体设计器时会提示未能加载工具箱项xxx&#xff0c;将从工具箱中将其删除&#xff0c;如下图所示: 点击确定后&#xff0c;控件会从工具箱中移除。 解决方法 将 生成>…...

avl树自实现(带图),探讨平衡因子与旋转

引子&#xff1a; 在此之前&#xff0c;我们学过了搜索二叉树&#xff0c;这种树&#xff0c;在如果数据有序或接近有序的情况下&#xff0c;二叉搜索树将退化为单支树&#xff0c;查找元素相当于在顺序表中搜索元素&#xff0c;效率低下&#xff0c;而且普通搜索二叉树无法有…...

Elasticsearch 的DSL查询,聚合查询与多维度数据统计

文章目录 搜索聚合高阶概念 搜索 即从一个索引下按照特定的字段或关键词搜索出符合用户预期的一个或者一堆cocument&#xff0c;然后根据文档的相关度得分&#xff0c;在返回的结果集里并根据得分对这些文档进行一定的排序。 聚合 根据业务需求&#xff0c;对文档中的某个或…...

【如何高效处理前端常见问题:策略与实践】

在快速发展的Web开发领域&#xff0c;前端作为用户与应用程序直接交互的界面&#xff0c;其重要性不言而喻。然而&#xff0c;随着技术的不断演进和项目的复杂化&#xff0c;前端开发者在日常工作中难免会遇到各种挑战和问题。本文旨在深入探讨前端开发中常见的问题类型&#x…...

聊聊前端 JavaScript 的扩展运算符 “...“ 的使用场景

前言 在 JavaScript 中&#xff0c;... 被称为 “扩展运算符” 或 “剩余参数运算符”。 扩展运算符是在 ES6&#xff08;ECMAScript 2015&#xff09;中被引入的&#xff0c;目的是为了提高语言的表达能力和代码的可读性。 根据上下文不同&#xff0c;它主要用在数组、对象…...