Codeforces Round #781 (Div. 2)

Tree Infection

[Link](Problem - C - Codeforces)

题意

​ 给你一棵树,每秒你可以进行两种操作, 1. 1. 1.如果 u u u被染色了,可以选择 u u u的一个没染色的兄弟结点染色, 2. 2. 2.选择任意一个结点染色。

思路 二分

​ 首先贪心来看,每个点被兄弟结点染色更优,这个时候操作 2 2 2就可以染别的了。将所有的结点按兄弟分堆,一号点特殊开一堆。

​ 贪心来看我们一定是先染大堆的更优,这样可以让大堆的附带操作 1 1 1,,所以从大到小排序,每一堆至少要用操作 2 2 2染色一次,这样才可以开始操作 1 1 1,那么怎么得到最少的时间使得满足都染色了还不多于呢,我们可以二分时间,对于当前时间就是要看没有被操作 1 1 1附带掉的还有多少个点,这些点都需要操作 2 2 2单独搞掉,看一下需要操作 2 2 2搞掉的加上开始给每一堆染色是否超时即可。

Code

#include <bits/stdc++.h>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
    e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int T;
    cin >> T;
    while (T -- ) {
        cin >> n;
        vector<int> ve(n);
        for (int i = 1; i < n; i ++) {
            int x; cin >> x;
            ve[x - 1] ++;
        }
        vector<int> t;
        for (int i = 0; i < n; i ++)
            if (ve[i])
                t.push_back(ve[i]);
        t.push_back(0);
        sort(t.begin(), t.end());        
        reverse(t.begin(), t.end());    
        int res = t.size();
        auto check = [&](int x) {
            int sum = 0;
            for (int i = 0; i < t.size(); i ++)
                sum += max(0, t[i] - (x - i));
            return sum + res <= x;
        };
       
        int l = res, r = n;
        while (l < r) {
            int mid = l + r >> 1;
            if (check(mid)) r = mid;
            else l = mid  + 1;
        }
        cout << l << '\n';
    }
    return 0;
}

GCD Guess

[Link](Problem - D - Codeforces)

题意

​ 让你猜一个 1 ≤ x ≤ 1 0 9 1\le x\le10^9 1x109,你最多提问三十次,每次可以询问 a , b a,b a,b a ≠ b a\ne b a=b返回 g c d ( x + a , x + b ) gcd(x+a,x+b) gcd(x+a,x+b)

思路 二进制

​ 我们知道 g c d ( x + a , x + b ) = g c d ( x + a , b − a ) gcd(x+a,x+b)=gcd(x+a,b-a) gcd(x+a,x+b)=gcd(x+a,ba),三十次很容易想到按位枚举,从低往高枚举,假设我们已经知道了 x x x二进制前 i − 1 i-1 i1 r e s res res,那么怎么来确定第 i i i位是 0 0 0还是 1 1 1

​ 首先我们应该把 x x x i − 1 i-1 i1位都变成零,这样就可以单独考虑第 i i i位了,我们可以让 a = 2 i − r e s a=2^i-res a=2ires这样 x + a x+a x+a就把前缀消除了,然后让 b − a = 2 i + 1 b-a=2^{i+1} ba=2i+1,如果 x x x的第 i i i位为 1 1 1 x + a x+a x+a的第 i i i位往后即 x x x x x x x 0 xxxxxxx0 xxxxxxx0 b − a b-a ba 10 10 10,他们的 g c d gcd gcd 2 i + 1 2^{i+1} 2i+1,如果 x x x的第 i i i位为 0 0 0 x + a x+a x+a的第 i i i位往后即 x x x x x x x 1 xxxxxxx1 xxxxxxx1 b − 1 b-1 b1 10 10 10,他们的 g c d gcd gcd 2 i 2^i 2i,这样就可以判断出 x x x的第 i i i位是 0 0 0还是 1 1 1了。

Code

