• Acwing-----算法基础课之第一讲


    785. 快速排序

    #include <iostream>
    using namespace std;
    const int N = 1000010;
    int q[N];
    
    void quick_sort(int q[], int l, int r) {
        if (l >= r) return ;
        int x = q[(l + r) / 2], i = l - 1, j = r + 1;
        while (i < j) {
            do i++;while (q[i] < x);
            do j--;while (q[j] > x);
            if (i < j) swap(q[i], q[j]);
        }
        quick_sort(q, l, j);
        quick_sort(q, j + 1, r);
    }
    
    int main() {
        int n;
        cin >> n;
        
        for (int i = 0; i < n; ++i) cin >> q[i];
        quick_sort(q, 0, n - 1);
        for (int i = 0; i < n; ++i) cout << q[i] << " ";
        puts("");
        
        return 0;
    }
    

    786. 第k个数

    #include <iostream>
    using namespace std;
    
    const int N = 100010;
    int n, k, q[N];
    
    int quick_sort(int l, int r, int k) {
        if (l >= r) return q[l];
        
        int i = l - 1, j = r + 1, x = q[l + r >> 1];
        
        while (i < j) {
            do i ++ ; while (q[i] < x);
            do j -- ; while (q[j] > x);
            if (i < j) swap(q[i], q[j]);
        }
        
        int sl = j - l + 1;
        if (k <= sl) return quick_sort(l, j, k);
        return quick_sort(j + 1, r, k - sl);
    }
    
    int main() {
        scanf("%d%d", &n, &k);
        for (int i = 0; i < n; ++i) scanf("%d", &q[i]);
        cout << quick_sort(0, n - 1, k) << endl;
    }
    

    787. 归并排序

    #include <iostream>
    using namespace std;
    
    const int N = 1e6 + 10;
    int q[N], tmp[N], n;
    
    void merge_sort(int q[], int l, int r) {
        if (l >= r) return;
        
        int mid = l + r >> 1;
        merge_sort(q, l, mid);
        merge_sort(q, mid + 1, r);
        
        int k = 0, i = l, j = mid + 1;
        
        while (i <= mid && j <= r) {
            if (q[i] < q[j]) tmp[k++] = q[i++];
            else tmp[k++] = q[j++];
        }
        
        while (i <= mid) tmp[k++] = q[i++];
        while (j <= r) tmp[k++] = q[j++];
        
        for (int i = l, j = 0; i <= r; i++, j++) q[i] = tmp[j]; 
    }
    
    int main () {
        scanf("%d", &n);
        for (int i = 0; i < n; ++i) scanf("%d", &q[i]);
        merge_sort(q, 0, n - 1);
        for (int i = 0; i < n; ++i) printf("%d ", q[i]);
        return 0;
    }
    

    788. 逆序对的数量

    #include <iostream>
    
    using namespace std;
    typedef long long LL;
    const int N = 1e5 + 10;
    int a[N], tmp[N];
    
    LL merge_sort(int q[], int l, int r)
    {
        if (l >= r) return 0;
    
        int mid = l + r >> 1;
    
        LL res = merge_sort(q, l, mid) + merge_sort(q, mid + 1, r);
    
        int k = 0, i = l, j = mid + 1;
        while (i <= mid && j <= r)
            if (q[i] <= q[j]) tmp[k ++ ] = q[i ++ ];
            else
            {
                res += mid - i + 1;
                tmp[k ++ ] = q[j ++ ];
            }
        while (i <= mid) tmp[k ++ ] = q[i ++ ];
        while (j <= r) tmp[k ++ ] = q[j ++ ];
    
        for (i = l, j = 0; i <= r; i ++, j ++ ) q[i] = tmp[j];
    
        return res;
    }
    
    int main()
    {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
    
        cout << merge_sort(a, 0, n - 1) << endl;
    
        return 0;
    }
    

    789. 数的范围

    #include <iostream>
    using namespace std;
    
    const int N = 100010;
    int q[N], m, n;
    
    int main () {
        scanf("%d%d", &n, &m);
        
        for (int i = 0; i < n; ++i) scanf("%d", &q[i]);
        
        while (m--) {
            int x;
            scanf("%d", &x);
            
            int l = 0, r = n - 1;
            while (l < r) {
                int mid = l + r >> 1;
                if (q[mid] >= x) r = mid;
                else l = mid + 1;
            }
            if (q[l] != x) {
                cout << "-1 -1" << endl;
            } else {
                cout << l << " ";
                int l = 0, r = n - 1;
                while (l < r) {
                    int  mid = l + r + 1 >> 1;
                    if (q[mid] <= x) l = mid;
                    else r = mid - 1;
                }
                cout << l << endl;
            }
        }
    }
    

    790. 数的三次方根

    `#include <iostream>
    using namespace std;
    
    double n;
    
    int main() {
        cin >> n;
        double l = -10000, r = 10000;
        while (r - l >= 1e-7) {
            double mid = (l + r) / 2;
            if (mid * mid * mid >= n) r = mid;
            else l = mid;
        }
        printf("%.6lf
    ", l);
    }
    

    795. 前缀和

    #include <iostream>
    using namespace std;
    
    const int N = 100010;
    
    int n, m, q[N], s[N];
    
    int main() {
        cin >> n >> m;
        for (int i = 1; i <= n; ++i) {
            cin >> q[i];
            s[i] = s[i - 1] + q[i];
        }
        
        
        int a, b;
        for (int i = 0; i < m; ++i) {
            cin >> a >> b;
            cout << s[b] - s[a - 1] << endl;
        }
        return 0;
    }
    
    1. 子矩阵的和
    #include <iostream>
    using namespace std;
    
    const int N = 1010;
    int n, m, q, s[N][N];
    
    int main() {
        cin >> n >> m >> q;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) cin >> s[i][j];
        }
        
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
        }
        
        while (q--) {
            int x1, x2, y1, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            cout << s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1] << endl;
        }
    }
    

    799. 最长连续不重复子序列

    #include <iostream>
    using namespace std;
    
    int n;
    const int N = 100010;
    int a[N], s[N];
    
    int main() {
        cin >> n;
        for (int i = 0; i < n; ++i) cin >> a[i];
        int res = 0;
        
        for (int i = 0, j = 0; i < n; ++i) {
            s[a[i]]++;
            while (s[a[i]] > 1) {
                s[a[j]]--;
                j++;
            }
            res = max(res, i - j + 1);
        }
        cout << res << endl;
        return 0;
    }
    

    800. 数组元素的目标和

    #include <iostream>
    using namespace std;
    
    const int N = 100010;
    int n, m, x, a[N], b[N];
    
    int main() {
        cin >> n >> m >> x;
        for (int i = 0; i < n; ++i) cin >> a[i];
        for (int i = 0; i < m; ++i) cin >> b[i];
        
        for (int i = 0, j = m - 1; i < n; ++i) {
            while (j >= 0 && a[i] + b[j] > x) j--;
            if (j >= 0 && a[i] + b[j] == x) cout << i << " " << j << endl;
        }
        return 0;
    }
    

    803. 区间合并

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    typedef pair<int, int> PII;
    const int N = 100010;
    int n;
    vector<PII> segs;
    
    void merge(vector<PII> &segs) {
        vector<PII> res;
        
        sort(segs.begin(), segs.end());
        int st = -2e9, ed = -2e9;
        for (auto seg: segs) {
            if (ed < seg.first) {
                if (st != -2e9) res.push_back({st, ed});
                st = seg.first, ed = seg.second;
            }
            else ed = max(ed, seg.second);
        }
        if (st != -2e9) res.push_back({st, ed});
        segs = res;
    }
    
    int main() {
        cin >> n;
        for (int i = 0; i < n; ++i) {
            int a, b;
            cin >> a >> b;
            segs.push_back({a, b});
        }
        
        merge(segs);
        cout << segs.size() << endl;
        return 0;
    }
    
  • 相关阅读:
    oracle 与mysql 的当前时间比较
    easyui 时间定格为 时分
    date类型数据插入
    mac 获取idea&&datagrip激活码
    静态代码块
    nginx mac 下启动 停止 重启,查看安装位置
    定时任务的时间规则
    雅酷帮微信公众平台操作手册
    微信公众平台中通过网页增加好友
    微信公众平台消息接口开发之微信浏览器HTTP_USER_AGENT判断
  • 原文地址:https://www.cnblogs.com/clown9804/p/12628754.html
Copyright © 2020-2023  润新知