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. Visual Studio 2008项目中WinForm窗口图标显示为类图标,仅仅能打开代码而无法打开视图问题解决

    背景:         今天打开一个Winform项目的时候.图标显示为类文件的样子而不是窗口的样子,在代码中右键也没有View Designer选项.双击图标打开的是代码而非窗口设计界面,百度后也没 ...

  2. 你必须掌握的Java基础:JSON解析工具-json-lib

    一.简介  json-lib是一个Java类库,提供将Java对象,包括beans,maps,collections,java arrays和xml等转换成JSON,或者反向转换的功能. 二.准备 在 ...

  3. HDU 4760 Good FireWall 完好Trie题解

    本题乍看像是线段树之类的区间操作,只是由于仅仅是须要查找ip的前缀,故此事实上是使用Trie来做. 挺高难度的Trie应用,做完这道题之后说明Trie功力有一定火候了. 这里的Trie使用到了Dele ...

  4. php不会的点

    1.DIRECTORY_SEPARATOR:DIRECTORY_SEPARATOR是一个显示系统分隔符的命令,DIRECTORY_SEPARATOR是PHP的内部常量,不需要任何定义与包含即可直接使用 ...

  5. ASP.NET网页抓取数据

    我的数据通过一个TextBox输入,这些代码是写在一个button的点击事件里的. 网页数据抓取大概分为两步,第一步是获取网页源代码: 具体注释如下: var currentUrl = TextBox ...

  6. 安卓状态栏通知Status Bar Notification

    安卓系统通知用户三种方式: 1.Toast Notification 2.Dialog Notification 3.Status Bar Notification Status Bar Notifi ...

  7. oc随笔四:NSString、NSNumber

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...

  8. some logo.

    发现一些logo , 储存在这里

  9. Modified Kaprekar Numbers

    Link: https://www.hackerrank.com/challenges/kaprekar-numbers from __future__ import print_function d ...

  10. Scala学习笔记--正则表达式基础知识、如何在scala内使用

    正则表达式语法:https://msdn.microsoft.com/zh-cn/library/ae5bf541(VS.80).aspx 基础知识:正则表达式30分钟入门教程 http://www. ...