【算法竞赛入门经典—训练指南】学习笔记(含例题代码与思路)第三章:实用数据结构

时间:2019-04-24 00:13:03   收藏:0   阅读:185

值得注意的是,本章虽然依然有很多不错的思想和题目,但并不建议初学知识点时从这里入门。并不是因为题目难,而是讲解并没有看网上其他博客来的清楚。



例题\(1\)?猜猜数据结构(\(UVa11995\)

#include <bits/stdc++.h>
using namespace std;

stack <int> s;
queue <int> q;
priority_queue <int> pq;

int n, x;

int read () {
    int s = 0, w = 1, ch = getchar ();
    while ('9' < ch || ch < '0') {
        if (ch == '-') w = -1;
        ch = getchar ();
    }
    while ('0' <= ch && ch <= '9') {
        s = s * 10 + ch - '0';
        ch = getchar ();
    }
    return s * w;
}

int main () {
//  freopen ("data.in", "r", stdin);
    while (scanf ("%d", &n) == 1) {
        int can1 = 1, can2 = 1, can3 = 1;
        for (int i = 1; i <= n; ++i) {
            if (read () == 1) {
                x = read ();
                s.push (x);
                q.push (x);
                pq.push (x);
            } else {
                x = read ();
                if (s.empty ()) {
                    while (i != n) {
                        read (), read (), ++i;
                    }
                    can1 = can2 = can3 = 0;
                    break;
                }
                can1 &= (s.top () == x); s.pop ();
                can2 &= (q.front () == x); q.pop ();
                can3 &= (pq.top () == x); pq.pop ();
            }
        }
        if (can1 + can2 + can3 > 1) {
            puts ("not sure");
        } else if (can1 + can2 + can3 == 0) {
            puts ("impossible");
        } else {
            if (can1) puts ("stack");
            if (can2) puts ("queue");
            if (can3) puts ("priority queue");
        }
        while (!s.empty ()) s.pop ();
        while (!q.empty ()) q.pop ();
        while (!pq.empty ()) pq.pop ();
    }
}

例题\(2\)?一道简单题(\(UVa11991\)

#include <bits/stdc++.h>
using namespace std;

const int N = 1000010;

int n, m, arr[N];

map <int, int> mp[N];

int main () {
//  freopen ("data.in", "r", stdin);
    ios :: sync_with_stdio (false);
    while (cin >> n >> m) {
        for (int i = 1; i <= n; ++i) {
            cin >> arr[i]; int w = arr[i];
            mp[w][++mp[w][0]] = i; 
        }
        for (int i = 1; i <= m; ++i) {
            static int k, v;
            cin >> k >> v;
            cout << mp[v][k] << endl;
        }
        for (int i = 1; i <= n; ++i) {
            mp[arr[i]].clear ();
        }
    }
}

例题\(3\)?阿格斯(\(LA3135\)

#include <bits/stdc++.h>
using namespace std;

struct Node {
    int Q_num, dotime, period;
    
    bool operator < (Node rhs) const {
        return dotime == rhs.dotime ? Q_num > rhs.Q_num : dotime > rhs.dotime;
    }
    
    Node (int _Q_num = 0, int _dotime = 0, int _period = 0) {
        Q_num = _Q_num, dotime = _dotime, period = _period;
    }
};

priority_queue <Node> q;

char opt[10]; int k, Q_num, dotime, period;

int main () {
//  freopen ("data.in", "r", stdin);
    while (cin >> opt && opt[0] == 'R') {
        cin >> Q_num >> period;
        q.push (Node (Q_num, period, period));
    }
    cin >> k;
    while (k--) {
        Node u = q.top (); q.pop ();
        cout << u.Q_num << endl;
        u.dotime += u.period; 
        q.push (u);
    }
}

例题\(4\)?\(K\)个最小和(\(UVA11997\)

#include <bits/stdc++.h>
using namespace std;

const int N = 750 + 5;

int k, arr1[N], arr2[N], data[N];

struct Node {
    int p1, p2;
    
    bool operator < (Node rhs) const {
        return arr1[p1] + arr2[p2] > arr1[rhs.p1] + arr2[rhs.p2];
    } 
    
    int get_val () {return arr1[p1] + arr2[p2];}
    
    Node (int _p1 = 0, int _p2 = 0) {
        p1 = _p1, p2 = _p2;
    } 
};

priority_queue <Node> q;

int main () {
//  freopen ("data.in", "r", stdin);
//  freopen ("data.out", "w", stdout);
    while (scanf ("%d", &k) == 1) {
        memset (arr1, 0, sizeof (arr1));
        for (int i = 1; i <= k; ++i) {
            scanf ("%d", &arr1[i]);
        }
        for (int i = 2; i <= k; ++i) {
            for (int j = 1; j <= k; ++j) {
                scanf ("%d", &arr2[j]);
            }
            sort (arr2 + 1, arr2 + 1 + k); //从小到大 
//          printf ("arr1 : "); for (int i = 1; i <= k; ++i) printf ("%d ", arr1[i]); printf ("\n");
//          printf ("arr2 : "); for (int i = 1; i <= k; ++i) printf ("%d ", arr2[i]); printf ("\n");
            while (!q.empty ()) q.pop ();
            for (int p1 = 1; p1 <= k; ++p1) {
                q.push (Node (p1, 1));
            }
            for (int j = 1; j <= k; ++j) {
                Node u = q.top (); q.pop ();
//              printf ("u = {pos1 = %d, pos2 = %d}\n", u.p1, u.p2);
                data[j] = u.get_val ();
                u.p2++; q.push (u);
            }
            swap (arr1, data);
        }
        for (int i = 1; i <= k; ++i) {
            printf ("%d", arr1[i]); if (i != k) putchar (' ');
        }
        printf ("\n");
    }
}

原文:https://www.cnblogs.com/maomao9173/p/10759965.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!