CSU1335 高桥和地桥

proble:

有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不算“淹了两次”。举例说明:

假定高桥和低桥的高度分别是5和2,初始水位为1

第一次洪水:水位提高到6(两个桥都被淹),退到2(高桥不再被淹,但低桥仍然被淹)

第二次洪水:水位提高到8(高桥又被淹了),退到3。

没错,文字游戏。关键在于“又”的含义。如果某次洪水退去之后一座桥仍然被淹(即水位不小于桥的高度),那么下次洪水来临水位提高时不能算“又”淹一次。

输入n座桥的高度以及第i次洪水的涨水水位ai和退水水位bi,统计有多少座桥至少被淹了k次。初始水位为1,且每次洪水的涨水水位一定大于上次洪水的退水水位。

              input

输入文件最多包含25组测试数据。每组数据第一行为三个整数n, m, k(1<=n,m,k<=105)。第二行为n个整数hi(2<=hi<=108),即各个桥的高度。以下m行每行包含两个整数ai和bi(1<=bi<ai<=108, ai>bi-1)。输入文件不超过5MB。

Output 对于每组数据,输出至少被淹k次的桥的个数。 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

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
const int inf=1e9-1;
const int maxn=100005;

int a[maxn],dp[maxn];
int main()
{
    int n,m,k;
    int T=0;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        memset(a,0,sizeof(a));
        memset(dp,0,sizeof(dp));

        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n);

        int pos=0;
        while(m--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            x++,y++;               
            int pos1=lower_bound(a,a+n,pos)-a; //pos位置之后到x位置之前的都要++,包括x位置但不包括pos位置;
            int pos2=lower_bound(a,a+n,x)-a;
            dp[pos1]++,dp[pos2]--;
            pos=y;
        }

        int ans=0;
        for(int i=1;i<n;i++)
            dp[i]+=dp[i-1];
        for(int i=0;i<n;i++)
        {
            if(dp[i]>=k)
                ans++;
        }
        printf("Case %d: %d\n",++T,ans);
    }
    return 0;
}

STL 二分查找有关

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
const int inf=1e9-1;
const int maxn=100005;

int main()
{
    int n;
    int a[100005];

    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
            printf("%d ",i);
        printf("\n");

        int x;
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n);
        while(~scanf("%d",&x))
            printf("upp:%d\ndown:%d\n\n",upper_bound(a,a+n,x)-a,lower_bound(a,a+n,x)-a);
        //lower大于等于x的第一个位置,upper大于x的第一个位置;
    }
    return 0;
}

这道题还可以用线段树做;

        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值