DSA Range,dsarange

來源:互聯網
上載者:User

DSA Range,dsarange

最近開學了,又複習了下資料結構與演算法,我在MOOC上學的。這次是清華oj平台上的一題。

題目:範圍查詢(Range)

Descriptioin

Let S be a set of n integral points on the x-axis. For each given interval [a, b], you are asked to count the points lying inside.

Input

The first line contains two integers: n (size of S) and m (the number of queries).

The second line enumerates all the n points in S.

Each of the following m lines consists of two integers a and b and defines an query interval [a, b].

Output

The number of points in S lying inside each of the m query intervals.

Example

Input
5 2
1 3 7 9 11
4 6
7 12

Output
0
3

Restrictions

0 <= n, m <= 5 * 10^5

For each query interval [a, b], it is guaranteed that a <= b.

Points in S are distinct from each other.

Coordinates of each point as well as the query interval boundaries a and b are non-negative integers not greater than 10^7.

Time: 2 sec

Memory: 256 MB

代碼一

第一個解決方案是去年剛看到這題根據課程寫的代碼:

/*VC++6.0PA1 - RangeAsk.cppTsinghua Data Structures & Algorithmsby zerolevaat 2014-10-19*/#include <stdio.h>#define _OJ_const int MAX_SIZE = 500005;                    //0 ≤ n, m ≤ 5×10^5,點的總數void MSort(int [], int, int);                   //歸併排序void Merge(int [], int, int, int);              //合并int BSearch(const int [], int, int, bool&);     /*  折半尋找,找到則返回元素下標                                                    找不到時,若尋找的值小於所有元素則返回-1,                                                    否則返回不大於它的最大元素的下標*/int main(){#ifndef _OJ_    freopen("input.txt", "r", stdin);    //freopen("output.txt", "w", stdout);#endif    int m, n, i;    int* point = new int[MAX_SIZE];     //在堆上分配大的數組    scanf("%d %d", &n, &m);    for (i=0; i<n; i++)    {        scanf("%d", &point[i]);         //輸入所有座標    }    MSort(point, 0, n);                 //排序,O(nlogn)    while (m--)    {        int a, b;        scanf("%d %d", &a, &b);        bool find_b = false;        bool find_a = false;        int cnt = (b<point[0] || a>point[n-1]) ?             0 : (BSearch(point, n, b, find_b) - BSearch(point, n, a, find_a));  //直接相減,O(2logn)        cnt = find_a ? cnt+1 : cnt;             //由於BSearch返回不大於被尋找值的最大元素下標                                                //判斷a是否找到,若找到需要將cnt加1        printf("%d\n", cnt);    }#ifndef _OJ_    fclose(stdin);    //fclose(stdout);#endif    return 0;}void MSort(int a[], int lo, int hi){    if (hi-lo < 2)    {        return ;    }    int mi = (lo+hi) >> 1;    MSort(a, lo, mi);    MSort(a, mi, hi);    Merge(a, lo, mi, hi);    return ;}void Merge(int a[], int lo, int mi, int hi){    int* A = a+lo;    int i;    int lb = mi-lo;    int* b = new int[lb];       //建立一個b數組存a中lo-mi的元素    for (i=0; i<lb; i++)    {        b[i] = A[i];    }    int lc = hi-mi;    int* c = a+mi;              //用新指標c標識mi-hi的元素    int j, k;    for (i=0,j=0,k=0; (j<lb) && (k<lc); i++)    {                           //比較b數組以及c數組元素大小,並將其中小的存入原數組a                                //直到其中一個數組比較完        A[i] = (b[j] <= c[k]) ? b[j++] : c[k++];    }    while (j<lb)                //若c先比較完,則剩下的b數組中的元素都是大的,複製進原數組後面    {                           //若b先比較完,不必將c複製進a,因為c本就是a的        A[i++] = b[j++];    }    delete []b;    return ;}int BSearch(const int a[], int n, int x, bool& find){    int lo=0, hi=n;    int mi;    while (lo < hi)    {        mi = (lo+hi) >> 1;        if (a[mi] == x)        {            find = true;        //找到則將find改為true            return mi;          //找到則返回該元素下標        }        else if (x < a[mi])        {            hi = mi;        }         else        {            lo = mi+1;        }    }    return --lo;                //沒找到時,返回不大於被尋找值的最大元素下標}

