2020牛客暑期多校训练营(第五场)


A Portal


B. Graph · 异或最小生成树

https://ac.nowcoder.com/acm/contest/5670/B

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
int a[N];
ll ans = 0;
int n;

struct Edge {
    int to, next;
    int w;
} e[N << 2];
int head[N], tot = 0;

void add(int u, int v, int w) {
    e[++tot] = {v, head[u], w};
    head[u] = tot;
    e[++tot] = {u, head[v], w};
    head[v] = tot;
}

void dfs1(int u, int f) {
    for (int i = head[u]; ~i; i = e[i].next) {
        int v = e[i].to;
        if (v != f) {
            a[v] = a[u] ^ e[i].w;
            dfs1(v, u);
        }
    }
}

int trie[N * 30][2];
int cnt;

void Insert(int x) {
    int rt = 0;
    for (int i = 30; i >= 0; i--) {
        int u = (x >> i) & 1;
        if (!trie[rt][u])
            trie[rt][u] = ++cnt;
        rt = trie[rt][u];
    }
}

int Search(int x) {
    int res = 0;
    int rt = 0;
    for (int i = 30; i >= 0; i--) {
        int u = (x >> i) & 1;
        if (trie[rt][u]) {
            rt = trie[rt][u];
        } else {
            rt = trie[rt][u ^ 1];
            res |= (1 << i);
        }
    }
    return res;
}

void dfs2(int l, int r, int dep) {
    if (dep == -1 || l >= r) return;
    int mid = l - 1;
    while (mid < r && ((a[mid + 1] >> dep) & 1) == 0) mid++;
    dfs2(l, mid, dep - 1);
    dfs2(mid + 1, r, dep - 1);

    if (mid == l - 1 || mid == r) return;
    for (int i = l; i <= mid; i++) {
        Insert(a[i]);
    }

    int tmp = INT_MAX;
    for (int i = mid + 1; i <= r; i++) {
        tmp = min(tmp, Search(a[i]));
    }

    ans += tmp;

    for (int i = 0; i <= cnt; i++) {
        trie[i][0] = trie[i][1] = 0;
    }
    cnt = 0;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    cin >> n;
    memset(head, -1, sizeof head);

    for (int i = 1, u, v, w; i < n; i++) {
        cin >> u >> v >> w;
        add(u, v, w);
    }

    dfs1(0, -1);
    sort(a, a + n);
    dfs2(0, n - 1, 30);
    cout << ans << endl;
    return 0;
}

C Easy


D. Drop Voicing · lcs


移动序列中的一个数到任意位置,最少需要1次操作2的代价,as:
在这里插入图片描述
显然,原来就保持递增序列的个数越多,移动次数就越少

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int a[505 * 2];
int n;

int dp[505 * 2];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        a[i + n] = a[i];
    }

    int Mx = 0;
    for (int i = 1; i <= n; i++) {

        int ed = i + n;
        for (int j = i; j <= ed; j++) {
            dp[j] = INF;
        }
        for (int j = i; j <= ed; j++) {
            *lower_bound(dp + i, dp + j, a[j]) = a[j];
        }
        int cnt = lower_bound(dp + i, dp + ed + 1, INF) - dp - i;
        Mx = max(Mx, cnt);
    }
    printf("%d\n", n - Mx);

    return 0;
}

E. Bogo Sort · 置换

https://ac.nowcoder.com/acm/contest/5670/E

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
 
        int n = cin.nextInt();
        int[] p = new int[n + 5];
        boolean[] vis = new boolean[n + 5];
        for (int i = 1; i <= n; i++) {
            vis[i] = false;
        }
 
        for (int i = 1; i <= n; i++) {
            p[i] = cin.nextInt();
        }
 
        BigInteger res = new BigInteger("1");
        for (int i = 1; i <= n; i++) {
            if (!vis[i]) {
                int now = i;
                int cnt = 0;
                while (!vis[now]) {
                    vis[now] = true;
                    now = p[now]; // 咳咳 逆着走也一样 
                    cnt++;
                }
                BigInteger x = new BigInteger("" + cnt);
                res = res.multiply(x).divide(res.gcd(x));
            }
        }
        String s = res.toString();
        int len = s.length();
        System.out.println(s.substring(Math.max(0, len - n), len));
    }
}

F. DPS · 模拟

https://ac.nowcoder.com/acm/contest/5670/F

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
ll d[N], s[N];
int n;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> n;
    ll Mx = 0;
    for (int i = 1; i <= n; i++) {
        cin >> d[i];
        Mx = max(Mx, d[i]);
    }

    for (int i = 1; i <= n; i++) {
        s[i] = (ll) ceil(d[i] * 50 * 1.0 / Mx);

        cout << "+";
        for (int j = 1; j <= s[i]; j++) cout << "-";
        cout << "+" << endl;

        cout << "|";
        if (s[i]) {
            for (int j = 1; j < s[i]; j++) cout << " ";
            if (d[i] == Mx) cout << "*";
            else cout << " ";
        }
        cout << "|" << d[i] << endl;

        cout << "+";
        for (int j = 1; j <= s[i]; j++) cout << "-";
        cout << "+" << endl;

    }
    return 0;
}

G Greetings Souvenir
H Interval


I Hard Math Problem

https://ac.nowcoder.com/acm/contest/5670/I

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=50+10;
/*
最优布局 (i + j) % 3 == 0
0 1 0 0 2 0 0 1 0 0
2 0 0 1 0 0 2 0 0 1
0 0 2 0 0 1 0 0 2 0
0 1 0 0 2 0 0 1 0 0
2 0 0 1 0 0 2 0 0 1
0 0 2 0 0 1 0 0 2 0
0 1 0 0 2 0 0 1 0 0
*/
int main()
{
    printf("0.666667\n");   
    return 0;
}

J Cone walker
K Git Merge

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值