zoukankan      html  css  js  c++  java
  • High bridge, low bridge(离散化, 前缀和)

    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 + 100;
    int num[MAXN * 3];
    int brige[MAXN];
    int l[MAXN], r[MAXN];
    int cnt[MAXN * 3];
    int main(){
        int n, m, p;
        int kase = 0;
        while(~scanf("%d%d%d", &n, &m, &p)){
            int tp = 0;
            num[tp++] = 0;
            for(int i = 0; i < n; i++){
                scanf("%d", brige + i);
                num[tp++] = brige[i];
            }
            for(int i = 0; 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, 0, sizeof(cnt));
            int x, y, last = lower_bound(num, num + k, 0) - num;
            for(int i = 0; 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 + 1]++;
                cnt[y + 1]--;
                last = x;
            }
            for(int i = 1; i <= k; i++)
                cnt[i] += cnt[i - 1];
            int ans = 0;
            for(int i = 0; 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
    ", ++kase, ans);
        }
        return 0;
    }
  • 相关阅读:
    空间换时间之反范式设计之路/合理冗余/去除外键
    WebAPI接口设计:SwaggerUI文档 / 统一响应格式 / 统一异常处理 / 统一权限验证
    开放api接口签名验证
    EasyUI开发踩过的坑(EasyUI开发笔记)
    nuget挂了吗?
    C# 实体/集合差异比较,比较两个实体或集合值是否一样,将实体2的值动态赋值给实体1(名称一样的属性进行赋值)
    从应用的角度讲创业公司该如何选择域名?
    疑似easyui本身bug:easyui时间控件问题,试了几个版本都不行
    工作三年对程序的理解特来求证
    控制器读取视图表单中的数据的几种方式
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5528790.html
Copyright © 2011-2022 走看看