High bridge, low bridge(离散化, 前缀和)
High bridge, low bridge
Q:There are one high bridge and one low bridge across the river. The river has flooded twice, why the
high bridge is flooded twice but the low bridge is flooded only once?
A: Because the lower bridge is so low that it’s still under water after the first flood is over.
If you’re confused, here’s how it happens:
• Suppose high bridge and low bridge’s heights are 2 and 5, respectively, and river’s initial water
level is 1.
• First flood: the water level is raised to 6 (Both bridges are flooded), and then back to 2 (high
bridge is not flooded anymore, but low bridge is still flooded).
• Second flood: the water level is raised to 8 (The high bridge is flooded again), and then back to
3.
Just a word game, right? The key is that if a bridge is still under water (i.e. the water level is no
less than the bridge height) after a flood, then next time it will not be considered flooded again.
Suppose the i-th flood raises the water level to ai and then back to bi
. Given n bridges’ heights,
how many bridges are flooded at least k times? The initial water level is 1.
Input
The input contains at most 25 test cases. Each test case begins with 3 integers n, m, k in the first line
(1 ≤ n, m, k ≤ 105
). The next line contains n integers hi
, the heights of each bridge (2 ≤ hi ≤ 108
).
Each of the next m lines contains two integers ai and bi (1 ≤ bi < ai ≤ 108
, ai > bi−1). The file size of
the whole input does not exceed 5MB.
Output
For each test case, print the number of bridges that is flooded at least k times.
Explanation:
For the second sample, 5 bridges are flooded 1, 2, 3, 2, 0 times, respectively.
Sample Input
2 2 2
2 5
6 2
8 3
5 3 2
2 3 4 5 6
5 3
4 2
5 2
Sample Output
Case 1: 1
Case 2: 3
题解:涨潮,问会被淹没超过k次的桥的个数;
由于数据过大,要离散化下,这次涨潮要在上次最低潮的基础上也就是前一个x +1 到y;这之间加上1;最后只需要统计个数就好了;
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN = 1e5 + ;
int num[MAXN * ];
int brige[MAXN];
int l[MAXN], r[MAXN];
int cnt[MAXN * ];
int main(){
int n, m, p;
int kase = ;
while(~scanf("%d%d%d", &n, &m, &p)){
int tp = ;
num[tp++] = ;
for(int i = ; i < n; i++){
scanf("%d", brige + i);
num[tp++] = brige[i];
}
for(int i = ; i < m; i++){
scanf("%d%d", r + i, l + i);
num[tp++] = r[i];
num[tp++] = l[i];
}
sort(num, num + tp);
int k = unique(num, num + tp) - num;
memset(cnt, , sizeof(cnt));
int x, y, last = lower_bound(num, num + k, ) - num;
for(int i = ; i < m; i++){
x = lower_bound(num, num + k, l[i]) - num;
y = lower_bound(num, num + k, r[i]) - num;
int lx = last;
if(lx > y)swap(lx, y);
cnt[lx + ]++;
cnt[y + ]--;
last = x;
}
for(int i = ; i <= k; i++)
cnt[i] += cnt[i - ];
int ans = ;
for(int i = ; i < n; i++){
x = lower_bound(num, num + k, brige[i]) - num;
// printf("%d ", cnt[x]);
if(cnt[x] >= p)
ans++;
}
printf("Case %d: %d\n", ++kase, ans);
}
return ;
}
High bridge, low bridge(离散化, 前缀和)的更多相关文章
- P2344 奶牛抗议 离散化+前缀和+动态规划+树状数组
[题目背景] Generic Cow Protests, 2011 Feb [题目描述] 约翰家的N 头奶牛正在排队游行抗议.一些奶牛情绪激动,约翰测算下来,排在第i 位的奶牛的理智度为Ai,数字可正 ...
- 北桥芯片(north bridge/host bridge)
看下上面的图,会比较清晰的认识到北桥芯片所在位置 北桥芯片(North Bridge) 是mother board chipset(主板芯片组) 中起主导作用的最重要的组成部分,也称为主桥(Host ...
- Dull Chocolates Gym - 101991D 离散化 前缀和
题目链接:https://vjudge.net/problem/Gym-101991D 具体思路:首先看数据范围,暴力肯定不可以,可以下离散化,然后先求出离散化后每一个点到(1,1)的符合题目的要求的 ...
- 2019 ICPC Asia Nanchang Regional E Eating Plan 离散化+前缀和
题意: 给你n个盘子,这n个盘子里面分别装着1!到n!重量的食物,对于每一个询问k,找出一个最短的区间,使得区间和 mod 998857459 大于或等于k 盘子数量 n<=1e5 询问次数 m ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem H
Problem H High bridge, low bridge Q: There are one high bridge and one low bridge across the river. ...
- nenu contest3
http://vjudge.net/contest/view.action?cid=55702#overview 12656 - Almost Palindrome http://uva.online ...
- 理解 neutron(15):Neutron linux-bridge-agent 创建 linux bridge 的简要过程
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
- KVM 虚拟机联网方式:NAT 和 Bridge
KVM 客户机网络连接有两种方式: 用户网络(User Networking):让虚拟机访问主机.互联网或本地网络上的资源的简单方法,但是不能从网络或其他的客户机访问客户机,性能上也需要大的调整.NA ...
- Neutron 理解(14):Neutron ML2 + Linux bridge + VxLAN 组网
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
随机推荐
- 20个经典bootsrtap后台html站点模板推荐
今天为大家推荐20款不同风格的Bootstrap后台管理模板,每一款都经典可用,能预览和下载,保证让你挑得眼花缭乱. 1,Simpli flag蓝色 Simpli Flat蓝色管理模板是一款採用Fla ...
- C#扩展方法的理解
“扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.” 这是msdn上说的,也就是你可以对String,Int,DataRow,DataTable等这些 ...
- hdu4006 优先队列
A - 签到 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65768KB 64bit I ...
- visual studio 未将对象引用设置到对象的实例
我今天在win10上安装了Visual Studio 2015,结果新建项目后在模板中选择一项后就会弹出一个对话框: 查了许多种方法后,下面这个方法解决了我这个问题: 著作权归作者所有.商业转载请联系 ...
- Keil C减小代码编译量大小的方法(gai)
keil-C减小代码编译大小的方法整理 方法一:(通过优化代码减小) 1.1少做乘除运算,使用左/右移位来实现乘除 Eg ,普通:a = 0x80*4: 优化:a = 0x80<<2: 1 ...
- rr
times = gcd(rotdist,length); printf( ;i<times;i++) { t = vec[i]; j = i; ) { k = j+ r ...
- ueditor 百度编辑器 自定义图片上传路径和格式化上传文件名
今天项目中需要自定义图片上传的保存路径,并且不需要按照日期自动创建存储文件夹和文件名,我的ueditor版本是1.3.6.下面记录一下我配置成功的方法,如果有什么不对的地方欢迎指出,共同学习: 1:我 ...
- AFNetworking 官方文档
AFNetworking Version Minimum iOS Target Minimum OS X Target Notes 2.x iOS 6 OS X 10.8 Xcode 5 is req ...
- [TYVJ] P1002 谁拿了最多奖学金
谁拿了最多奖学金 背景 Background NOIP2005复赛提高组第一题 描述 Description 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同 ...
- layout_weight
最近写Demo,突然发现了Layout_weight这个属性,发现网上有很多关于这个属性的有意思的讨论,可是找了好多资料都没有找到一个能够说的清楚的,于是自己结合网上资料研究了一下,终于迎刃而解,写出 ...