#include <bits/stdc++.h>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
    e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
LL ask(LL a, LL b) {
    cout << "? " << a << ' ' << b << endl;
    LL x; cin >> x;
    return x;
}
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int T;
    cin >> T;
    while (T -- ) {
        LL res = 0;
        for (int i = 0; i <= 29; i ++) {
            LL t = ask((1ll << i) - res, (1ll << (i + 1)) + (1ll << i) - res);
            if (t == (1ll << (i + 1))) res |= (1ll << i);
        }
        cout << "! " << res << endl;
    }
    return 0;
}

MinimizOR

[Link](Problem - E - Codeforces)

题意

给你一个长度为 n n n的非负数组和 m m m个询问每次问你区间 a i ∣ a j 且 i ≠ j a_i|a_j且i\ne j aiaji=j的最小值。

思路

线段树

​ 首先有一个结论一个区间的最大值如果不超过 2 k 2^k 2k,那么这个区间 a i ∣ a j 且 i ≠ j a_i|a_j且i\ne j aiaji=j的最小值一定是由区间内最小的 k + 1 k+1 k+1个数中产生的。

  • 证明(数学归纳法)

首先当 m x < 2 1 mx < 2^1 mx<21时区间内只有 0 , 1 0,1 0,1因此只需要最小的两个数即可,即结论成立。

假设区间最大值不超过 2 k 2^k 2k的时候,只需要最小的 k + 1 k+1 k+1个成立。

那么当最大值不超过 2 k + 1 2^{k+1} 2k+1的时候,分类来看:

  1. k k k位是为 0 0 0的数少于两个,因此这个时候无论怎么选第 k k k位一定为 1 1 1,我们需要找到两个数是得剩下的 k − 1 k-1 k1位或完最小,即最大值不超过 2 k 2^k 2k的情况,因为可能存在一个第 k k k 0 0 0但单看前 k − 1 k-1 k1位它很大的数,因此我们需要区间最小的 k + 2 k+2 k+2个数才能保证选到了但看前 k − 1 k-1 k1位最小的 k + 1 k+1 k+1个数。
  2. k k k为为 0 0 0的数不少于两个,那么贪心来看第 k k k位一定为 0 0 0,所以答案即转化为找到第 k k k位为零的数里剩下 k − 1 k-1 k1位或按最下,即最大值不超过 2 k 2^k 2k的情况,因为在第 k k k位为 0 0 0的情况里找,因此一定是排序后最小的 k + 1 k+1 k+1个数里。

因此得证。

所以我们开一个线段数来维护区间前 m i n ( 31 , r − l + 1 ) min(31,r-l+1) min(31,rl+1)小的数即可, p u s h u p pushup pushup的时候因为左右子区间维护的都是从小到大排序的数,所以就类似于归并排序去搞即可。

Code

#include <bits/stdc++.h>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
    e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
int a[N];
struct Node {
    int l, r;
    int v[31];
}tr[N << 2];
void push(Node& rt, Node& ls, Node& rs) {
    rt.l = ls.l, rt.r = rs.r;
    int lsz = min(31, ls.r - ls.l + 1), rsz = min(31, rs.r - rs.l + 1);    
    int p = 0, i = 0, j = 0;
    while (p < 31 && i < lsz && j < rsz) {
        if (ls.v[i] < rs.v[j]) rt.v[p ++] = ls.v[i ++];
        else    rt.v[p ++] = rs.v[j ++];
    }
    while (p < 31 && i < lsz) rt.v[p ++] = ls.v[i ++];
    while (p < 31 && j < rsz) rt.v[p ++] = rs.v[j ++];
}
void build(int u, int l, int r) {
    tr[u] = {l, r};
    if (l == r) {
        tr[u].v[0] = a[l];
        return ;
    }
    int mid = l + r >> 1;
    build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
    push(tr[u], tr[u << 1], tr[u << 1 | 1]);
}
Node query(int u, int l, int r) {
    if (l <= tr[u].l && tr[u].r <= r) return tr[u];
    int mid = tr[u].l + tr[u].r >> 1;
    if (r <= mid) return query(u << 1, l, r);
    if (l > mid) return query(u << 1 | 1, l, r);
    Node t, ls, rs;
    ls = query(u << 1, l, r), rs = query(u << 1 | 1, l, r);
    push(t, ls, rs);
    return t;
}
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int T;
    cin >> T;
    while (T -- ) {
        cin >> n;
        for (int i = 1; i <= n; i ++) cin >> a[i];
        build(1, 1, n);
        
        cin >> m;
        while (m --) {
            int l, r; cin >> l >> r;
            int sz = min(31, r - l + 1);
            Node res = query(1, l, r);
            int ans = 2e9;
            for (int i = 0; i < sz; i ++)
                for (int j = i + 1; j < sz; j ++)
                    ans = min(ans, res.v[i] | res.v[j]);
            cout << ans << '\n';
        }
    }
    return 0;
}

