Codeforces Round #187 (Div. 2)

来源:互联网 发布:h5微场景页面源码 编辑:程序博客网 时间:2024/04/29 13:43

Problem set: http://codeforces.com/contest/315

Official solution:


Problem A: straightforward

#include <iostream>using namespace std;int main() {        int n, a[100], b[100], v[100] = {0};        cin >> n;        for (int i = 0; i < n; ++i)                cin >> a[i] >> b[i];        for (int i = 0; i < n; ++i) {                for (int j = 0; j < n; ++j) {                        if (i == j) continue;                        if (b[i] == a[j])                                v[j] = 1;                }        }        int ans = 0;        for (int i = 0; i < n; ++i)                if (!v[i])                        ++ans;        cout << ans << endl;        return 0;}


Problem B: store sum of update, use differences of two sum to get a partial sum in a range

#include <iostream>#include <vector>using namespace std;int main() {        int n, m;        cin >> n >> m;        vector<int> a(n), reset(n, -1);        for (int i = 0; i < n; ++i)                cin >> a[i];        vector<int> inc(m, 0);        for (int i = 0; i < m; ++i) {                if (i > 0)                        inc[i] = inc[i - 1];                int t;                cin >> t;                if (t == 1) {                        int v, x;                        cin >> v >> x;                        a[v - 1] = x;                        reset[v - 1] = i;                } else if (t == 2) {                        int y;                        cin >> y;                        inc[i] += y;                } else {                        int q;                        cin >> q;                        cout << a[q - 1] + inc[i] - (reset[q - 1] == -1 ? 0 : inc[reset[q - 1]]) << '\n';                }        }        return 0;}


Problem C: use doubly linked list as data structure; add the sum in linear time

#include <iostream>using namespace std;int a[200001];int head, nxt[200001], pre[200001];int main() {        int n, k;        cin >> n >> k;        for (int i = 1; i <= n; ++i) {                cin >> a[i];                pre[i] = i - 1;                if (i < n)                        nxt[i] = i + 1;        }        head = 1;        int m = n;        while (true) {                bool flag = true;                double d = 0;                int i = 0;                int j = head;                while (j) {                        ++i;                        if (j != head)                                d += (a[pre[j]] * double(i - 2));                        if (flag && d - double(m - i) * a[j] * (i - 1) + 1e-6 < k) {                                cout << j << '\n';                                flag = false;                                --m;                                //if (head == j) head = nxt[j];                                pre[nxt[j]] = pre[j];                                nxt[pre[j]] = nxt[j];                                break;                        }                        j = nxt[j];                }                if (flag) break;        }        return 0;}





原创粉丝点击