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(离散化, 前缀和)的更多相关文章

  1. P2344 奶牛抗议 离散化+前缀和+动态规划+树状数组

    [题目背景] Generic Cow Protests, 2011 Feb [题目描述] 约翰家的N 头奶牛正在排队游行抗议.一些奶牛情绪激动,约翰测算下来,排在第i 位的奶牛的理智度为Ai,数字可正 ...

  2. 北桥芯片(north bridge/host bridge)

    看下上面的图,会比较清晰的认识到北桥芯片所在位置 北桥芯片(North Bridge) 是mother board chipset(主板芯片组) 中起主导作用的最重要的组成部分,也称为主桥(Host ...

  3. Dull Chocolates Gym - 101991D 离散化 前缀和

    题目链接:https://vjudge.net/problem/Gym-101991D 具体思路:首先看数据范围,暴力肯定不可以,可以下离散化,然后先求出离散化后每一个点到(1,1)的符合题目的要求的 ...

  4. 2019 ICPC Asia Nanchang Regional E Eating Plan 离散化+前缀和

    题意: 给你n个盘子,这n个盘子里面分别装着1!到n!重量的食物,对于每一个询问k,找出一个最短的区间,使得区间和 mod 998857459 大于或等于k 盘子数量 n<=1e5 询问次数 m ...

  5. 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. ...

  6. nenu contest3

    http://vjudge.net/contest/view.action?cid=55702#overview 12656 - Almost Palindrome http://uva.online ...

  7. 理解 neutron(15):Neutron linux-bridge-agent 创建 linux bridge 的简要过程

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

  8. KVM 虚拟机联网方式:NAT 和 Bridge

    KVM 客户机网络连接有两种方式: 用户网络(User Networking):让虚拟机访问主机.互联网或本地网络上的资源的简单方法,但是不能从网络或其他的客户机访问客户机,性能上也需要大的调整.NA ...

  9. Neutron 理解(14):Neutron ML2 + Linux bridge + VxLAN 组网

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

随机推荐

  1. web前端代码规范 - HTML代码规范

    Bootstrap HTML编码规范 本文转载自w3cschool. 由于bootstrap得到了世界的认可,因此,此规范用于规范html代码有一定的说服力. 交流qq群:164858883.欢迎各位 ...

  2. 提交App到Apple Store(Xcode4)

    昨 天终于顺利把公司的App提交了,还是很开心的.这是我第一个开发超过2个月的项目,开发期间学到了很多东西,接下来的时间我会逐渐梳理一下.来个倒叙, 今天就先说下怎么提交的吧.Xcode4以后,提交过 ...

  3. core_cm3文件函数一览

    core_cm3是ARM公司推出来的统一规定,这是对下游芯片厂商的统一规定,因此可以再Cortex-M3(CM3)之间进行移植.此文件中定义了一些对特殊功能寄存器的C语言形式的操作,本质上是内敛汇编和 ...

  4. JavaScript获取元素样式

    原生的JavaScript获取写在标签内部的样式很简单: <div class="test" id="test" style="width:10 ...

  5. 支付宝SDK快速入口链接

    支付宝快捷支付SDK官方网站

  6. hdu Find a way

    算法:广搜: Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Le ...

  7. 对面向对象程序设计(OOP)的认识

    前言 本文主要介绍面向对象(OO)程序设计,以维基百科的解释: 面向对象程序设计(英语:Object-oriented programming,缩写:OOP),指一种程序设计范型,同时也是一种程序开发 ...

  8. 如何向java后台的对象中传数组

    1.后台对象的参数需要是是list对象 /* * copyright : GLOBALROAM Ptd Ltd * VmCreateInfo.java * Author: * zhangpengyan ...

  9. 从配置文件中读取数据获取Connection

    配置文件 db.driver=com.mysql.jdbc.Driver db.url=jdbc\:mysql\://localhost\:3306/mybase db.user=root db.ps ...

  10. 【笔记】Unix 平台标准

    POSIX 表示可移植操作系统接口(Portable Operating System Interface ,缩写为 POSIX ),POSIX标准定义了操作系统应该为应用程序提供的接口标准,是IEE ...