可持久化trie

​ 假设我们要查 [ l , r ] [l,r] [l,r]这个区间,我们对 [ l , r ] [l,r] [l,r]里的数建一个二进制字典树,那么就可以开始贪心了,从高为到低位如果当前这一位是 0 0 0的数的个数不少于两个那么这一位最终为 0 0 0往下爬即可,否则的话这一位最终一定为 1 1 1,但是如果这一位存在一个为 0 0 0的数,最终结果可能产生在这个数和前面的数或起来,因此我们把这个数存下来,因为最多会爬 l o g v logv logv层所以最多会存 l o g v logv logv的数,最后爬完再把贪心出来的数存下来。答案一定在着 l o g log log个数里产生,所以复杂度为 m l o g l o g mloglog mloglog

​ 因为有多个区间所以我们直接搞一个可持久化 t r i e trie trie即可。

Code

#include <bits/stdc++.h>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int n, m, k;
int a[N];
struct Trie {
    int idx, root[N], tr[N * 33][2], cnt[N * 33], val[N * 33];
    void insert(int p, int& q, int pos, int v) {
        if (!q) q = ++idx;
        cnt[q] = cnt[p] + 1;
        val[q] = v;
        if (pos < 0) return ;
        int u = v >> pos & 1;
        tr[q][u ^ 1] = tr[p][u ^ 1];
        insert(tr[p][u], tr[q][u], pos - 1, v);
    }
    int query(int l, int r) {
        int p = root[l], q = root[r];
        vector<int> ve;
        for (int i = 30; i >= 0; i --) {
            int cc = cnt[tr[q][0]] - cnt[tr[p][0]];
            if (cc >= 2) {
                p = tr[p][0];
                q = tr[q][0];
            }
            else {
                if (cc) ve.push_back(val[tr[q][0]]);
                p = tr[p][1];
                q = tr[q][1];
            }
        }
        for (int i = 0; i < min(2, cnt[q] - cnt[p]); i ++) ve.push_back(val[q]);

        int res = 2e9;
        for (int i = 0; i < ve.size(); i ++)
            for (int j = i + 1; j < ve.size(); j ++)
                res = min(res, ve[i] | ve[j]);
        return res;
    }
    void init() {
        for (int i = 0; i <= idx; i ++) tr[i][0] = tr[i][1] = 0, cnt[i] = 0, val[i] = 0;
        for (int i = 0; i <= n; i ++) root[i] = 0;
        idx = 0;
    }
}trie;
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int T;
    cin >> T;
    while (T -- ) {
        cin >> n;
        trie.init();
        for (int i = 1; i <= n; i ++) {
            cin >> a[i];
            trie.insert(trie.root[i - 1], trie.root[i], 30, a[i]);
        }
        cin >> m;
        while (m --) {
            int l, r; cin >> l >> r;
            l --;
            cout << trie.query(l, r) << '\n';
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值