码迷,mamicode.com
首页 > 其他好文 > 详细

BestCoder Round #35

时间:2015-03-29 10:48:31      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

01:枚举每个位置,求出期望,累加起来就是答案,注意最后要约分

02:这题居然被我用优先队列瞎搞过了,估计是数据水了,正解是要用线段树,做一个拓扑排序的时候,每次取出当前节点中,度数小于等于k的,下标最大的点,这个用线段树很好维护

代码:

01:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long ll;

int n, m;

ll C(int n, int m) {
    m = min(n - m, m);
    ll ans = 1;
    for (int i = 0; i < m; i++) {
        ans = ans * (n - i) / (i + 1);
    }
    return ans;
}

ll gcd(ll a, ll b) {
    if (!b) return a;
    return gcd(b, a % b);
}

int main() {
    while (~scanf("%d%d", &n, &m)) {
        ll zi = 0, mu;
        mu = C(n + m, n);
        for (int i = 0; i < n + m - 1; i++) {
            zi += C(n + m - 2, n - 1);
        }
        ll d = gcd(zi, mu);
        printf("%I64d/%I64d\n", zi / d, mu / d);
    }
    return 0;
}

02:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int N = 100005;

int n, m, k, du[N];
vector<int> g[N];

#define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2)

const int INF = 0x3f3f3f3f;

struct Node {
    int l, r, Min;
} node[N * 4];

void build(int l, int r, int x = 0) {
    node[x].l = l; node[x].r = r; node[x].Min = INF;
    if (l == r) return;
    int mid = (l + r) / 2;
    build(l, mid, lson(x));
    build(mid + 1, r, rson(x));
}

void add(int v, int val, int x = 0) {
    if (node[x].l == node[x].r) {
        node[x].Min = val;
        return;
    }
    int mid = (node[x].l + node[x].r) / 2;
    if (v <= mid) add(v, val, lson(x));
    else add(v, val, rson(x));
    node[x].Min = min(node[lson(x)].Min, node[rson(x)].Min);
}

int find(int v, int x = 0) {
    if (node[x].l == node[x].r)
        return node[x].l;
    if (node[rson(x)].Min <= v) return find(v, rson(x));
    return find(v, lson(x));
}

int main() {
    while (~scanf("%d%d%d", &n, &m, &k)) {
        int u, v;
        for (int i = 1; i <= n; i++) {
            du[i] = 0;
            g[i].clear();
        }
        while (m--) {
            scanf("%d%d", &u, &v);
            g[u].push_back(v);
            du[v]++;
        }
        build(1, n);
        for (int i = 1; i <= n; i++)
            add(i, du[i]);
        int bo = 0;
        for (int i = 0; i < n; i++) {
            int u = find(k);
            if (bo) printf(" ");
            else bo = 1;
            printf("%d", u);
            k -= du[u];
            du[u] = INF;
            add(u, du[u]);
            for (int i = 0; i < g[u].size(); i++) {
                int v = g[u][i];
                du[v]--;
                add(v, du[v]);
            }
        }
        printf("\n");
    }
    return 0;
}


BestCoder Round #35

标签:

原文地址:http://blog.csdn.net/accelerator_/article/details/44724941

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!