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. java6循环结构二

    public class jh_01_回顾与作业点评 { public static void main(String[] args) { int val = 12345; System.out.pr ...

  2. 题解 CF1294F 【Three Paths on a Tree】

    \[ Preface \] 打比赛的时候先开了 F 题(雾 然后一眼看出 F 题结论,最后居然因为没有判重,交了三次才过. \[ Description \] 给出一棵无权树(可理解为边权为 \(1\ ...

  3. Java线程核心基础(上)

    Java线程核心基础(上) 一.实现多线程 根据Oracle官方文档,目前推荐的创建线程方法主要有两种,分别是继承Thread类和实现Runnable接口.通过阅读Thread类源码,可以发现二者不同 ...

  4. 【C++】随机数引擎

    rand() 基本:使用随机数时,经常见到的是C标准库提供的函数rand(),这个函数会生成一个0到RAND_MAX之间的一个整形数: 分布:为了得到一个给定范围内的随机数,通常会对生成的随机数取余: ...

  5. pytorch之 classification

    import torch import torch.nn.functional as F import matplotlib.pyplot as plt # torch.manual_seed(1) ...

  6. win10配置CUDA+Tensorflow2.0的一些经验

    目录 问题描述 安装 tensorflow-cpu-2.0 编译 Nvidia Samples 问题描述 网上已经很多关于配置CUDA的文章,自己这篇文章只是个大致的安装步骤,文章重点是安装和配置的一 ...

  7. K8S ConfigMap使用

    k8s系列文章: 什么是K8S configmap是k8s的一个配置管理组件,可以将配置以key-value的形式传递,通常用来保存不需要加密的配置信息,加密信息则需用到Secret,主要用来应对以下 ...

  8. (数据科学学习手札78)基于geopandas的空间数据分析——基础可视化

    本文对应代码和数据已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 通过前面的文章,我们已经对geopanda ...

  9. jQuery的动画以及扩展功能

    动画DOM及CSS操作 自定义动画 animate(最终css状态,时间) 这个最终css状态是一个对象 <!DOCTYPE html> <html lang="en&qu ...

  10. Python模块之Requests

    目录 Requests 模块 常规的get请求 基于ajax的get请求 常规的post请求 基于ajax的post请求 综合项目实战 requests模块高级 requests代理 验证码处理 Re ...