Chapter 2 二分与前缀和

+++

  • 二分

  • 套路

如果更新方式写的是R = mid, 则不用做任何处理,如果更新方式写的是L = mid,则需要在计算mid是加上1。

1.数的范围 789

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
//整数二分
using namespace std; int st[100005];
int n, q;
int u; int main()
{
scanf("%d%d", &n, &q);
for(int i = 0; i < n; i++)
scanf("%d", &st[i]);
while(q--)
{
int L = 0; int R = n - 1;
scanf("%d", &u);
while(L < R)
{
int md = L + R >> 1;
if(st[md] >= u) R = md;//注意边界问题
else L = md + 1;
}
if(st[R] == u)
{
cout << R << " ";
R = n - 1;
while(L < R)
{
int md = L + R + 1 >> 1;//注意边界问题
if(st[md] <= u) L = md;
else R = md - 1;
}
cout << L << endl;
}
else cout << "-1 -1" << endl;
}
return 0;
}

2.数的三次方根 790

#include <iostream>
//实数二分
using namespace std; int main()
{
double n;
cin >> n;
double l = -10000, r = 10000;
while(r - l > 1e-8)
{
double mid = (l + r) / 2;
if(mid * mid * mid >= n) r = mid;
else l = mid;
}
printf("%f\n", l);
return 0;
}

3.机器人跳跃问题 730

//AC code
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = 1e5 + 5;
int h[maxn], temp, n; int main()
{
cin >> n;
for(int i = 0; i < n; i++)
scanf("%d", h + i);
int l = 0, r = 1e5;
while(l < r)
{
int mid =(l + r) >> 1;//注意边界问题
temp = mid;
for(int i = 0; i < n && mid >= 0; i++)
{
mid = mid * 2 - h[i];
if(mid >= 1e5) break; //没有这句ac不了,防止中间过程爆掉
}
if(mid >= 0)
r = temp;
else
l = temp + 1;
}
cout << r << endl;
return 0;
}

4.四平方和 1221

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = 5e6;//注意
int n, m; struct Sum{
int s, c, d; bool operator< (const Sum &t)const
{
if(s != t.s) return s < t.s;
if(c != t.c) return c < t.c;
return d < t.d;
}
}p[maxn]; int main()
{
cin >> n;
for(int c = 0; c * c <= n; c++ )
for(int d = c; d * d + c * c <= n; d++ )
p[m++] = {c * c + d * d, c, d}; sort(p, p + m); for(int a = 0; a * a <= n; a++ )
for(int b = a; b * b + a * a <= n; b++ )
{
int t = n - a * a - b * b;
int l = 0, r = m - 1;
while(l < r)
{
int mid = (l + r) >> 1;
if(p[mid].s >= t) r = mid;
else l = mid + 1;
}
if(p[l].s == t)
{
printf("%d %d %d %d\n", a, b, p[l].c, p[l].d);
return 0;
}
}
}
5.分巧克力 1227
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = 100010;
int n, k;
int h[maxn], w[maxn]; bool check(int u)
{
int sum = 0;
for(int i = 0; i < n; i++)
sum += (h[i] / u) * (w[i] / u);
if(sum >= k) return true;
return false;
} int main()
{
cin >> n >> k;
for(int i = 0; i < n; i++)
scanf("%d%d", h + i, w + i);
int L = 1, R = 1e5;
while(L < R)
{
int mid = L + R + 1 >> 1;
if(check(mid)) L = mid;
else R = mid - 1;
}
cout << R << endl;
return 0;
}

+++

  • 前缀和

1.前缀和 795
//一维数组前缀和
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn = 100010; int a[maxn], s[maxn];
int n, m; int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++ )
{
scanf("%d", a + i);
s[i] = s[i - 1] + a[i];//存储前i个数的和
}
while(m--)
{
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", s[y] - s[x - 1]);
}
return 0;
}
2.子矩阵的和 796
// 二维数组前缀和
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std; const int maxn = 1010;
int a[maxn][maxn], s[maxn][maxn];
int n, m, q; int main()
{
scanf("%d%d%d", &n, &m, &q);
for(int i = 1; i <= n; i++ )
for(int j = 1; j <= m; j++ )
{
scanf("%d", &a[i][j]);
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
}
while(q--)
{
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
printf("%d\n", s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]);
}
return 0;
}
3.激光炸弹 99

4.K倍区间 1230

//优化时间复杂度

//O(n)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long LL; const int maxn = 100010;
int s[maxn], cnt[maxn];
int n, k, temp;
LL res; int main()
{
cnt[0] = 1;
cin >> n >> k;
for(int i = 1; i <= n; i++ )
{
scanf("%d", &temp);
s[i] = (s[i - 1] + temp) % k;
res += cnt[s[i]];
cnt[s[i]]++;
} printf("%lld\n", res);
return 0;
}
K倍区间学习到的经验:

1.首先因为知道考的是前缀和,我就用O(n²)的复杂度的二重for循环暴力枚举,果不其然超时,然后就没有了进一步的简化思路。

