Codeforces Round #148 (Div. 1)

A. Not Wool Sequences

要使得任何区间异或值不为0,可以转化成使得所有前缀异或值不相等。于是就解决了。

// Author : JayYe  Created Time: 2013-11-22 19:04:57
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef __int64 ll;

const int mod = 1000000009;

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    ll cur = 1;
    bool flag = 1;
    while(m--) {
        cur = cur*2%mod;
        if(cur-1 >= n) flag = 1;
    }
    cur--;
    cur += mod;
    ll ans = 1;
    while(n--) {
        ans = ans*cur%mod;
        cur--;
    }
    printf("%I64d\n", (ans%mod + mod)%mod);
    return 0;
}

B. Boring Partition

首先对数列排个序,分别为a(1).. a(n),首先a(n)和a(n-1)肯定是要分在一集合中,a(1)和a(2)也要分在不同集合中,这个很容易想通,然后我们是要使得最大值尽量小,最小值尽量大,这样子差值才会更小。最大值只有两个可能,一个是 a(n) + a(n-1),另一个是a(n) + a(x) + h,a(x)为另一个集合中的最大值,所以如果a(x) = a(1)的话肯定是最好的。 对于最小值,让a(1)分在左集合,a(2)分在右集合,有三种情况 a(1) + a(2) + h, a(1) + 左集合次小值,a(2) + 右集合次小值。因为a(3)至少是某一个集合的次小值,所以令a(3)和a(2)在一个集合中,然后a(1)就单独算作一个集合是最优的。

综上,要么所有的都在一个集合中,要么a(1)单独分在一个集合,取最优值。

// Author : JayYe  Created Time: 2013-11-22 19:56:37
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int maxn = 100000 + 5;

struct PP {
    int x, id;
    bool operator < (const PP & a) const {
        return x < a.x;
    }
}a[maxn];

int vis[maxn];

int main() {
    int n, h;
    scanf("%d%d", &n, &h);
    for(int i = 1;i <= n; i++) {
        scanf("%d", &a[i].x);
        a[i].id = i;
    }
    sort(a + 1, a + n + 1);
    int ans = a[n].x + a[n-1].x - a[1].x - a[2].x;
    int mn = min(a[1].x + a[2].x + h, a[2].x + a[3].x);
    int mx = max(a[n].x + a[n-1].x, a[1].x + a[n].x + h);
    if(ans > mx - mn) {
        vis[a[1].id] = 1;
        ans = mx - mn;
    }
    printf("%d\n", ans);
    for(int i = 1;i <= n; i++)
        printf("%d ", vis[i] ? 2 : 1);
    puts("");
    return 0;
}

C. World Eater Brothers

题意:给你一个有向树,改变最少的边的方向使得你从某两个点可以走到所有节点。


思路:枚举边作为分界点,把一个树分成两棵树,两颗树分别由一个节点走通所有节点,树形dp,细节要好好想。

// Author : JayYe  Created Time: 2013-11-22 21:35:07
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int maxn = 3000 + 10;

struct Edge {
    int u, to, next, dir;
}edge[maxn<<1];

int head[maxn], E, cnt[maxn];

void init() {
    memset(head, -1, sizeof(head));
    E = 0;
}

void newedge(int u, int to, int dir) {
    edge[E].u = u;
    edge[E].to = to;
    edge[E].dir = dir;
    edge[E].next = head[u];
    head[u] = E++;
}

int mx;
void dfs(int pre, int u, int val) {
    cnt[u] = 0;
    for(int i = head[u];i != -1;i = edge[i].next) {
        int to = edge[i].to;
        if(to == pre)   continue;
        dfs(u, to, val + (edge[i].dir == 1 ? 1 : -1));
        cnt[u] += cnt[to] + edge[i].dir;
    }
    mx = max(mx, val);
}

int main() {
    init();
    int n, u, to;
    scanf("%d", &n);
    if(n == 1)  return puts("0"), 0;
    for(int i = 0;i < n-1; i++) {
        scanf("%d%d", &u, &to);
        newedge(u, to, 0);
        newedge(to, u, 1);
    }
    int ans = 1<<30;
    for(int i = 0;i < E;i += 2) {
        u = edge[i].u;
        to = edge[i].to;
        mx = -(1<<30);
        dfs(u, to, 0);
        int now = cnt[to] - mx;
        mx = -(1<<30);
        dfs(to, u, 0);
        now += cnt[u] - mx;
        ans = min(ans, now);
    }
    printf("%d\n", ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值