當時寫的坑坑窪窪的,不過大體思路很清晰:輸入,排序,尋找,計算。這裡排序和尋找用的是歸併排序以及二分尋找,具體的見代碼,有注釋。

代碼二
#include <stdio.h>#include <string.h>#define MAXN 500005#define MAXNUM 10000005int n, m;int BSearch(const int sorted[], int x);         //折半尋找,返回不大於所尋找數位元素的下標 int main(){    scanf("%d %d", &n, &m);     int* sorted = new int[MAXN];    //儲存已排序的數     char* p = new char[MAXNUM];    memset(p, 0, sizeof(p)/sizeof(char));    int max=0;    int i;    for (i=0; i<n; i++)    {        int tmp;        scanf("%d", &tmp);        p[tmp] = 1;         //若輸入數字num,則p[num]=1,否則p[num]=0         max = (tmp<max) ? max : tmp;    }    //排序     //由0-max,用sorted數組儲存排序的數字     int j=0;    for (i=0; i<=max; ++i)    {        if(1 == p[i])        {            sorted[j++] = i;        }    }    //由於座標互異,排序完後應該j==n-1     while(m--)    {        int cnt = 0;        int a, b;        scanf("%d %d", &a, &b);        int pA = BSearch(sorted, a);        int pB = BSearch(sorted, b);        cnt = pB-pA;        cnt = (pA >= 0 && a == sorted[pA]) ? cnt+1 : cnt;       //判斷a是否找到,若找到需要將cnt加1//      printf("pA=%d && pB=%d\n", pA, pB);        if(0 == n)        {            printf("0\n");            continue;        }        printf("%d\n", cnt);    }    return 0;}int BSearch(const int sorted[], int x){    int lo=0, hi=n;    while(lo < hi)    {        int mid = (lo+hi) >> 1;        if(sorted[mid] == x)        {            return mid;        }        else if(x < sorted[mid])        {            hi = mid;        }        else        {            lo = mid+1;        }    }//  printf("%d\n", lo);    return --lo;}

這個是現在寫的,投機取巧了點。這裡沒有用歸併排序,寫起來太麻煩。利用了各座標點互異這個因素,具體看代碼。其他的還是跟代碼一一樣的。

主要說一下寫這段代碼時主要遇到的bug:

代碼三

這裡是學霸室友的代碼:

#include <stdio.h>#include <string.h>int flag[10000005];int cnt[10000005];int main(){    int i, n, q, num, a, b, Min = 10000000, Max = 0;    memset(flag, 0, sizeof(flag));    scanf("%d%d", &n, &q);    for (i = 0; i < n; i++)    {        scanf("%d", &num);        if (num < Min)        {            Min = num;        }        if (num > Max)        {            Max = num;        }        flag[num] = 1;    }    cnt[Min] = 1;    for (i = Min+1; i <= Max; i++)    {        cnt[i] = cnt[i-1] + (flag[i] ? 1 : 0);    }    while (q--)    {        scanf("%d%d", &a, &b);        if (a > Max || b < Min)        {            printf("0\n");            continue;        }        if (a < Min)        {            a = Min;        }        if (b > Max)        {            b = Max;        }        printf("%d\n", cnt[b] - cnt[a] + (flag[a] ? 1 : 0));    }    return 0;}

看完後我無地自容。。雖然沒有注釋但是挺好理解的。總感覺那個cnt數組的推導式像動態規劃呢,嘛,別在意我的逗比話語~

OK!今天就到這裡,我還要繼續複習~努力~
PS:藍橋杯延遲到4月份,我這樣的渣渣只能說太好了~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.