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. 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.
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
Output for the Sample Input
Case 1: 1
Case 2: 3
Explanation
For the second sample, 5 bridges are flooded 1, 2, 3, 2, 0 times, respectively.
The Ninth Hunan Collegiate Programming Contest (2013)
Problemsetter: Rujia Liu Special Thanks: Feng Chen, Md. Mahbubul Hasan
这道试题很好。用笔画一下,其实就是区间更新,区间询问的树状数组吧,关键是求更新的区间,其实直接使用StL的查找也是可以的。还是那个道理,二分查找的变法很多,
不要太依赖STL ,基础一定要打好。
#include <iostream>
#include <stdio.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <stack>
#include <math.h>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
typedef long long LL ;
const int Max_N= ;
struct Node{
int raise ;
int down ;
};
Node water[Max_N] ;
int bridge[Max_N] ;
int N ,M ,K ;
int find_first_big_id(int x){
int Left= ;
int Right=N ;
int mid ;
int ans_id=- ;
while(Left<=Right){
mid=(Left+Right)>> ;
if(bridge[mid]>x){
ans_id=mid ;
Right=mid- ;
}
else
Left=mid+ ;
}
return ans_id ;
}
int find_last_less_or_equal_id(int x){
int Left= ;
int Right=N ;
int mid ;
int ans_id=- ;
while(Left<=Right){
mid=(Left+Right)>> ;
if(bridge[mid]>x){
Right=mid- ;
}
else{
ans_id=mid ;
Left=mid+ ;
}
}
return ans_id ;
}
int C[Max_N] ;
inline int lowbit(int x){
return x&(-x) ;
}
void Insert(int id ,int x){
while(id<=N){
C[id]+=x ;
id+=lowbit(id) ;
}
}
int get_sum(int id){
int sum= ;
while(id>=){
sum+=C[id] ;
id-=lowbit(id) ;
}
return sum ;
}
int main(){
/*int x ;
while(cin>>N>>x){
for(int i=1;i<=N;i++)
cin>>bridge[i] ;
cout<<find_last_less_or_equal_id(x)<<endl ;
}*/
int L ,R ,ans ,k= ;
while(scanf("%d%d%d",&N,&M,&K)!=EOF){
for(int i=;i<=N;i++)
scanf("%d",&bridge[i]) ;
for(int i=;i<=M;i++)
scanf("%d%d",&water[i].raise,&water[i].down) ;
sort(bridge+,bridge++N) ;
water[].down= ;
fill(C,C+N+,) ;
for(int i=;i<M;i++){
L=find_first_big_id(water[i].down) ;
R=find_last_less_or_equal_id(water[i+].raise) ;
// cout<<L<<" "<<R<<endl ;
if(L==-||R==-)
continue ; Insert(L,) ;
Insert(R+,-) ;
}
ans= ;
for(int i=;i<=N;i++){
if(get_sum(i)>=K)
ans++ ;
}
printf("Case %d: %d\n",k++,ans) ;
}
return ;
}
The Ninth Hunan Collegiate Programming Contest (2013) Problem H的更多相关文章
- The Ninth Hunan Collegiate Programming Contest (2013) Problem A
Problem A Almost Palindrome Given a line of text, find the longest almost-palindrome substring. A st ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem F
Problem F Funny Car Racing There is a funny car racing in a city with n junctions and m directed roa ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem I
Problem I Interesting Calculator There is an interesting calculator. It has 3 rows of button. Row 1: ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem J
Problem J Joking with Fermat's Last Theorem Fermat's Last Theorem: no three positive integers a, b, ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem G
Problem G Good Teacher I want to be a good teacher, so at least I need to remember all the student n ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem L
Problem L Last Blood In many programming contests, special prizes are given to teams who solved a pa ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem C
Problem C Character Recognition? Write a program that recognizes characters. Don't worry, because yo ...
- German Collegiate Programming Contest 2013:E
数值计算: 这种积分的计算方法很好,学习一下! 代码: #include <iostream> #include <cmath> using namespace std; ; ...
- German Collegiate Programming Contest 2013:B
一个离散化的简单题: 我用的是STL来做的离散化: 好久没写离散化了,纪念一下! 代码: #include<cstdio> #include<cstring> #include ...
随机推荐
- Nexus手动更新索引
如果有耐心的话,完全可以通过在线更新索引的方式来做,但所消耗的时间较长,下面介绍一种简单.可行的方式来手动更新索引文件. 访问http://repo.maven.apache.org/maven2/. ...
- 【maven】之开发pom配置常用插件
1.打包跳过测试代码 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId&g ...
- 【MySQL】数据行长度的一些限制
今天开发在导入数据的时候报一个错误: Row size too large. The maximum row size for the used table type, not counting BL ...
- 常用的邮箱服务器(SMTP、POP3)地址、端口
常用的邮箱服务器(SMTP.POP3)地址.端口 gmail(google.com) POP3服务器地址:pop.gmail.com(SSL启用 端口:995) SMTP服务器地址:smtp.gm ...
- php没有开启Memcache扩展类时
模拟PHP Memcache 类.当服务器没有开启Memcache扩展的时候.可以采用本类使用方法class_exists('Memcache') or include './Memcache.cla ...
- SSH登录很慢问题的解决
用ssh连其他linux机器,会等待10-30秒才有提示输入密码.严重影响工作效率.登录很慢,登录上去后速度正常,这种情况主要有两种可能的原因: 1. DNS反向解析的问题 OpenSSH在用户登录的 ...
- Redis常用方法
首先构建非切片连接池jedisPool对象,写好配置redis连接的方法. /** * 构建redis切片连接池 * * @param ip * @param port * @return Jedis ...
- hdu 1532 Dinic模板(小白书)
hdu1532 输入n,m. n条边,m个点,之后给出a到b的容量,求1到m的最大流. 注意:Dinic只能调用一次,因为原理是改变cap的值,如果调用多次一样的,那么第一次会对,其余的都会是0,因为 ...
- 把centos 的mysql 重装一下 把原来的lnmp删除,怎么备份还原数据库
mysqldump --lock-all-tables -u root -p --databases mydb > /opt/database/mydb.sql,或者直接备份mysql的数据存储 ...
- 使用C#的is和as操作符来转型
开发人员经常需要将一个对象从一种类型转换为其他各种类型.C#不要求任何特殊语法即可将一个对象转换为它的任何基类型,因为向基类型的转换被认为是一种安全的隐式转换.然而,将对象转换为它的某个派生类型时,C ...