• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            JulyRina's blog
            welcome to July Rina's blog
            posts - 22,comments - 1,trackbacks - 0
            【練習】
            2.3-2 MERGE的改進
            void MERGE(int *A, int p,int q, int r) {
                int B[maxn] , i = p , j = q+1 , k = 0;
                while(k < r - p + 1) {
                    if(i > q || j <= r && A[i] > A[j]) B[k++] = A[j++];
                    else B[k++] = A[i++];
                }
                for(i=0;i<r-p+1;i++) A[p+i] = B[i];
            }

            2.3-5 二分查找的C++代碼
            int find(int *a, int l, int r, int value) {
                if(l == r) return l;
                int mid = (l+r) >> 1;
                if(a[mid] >= value) return find(a, l, mid, value);
                else return find(a , mid+1, r , value);
            }

            *2.3-7 (這道題其實有O(n)的算法,而且寫起來更方便些)這里是O(nlogn)的算法
            O(nlogn)算法思想:1.首先進行排序;2.然后枚舉每一個小于等于x/2的數S[i],二分查找對應的x-S[i]是否存在
            #include <cstdio>
            #include <cstring>
            #include <iostream>
            #include <algorithm>
            using namespace std;
            const int maxn = 1010;
            bool findx(int *S,int n, int x,int l,int r) {
                if(l > r) return false;
                if(l ==r) return S[l] == x;
                int mid = (l+r) >> 1;
                if(S[mid] >= x) return findx(S, n, x, l, mid);
                else return findx(S, n, x, mid+1, r);
            }
            bool check(int *S,int n,int x) {
                for(int i=0;S[i]<=x/2 && i < n;i++) {
                    if(findx(S, n, x-S[i], i+1, n-1)) return true;
                }
                return false;
            }
            int main() {
                int S[1010] , x , n;
                while(~scanf("%d%d" , &n , &x)) {
                    for(int i=0;i<n;i++) cin >> S[i];
                    if(check(S, n, x)) puts("yes");
                    else puts("no");
                }
                return 0;
            }

            O(n)的方法是在數的范圍不是特別大的時候(或者數的范圍比較大,此時采用hash的方法)標記的方法,這里假設數的范圍<=10000,并且假設數沒有重復的情況下,其他情況稍許改變一下就行:
            #include <cstdio>
            #include <cstring>
            #include <iostream>
            #include <algorithm>
            using namespace std;
            const int maxn = 1010;

            bool check(int *S,int n,int x) {
                bool vis[10001] = {0};
                for(int i=0;i<n;i++) vis[x-S[i]] = true;
                for(int i=0;i<n;i++) if(vis[S[i]]) return true;
                return false;
            }
            int n ,x , S[maxn];
            int main() {
                while(~scanf("%d%d" , &n , &x)) {
                    for(int i=0;i<n;i++) cin >> S[i];
                    if(check(S, n, x)) puts("yes");
                    else puts("no");
                }
                return 0;
            }

            2-4(逆序對):這道題就是在歸并排序中得到逆序對,具體見代碼:
            #include <cstdio>
            #include <cstring>
            #include <iostream>
            #include <algorithm>
            using namespace std;
            const int maxn = 1010;
            int ans;
            void merge_sort(int *A, int l,int r) {
                if(l >= r) return;
                int mid = (l+r) >> 1;
                merge_sort(A, l, mid);
                merge_sort(A, mid+1, r);
                int i = l , j = mid+1 ,B[maxn] , k = l;
                while(i <= mid || j <= r) {
                    if(i > mid || j <= r && A[j] < A[i]) B[k++] = A[j++] , ans += mid-i+1;
                    else B[k++] = A[i++];
                }
                for(i=l;i<=r;i++) A[i] = B[i];
            }
            int main() {
                int A[maxn] , n;
                while(~scanf("%d" , &n)) {
                    for(int i=0;i<n;i++) cin >> A[i];
                    ans = 0;
                    merge_sort(A, 0, n-1);
                    cout << ans << endl;
                    //for(int i=0;i<n;i++) cout << A[i] << " "; cout << endl;
                }
                return 0;
            }
            posted on 2015-03-07 15:19 JulyRina 閱讀(311) 評論(0)  編輯 收藏 引用 所屬分類: 算法專題
            久久青草国产精品一区| 久久亚洲欧美日本精品| 久久精品国产99久久丝袜| 亚洲国产成人久久综合区| 三级三级久久三级久久| 青草影院天堂男人久久| 国产精品综合久久第一页| 久久婷婷国产综合精品| 欧美一级久久久久久久大| 国产精品一久久香蕉国产线看| 婷婷久久久亚洲欧洲日产国码AV| 国产精品激情综合久久| 麻豆久久久9性大片| 久久久久一级精品亚洲国产成人综合AV区| 中文字幕日本人妻久久久免费| 四虎久久影院| 久久99国产精品久久久| 欧美精品国产综合久久| 国产成人精品久久综合 | 日本久久久久亚洲中字幕| 久久国产精品-久久精品| 久久久无码精品亚洲日韩京东传媒 | 麻豆av久久av盛宴av| 嫩草影院久久99| 无码精品久久久天天影视 | 久久久久综合中文字幕| 久久精品天天中文字幕人妻 | 777午夜精品久久av蜜臀| 久久天天躁狠狠躁夜夜2020| 久久免费视频观看| 久久99精品久久久久久| 国产精品99久久免费观看| 久久精品欧美日韩精品| 国内高清久久久久久| 久久99这里只有精品国产| 色综合久久天天综线观看| 久久一区二区免费播放| 欧美久久综合九色综合| 青青热久久国产久精品 | 91久久精品电影| 中文精品久久久久人妻|