lintcode - 统计比给定整数小的数的个数(两种方法)

 1 class Solution {
 2 public:
 3     /*
 4      * @param A: An integer array
 5      * @param queries: The query list
 6      * @return: The number of element in the array that are smaller that the given integer
 7      */
 8
 9     vector<int> countOfSmallerNumber(vector<int> A, vector<int> queries) {
10         // write your code here
11         vector<int> ans;
12         sort(A.begin(), A.end());
13         for(int i = 0; i < queries.size(); ++i){
14                 int tmp = lower_bound(A.begin(), A.end(), queries[i]) - A.begin();
15                 ans.push_back(tmp);
16         }
17         return ans;
18     }
19
20 };

二分查找的方法

/*
把A数组各个数字出现的次数放进线段树里,然后每次查询 0到A[i] - 1的个数,就是小于A[i]的个数
*/
class Solution {
public:
    /*
     * @param A: An integer array
     * @param queries: The query list
     * @return: The number of element in the array that are smaller that the given integer
     */
     //const int inf = -0x3f3f3f3f;
class SegmentTree{
                public:
                SegmentTree* left;
                SegmentTree* right;
                int start;
                int end;
                int sum;
                SegmentTree(int start, int end, int sum){
                        this -> start = start;
                        this -> end = end;
                        this -> left = NULL;
                        this -> right = NULL;
                        this -> sum = sum;
                }
        };
        int build(SegmentTree* &root, int left, int right, vector<int> &st){
                if(left > right) return 0;
                root -> start = left;
                root -> end = right;

                if(left == right) root -> sum = st[left];
                else {
                        int mid = (left + right) / 2;
                        root -> left = new SegmentTree(left, mid, 0);
                        root -> right = new SegmentTree(mid+1, right, 0);
                        root -> sum = build(root -> left, left, mid, st) + build(root -> right, mid + 1, right, st);
                }
                return root -> sum;
        }
        int query(SegmentTree* &root, int left, int right){
                if(root == NULL || left > right) return 0;
                if(left <= root -> start && root -> end <= right){
                        return root -> sum;
                }

                int mid = (root -> start + root -> end) / 2;
                if(left > mid){
                        return query(root -> right, left, right);
                } else if(right <= mid){
                        return query(root -> left, left, right);
                } else {
                        return query(root -> left, left, mid) + query(root -> right, mid+1, right);
                }

        }
    vector<int> countOfSmallerNumber(vector<int> A, vector<int> queries) {
        // write your code here
        vector<int> ans;
        if(A.size() == 0){
                for(int i = 0; i < queries.size(); ++i){
                        ans.push_back(0);
                }
                return ans;
        }
        sort(A.begin(), A.end());
        vector<int> st;
        st.resize(A[A.size() - 1] + 10, 0);
        for(int i = 0; i < A.size(); ++i){
                st[A[i]] += 1;
        }
        SegmentTree* root = new SegmentTree(0, 0, 0);
        build(root, 0, A[A.size() - 1], st);
        for(int i = 0; i < queries.size(); ++i){
                int tmp = query(root, 0, queries[i] - 1);
                ans.push_back(tmp);
        }
        delete(root);
        return ans;
    }

};

线段树

时间: 2024-10-13 16:53:54

lintcode - 统计比给定整数小的数的个数(两种方法)的相关文章

LintCode 249. 统计前面比自己小的数的个数

给定一个整数数组(下标由 0 到 n-1, n 表示数组的规模,取值范围由 0 到10000).对于数组中的每个 ai 元素,请计算 ai 前的数中比它小的元素的数量. 注意事项 We suggest you finish problem Segment Tree Build, Segment Tree Query II and Count of Smaller Number first. 样例 对于数组[1,2,7,8,5] ,返回 [0,1,2,3,2] 解题思路: 题目提示我们使用线段树,

求序列A中每个数的左边比它小的数的个数(树状数组)

给定一个有N个正整数的序列A(N<=10^5,A[i]<=10^5),对序列中的每一个数,求出序列中它左边比它小的数的个数. 思路:树状数组的经典应用(裸题) 1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 5 using namespace std ; 6 7 const int N = 100010 ; 8 9 int c[N] ; 10 11 int lowbit(

hdu4417 树状数组(求指定区间比指定数小的数的个数)

http://acm.hdu.edu.cn/showproblem.php?pid=4417 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover.

hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数

Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4766    Accepted Submission(s): 1727 Problem Description Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening.

实现两数交换的几种方法

#实现两数交换的几种方法: - - 1.常见的方法(采用临时变量) int x=10; int y=20; int temp=x;//定义临时变量 x=y; y=temp; - 2.异或的方法 int x=10; int y=20; x=x^y; y=x^y;//y=x^y^y=x;即y=10; x=x^y;//x=x^y^x=y;即x=20; ``` - 3.自加的方法 int x=10; int y=20; x=x+y;//x=30; y=x-y;//y=30-20=10; x=x-y;//

两种方法求丑数

我们把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 方法1 : 暴力破解,逐个判断 代码: <pre name="code" class="cpp">#include <iostream> #include <vector> using namespace std; //判断是否是丑数 bool isUgly(int index){ while(index % 2

HDU 1013 Digital Roots(两种方法,求数字根)

Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 67949    Accepted Submission(s): 21237 Problem Description The digital root of a positive integer is found by summing the digits of

设置Weblogic的线程数有两种方法

使用Weblogic版本:Weblogic 11g(10.3.6) 设置Weblogic的线程数有两种方法, 第一种,通过启动参数设置: 1 -Dweblogic.threadpool.MinPoolSize=1000 -Dweblogic.threadpool.MaxPoolSize=1000 可以加在setDomainEnv.sh文件JAVA_OPTIONS下面: 第二种,通过config.xml配置文件: 1 2 3 4 5 <server> <name>AdminServe

小程序的组件通讯三种方法==子向父传值

小程序的组件通讯三种方法 ============================ ================================ 子向父传值 第一步:小程序子向父传值在父组件定义方法 第二步:小程序子向父传值第二部在使用子组件的标签上在父的wxml文件中把方法传递给子组件 第三步:小程序子向父传值第三步在子组件的js文件中调用this·triggerEvent触发方法同时传递参数给父组件 第四步:第四步在第一步定义好的方法内部通过e·detail来接收子组件传递回来的参数 原