interviewstreet-median -類別search

來源:互聯網
上載者:User

題目來源:https://www.interviewstreet.com/challenges/dashboard/#problem/4fcf919f11817

解題報告:

這道題我用的演算法是最普通的演算法,數組是排序的。刪除操作時,用二分尋找找到那個元素,然後數組後面的元素往前移一格。增加操作時,用二分尋找找到元素應該在的位置,插入元素,後面的元素依次往後挪。

但是,這道題的關鍵在於,不能用int,因為兩個int相加可能會越界!因為這個WA了好多遍。所以改用long long。

對double,使用math.h中的函數ceil(double)可以取整,根據ceil(v) == v的結果可以判斷v是否是整數。

然後輸出格式方面 printf("%.0lf", v),代表輸出double,且保留0位小數。

/* Sample program illustrating input and output */#include<iostream>#include<math.h>using namespace std;long long a[100001];void getMedian(int size){    double sum = 0;    if (size % 2 == 0)    {         sum = (double(a[size/2]) + double(a[size/2-1])) / 2;    }    elsesum = a[size/2];if (ceil(sum) == sum)printf("%.0lf\n", sum);elseprintf("%.1lf\n", sum);}int findAndRemove(long long key, int size){    int begin = 0;    int end = size - 1;    while(begin <= end)    {        int mid = (begin+end)/2;        if (a[mid] < key)            begin = mid + 1;        else if (a[mid] > key)            end = mid - 1;        else        {            int i = mid;            while(i < size - 1)            {                a[i] = a[i+1];                i++;            }            return true;        }    }    return false;}int main(){    int currentSize = 0;    int n;    cin >> n;    for (int i = 0; i < n; i++)    {        char operation;        long long value;        cin >> operation >> value;        if (operation == 'r')        {            if (currentSize <= 0)                cout << "Wrong!" << endl;            else            {                if (findAndRemove(value, currentSize))                {                    currentSize--;                    if (currentSize == 0)                        cout << "Wrong!" << endl;                    else                        getMedian(currentSize);                }                else                    cout << "Wrong!" << endl;            }        }        else        {            if (currentSize == 0)                a[0] = value;            else            {                int begin = 0;                int end = currentSize - 1;                while(begin <= end)                {                    int mid = (begin+end) / 2;                    if (a[mid] < value)                        begin = mid + 1;                    else                        end = mid - 1;                }                int i = currentSize;                while(i> begin)                {                   a[i] = a[i-1];                    i--;                }                a[begin] = value;             }             currentSize++;             getMedian(currentSize);        }    }}

附錄:

The median of  M numbers is defined as the middle number after sorting them in order, if  M is odd or the average number of the middle 2 numbers (again after sorting) if  M is even. You have an empty number
list at first. Then you can add or remove some number from the list. For each add or remove operation, output the median of numbers in the list.  Example : For a set of m = 5 numbers, { 9, 2, 8, 4, 1 } the median is the third number in sorted set { 1, 2, 4, 8, 9 } which is 4. Similarly for set of m = 4, { 5, 2, 10, 4 }, the median is the average of second and the third element in
the sorted set { 2, 4, 5, 10 } which is (4+5)/2 = 4.5    Input: The first line is an integer n indicates the number of operations. Each of the next n lines is either "a x" or "r x" which indicates the operation is add or remove.  Output: For each operation: If the operation is add output the median after adding x in a single line. If the operation is remove and the number x is not in the list, output "Wrong!" in a single line. If the operation is remove and the number x is in the list,
output the median after deleting x in a single line. (if the result is an integer DO NOT output decimal point. And if the result is a double number , DO NOT output trailing 0s.)  Constraints: 0 < n <= 100,000<n<=100000< div="">

 for each "a x" or "r x" , x will fit in 32-bit integer.  Sample Input: 7r 1a 1a 2a 1r 1r 2r 1  Sample Output:Wrong!11.511.51Wrong!  Note: As evident from the last line of the input, if after remove operation the list becomes empty you have to print "Wrong!" ( quotes are for clarity )

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.