Chapter2二分与前缀和
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二分与前缀和的更多相关文章
- [POJ3061]Subsequence(二分,前缀和)
题目链接:http://poj.org/problem?id=3061 题意:给一个长为n的数列和整数s,求一个连续的子序列,使得这个子序列长度最短并且不小于这个整数s. 统计[1~i]的子序列和su ...
- 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到当前点被摘掉的桃子的 ...
- suoi07 区间平均++ (二分答案+前缀和)
https://www.vijos.org/d/SUOI/p/59dc5af7d3d8a1361ae62b97 二分一个答案,然后做一做前缀和,用满足区间大小的最小值减一减,判断答案合不合法 然而还要 ...
- 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 ...
- LuoguP1314 聪明的质检员 【二分答案/前缀和】
美丽的题号预示着什么... 描述 小 T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有n个矿石,从1到n逐一编号,每个矿石都有自己的重量wi以及价值vi.检验矿产的流程是: 1.给定m个 ...
- 139. 回文子串的最大长度(回文树/二分,前缀,后缀和,Hash)
题目链接 : https://www.acwing.com/problem/content/141/ #include <bits/stdc++.h> using namespace st ...
- $Noip2011/Luogu1314$ 聪明的质监员 二分+巧妙前缀和
$Luogu$ $Sol$ 首先$W$一定是某个$w_i$.于是一种暴力方法就出炉了,枚举$W$再计算. 注意到,满足$S-Y$的绝对值最小的$Y$只可能是两种,一种是$<S$的最大的$Y$,一 ...
- 洛谷P2468 [SDOI2010]粟粟的书架(二分答案 前缀和 主席树)
题意 题目链接 给出一个矩形,每个点都有一些值,每次询问一个子矩阵最少需要拿几个数才能构成给出的值 Sol 这题是真坑啊.. 首先出题人强行把两个题拼到了一起, 对于前$50 \%$的数据,考虑二分答 ...
- Chapter2 二分与三分
T1 给一个N个数的序列,分成M段,每段最大值最小 sol:二分最大值,贪心Check T2 平面上n个点,每个点每s会向周围扩散一个单位长度,两个点联通当且仅当扩散有交点,问什么时候这n个点联通 s ...
随机推荐
- 如何最快实现物流即使查询功能-物流轨迹查询API
上一篇文章我们介绍了一个物流服务提供商,推荐大家使用快递鸟接口,主要介绍了如何注册账号,获得密钥,找不到注册地址的,我在发一下: http://kdniao.com/reg 今天我们来聊如何利用快递鸟 ...
- OpenCV3入门(四)图像的基础操作
1.访问图像像素 1)灰度图像 2)彩色图像 OpenCV中的颜色顺序是BGR而不是RGB. 访问图像的像素在OpenCV中就是访问Mat矩阵,常用的有三种方法. at定位符访问 Mat数据结构,操作 ...
- HDU_1455_dfs
http://acm.hdu.edu.cn/showproblem.php?pid=1455 int dfs(int all,int sum,int now),all代表剩余总长,sum,代表每段长, ...
- Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest
Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest(dp+线段树) 题目链接 题意: 给定3个人互不相同的多个数字,可以 ...
- Go语言实现:【剑指offer】不用加减乘除做加法
该题目来源于牛客网<剑指offer>专题. 第一步:相加各位的值,不算进位,得到010,二进制每位相加就相当于各位做异或操作,101^111. 第二步:计算进位值,得到1010,相当于各位 ...
- 面试官:“看你简历上写熟悉 Handler 机制,那聊聊 IdleHandler 吧?”
一. 序 Handler 机制算是 Android 基本功,面试常客.但现在面试,多数已经不会直接让你讲讲 Handler 的机制,Looper 是如何循环的,MessageQueue 是如何管理 M ...
- win7下彻底卸载和重装mysql
1 .目的:第一次安装完mysql后忘记了临时密码,通过各种途径都无法更改密码,因此不得不把mysql卸载了. 2 .建议:第一次安装mysql时会分配一个临时密码,如最后一行的se_:j<tq ...
- 如何通过adb command 完成自动SD卡升级?
如何通过adb command 完成自动SD卡升级? 原创 2014年09月09日 10:50:57 2746 通过adb 命令的方式,免去了按powerkey+volumeup进入menu sele ...
- 面试官:你说你熟悉jvm?那你讲一下并发的可达性分析
这是why技术的第35篇原创文章 上面这张图是我还是北漂的时候,在鼓楼附近的胡同里面拍的. 那天刚刚下完雨,路过这个地方的时候,一瞬间就被这五颜六色的门板和自行车给吸引了,于是拍下了这张图片.看到这张 ...
- [Redis-CentOS7]Redis数据持久化(八)
配置文件位置 /ect/redis.conf RDB存储配置 save 900 1 # 900秒之内发生一次写操作保存 save 300 10 # 300秒内写10次保存 save 60 10000 ...