zoukankan      html  css  js  c++  java
  • 离散化 + unique + lower_bound的学习,hdu4325

    http://acm.hdu.edu.cn/showproblem.php?pid=4325

    这题看了数据就会发现,数据都很小的。。。。

    所以直接暴力能过

    当然开始我用的线段树

    后来想到,既然离散化了,那来暴力试试吧

    所以当作学习吧,同时学习了unique这个函数了

    用unique之前最好先排序

    一开始我以为unique是去除所有相同元素,哪想到其实也是个排序罢了

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    const int maxn = 100000 + 10;
    int a[maxn], b[maxn], c[maxn];
    int d[maxn<<2];
    int e[maxn<<2];
    int main(){
        //freopen("1006.in", "r", stdin);freopen("out.out", "w", stdout);
        int tcase;
        scanf("%d", &tcase);
        int z = 1;
        while(tcase --){
            int n, m;
            scanf("%d%d", &n, &m);
            int cnt = 0;
            for(int i = 0; i < n;i  ++){
                scanf("%d%d", a+i, b+i);
                d[cnt++] = a[i];
                d[cnt++] = b[i];
            }
            for(int i = 0; i < m; i ++){
                scanf("%d", c+i);
                d[cnt++] = c[i];
            }
            sort(d, d + cnt);
            int tmp = unique(d,d + cnt) - d;//返回重新排列的个数
            //printf("----------- %d\n", tmp);
            cnt = tmp;
            int count = 0;
            memset(e, 0, sizeof e);
            for(int i = 0; i < n;i ++){
                int l = lower_bound(d, d+cnt, a[i]) - d;
                int r = lower_bound(d, d+cnt, b[i]) - d;
                for(int j = l; j <= r; j ++){
                    e[j] ++;
                }
            }
            printf("Case #%d:\n", z++);
            for(int i = 0; i < m; i ++){
                int pos = lower_bound(d, d+cnt, c[i]) - d;
                printf("%d\n", e[pos]);
            }
        }
        return 0;
    }
  • 相关阅读:
    程序员开发工作之算法和架构
    iOS开发学习概述及知识整理
    git基本技巧及进阶
    使用命令行工具运行Xcode 7 UI Tests
    技巧集锦2
    Xcode开发小问题集锦
    Xcode 7如何 免费 真机调试iOS应用
    常用shell script 珍藏
    多线程学习7--CountDownLatch
    学习多线程6---栅栏
  • 原文地址:https://www.cnblogs.com/louzhang/p/2619130.html
Copyright © 2011-2022 走看看