序列相關的趣題 之二,序列相關趣題

來源:互聯網
上載者:User

序列相關的趣題 之二,序列相關趣題

(4)數組中找到兩個數和的絕對值最小 

像不像2-SUM? 不多解釋,主要是絕對值大的動就行,兩頭掃的方法真好!當然要先排序,出去排序就是O(n),算上排序的話退化到O(nlogn)

這也是codility上的問題,還沒來得及整理。

上個代碼:

// you can also use includes, for example:// #include <algorithm>#include <algorithm>int ab(int x) {    return (x >= 0)?x:(-x);}int f(int x,int y) {    return ab(x + y);}int solution(vector<int> &A) {    // write your code in C++98    vector<int> a = A;    sort(a.begin(),a.end());    int answer = f(a[0],a[0]);     for (int i = 0, j = a.size() - 1; i <= j; ) {        answer = min(answer, f(a[i], a[j]));        if (ab(a[i]) > ab(a[j])) {            ++i;        }        else {            --j;        }            }    return answer;}


(5) 給定非負實數數組,已經按照非遞減排好序了。設數組為C,長度為N,0 ≤ P < Q < N and C[P] * C[Q] ≥ C[P] + C[Q]的下標對數。

codility上問題是這樣定義的C[i] = A[i] + B[i] / 10^6,A是整數部分[0..1000], 而B是分數部分的分子[0..999999] (分數部分的分母統一是1000000)。時間複雜度要O(N)

這個題其實就是分析要細緻,再次強調數個數並不一定要枚舉。我們無非要計算a * b >= a + b,由雩都是正數,再由對稱性我們只考慮0<=a<=b的情況,

考慮較大的數b可能的情形

(1) b == 0 只有a == 0才符合條件

  (2)   0 < b < 2 無解

  (3) b >= 2 則 b / (b - 1) <= a <= b 


注意到如果我們由小到大 枚舉b, 對條件(3) 那個b / (b - 1) = 1 / (1 - 1 / b)  是單調減小的,所以對更大的b,我們考慮a的時候,之前合法的a也是合法的,這是O(N)的關鍵,我們只需要記錄上一次最後一個合法的a的位置即可。

上代碼:


// you can use includes, for example:// #include <algorithm>// you can write to stdout for debugging purposes, e.g.// cout << "this is a debug message" << endl;const int M = 1000000000;const int W = 1000000;long long cmp(long long x1,long long y1, long long x2, long long y2) { // x1 / y1 - x2 / y2    return x1 * y2 - x2 * y1;   }int solution(vector<int> &A, vector<int> &B) {    // write your code in C++11    /*        let a <= b        a * b >= a + b        b == 0 a == 0        0 < b < 2  no solution        b >= 2        b / (b - 1) <= a <= b     */    int n = A.size(), num0 = 0, last = n, answer = 0;    for (int i = 0; i < n; ++i) {        if ((A[i] == 0) && (B[i] == 0)) { // b == 0            answer += num0++;        }        else if (A[i] >= 2) { // b >= 2            if (last >= n) {                last = i - 1;            }            int x = A[i] * W + B[i], y = x - W;            for (; (last >= 0) && (cmp(A[last] * W + B[last],W ,x ,y) >= 0); --last)            ;            answer += i - 1 - last;        }        if (answer >= M) {            return M;        }       // printf("%d %d\n",i, answer);            }    return answer;        }





王燕的《應用時間序列分析》(第二版)課後題答案主要sas編程那些

應用時間序列分析_王燕編著_講稿希望能幫到你
 
希爾排序法的一個題目,已知增量序列,分別畫出一趟、二趟與三趟排序的分組情況以及每一趟的排序

void ShellSort(int a[], int n)
{
int d, i, j, temp;
for(d = n/2;d >= 1;d = d/2)
{
for(i = d; i < n;i++)
{
temp = a[i];
for(j = i - d;(j >= 0) && (a[j] > temp);j = j-d)
{
a[j + d] = a[j];
}
a[j + d] = temp;
}
}
}
 

聯繫我們

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