2.好的思路是从简单暴力的方法中优化出来的,就如本题的AC code,把复杂度降到了O(n),成功AC。

3.找到右端点后遍历前面所有前缀和,符合K倍区间性质的其实就是mod k后与右端点前缀和mod k后余数相等的点。(

Chapter2二分与前缀和的更多相关文章

  1. [POJ3061]Subsequence(二分,前缀和)

    题目链接:http://poj.org/problem?id=3061 题意:给一个长为n的数列和整数s,求一个连续的子序列,使得这个子序列长度最短并且不小于这个整数s. 统计[1~i]的子序列和su ...

  2. hdu 6406 Taotao Picks Apples (2018 Multi-University Training Contest 8 1010)(二分,前缀和)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6406 思路: 暴力,预处理三个前缀和:[1,n]桃子会被摘掉,1到当前点的最大值,1到当前点被摘掉的桃子的 ...

  3. suoi07 区间平均++ (二分答案+前缀和)

    https://www.vijos.org/d/SUOI/p/59dc5af7d3d8a1361ae62b97 二分一个答案,然后做一做前缀和,用满足区间大小的最小值减一减,判断答案合不合法 然而还要 ...

  4. D. Frets On Fire 【二分,前缀和】 (Codeforces Global Round 2)

    题目传送门:http://codeforces.com/contest/1119/problem/D D. Frets On Fire time limit per test 1.5 seconds ...

  5. LuoguP1314 聪明的质检员 【二分答案/前缀和】

    美丽的题号预示着什么... 描述 小 T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有n个矿石,从1到n逐一编号,每个矿石都有自己的重量wi以及价值vi.检验矿产的流程是: 1.给定m个 ...

  6. 139. 回文子串的最大长度(回文树/二分,前缀,后缀和,Hash)

    题目链接 : https://www.acwing.com/problem/content/141/ #include <bits/stdc++.h> using namespace st ...

  7. $Noip2011/Luogu1314$ 聪明的质监员 二分+巧妙前缀和

    $Luogu$ $Sol$ 首先$W$一定是某个$w_i$.于是一种暴力方法就出炉了,枚举$W$再计算. 注意到,满足$S-Y$的绝对值最小的$Y$只可能是两种,一种是$<S$的最大的$Y$,一 ...

  8. 洛谷P2468 [SDOI2010]粟粟的书架(二分答案 前缀和 主席树)

    题意 题目链接 给出一个矩形,每个点都有一些值,每次询问一个子矩阵最少需要拿几个数才能构成给出的值 Sol 这题是真坑啊.. 首先出题人强行把两个题拼到了一起, 对于前$50 \%$的数据,考虑二分答 ...

  9. Chapter2 二分与三分

    T1 给一个N个数的序列,分成M段,每段最大值最小 sol:二分最大值,贪心Check T2 平面上n个点,每个点每s会向周围扩散一个单位长度,两个点联通当且仅当扩散有交点,问什么时候这n个点联通 s ...

随机推荐

  1. leetcode水题题解

    344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...

  2. js小练习---实现红绿灯

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 工业狗转行AI的心路历程

    2020年2月16日,晴 昨晚下了一场雪,大概是晚上八九点的样子,细碎的雪花在无风的夜里直嗖嗖的往下掉,门前的轿车顶上覆了薄薄的一层.路灯下的小巷格外的寂静,裹着我的睡衣在昏黄的灯光下站了许久,冠状病 ...

  4. java jni 调用c语言函数

    今日在hibernate源代码中遇到了native关键词,甚是陌生,就查了点资料,对native是什么东西有了那么一点了解,并做一小记. native关键字说明其修饰的方法是一个原生态方法,方法对应的 ...

  5. 03(a)多元有约束优化问题(准备知识)

    转成Latex上传太麻烦,直接截图上传了,需要电子版的可以关注一下,微信公众号:“实干小海豹”,回复:”优化01a“,”优化01b“,”优化02a“,”优化02b“,”优化02c“,”优化02c“.. ...

  6. MongoDB入门(介绍、安装)

    一.什么是MongoDB? MongoDB is a document database with the scalability and flexibility that you want with ...

  7. asp.net core 3.x 授权默认流程

    一.前言 接上一篇<asp.net core 3.x 授权中的概念>,本篇看看asp.net core默认授权的流程.从两个方面来看整个授权系统是怎么运行的:启动阶段的配置.请求阶段中间件 ...

  8. 【全集】大数据Java基础

    课程介绍 本课程是由猎豹移动大数据架构师,根据Java在公司大数据开发中的实际应用,精心设计和打磨的大数据必备Java课程.通过本课程的学习大数据新手能够少走弯路,以较短的时间系统掌握大数据开发必备语 ...

  9. java 循环变量

    方法:是一段完成特定功能代码的片段. 格式: [修饰符1 修饰符2 ......]返回值类型 方法名(形式参数列表){ java语句;....... } 形式参数:在方法被调用时用于接收外界输入的数据 ...

  10. js循环小练习

    function fn(){ //打印三角形 for(var i = 1 ; i <= 30 ; i++){ for(var x = 1; x <= i; x++ ){ document. ...