当前位置:网站首页>On the use of bisection and double pointer
On the use of bisection and double pointer
2022-06-26 00:04:00 【AMjieker】
In some topics , We need to find the best value by dichotomy , here , Set generally satisfies [l,mid] [mid+1,r] In this case, the first half or the second half is satisfied , You can split the answer
Sometimes we need to use double pointers to check This thing ,
The following is the use of double pointers to check The subject of
Binary String
Bisection uses a double pointer to check Section , Fix one end with a double pointer , Slide another section to check whether a section meets the conditions
#include <bits/stdc++.h>
typedef int INT;
#define int long long
#define sll(x) scanf("%lld", &x)
#define s(x) scanf("%d", &x)
using namespace std;
string s;
int _1;
int check(int x) {
int a = 0, b = 0;
for (int l = 0, r = 0; r < s.size(); r ++) {
if (s[r] == '1') b ++;
else a ++;
while (a > x && l <= r) {
if (s[l] == '1') b --;
else a --;
l ++;
}
if (_1 - b <= x) return true;
}
return false;
}
signed main() {
int t;cin>> t;
while (t --) {
_1 = 0;
cin >> s;
for (int i = 0; i < s.size(); i ++) if (s[i] == '1') _1 ++;
int l = 0, r = s.size();
while (l < r) {
int x = l + r >> 1;
if (check(x)) r = x;
else l = x + 1;
}
cout << r << endl;
}
return 0;
}
Difference
Use double pointers to check the interval of sum method , The same thing is to fix a section , Move the other end , For example, for every r Come on , De sliding l, That is, the tail determines , Look for legal heads
#include <bits/stdc++.h>
typedef int INT;
// #define int long long
#define sll(x) scanf("%lld", &x)
#define s(x) scanf("%d", &x)
using namespace std;
const int N = 5e5 + 111;
long long n, m;
int g[N];
int ma[N][26], mi[N][26];
int p[N];
void init() {
for (int i = 2; i < N; i ++) p[i] = p[i / 2] + 1;
for (int i = 1; i <= n; i ++)
ma[i][0] = mi[i][0] = g[i];
for (int j = 1; j < 26; j ++)
for (int i = 1; i + (1 << j) - 1 <= n; i ++)
ma[i][j] = max(ma[i][j - 1], ma[i + (1 << j - 1)][j - 1]),
mi[i][j] = min(mi[i][j - 1], mi[i + (1 << j - 1)][j - 1]);
}
long long q(int l, int r) {
int k = p[r - l + 1];
int a = max(ma[l][k], ma[r - (1 << k) + 1][k]);
int b = min(mi[l][k], mi[r - (1 << k) + 1][k]);
return ((long long)(r - l + 1)) * (a - b);
}
int check(long long x) {
long long cnt = 0;
for (int l = 1, r = 1; r <= n; r ++) {
while (l <= r && q(l, r) >= x) l ++;
cnt += r - l + 1;
}
cnt ++;
return cnt > m;
}
signed main() {
sll(n), sll(m);
for (int i = 1; i <= n; i ++) s(g[i]);
init();
m = n * (n + 1) / 2 - m + 1;
long long l = 0, r = q(1, n);
while (l < r) {
long long x = l + r + 1 >> 1;
if (check(x)) r = x - 1;
else l = x;
}
cout << l << endl;
return 0;
}
边栏推荐
- Backup restore of xtrabackup
- ValueError: color kwarg must have one color per data set. 9 data sets and 1 colors were provided解决
- On the quantity control mechanism of swoole collaboration creation in production environment
- unsigned与signed之大白话
- redis之集群
- dhcp复习
- Unsigned and signed vernacular
- 在step7中实现模拟量数值与工程量数值之间的转换_过路老熊_新浪博客
- line-height小用
- 常用的几款富文本编辑器
猜你喜欢

Circuit de fabrication manuelle d'un port série de niveau USB à TTL pour PL - 2303hx Old bear passing Sina blog
![[wechat official account H5] generates a QR code with parameters to enter the official account attention page to listen to user-defined menu bar for official account events (server)](/img/d9/935bad29005e5846dc514c966e3b0e.png)
[wechat official account H5] generates a QR code with parameters to enter the official account attention page to listen to user-defined menu bar for official account events (server)

Recherche documentaire (3): examen des modèles de prévision de la consommation d'énergie des bâtiments fondés sur les données
![mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动](/img/b2/2b87b3cea1422e2a860f5e0e7dcc40.png)
mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动

ValueError: color kwarg must have one color per data set. 9 data sets and 1 colors were provided解决
![Find the minimum value of flipped array [Abstract bisection]](/img/b9/1e0c6196e6dc51ae2c48f6c5e83289.png)
Find the minimum value of flipped array [Abstract bisection]

dhcp复习

Studio5k V28 installation and cracking_ Old bear passing by_ Sina blog
![搜索旋转数组II[抽象二分练习]](/img/db/3ea01cf1ad8446a7007891ef1d8e7f.png)
搜索旋转数组II[抽象二分练习]

Establishment of multiple background blocks in botu software_ Old bear passing by_ Sina blog
随机推荐
php中使用Makefile编译protobuf协议文件
Jenkins releases PHP project code
Lazy people teach you to use kiwi fruit to lose 16 kg in a month_ Old bear passing by_ Sina blog
P3052 [USACO12MAR]Cows in a Skyscraper G
Prototype chain test questions in JS --foo and getname
Talk about singleton mode!
Understanding of pseudo classes
网络协议之:redis protocol详解
Summary of c++ references and pointers
Mutual conversion of float type data and datetime type data in sqlserver2008
Common knowledge points in JS
InputStream流已经关闭了,但是依旧无法delete文件或者文件夹,提示被JVM占用等
Studio5k v28安装及破解_过路老熊_新浪博客
文献调研(一):基于集成学习和能耗模式分类的办公楼小时能耗预测
Realize the conversion between analog quantity value and engineering quantity value in STEP7_ Old bear passing by_ Sina blog
Raspberry pie sends hotspot for remote login
STEP7 master station and remote i/o networking_ Old bear passing by_ Sina blog
搜索旋转数组II[抽象二分练习]
SSM integrated learning notes (mainly ideas)
6.常用指令(上)v-cloak,v-once,v-pre