高桥和低桥----题解

在这里插入图片描述
在这里插入图片描述

输入:
2 2 2
2 5
6 2
8 3
5 3 2
2 3 4 5 6
5 3
4 2
5 2

输出:
Case 1: 1
Case 2: 3
#include<iostream>
#include<cstdio>
#include<algorithm>
#include <stack>
#include <map>
#include <queue>
#include <vector>

using namespace std;

int main(){
	int n,m,k1;
	int fl=0;
	while(~scanf("%d%d%d",&n,&m,&k1)){
		fl++;        //样例个数 
		int a[n+5]={0},b[n+5]={0};
		int z,j,k=1;
		for(int i=0;i<n;i++) scanf("%d",&a[i]);  // 输入桥的高度 
		sort(a,a+n);               //将桥按高度从小到大排序 
		
		for(int i=0;i<m;i++){
			scanf("%d%d",&z,&j);      //输入涨水和退潮高度 
			//二分法 lower_bound
			int temp1=lower_bound(a,a+n,k+1)-a;   //求第一个大于等于k+1高度的桥的位置 
			int temp2=lower_bound(a,a+n,z+1)-a;   //求第一个大于等于z+1高度的桥的位置
			k=j;                  //k记录每次水停留的高度 
			b[temp1]++;           // temp1-temp2之间的桥被淹的次数加 1, temp2之后的次数不用加,所以减 1 
			b[temp2]--;           //例如:第三座桥的被淹次数 = b[0]+b[1]+b[2] 
//			cout<<temp1<<"temp1="<<b[temp1]<<endl; 
//			cout<<temp2<<"temp2="<<b[temp2]<<endl; 
		}
		
		int num=0,s=0;
		for(int i=0;i<n;i++){
			num+=b[i];
			
			if(k1<=num) s++; 
		}
		printf("Case %d: %d\n",fl,s);
	}
	
	
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值