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 ...
随机推荐
- Direct2D 简介
Direct2D是什么? Direct2D是一套高性能的硬件加速API(代码运行在GPU),用于渲染几何图形,图片和文本,并且可以与Direct3D,GDI,GDI+交互. 什么程序员应该使用Dire ...
- [置顶] 强制访问控制内核模块Smack
Smack(Simplified Mandatory Access Control Kernel)是Casey Schaufler[15]于2007年在LSM基础上实现的Linux强制访问控制安全模块 ...
- OCP-1Z0-051-题目解析-第28题
28. Which two statements are true regarding constraints? (Choose two.) A. A foreign key cannot cont ...
- 如何单独编译Android源代码中的模块
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6566662 第一次下载好Android源代码工 ...
- String源码学习
String源码学习 零散的收获 数组的元素类型可以通过getComponentType来获取到 子类型的数组可以赋值给父类型的数组,.但是并不存在继承关系.数组的父类是Object. 通过声明如下代 ...
- JavaScript学习笔记之 数组方法一 堆栈 和队列
数组的方法 以及 堆栈的操作的方法 JavaScript是一种弱类型语言,不像其它程序语言需要严格定义数据类型.在JavaScript中数组可以任意修改变动,这样也就出现了一个问题,如果边遍历数组边操 ...
- c#、vb 自动属性
vb示例: Public Property Name() As String = "Bob" 等效于 Private _name As String = "Bob&quo ...
- FineUI模拟树下拉列表
模拟树的下拉列表 很多时候,我们希望在下拉列表中显示简单树状的层次结构,在菜单设置.机构设置等场景下这个需求尤为突出.也是基于项目需求的考虑,FineUI增加了模拟树的下拉列表的功能,显示效果如下所示 ...
- Android-图标
首先需要申明一点,系统图标并不存在于项目资源中,而是存在于设备中. 这就带来一个大问题,界面风格的控制权交到了不同的设备手中.这是我们不愿意看到的. 如何解决这个问题?有两种方法: 1.创建自己的图标 ...
- Android与JS混编(多图选择器)
github: https://github.com/weifengzz/AndroidJSSelectImg